Skip to content

Commit

Permalink
rlm_json: json_encode xlat module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnewton committed Feb 21, 2020
1 parent 513baa8 commit 3f865d3
Show file tree
Hide file tree
Showing 3 changed files with 492 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/tests/modules/json/encode.attrs
@@ -0,0 +1,13 @@
#
# Input packet
#
User-Name = 'john'
Filter-Id = "f1"
Filter-Id += "f2"
NAS-Port = 999
Service-Type = Login-User

#
# Expected answer
#
Packet-Type == Access-Accept
332 changes: 332 additions & 0 deletions src/tests/modules/json/encode.unlang
@@ -0,0 +1,332 @@
#
# json_encode tests
#


# 0. Check basic xlat parsing

update request {
&Tmp-String-1 := "%{json_encode:&request:[*]}"
&Tmp-String-2 := "%{json_encode:&request:[*] }"
&Tmp-String-3 := "%{json_encode: &request:[*]}"
&Tmp-String-4 := "%{json_encode: &request:[*] }"
&Tmp-String-5 := "%{json_encode: &request:[*] !&Filter-Id }"
&Tmp-String-6 := "%{json_encode:&request:[*] ! }"
# Check defaults are the same as output_mode "object":
&Tmp-String-7 := "%{json_object_encode:&request:[*]}"
&Tmp-String-8 := "%{json_object_no_encode:&request:[*]}"
}


if (&Tmp-String-1 == '{"User-Name":{"type":"string","value":"john"},"Filter-Id":{"type":"string","value":["f1","f2"]},"NAS-Port":{"type":"uint32","value":999},"Service-Type":{"type":"uint32","value":"Login-User"}}') {
test_pass
} else {
test_fail
}

# Check xlat input formats
if (&Tmp-String-1 != &Tmp-String-2 ||
&Tmp-String-1 != &Tmp-String-3 ||
&Tmp-String-1 != &Tmp-String-4) {
test_fail
}

# Check defaults
if (&Tmp-String-1 != &Tmp-String-7 ||
&Tmp-String-1 != &Tmp-String-8) {
test_fail
}

if (&Tmp-String-5 == '{"User-Name":{"type":"string","value":"john"},"NAS-Port":{"type":"uint32","value":999},"Service-Type":{"type":"uint32","value":"Login-User"}}') {
test_pass
} else {
test_fail
}

if (&Tmp-String-6 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Tmp-String-3 !* ANY
&Tmp-String-4 !* ANY
&Tmp-String-5 !* ANY
&Tmp-String-6 !* ANY
&Tmp-String-7 !* ANY
&Tmp-String-8 !* ANY
}


# 1a. Output mode "object" tests

# These are unsorted dictionaries. Hopefully json-c doesn't suddenly
# decide that it's going to use a different ordering of the keys...

update request {
&Tmp-String-1 := "%{json_object_encode:&request:[*]}"
&Tmp-String-2 := "%{json_object_ex_encode:&request:[*]}"
}

if (&Tmp-String-1 == '{"User-Name":{"type":"string","value":"john"},"Filter-Id":{"type":"string","value":["f1","f2"]},"NAS-Port":{"type":"uint32","value":999},"Service-Type":{"type":"uint32","value":"Login-User"}}') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '{"pf:User-Name":{"type":"string","value":["john"]},"pf:Filter-Id":{"type":"string","value":["f1","f2"]},"pf:NAS-Port":{"type":"uint32","value":["999"]},"pf:Service-Type":{"type":"uint32","value":["1"]}}') {
test_pass
} else {
test_fail
}

# 1b. "object" empty inputs

update request {
&Tmp-String-1 := "%{json_object_encode:!&request:[*]}"
&Tmp-String-2 := "%{json_object_ex_encode:}"
}

if (&Module-Failure-Message == 'Failed concatenating input: Invalid arguments. List was NULL') {
test_pass
} else {
test_fail
}

if (&Tmp-String-1 == '{}') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Module-Failure-Message !* ANY
}


# 2a. Output mode "object_simple" tests

update request {
&Tmp-String-1 := "%{json_object_simple_encode:&request:[*]}"
&Tmp-String-2 := "%{json_object_simple_ex_encode:&request:[*]}"
}

if (&Tmp-String-1 == '{"User-Name":"john","Filter-Id":["f1","f2"],"NAS-Port":999,"Service-Type":"Login-User"}') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '{"pf:User-Name":["john"],"pf:Filter-Id":["f1","f2"],"pf:NAS-Port":["999"],"pf:Service-Type":["1"]}') {
test_pass
} else {
test_fail
}

