proto-dictionary radius

#
#  Literals
#
xlat \t\n\t
match \t\n\t

xlat the quick brown fox jumped over the lazy dog
match the quick brown fox jumped over the lazy dog

# The second % should not be present in the output.  We should also not see 'Missing closing brace' type errors
xlat should not start \%{expansion
match should not start \%{expansion

xlat %
match \%

xlat \%
match \%

xlat \
match \\

#
#  Literals mixed with attributes
#
xlat literal%{User-Password}
match literal%{User-Password}

xlat {literal}%{User-Password}
match {literal}%{User-Password}

xlat literal%{User-Password}{literal}
match literal%{User-Password}{literal}

xlat {literal%{User-Password}literal}
match {literal%{User-Password}literal}

xlat \%{literal%{User-Password}literal}
match \%{literal%{User-Password}literal}

#
#  Regex capture groups
#
xlat %{0}
match %{0}

xlat %{32}
match %{32}

xlat %{33}
match ERROR offset 3: Invalid regex reference.  Must be in range 0-32

xlat %{3a}
match ERROR offset 3: Unexpected text after attribute reference

xlat %{-3}
match ERROR offset 3: Unresolved attributes not allowed in expansions here

xlat %{3
match ERROR offset 4: Missing closing brace

#
#  Tests for xlat expansion
#
xlat %{Tunnel-Password}
match %{Tunnel-Password}

xlat %test('bar')
match %test('bar')

xlat %{Tunnel-Password}
match %{Tunnel-Password}

xlat %{reply.Tunnel-Password}
match %{reply.Tunnel-Password}

xlat %{User-Name[3]}
match %{User-Name[3]}

xlat %{User-Name[*]}
match %{User-Name[*]}

xlat %{User-Name[#]}
match %{User-Name[#]}

xlat %{User-Name[n]}
match %{User-Name[n]}

xlat %{request.User-Name[3]}
match %{User-Name[3]}

xlat %{request.User-Name[*]}
match %{User-Name[*]}

xlat %{request.User-Name[#]}
match %{User-Name[#]}

xlat %{Vendor-Specific.3GPP.SGSN-Address}
match %{Vendor-Specific.3GPP.SGSN-Address}

xlat \"Hello %S goo\"
match \"Hello %S goo\"

xlat \"%{Foreach-Variable-0}\"
match \"%{Foreach-Variable-0}\"

#
#  3GPP stuff
#
xlat \"%{request.Vendor-Specific.3GPP.IMSI}\"
match \"%{Vendor-Specific.3GPP.IMSI}\"

xlat \"%{reply.Vendor-Specific.3GPP.IMSI}\"
match \"%{reply.Vendor-Specific.3GPP.IMSI}\"

xlat \"%{reply.Vendor-Specific.3GPP.IMSI[2]}\"
match \"%{reply.Vendor-Specific.3GPP.IMSI[2]}\"

xlat /([A-Z0-9\\-]*)_%{Calling-Station-Id}/
match /([A-Z0-9\\-]*)_%{Calling-Station-Id}/

xlat %length()
match %length()

xlat %length(
match ERROR offset 9: Missing closing brace

xlat %length(1 + 2
match ERROR offset 11: Unexpected text after argument 1

xlat \"%t\tfoo\"
match \"%t\tfoo\"

xlat \"%t\t%{NAS-IP-Address}\"
match \"%t\t%{NAS-IP-Address}\"

xlat \"foo %test('foo')\"
match \"foo %test('foo')\"

#
#  Alternations
#
xlat %{%{foo} || %{bar}}
match ERROR offset 5: Unresolved attributes not allowed in expansions here

xlat %{%{User-Name} || %{bar}}
match ERROR offset 21: Unresolved attributes not allowed in expansions here

#
#  Empty and malformed expansions
#
xlat %{
match ERROR offset 3: Missing closing brace

xlat %{}
match ERROR offset 3: Empty expression is invalid

xlat %{:}
match ERROR offset 3: No operand found.  Expected &ref, literal, 'quoted literal', "%{expansion}", or enum value

xlat %{:bar}
match ERROR offset 3: No operand found.  Expected &ref, literal, 'quoted literal', "%{expansion}", or enum value

xlat %foo(' bar')
match ERROR offset 2: Unresolved expansion functions are not allowed here

xlat %{[}
match ERROR offset 3: Missing attribute name

xlat %{[baz}
match ERROR offset 3: Missing attribute name

xlat %{ }
match ERROR offset 4: No operand found.  Expected &ref, literal, 'quoted literal', "%{expansion}", or enum value

xlat %{\t}
match ERROR offset 3: No operand found.  Expected &ref, literal, 'quoted literal', "%{expansion}", or enum value

xlat %{\n}
match ERROR offset 3: No operand found.  Expected &ref, literal, 'quoted literal', "%{expansion}", or enum value

xlat %{foo }
match ERROR offset 7: Unexpected text - attribute names must prefixed with '&'

xlat %{foo bar}
match ERROR offset 7: Invalid operator

xlat %test(
match ERROR offset 7: Missing closing brace

xlat %test(%{User-Name)
match ERROR offset 18: Unexpected text - attribute names must prefixed with '&'

# Discuss - Not sure the offset/message is correct here, but not sure if we can determine the correct offset either
xlat %test(%{User-Name}
match ERROR offset 19: Unexpected text after argument 1

xlat %{myfirstxlat
match ERROR offset 14: Missing closing brace

# Issue seen in the wild that caused an SEGV during pass2
xlat %{%{control.IP-Pool.Name}:%{reply.IP-Pool.Range}
match ERROR offset 49: Missing closing brace

#
#  API to split xlat strings into argv-style arguments.
#
xlat_argv /bin/sh %{User-Name}
match [0]{ /bin/sh }, [1]{ %{User-Name} }

xlat_argv /bin/sh "%{User-Name}"
match [0]{ /bin/sh }, [1]{ %{User-Name} }

# with quotes
xlat_argv /bin/sh "foo" "%{User-Name}"
match [0]{ /bin/sh }, [1]{ foo }, [2]{ %{User-Name} }

# many spaces between arguments
xlat_argv /bin/sh         "foo"       "%{User-Name}"
match [0]{ /bin/sh }, [1]{ foo }, [2]{ %{User-Name} }

# and spaces in quotes
xlat_argv /bin/sh "foo bar" "%{User-Name}"
match [0]{ /bin/sh }, [1]{ foo bar }, [2]{ %{User-Name} }

# and multiple xlats inside of quotes
xlat_argv /bin/sh "foo bar" "%{User-Name} %{Filter-Id}"
match [0]{ /bin/sh }, [1]{ foo bar }, [2]{ %{User-Name} %{Filter-Id} }

# and errors
#
#  @todo - we should really be unescaping the input in the sbuff, OR passing in the terminals
#  to xlat_tokenize_expansion()
#
xlat_argv /bin/sh "foo bar" "%{User-Name} %{Filter-Id"
match ERROR offset 44: Unexpected text after enum value.  Expected operator

# and text immediately after a variable expansion
xlat_argv echo hello %{Filter-Id}:1234 world
match [0]{ echo }, [1]{ hello }, [2]{ %{Filter-Id}:1234 }, [3]{ world }

xlat %debug(5)
match %debug(5)

xlat %debug( 5)
match %debug(5)

xlat %debug("foo")
match %debug("foo")

#
#  This is correct.
#
xlat %rpad(&User-Name, 5, 'x')
match %rpad(&User-Name, 5, 'x')

#
#  The second argument should be an integer.
#
#  @todo - parsing - we don't currently track string offsets for intermediate nodes,
#  so the "offset 23" is wrong.  It also doesn't say *which* string is wrong.  We'll fix that later.
#
xlat %rpad(&User-Name, foo, 'x')
match ERROR offset 28: Failed parsing argument 1 as type 'uint64'

#
#  Argument quoting
#
xlat %md5('"arg"')
match %md5('"arg"')

xlat %md5('"arg')"}
match %md5('"arg')\"}

xlat %md5('"arg')
match %md5('"arg')

#
#  @todo - ??? not sure here
#
xlat %md5('"arg\""')
match  %md5('"arg\\""')

xlat %md5('arg')
match %md5('arg')

xlat %md5('arg"')
match %md5('arg"')

count
match 159
