summaryrefslogtreecommitdiffstats
path: root/src/tests/modules/rest/rest_xlat.unlang
blob: 52eb59784eb10d1ed7e5b734c87211ab06739fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#
# PRE rest_module eval
#
# Largely a back port of the rlm_rest tests from v4, with v4 specific functionality removed.
#

update {
	&Tmp-String-0 := "$ENV{REST_TEST_SERVER}"
	&Tmp-Integer-0 := "$ENV{REST_TEST_SERVER_PORT}"
	&Tmp-Integer-1 := "$ENV{REST_TEST_SERVER_SSL_PORT}"
	&Tmp-String-1 := "notfound"
}

# Retrieve a plain text file
update {
	&control:Tmp-String-1 := "%{rest:GET http://%{Tmp-String-0}:%{Tmp-Integer-0}/test.txt}"
}

if (!(&reply:REST-HTTP-Status-Code == 200)) {
	test_fail
}

if (!(&control:Tmp-String-1 == "Sample text response\n")) {
	test_fail
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# Take host from incomming packet
update {
	&control:Tmp-String-1 := "%{rest:http://%{Login-IP-Host}:%{Tmp-Integer-0}/test.txt}"
}

if (!(&reply:REST-HTTP-Status-Code == 200) || !(&control:Tmp-String-1 == "Sample text response\n")) {
	test_fail
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# Check a "not found" gives a 404 status code
update {
	&control:Tmp-String-1 := "%{rest:GET http://%{Tmp-String-0}:%{Tmp-Integer-0}/%{Tmp-String-1}}"
}

if (!(&reply:REST-HTTP-Status-Code == 404)) {
	test_fail
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# GET with URL parameters
update {
	&Tmp-String-2 := "%{rest:GET http://%{Tmp-String-0}:%{Tmp-Integer-0}/user/%{User-Name}/mac/%{Called-Station-Id}}"
}

if (!(&reply:REST-HTTP-Status-Code == 200)) {
	test_fail
}

if (!(&Tmp-String-2 =~ /"control:Tmp-String-1":\["GET","\\\/user\\\/<username>\\\/mac\\\/<client>"\]/)) {
	test_fail
}

if (&Tmp-String-2 =~ /"control:User-Name":\{([^}]+)\}/) {
	if (!("%{1}" =~ /"value":"Bob"/)) {
		test_fail
	}
} else {
	test_fail
}

update {
	&control:Tmp-String-3 := 'dummy'
}

update {
	&control:Tmp-String-2 = "%{json_encode:&NAS-IP-Address}"
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# POST to https with JSON body data
update {
	&Tmp-String-2 := "%{rest:POST https://%{Tmp-String-0}:%{Tmp-Integer-1}/user/%{User-Name}/mac/%{Called-Station-Id}?section=accounting %{control:Tmp-String-2}}"
}

if (!(&reply:REST-HTTP-Status-Code == 200)) {
	test_fail
}

if (!(&Tmp-String-2 =~ /"control:Tmp-String-1":\["POST","\\\/user\\\/<username>\\\/mac\\\/<client>"\]/)) {
	test_fail
}

if (&Tmp-String-2 =~ /"control:User-Name":\{([^}]+)\}/) {
	if (!("%{1}" =~ /"value":"Bob"/)) {
		test_fail
	}
} else {
	test_fail
}

update {
	&control:Tmp-String-2 := "NAS=%{NAS-IP-Address}&user=%{User-Name}"
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# POST to https with POST body data
update {
	&Tmp-String-2 := "%{rest:POST https://%{Tmp-String-0}:%{Tmp-Integer-1}/post/test?section=dummy %{control:Tmp-String-2}}"
}

if (!(&reply:REST-HTTP-Status-Code == 200)) {
	test_fail
}

if (!(&Tmp-String-2 == "Section: dummy, User: Bob\n")) {
	test_fail
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# URI with tainted values in the arguments - input argument includes URI argument
# separator - make sure this doesn't end up generating extra arguments, but gets escaped.
update {
	&Tmp-String-2 := "%{rest:GET http://%{Tmp-String-0}:%{Tmp-Integer-0}/user/%{User-Name}/reflect/?station=%{Calling-Station-Id}}"
}

if (!(&Tmp-String-2 == "{\"station\":\"dummy&unsafe=escaped\"}\n" )) {
	test_fail
}

# Zero length untainted value - check parsing doesn't break on zero length string
update {
	&Tmp-String-8 := ""
}
update {
	&Tmp-String-2 := "%{rest:http://%{Tmp-String-0}:%{Tmp-Integer-0}/user/%{User-Name}/reflect/%{Tmp-String-8}?station=%{User-Name}}"
}

if (!(&Tmp-String-2 == "{\"station\":\"Bob\"}\n" )) {
	test_fail
}

# Clear previous status code and body
update reply {
	&REST-HTTP-Status-Code !* ANY
	&REST-HTTP-Body !* ANY
}

# Zero length tainted value - check escaping doesn't break on zero length string
update {
	&Tmp-String-2 := "%{rest:http://%{Tmp-String-0}:%{Tmp-Integer-0}/user/%{User-Name}/reflect/%{Tmp-String-9}?station=%{Called-Station-Id}}"
}

if (!(&Tmp-String-2 == "{\"station\":\"aa:bb:cc:dd:ee:ff\"}\n" )) {
	test_fail
}

# Clear previous status code and body
update reply {
	REST-HTTP-Status-Code !* ANY
	REST-HTTP-Body !* ANY
}

# A request which will take longer than the timeout set for xlats
update {
	&Tmp-String-2 := "%{rest:http://%{Tmp-String-0}:%{Tmp-Integer-0}/delay}"
}

if (&reply:REST-HTTP-Status-Code) {
	test_fail
}

if (!(&Tmp-String-2 == "")) {
	test_fail
}

if (!(&Module-Failure-Message[*] == 'Request failed: 28 - Timeout was reached')) {
	test_fail
}

test_pass