# 2b. "object_simple" empty inputs

update request {
&Tmp-String-1 := "%{json_object_simple_encode:!&request:[*]}"
&Tmp-String-2 := "%{json_object_simple_ex_encode:}"
}

if (&Module-Failure-Message == 'Failed concatenating input: Invalid arguments. List was NULL') {
test_pass
} else {
test_fail
}

if (&Tmp-String-1 == '{}') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Module-Failure-Message !* ANY
}


# 3a. Output mode "array" tests

update request {
&Tmp-String-1 := "%{json_array_encode:&request:[*]}"
&Tmp-String-2 := "%{json_array_ex_encode:&request:[*]}"
}

if (&Tmp-String-1 == '[{"name":"User-Name","type":"string","value":"john"},{"name":"Filter-Id","type":"string","value":"f1"},{"name":"Filter-Id","type":"string","value":"f2"},{"name":"NAS-Port","type":"uint32","value":999},{"name":"Service-Type","type":"uint32","value":"Login-User"}]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '[{"name":"pf:User-Name","type":"string","value":["john"]},{"name":"pf:Filter-Id","type":"string","value":["f1","f2"]},{"name":"pf:NAS-Port","type":"uint32","value":["999"]},{"name":"pf:Service-Type","type":"uint32","value":["1"]}]') {
test_pass
} else {
test_fail
}

# 3b. "array" empty inputs

update request {
&Tmp-String-1 := "%{json_array_encode:!&request:[*]}"
&Tmp-String-2 := "%{json_array_ex_encode:}"
}

if (&Module-Failure-Message == 'Failed concatenating input: Invalid arguments. List was NULL') {
test_pass
} else {
test_fail
}

if (&Tmp-String-1 == '[]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Module-Failure-Message !* ANY
}


# 4a. Output mode "array_of_names" tests

update request {
&Tmp-String-1 := "%{json_array_names_encode:&request:[*]}"
&Tmp-String-2 := "%{json_array_names_ex_encode:&request:[*]}"
}

if (&Tmp-String-1 == '["User-Name","Filter-Id","Filter-Id","NAS-Port","Service-Type"]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '["pf:User-Name","pf:Filter-Id","pf:Filter-Id","pf:NAS-Port","pf:Service-Type"]') {
test_pass
} else {
test_fail
}

# 4b. "array_of_names" empty inputs

update request {
&Tmp-String-1 := "%{json_array_names_encode:!&request:[*]}"
&Tmp-String-2 := "%{json_array_names_ex_encode:}"
}

if (&Module-Failure-Message == 'Failed concatenating input: Invalid arguments. List was NULL') {
test_pass
} else {
test_fail
}

if (&Tmp-String-1 == '[]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Module-Failure-Message !* ANY
}


# 5a. Output mode "array_of_values" tests

update request {
&Tmp-String-1 := "%{json_array_values_encode:&request:[*]}"
&Tmp-String-2 := "%{json_array_values_ex_encode:&request:[*]}"
}

if (&Tmp-String-1 == '["john","f1","f2",999,"Login-User"]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '["john","f1","f2","999","1"]') {
test_pass
} else {
test_fail
}

# 5b. "array_of_values" empty inputs

update request {
&Tmp-String-1 := "%{json_array_values_encode:!&request:[*]}"
&Tmp-String-2 := "%{json_array_values_ex_encode:}"
}

if (&Module-Failure-Message == 'Failed concatenating input: Invalid arguments. List was NULL') {
test_pass
} else {
test_fail
}

if (&Tmp-String-1 == '[]') {
test_pass
} else {
test_fail
}

if (&Tmp-String-2 == '') {
test_pass
} else {
test_fail
}

update request {
&Tmp-String-1 !* ANY
&Tmp-String-2 !* ANY
&Module-Failure-Message !* ANY
}


# Convert `make json.test` unlang update output to tests, for when
# things need updating.
#
# cat \
# | cut -c44- \
# | sed -e 's/\\"/"/g' \
# -e 's/\s*$//' \
# -e "s/:= \"/== '/" \
# -e 's/^/if (/' \
# -e "s/\"$/') {/" \
# -e "s/$/\n test_pass\n} else {\n test_fail\n}\n/"

0 comments on commit 3f865d3

Please sign in to comment.