summaryrefslogtreecommitdiffstats
path: root/src/tests/unit/condition.txt
blob: bbe24d2bc490e7351a5b382c7800b8623ca63c7f (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#
#  Tests for parsing conditional expressions.
#
#  $Id$
#

#
#  A bunch of errors, in the order that the error strings
#  appear in parser.c
#


# All IP address literals should be parsed as prefixes
condition ("foo\
data ERROR offset 6 End of string after escape

condition ("foo
data ERROR offset 2 Unterminated string

condition ()
data ERROR offset 1 Empty string is invalid

condition (!)
data ERROR offset 2 Empty string is invalid

condition (foo == bar
data ERROR offset 11 No closing brace at end of string

condition (|| b)
data ERROR offset 1 Empty string is invalid

condition ((ok || handled) foo)
data ERROR offset 17 Unexpected text after condition

# escapes in names are illegal
condition (ok\ foo || handled)
data ERROR offset 3 Unexpected escape

condition (ok FOO handled)
data ERROR offset 4 Invalid text. Expected comparison operator

condition (ok !x handled)
data ERROR offset 4 Invalid operator

condition (ok =x handled)
data ERROR offset 4 Invalid operator

condition (ok =~ handled)
data ERROR offset 7 Expected regular expression

condition (ok == /foo/)
data ERROR offset 7 Unexpected regular expression

condition (ok == handled"foo")
data ERROR offset 14 Unexpected start of string

# And now we have a bunch of VALID conditions we want to parse.

# sillyness is OK
condition ((((((ok))))))
data ok

#
#  Extra braces get squashed
#
condition (&User-Name == &User-Password)
data &User-Name == &User-Password

condition (!ok)
data !ok

condition !(ok)
data !ok

condition !!ok
data ERROR offset 1 Double negation is invalid

condition !(!ok)
data ok

#
#  These next two are identical after normalization
#
condition (&User-Name == &User-Password || &Filter-Id == &Reply-Message)
data &User-Name == &User-Password || &Filter-Id == &Reply-Message

condition ((&User-Name == &User-Password) || (&Filter-Id == &Reply-Message))
data &User-Name == &User-Password || &Filter-Id == &Reply-Message

condition (!(&User-Name == &User-Password) || (&Filter-Id == &Reply-Message))
data !&User-Name == &User-Password || &Filter-Id == &Reply-Message

#  different from the previous ones.
condition (!((&User-Name == &User-Password) || (&Filter-Id == &Reply-Message)))
data !(&User-Name == &User-Password || &Filter-Id == &Reply-Message)

condition (!(&User-Name == &User-Password) || (&Filter-Id == &Reply-Message))
data !&User-Name == &User-Password || &Filter-Id == &Reply-Message

condition ((a == b) || (c == d)))
data ERROR offset 22 Unexpected closing brace

condition (handled && (Response-Packet-Type == Access-Challenge))
data handled && &Response-Packet-Type == Access-Challenge

# This is OK, without the braces
condition handled && &Response-Packet-Type == Access-Challenge
data handled && &Response-Packet-Type == Access-Challenge

# and this, though it's not a good idea.
condition handled &&&Response-Packet-Type == Access-Challenge
data handled && &Response-Packet-Type == Access-Challenge

condition /foo/ =~ bar
data ERROR offset 0 Conditional check cannot begin with a regular expression

condition reply == request
data ERROR offset 0 Cannot use list references in condition

condition reply == "hello"
data ERROR offset 0 Cannot use list references in condition

condition "hello" == reply
data ERROR offset 0 Cannot use list references in condition

condition request:User-Name == reply:User-Name
data &User-Name == &reply:User-Name

#
#  Convert !~ to !(COND) for regex
#
condition foo =~ /bar/
data foo =~ /bar/

condition foo !~ /bar/
data !foo =~ /bar/

condition !foo !~ /bar/
data foo =~ /bar/

#
#  Convert != to !(COND) for normal checks
#
condition &User-Name == &User-Password
data &User-Name == &User-Password

condition &User-Name != &User-Password
data !&User-Name == &User-Password

condition !&User-Name != &User-Password
data &User-Name == &User-Password

condition <ipv6addr>foo
data ERROR offset 0 Cannot do cast for existence check

condition <ipaddr>Filter-Id == &Framed-IP-Address
data <ipaddr>&Filter-Id == &Framed-IP-Address

condition <ipaddr>Filter-Id == <ipaddr>&Framed-IP-Address
data ERROR offset 21 Unnecessary cast

condition <ipaddr>Filter-Id == <integer>&Framed-IP-Address
data ERROR offset 21 Cannot cast to a different data type

condition <ipaddr>Filter-Id == <blerg>&Framed-IP-Address
data ERROR offset 22 Invalid data type in cast

#
#  Normalize things
#
condition <ipaddr>Filter-Id == "127.0.0.1"
data <ipaddr>&Filter-Id == '127.0.0.1'

condition <ipaddr>127.0.0.1 < &Framed-IP-Address
data &Framed-IP-Address > 127.0.0.1

# =* and !* are only for attrs / lists
condition "foo" !* bar
data ERROR offset 6 Cannot use !* on a string

condition "foo" =* bar
data ERROR offset 6 Cannot use =* on a string

# existence checks don't need the RHS
condition User-Name =* bar
data &User-Name

condition User-Name !* bar
data !&User-Name

condition !User-Name =* bar
data !&User-Name

condition !User-Name !* bar
data &User-Name

# redundant casts get squashed
condition <ipaddr>Framed-IP-Address == 127.0.0.1
data &Framed-IP-Address == 127.0.0.1

condition <cidr>Framed-IP-Address <= 192.168.0.0/16
data <ipv4prefix>&Framed-IP-Address <= 192.168.0.0/16

# All IP address literals should be parsed as prefixes
condition Framed-IP-Address <= 192.168.0.0/16
data <ipv4prefix>&Framed-IP-Address <= 192.168.0.0/16

# string attributes must be string
condition User-Name == "bob"
data &User-Name == "bob"

condition User-Name == `bob`
data &User-Name == `bob`

condition User-Name == 'bob'
data &User-Name == 'bob'

condition User-Name == bob
data ERROR offset 13 Must have string as value for attribute

# Integer (etc.) types must be "bare"
condition Session-Timeout == 10
data &Session-Timeout == 10

condition Session-Timeout == '10'
data ERROR offset 19 Value must be an unquoted string

# Except for dates, which can be humanly readable!
# This one is be an expansion, so it's left as-is.
condition Event-Timestamp == "January 1, 2012 %{blah}"
data &Event-Timestamp == "January 1, 2012 %{blah}"

# This one is NOT an expansion, so it's parsed into normal form
condition Event-Timestamp == 'January 1, 2012'
#data &Event-Timestamp == 'Jan  1 2012 00:00:00 EST'

# literals are parsed when the conditions are parsed
condition <integer>X == 1
data ERROR offset 9 Failed to parse field

condition NAS-Port == X
data ERROR offset 12 Failed to parse value for attribute

#
#  The RHS is a static string, so this gets mashed to a literal,
#  and then statically evaluated.
#
condition <ipaddr>127.0.0.1 == "127.0.0.1"
data true

condition <ipaddr>127.0.0.1 == "%{sql: 127.0.0.1}"
data <ipaddr>127.0.0.1 == "%{sql: 127.0.0.1}"

condition <ether> 00:11:22:33:44:55 == "00:11:22:33:44:55"
data true

condition <ether> 00:11:22:33:44:55 == "ff:11:22:33:44:55"
data false

condition <ether> 00:11:22:33:44:55 == "%{sql:00:11:22:33:44:55}"
data <ether>00:11:22:33:44:55 == "%{sql:00:11:22:33:44:55}"

condition <ether> 00:XX:22:33:44:55 == 00:11:22:33:44:55
data ERROR offset 8 Failed to parse field

#
#  Tests for boolean data types.
#
condition true
data true

condition 1
data true

condition false
data false

condition 0
data false

condition true && (User-Name == "bob")
data &User-Name == "bob"

condition false && (User-Name == "bob")
data false

condition false || (User-Name == "bob")
data &User-Name == "bob"

condition true || (User-Name == "bob")
data true

#
#  Both sides static data with a cast: evaluate at parse time.
#
condition <integer>20 < 100
data true

#
#  Both sides literal: evaluate at parse time
#
condition ('foo' == 'bar')
data false

condition ('foo' < 'bar')
data false

condition ('foo' > 'bar')
data true

condition ('foo' == 'foo')
data true

#
#  Double-quotes strings without expansions are literals
#
condition ("foo" == "%{sql: foo}")
data foo == "%{sql: foo}"

condition ("foo bar" == "%{sql: foo}")
data "foo bar" == "%{sql: foo}"

condition ("foo" == "bar")
data false

condition ("foo" == 'bar')
data false

#
#  The RHS gets parsed as a VPT_TYPE_DATA, which is
#  a double-quoted string.  Except that there's no '%'
#  in it, so it reverts back to a literal.
#
condition (&User-Name == "bob")
data &User-Name == "bob"

condition (&User-Name == "%{sql: blah}")
data &User-Name == "%{sql: blah}"

condition <ipaddr>127.0.0.1 == 2130706433
data true

# /32 suffix should be trimmed for this type
condition <ipaddr>127.0.0.1/32 == 127.0.0.1
data true

condition <ipaddr>127.0.0.1/327 == 127.0.0.1
data ERROR offset 8 Failed to parse field

condition <ipaddr>127.0.0.1/32 == 127.0.0.1
data true

condition (/foo/)
data ERROR offset 1 Conditional check cannot begin with a regular expression

#
#  Tests for (FOO).
#
condition (1)
data true

condition (0)
data false

condition (true)
data true

condition (false)
data false

condition ('')
data false

condition ("")
data false

#
#  Integers are true, as are non-zero strings
#
condition (4)
data true

condition ('a')
data true

condition (a)
data ERROR offset 1 Expected a module return code

#
#  Module return codes are OK
#
condition (ok)
data ok

condition (handled)
data handled

condition (fail)
data fail

condition ("a")
data true

condition (`a`)
data `a`

condition (User-name)
data &User-Name

#
#  Forbidden data types in cast
#
condition (<vsa>"foo" == &User-Name)
data ERROR offset 2 Forbidden data type in cast

#
#  Must have attribute references on the LHS of a condition.
#
condition ("foo" == &User-Name)
data ERROR offset 1 Cannot use attribute reference on right side of condition

#
#  If the LHS is a cast to a type, and the RHS is an attribute
#  of the same type, then re-write it so that the attribute
#  is on the LHS of the condition.
#
condition <string>"foo" == &User-Name
data &User-Name == "foo"

condition <integer>"%{expr: 1 + 1}" < &NAS-Port
data &NAS-Port > "%{expr: 1 + 1}"

condition &Filter-Id == &Framed-IP-Address
data ERROR offset 0 Attribute comparisons must be of the same data type

condition <ipaddr>127.0.0.1 == &Filter-Id
data ERROR offset 0 Attribute comparisons must be of the same data type

condition &Tmp-Integer64-0 == &request:Foo-Stuff-Bar
data &Tmp-Integer64-0 == &Foo-Stuff-Bar

condition &Tmp-Integer64-0 == &reply:Foo-Stuff-Bar
data &Tmp-Integer64-0 == &reply:Foo-Stuff-Bar

#
#  Casting attributes of different size
#
condition <ipaddr>&Tmp-Integer64-0 == &Framed-IP-Address
data ERROR offset 0 Cannot cast to attribute of incompatible size

condition <ipaddr>&PMIP6-Home-IPv4-HoA == &Framed-IP-Address
data ERROR offset 0 Cannot cast to attribute of incompatible size

# but these are allowed
condition <ether>&Tmp-Integer64-0 == "%{module: foo}"
data <ether>&Tmp-Integer64-0 == "%{module: foo}"

condition <ipaddr>&Filter-Id == &Framed-IP-Address
data <ipaddr>&Filter-Id == &Framed-IP-Address

condition <ipaddr>&Class == &Framed-IP-Address
data <ipaddr>&Class == &Framed-IP-Address

#
#  Tags of zero mean restrict to attributes with no tag
#
condition &Tunnel-Password:0 == "Hello"
data &Tunnel-Password:0 == "Hello"

condition &Tunnel-Password:1 == "Hello"
data &Tunnel-Password:1 == "Hello"

#
# Single quoted strings are left as-is
#
condition &Tunnel-Password:1 == 'Hello'
data &Tunnel-Password:1 == 'Hello'

#
#  zero offset into arrays get parsed and ignored
#
condition &User-Name[0] == "bob"
data &User-Name[0] == "bob"

condition &User-Name[1] == "bob"
data &User-Name[1] == "bob"

condition &User-Name[n] == "bob"
data &User-Name[n] == "bob"

condition &Tunnel-Password:1[0] == "Hello"
data &Tunnel-Password:1[0] == "Hello"

condition &Tunnel-Password:1[3] == "Hello"
data &Tunnel-Password:1[3] == "Hello"

#
#  This is allowed for pass2-fixups.  Foo-Bar MAY be an attribute.
#  If so allow it so that pass2 can fix it up.  Until then,
#  it's an unknown attribute
#
condition &Foo-Bar
data &Foo-Bar

#  Same types are optimized
#
#  FIXME: the tests don't currently run the "pass2" checks.
#  This test should really be:
#
#	data &Acct-Input-Octets > &Session-Timeout
#
condition &Acct-Input-Octets > "%{Session-Timeout}"
data &Acct-Input-Octets > "%{Session-Timeout}"

#  Separate types aren't optimized
condition &Acct-Input-Octets-64 > "%{Session-Timeout}"
data &Acct-Input-Octets-64 > "%{Session-Timeout}"

#
#  Parse OIDs into known attributes, where possible.
#
condition &Attr-26.24757.84.9.5.4 == 0x1a99
data &WiMAX-PFDv2-Src-Port == 6809

#
#  This OID is known, but the data is malformed.
#  This is disallowed.  Fix the configuration so that
#  it works.
#
condition &Attr-26.24757.84.9.5.7 == 0x1a99
data ERROR offset 27 Failed to parse value for attribute

#  This one is really unknown
condition &Attr-26.24757.84.9.5.15 == 0x1a99
data &Attr-26.24757.84.9.5.15 == 0x1a99

#
#  Invalid array references.
#
condition &User-Name[a] == 'bob'
data ERROR offset 11 Array index is not an integer

condition &User-Name == &Filter-Id[a]
data ERROR offset 25 Array index is not an integer

#
#  This one is still wrong.
#
condition User-Name[a] == 'bob'
data false

#
#  Bounds checks...
#
condition &User-Name[1001] == 'bob'
data ERROR offset 11 Invalid array reference '1001' (should be between 0-1000)

condition &User-Name[-1] == 'bob'
data ERROR offset 11 Invalid array reference '-1' (should be between 0-1000)

#
#  Tags
#
condition &Tunnel-Private-Group-Id:10 == 'test'
data &Tunnel-Private-Group-Id:10 == 'test'

condition &User-Name:10 == 'test'
data ERROR offset 10 Attribute 'User-Name' cannot have a tag

#
#  Tags are always wrong for attributes which aren't tagged.
#
condition &User-Name:0 == 'test'
data ERROR offset 10 Attribute 'User-Name' cannot have a tag

#
#  Bounds checks...
#
condition &Tunnel-Private-Group-Id:32 == 'test'
data ERROR offset 25 Invalid tag value '32' (should be between 0-31)

condition &request:Tunnel-Private-Group-Id:-1 == 'test'
data ERROR offset 33 Invalid tag value '-1' (should be between 0-31)

#
#  Sometimes the attribute/condition parser needs to fallback to bare words
#
condition request:Foo == 'request:Foo'
data true

condition request:Foo+Bar == request:Foo+Bar
data true

condition &request:Foo+Bar == 'request:Foo+Bar'
data ERROR offset 12 Unexpected text after unknown attr

condition 'request:Foo+d' == &request:Foo+Bar
data ERROR offset 31 Unexpected text after unknown attr

#  Attribute tags are not allowed for unknown attributes
condition &request:FooBar:0 == &request:FooBar
data ERROR offset 15 Unexpected text after unknown attr

condition &not-a-list:User-Name == &not-a-list:User-Name
data ERROR offset 1 Invalid list qualifier

# . is a valid dictionary name attribute, so we can't error out in pass1
condition &not-a-packet.User-Name == &not-a-packet.User-Name
data &not-a-packet.User-Name == &not-a-packet.User-Name

#
#  The LHS is a string with ASCII 5C 30 30 30 inside of it.
#
condition ('i have scary embedded things\000 inside me' == "i have scary embedded things\000 inside me")
data false

#
#  'Unknown' attributes which are defined in the main dictionary
#  should be resolved to their real names.
condition &Attr-1 == 'bar'
data &User-Name == 'bar'

condition &Vendor-11344-Attr-1 == 127.0.0.1
data &FreeRADIUS-Proxied-To == 127.0.0.1

condition &FreeRADIUS-Attr-1 == 127.0.0.1
data &FreeRADIUS-Proxied-To == 127.0.0.1

#
#  Escape the backslashes correctly
#  And print them correctly
#
condition &User-Name =~ /@|\\/
data &User-Name =~ /@|\\/

condition &User-Name == '\\'
data &User-Name == '\\'

condition &User-Name !~ /^foo\nbar$/
data !&User-Name =~ /^foo\nbar$/

condition &User-Name == "@|\\"
data &User-Name == "@|\\"

condition &User-Name != "foo\nbar"
data !&User-Name == "foo\nbar"

condition User-Name =~ /^([^\\]*)\\(.*)$/
data &User-Name =~ /^([^\\]*)\\(.*)$/

#
#  We require explicit casts
#
condition 192.168.0.0/16 > 192.168.1.2
data false

condition <ipv4prefix>192.168.0.0/16 > 192.168.1.2
data true

condition <ipv4prefix>&NAS-IP-Address == 192.168.0.0/24
data <ipv4prefix>&NAS-IP-Address == 192.168.0.0/24

condition <ipv4prefix>192.168.0.0/24 > &NAS-IP-Address
data <ipv4prefix>192.168.0.0/24 > &NAS-IP-Address

#
#  We add casts to the LHS if necessary
#
condition &NAS-IP-Address < &PMIP6-Home-IPv4-HoA
data <ipv4prefix>&NAS-IP-Address < &PMIP6-Home-IPv4-HoA

condition &NAS-IP-Address < 192.168/16
data <ipv4prefix>&NAS-IP-Address < 192.168.0.0/16

condition &NAS-IP-Address < "%{echo: 192.168/16}"
data <ipv4prefix>&NAS-IP-Address < "%{echo: 192.168/16}"

condition &NAS-IP-Address < `/bin/echo 192.168/16`
data <ipv4prefix>&NAS-IP-Address < `/bin/echo 192.168/16`