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
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <fstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <eval/token.h>
#include <eval/eval_context.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option_string.h>
#include <dhcp/option_vendor.h>
#include <dhcp/option_vendor_class.h>
#include <log/logger_manager.h>
#include <log/logger_name.h>
#include <log/logger_support.h>
#include <testutils/log_utils.h>

#include <boost/algorithm/string/classification.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/algorithm/string/constants.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/algorithm/string/split.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/scoped_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <arpa/inet.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc::dhcp;
using namespace isc::asiolink;
using namespace isc::log;
using namespace isc::dhcp::test;

namespace {

/// @brief Test fixture for testing Tokens.
///
/// This class provides several convenience objects to be used during testing
/// of the Token family of classes.

class TokenTest : public LogContentTest {
public:

    /// @brief Initializes Pkt4, Pkt6 and options that can be useful for
    ///        evaluation tests.
    TokenTest() {
        pkt4_.reset(new Pkt4(DHCPDISCOVER, 12345));
        pkt6_.reset(new Pkt6(DHCPV6_SOLICIT, 12345));

        // Add options with easily identifiable strings in them
        option_str4_.reset(new OptionString(Option::V4, 100, "hundred4"));
        option_str6_.reset(new OptionString(Option::V6, 100, "hundred6"));

        pkt4_->addOption(option_str4_);
        pkt6_->addOption(option_str6_);

        // Change this to true if you need extra information about logging
        // checks to be printed.
        logCheckVerbose(false);
    }

    /// @brief Test evaluation.
    ///
    /// Check that evaluation does not fail and returns 0.
    ///
    /// @param token the pointer to the token to evaluate
    /// @param pkt the packet (*pkt4_ or *pkt6_)
    /// @param values the stack of values (values_)
    void testEvaluate(const TokenPtr& token, Pkt& pkt, ValueStack& values) {
        unsigned next(123);
        ASSERT_NO_THROW(next = token->evaluate(pkt, values));
        EXPECT_EQ(0, next);
    }

    /// @brief Inserts RAI option with several suboptions
    ///
    /// The structure inserted is:
    ///  - RAI (option 82)
    ///      - option 1 (containing string "one")
    ///      - option 13 (containing string "thirteen")
    void insertRelay4Option() {

        // RAI (Relay Agent Information) option
        OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
        OptionPtr sub1(new OptionString(Option::V4, 1, "one"));
        OptionPtr sub13(new OptionString(Option::V4, 13, "thirteen"));

        rai->addOption(sub1);
        rai->addOption(sub13);
        pkt4_->addOption(rai);
    }

    /// @brief Adds relay encapsulations with some suboptions
    ///
    /// This will add 2 relay encapsulations all will have
    /// msg_type of RELAY_FORW
    /// Relay 0 (closest to server) will have
    /// linkaddr = peeraddr = 0, hop-count = 1
    /// option 100 "hundred.zero", option 101 "hundredone.zero"
    /// Relay 1 (closest to client) will have
    /// linkaddr 1::1= peeraddr = 1::2, hop-count = 0
    /// option 100 "hundred.one", option 102 "hundredtwo.one"
    void addRelay6Encapsulations() {
        // First relay
        Pkt6::RelayInfo relay0;
        relay0.msg_type_ = DHCPV6_RELAY_FORW;
        relay0.hop_count_ = 1;
        relay0.linkaddr_ = isc::asiolink::IOAddress("::");
        relay0.peeraddr_ = isc::asiolink::IOAddress("::");
        OptionPtr optRelay01(new OptionString(Option::V6, 100,
                                              "hundred.zero"));
        OptionPtr optRelay02(new OptionString(Option::V6, 101,
                                              "hundredone.zero"));

        relay0.options_.insert(make_pair(optRelay01->getType(), optRelay01));
        relay0.options_.insert(make_pair(optRelay02->getType(), optRelay02));

        pkt6_->addRelayInfo(relay0);
        // Second relay
        Pkt6::RelayInfo relay1;
        relay1.msg_type_ = DHCPV6_RELAY_FORW;
        relay1.hop_count_ = 0;
        relay1.linkaddr_ = isc::asiolink::IOAddress("1::1");
        relay1.peeraddr_ = isc::asiolink::IOAddress("1::2");
        OptionPtr optRelay11(new OptionString(Option::V6, 100,
                                              "hundred.one"));
        OptionPtr optRelay12(new OptionString(Option::V6, 102,
                                              "hundredtwo.one"));

        relay1.options_.insert(make_pair(optRelay11->getType(), optRelay11));
        relay1.options_.insert(make_pair(optRelay12->getType(), optRelay12));
        pkt6_->addRelayInfo(relay1);
    }

    /// @brief Verify that the relay6 option evaluations work properly
    ///
    /// Given the nesting level and option code extract the option
    /// and compare it to the expected string.
    ///
    /// @param test_level The nesting level
    /// @param test_code The code of the option to extract
    /// @param result_addr The expected result of the address as a string
    void verifyRelay6Option(const int8_t test_level,
                            const uint16_t test_code,
                            const TokenOption::RepresentationType& test_rep,
                            const std::string& result_string) {
        // Create the token
        ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(test_level,
                                                       test_code,
                                                       test_rep)));

        // We should be able to evaluate it
        testEvaluate(t_, *pkt6_, values_);

        // We should have one value on the stack
        ASSERT_EQ(1, values_.size());

        // And it should match the expected result
        // Invalid nesting levels result in a 0 length string
        EXPECT_EQ(result_string, values_.top());

        // Then we clear the stack
        clearStack();
    }

    /// @brief Verify that the relay6 field evaluations work properly
    ///
    /// Given the nesting level, the field to extract and the expected
    /// address create a token and evaluate it then compare the addresses
    ///
    /// @param test_level The nesting level
    /// @param test_field The type of the field to extract
    /// @param result_addr The expected result of the address as a string
    void verifyRelay6Eval(const int8_t test_level,
                          const TokenRelay6Field::FieldType test_field,
                          const int result_len,
                          const uint8_t result_addr[]) {
        // Create the token
        ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(test_level, test_field)));

        // We should be able to evaluate it
        testEvaluate(t_, *pkt6_, values_);

        // We should have one value on the stack
        ASSERT_EQ(1, values_.size());

        // And it should match the expected result
        // Invalid nesting levels result in a 0 length string
        EXPECT_EQ(result_len, values_.top().size());
        if (result_len != 0) {
            EXPECT_EQ(0, memcmp(result_addr, &values_.top()[0], result_len));
        }

        // Then we clear the stack
        clearStack();
    }

    /// @brief Convenience function. Removes token and values stacks.
    /// @param token specifies if the convenience token should be removed or not
    void clearStack(bool token = true) {
        while (!values_.empty()) {
            values_.pop();
        }
        if (token) {
            t_.reset();
        }
    }

    /// @brief Aux. function that stores integer values as 4 byte string.
    ///
    /// @param value integer value to be stored
    /// @return 4 byte long string with encoded value.
    string encode(uint32_t value) {
        return EvalContext::fromUint32(value);
    }

    TokenPtr t_; ///< Just a convenience pointer

    ValueStack values_; ///< evaluated values will be stored here

    Pkt4Ptr pkt4_; ///< A stub DHCPv4 packet
    Pkt6Ptr pkt6_; ///< A stub DHCPv6 packet

    OptionPtr option_str4_; ///< A string option for DHCPv4
    OptionPtr option_str6_; ///< A string option for DHCPv6

    OptionVendorPtr vendor_; ///< Vendor option used during tests
    OptionVendorClassPtr vendor_class_; ///< Vendor class option used during tests

    /// @brief Verify that the substring eval works properly
    ///
    /// This function takes the parameters and sets up the value
    /// stack then executes the eval and checks the results.
    ///
    /// @param test_string The string to operate on
    /// @param test_start The position to start when getting a substring
    /// @param test_length The length of the substring to get
    /// @param result_string The expected result of the eval
    /// @param should_throw The eval will throw
    void verifySubstringEval(const std::string& test_string,
                             const std::string& test_start,
                             const std::string& test_length,
                             const std::string& result_string,
                             bool should_throw = false) {

        // create the token
        ASSERT_NO_THROW(t_.reset(new TokenSubstring()));

        // push values on stack
        values_.push(test_string);
        values_.push(test_start);
        values_.push(test_length);

        // evaluate the token
        if (should_throw) {
            EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
            ASSERT_EQ(0, values_.size());
        } else {
            testEvaluate(t_, *pkt4_, values_);

            // verify results
            ASSERT_EQ(1, values_.size());
            EXPECT_EQ(result_string, values_.top());

            // remove result
            values_.pop();
        }
    }

    /// @brief Verify that the split eval works properly
    ///
    /// This function takes the parameters and sets up the value
    /// stack then executes the eval and checks the results.
    ///
    /// @param test_string The string to operate on
    /// @param test_delimiters The string of delimiter characters to split upon
    /// @param test_field The field number of the desired field
    /// @param result_string The expected result of the eval
    /// @param should_throw The eval will throw
    void verifySplitEval(const std::string& test_string,
                         const std::string& test_delimiters,
                         const std::string& test_field,
                         const std::string& result_string,
                         bool should_throw = false) {
        // create the token
        ASSERT_NO_THROW(t_.reset(new TokenSplit()));

        // push values on stack
        values_.push(test_string);
        values_.push(test_delimiters);
        values_.push(test_field);

        // evaluate the token
        if (should_throw) {
            EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
            ASSERT_EQ(0, values_.size());
        } else {
            testEvaluate(t_, *pkt4_, values_);

            // verify results
            ASSERT_EQ(1, values_.size());
            EXPECT_EQ(result_string, values_.top());

            // remove result
            values_.pop();
        }
    }

    /// @brief Creates vendor-option with specified value and adds it to packet
    ///
    /// This method creates specified vendor option, removes any existing
    /// vendor options and adds the new one to v4 or v6 packet.
    ///
    /// @param u universe (V4 or V6)
    /// @param vendor_id specifies enterprise-id value.
    void setVendorOption(Option::Universe u, uint32_t vendor_id) {
        vendor_.reset(new OptionVendor(u, vendor_id));
        switch (u) {
        case Option::V4:
            pkt4_->delOption(DHO_VIVSO_SUBOPTIONS);
            pkt4_->addOption(vendor_);
            break;
        case Option::V6:
            pkt6_->delOption(D6O_VENDOR_OPTS);
            pkt6_->addOption(vendor_);
            break;
        }
    }

    /// @brief Creates vendor-class option with specified values and adds it to packet
    ///
    /// This method creates specified vendor-class option, removes any existing
    /// vendor class options and adds the new one to v4 or v6 packet.
    /// It also creates data tuples with greek alphabet names.
    ///
    /// @param u universe (V4 or V6)
    /// @param vendor_id specifies enterprise-id value.
    /// @param tuples_size number of data tuples to create.
    void setVendorClassOption(Option::Universe u, uint32_t vendor_id,
                              size_t tuples_size = 0) {
        // Create the option first.
        vendor_class_.reset(new OptionVendorClass(u, vendor_id));

        // Now let's add specified number of data tuples
        OpaqueDataTuple::LengthFieldType len = (u == Option::V4?OpaqueDataTuple::LENGTH_1_BYTE:
                                                OpaqueDataTuple::LENGTH_2_BYTES);
        const char* content[] = { "alpha", "beta", "delta", "gamma", "epsilon",
                                  "zeta", "eta", "theta", "iota", "kappa" };
        const size_t nb_content = sizeof(content) / sizeof(char*);
        ASSERT_TRUE(tuples_size < nb_content);
        for (size_t i = 0; i < tuples_size; ++i) {
            OpaqueDataTuple tuple(len);
            tuple.assign(string(content[i]));
            if (u == Option::V4 && i == 0) {
                // vendor-class for v4 has a peculiar quirk. The first tuple is being
                // added, even if there's no data at all.
                vendor_class_->setTuple(0, tuple);
            } else {
                vendor_class_->addTuple(tuple);
            }
        }

        switch (u) {
        case Option::V4:
            pkt4_->delOption(DHO_VIVCO_SUBOPTIONS);
            pkt4_->addOption(vendor_class_);
            break;
        case Option::V6:
            pkt6_->delOption(D6O_VENDOR_CLASS);
            pkt6_->addOption(vendor_class_);
            break;
        }
    }

    /// @brief Auxiliary function that evaluates tokens and checks result
    ///
    /// Depending on the universe, either pkt4_ or pkt6_ are supposed to have
    /// all the necessary values and options set. The result is checked
    /// on the values_ stack.
    ///
    /// @param u universe (V4 or V6)
    /// @param expected_result text representation of the expected outcome
    void evaluate(Option::Universe u, std::string expected_result) {
        switch (u) {
        case Option::V4:
            testEvaluate(t_, *pkt4_, values_);
            break;
        case Option::V6:
            testEvaluate(t_, *pkt6_, values_);
            break;
        default:
            ADD_FAILURE() << "Invalid universe specified.";
        }
        ASSERT_EQ(1, values_.size());
        EXPECT_EQ(expected_result, values_.top());
    }

    /// @brief Tests if vendor token behaves properly.
    ///
    /// @param u universe (V4 or V6)
    /// @param token_vendor_id enterprise-id used in the token
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param expected_result text representation of the expected outcome
    void testVendorExists(Option::Universe u, uint32_t token_vendor_id,
                          uint32_t option_vendor_id,
                          const std::string& expected_result) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        // Create the token
        ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id,
                                                 TokenOption::EXISTS)));

        // If specified option is non-zero, create it.
        if (option_vendor_id) {
            setVendorOption(u, option_vendor_id);
        }

        evaluate(u, expected_result);
    }

    /// @brief Tests if vendor token properly returns enterprise-id.
    ///
    /// @param u universe (V4 or V6)
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param expected_result text representation of the expected outcome
    void testVendorEnterprise(Option::Universe u, uint32_t option_vendor_id,
                              const std::string& expected_result) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenVendor(u, 0, TokenVendor::ENTERPRISE_ID)));
        if (option_vendor_id) {
            setVendorOption(u, option_vendor_id);
        }

        evaluate(u, expected_result);
    }

    /// @brief Tests if vendor class token properly returns enterprise-id.
    ///
    /// @param u universe (V4 or V6)
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param expected_result text representation of the expected outcome
    void testVendorClassEnterprise(Option::Universe u, uint32_t option_vendor_id,
                                   const std::string& expected_result) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, 0, TokenVendor::ENTERPRISE_ID)));
        if (option_vendor_id) {
            setVendorClassOption(u, option_vendor_id);
        }

        evaluate(u, expected_result);
    }

    /// @brief Tests if vendor class token can report existence properly.
    ///
    /// @param u universe (V4 or V6)
    /// @param token_vendor_id enterprise-id used in the token
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param expected_result text representation of the expected outcome
    void testVendorClassExists(Option::Universe u, uint32_t token_vendor_id,
                               uint32_t option_vendor_id,
                               const std::string& expected_result) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id,
                                                      TokenOption::EXISTS)));

        if (option_vendor_id) {
            setVendorClassOption(u, option_vendor_id);
        }

        evaluate(u, expected_result);
    }

    /// @brief Tests if vendor token can handle sub-options properly.
    ///
    /// @param u universe (V4 or V6)
    /// @param token_vendor_id enterprise-id used in the token
    /// @param token_option_code option code in the token
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param option_code sub-option code (0 means don't create suboption)
    /// @param repr representation (TokenOption::EXISTS or HEXADECIMAL)
    /// @param expected_result text representation of the expected outcome
    void testVendorSuboption(Option::Universe u,
                             uint32_t token_vendor_id, uint16_t token_option_code,
                             uint32_t option_vendor_id, uint16_t option_code,
                             TokenOption::RepresentationType repr,
                             const std::string& expected) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id, repr,
                                                 token_option_code)));
        if (option_vendor_id) {
            setVendorOption(u, option_vendor_id);
            if (option_code) {
                ASSERT_TRUE(vendor_);
                OptionPtr subopt(new OptionString(u, option_code, "alpha"));
                vendor_->addOption(subopt);
            }
        }

        evaluate(u, expected);
    }

    /// @brief Tests if vendor class token can handle data chunks properly.
    ///
    /// @param u universe (V4 or V6)
    /// @param token_vendor_id enterprise-id used in the token
    /// @param token_index data index used in the token
    /// @param option_vendor_id enterprise-id used in option (0 means don't
    ///        create the option)
    /// @param data_tuples number of data tuples in the option
    /// @param expected_result text representation of the expected outcome
    void testVendorClassData(Option::Universe u,
                             uint32_t token_vendor_id, uint16_t token_index,
                             uint32_t option_vendor_id, uint16_t data_tuples,
                             const std::string& expected) {
        // Let's clear any old values, so we can run multiple cases in each test
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id,
                                                      TokenVendor::DATA, token_index)));
        if (option_vendor_id) {
            setVendorClassOption(u, option_vendor_id, data_tuples);
        }

        evaluate(u, expected);
    }

    /// @brief Tests if TokenInteger evaluates to the proper value
    ///
    /// @param expected expected string representation on stack after evaluation
    /// @param value integer value passed to constructor
    void testInteger(const std::string& expected, uint32_t value) {

        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenInteger(value)));

        // The universe (v4 or v6) shouldn't have any impact on this,
        // but let's check it anyway.
        evaluate(Option::V4, expected);

        clearStack(false);
        evaluate(Option::V6, expected);

        clearStack(true);
    }

    /// @brief Tests if TokenMatch works as expected.
    ///
    /// @param reg_exp regular expression
    /// @param value value to match
    /// @param matched expected outcome of the match
    void testMatch(const std::string& reg_exp, const std::string& value,
                   bool matched) {
        clearStack();

        ASSERT_NO_THROW(t_.reset(new TokenMatch(reg_exp)));
        values_.push(value);
        // BTW not known easy way to get a runtime error with C++ regex.
        ASSERT_NO_THROW(evaluate(Option::V4, matched ? "true" : "false"));

        clearStack();
    }
};

// This tests the toBool() conversions
TEST_F(TokenTest, toBool) {<--- syntax error

    ASSERT_NO_THROW(Token::toBool("true"));
    EXPECT_TRUE(Token::toBool("true"));
    ASSERT_NO_THROW(Token::toBool("false"));
    EXPECT_FALSE(Token::toBool("false"));

    // Token::toBool() is case-sensitive
    EXPECT_THROW(Token::toBool("True"), EvalTypeError);
    EXPECT_THROW(Token::toBool("TRUE"), EvalTypeError);

    // Proposed aliases
    EXPECT_THROW(Token::toBool("1"), EvalTypeError);
    EXPECT_THROW(Token::toBool("0"), EvalTypeError);
    EXPECT_THROW(Token::toBool(""), EvalTypeError);
}

// This simple test checks that a TokenString, representing a constant string,
// can be used in Pkt4 evaluation. (The actual packet is not used)
TEST_F(TokenTest, string4) {

    // Store constant string "foo" in the TokenString object.
    ASSERT_NO_THROW(t_.reset(new TokenString("foo")));
    EXPECT_EQ(0, t_->getLabel());

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("foo", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_STRING", "Pushing text string 'foo'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenString, representing a constant string,
// can be used in Pkt4 evaluation. (The actual packet is not used)
TEST_F(TokenTest, string4Complex) {
    char data[] = "12345~!@#$%^&*()_+{}[];:<>/?\\67890\t \0\b\r\f'\""
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string data_str(data, sizeof(data) - 1);
    // Store constant string in the TokenString object.
    ASSERT_NO_THROW(t_.reset(new TokenString(data_str)));

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ(data_str, values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    char expected[] = "Pushing text string '"
                      "12345~!@#$%^&*()_+{}[];:<>/?\\67890\t \0\b\r\f'\""
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'";
    string expected_str(expected, sizeof(expected) - 1);
    addString("EVAL_DEBUG_STRING", expected_str, pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenString, representing a constant string,
// can be used in Pkt6 evaluation. (The actual packet is not used)
TEST_F(TokenTest, string6) {

    // Store constant string "foo" in the TokenString object.
    ASSERT_NO_THROW(t_.reset(new TokenString("foo")));

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("foo", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_STRING", "Pushing text string 'foo'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenString, representing a constant string,
// can be used in Pkt6 evaluation. (The actual packet is not used)
TEST_F(TokenTest, string6Complex) {
    char data[] = "12345~!@#$%^&*()_+{}[];:<>/?\\67890\t \0\b\r\f'\""
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string data_str(data, sizeof(data) - 1);
    // Store constant string in the TokenString object.
    ASSERT_NO_THROW(t_.reset(new TokenString(data_str)));

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ(data_str, values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    char expected[] = "Pushing text string '"
                      "12345~!@#$%^&*()_+{}[];:<>/?\\67890\t \0\b\r\f'\""
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'";
    string expected_str(expected, sizeof(expected) - 1);
    addString("EVAL_DEBUG_STRING", expected_str, pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenHexString, representing a constant
// string coded in hexadecimal, can be used in Pkt4 evaluation.
// (The actual packet is not used)
TEST_F(TokenTest, hexstring4) {
    TokenPtr empty;
    TokenPtr bad;
    TokenPtr nodigit;
    TokenPtr baddigit;
    TokenPtr bell;
    TokenPtr foo;
    TokenPtr cookie;

    // Store constant empty hexstring "" ("") in the TokenHexString object.
    ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
    EXPECT_EQ(0, empty->getLabel());
    // Store bad encoded hexstring "0abc" ("").
    ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
    // Store hexstring with no digits "0x" ("").
    ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
    // Store hexstring with a bad hexdigit "0xxabc" ("").
    ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
    // Store hexstring with an odd number of hexdigits "0x7" ("\a").
    ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
    // Store constant hexstring "0x666f6f" ("foo").
    ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
    // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
    ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));

    // Make sure that tokens can be evaluated without exceptions,
    // and verify the debug output
    testEvaluate(empty, *pkt4_, values_);
    testEvaluate(bad, *pkt4_, values_);
    testEvaluate(nodigit, *pkt4_, values_);
    testEvaluate(baddigit, *pkt4_, values_);
    testEvaluate(bell, *pkt4_, values_);
    testEvaluate(foo, *pkt4_, values_);
    testEvaluate(cookie, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(7, values_.size());
    uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
    EXPECT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
    values_.pop();
    EXPECT_EQ("foo", values_.top());
    values_.pop();
    EXPECT_EQ("\a", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x07", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x666F6F", pkt4_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x63825363", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenHexString, representing a constant
// string coded in hexadecimal, can be used in Pkt6 evaluation.
// (The actual packet is not used)
TEST_F(TokenTest, hexstring6) {
    TokenPtr empty;
    TokenPtr bad;
    TokenPtr nodigit;
    TokenPtr baddigit;
    TokenPtr bell;
    TokenPtr foo;
    TokenPtr cookie;

    // Store constant empty hexstring "" ("") in the TokenHexString object.
    ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
    // Store bad encoded hexstring "0abc" ("").
    ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
    // Store hexstring with no digits "0x" ("").
    ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
    // Store hexstring with a bad hexdigit "0xxabc" ("").
    ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
    // Store hexstring with an odd number of hexdigits "0x7" ("\a").
    ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
    // Store constant hexstring "0x666f6f" ("foo").
    ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
    // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
    ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));

    // Make sure that tokens can be evaluated without exceptions.
    testEvaluate(empty, *pkt6_, values_);
    testEvaluate(bad, *pkt6_, values_);
    testEvaluate(nodigit, *pkt6_, values_);
    testEvaluate(baddigit, *pkt6_, values_);
    testEvaluate(bell, *pkt6_, values_);
    testEvaluate(foo, *pkt6_, values_);
    testEvaluate(cookie, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(7, values_.size());
    uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
    EXPECT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
    values_.pop();
    EXPECT_EQ("foo", values_.top());
    values_.pop();
    EXPECT_EQ("\a", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());
    values_.pop();
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x07", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x666F6F", pkt6_->getLabel());
    addString("EVAL_DEBUG_HEXSTRING", "Pushing hex string 0x63825363", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenLowerCase, representing a string
// converted to lower case, can be used in Pkt evaluation.  (The actual packet
// is not used)
TEST_F(TokenTest, lcase) {
    ASSERT_NO_THROW(t_.reset(new TokenLowerCase()));
    EXPECT_EQ(0, t_->getLabel());
    values_.push("LoWeR");

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("lower", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_LCASE",
              "Popping string 'LoWeR' and pushing converted value to lower case 'lower'",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenLowerCase, representing a complex string
// converted to lower case, can be used in Pkt evaluation.  (The actual packet
// is not used)
TEST_F(TokenTest, lcaseComplex) {
    ASSERT_NO_THROW(t_.reset(new TokenLowerCase()));
    char data[] = "12345~!@#$%^&*()_+LoWeR{}[];:<>/?\\67890\t \0\b\r\f";
    string data_str(data, sizeof(data) - 1);
    values_.push(data_str);

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    char expected_data[] = "12345~!@#$%^&*()_+lower{}[];:<>/?\\67890\t \0\b\r\f";
    string expected_data_str(expected_data, sizeof(expected_data) - 1);
    EXPECT_EQ(expected_data_str, values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    char expected[] = "Popping string '"
                      "12345~!@#$%^&*()_+LoWeR{}[];:<>/?\\67890\t \0\b\r\f' "
                      "and pushing converted value to lower case '"
                      "12345~!@#$%^&*()_+lower{}[];:<>/?\\67890\t \0\b\r\f'";
    string expected_str(expected, sizeof(expected) - 1);
    addString("EVAL_DEBUG_LCASE", expected_str, pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenUpperCase, representing a string
// converted to upper case, can be used in Pkt evaluation.  (The actual packet
// is not used)
TEST_F(TokenTest, ucase) {
    ASSERT_NO_THROW(t_.reset(new TokenUpperCase()));
    EXPECT_EQ(0, t_->getLabel());
    values_.push("uPpEr");

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("UPPER", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_UCASE",
              "Popping string 'uPpEr' and pushing converted value to upper case 'UPPER'",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This simple test checks that a TokenUpperCase, representing a complex string
// converted to upper case, can be used in Pkt evaluation.  (The actual packet
// is not used)
TEST_F(TokenTest, ucaseComplex) {
    ASSERT_NO_THROW(t_.reset(new TokenUpperCase()));
    char data[] = "12345~!@#$%^&*()_+uPpEr{}[];:<>/?\\67890\t \0\b\r\f";
    string data_str(data, sizeof(data) - 1);
    values_.push(data_str);

    // Make sure that the token can be evaluated without exceptions.
    testEvaluate(t_, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());
    char expected_data[] = "12345~!@#$%^&*()_+UPPER{}[];:<>/?\\67890\t \0\b\r\f";
    string expected_data_str(expected_data, sizeof(expected_data) - 1);
    EXPECT_EQ(expected_data_str, values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    char expected[] = "Popping string '"
                      "12345~!@#$%^&*()_+uPpEr{}[];:<>/?\\67890\t \0\b\r\f' "
                      "and pushing converted value to upper case '"
                      "12345~!@#$%^&*()_+UPPER{}[];:<>/?\\67890\t \0\b\r\f'";
    string expected_str(expected, sizeof(expected) - 1);
    addString("EVAL_DEBUG_UCASE", expected_str, pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that a TokenIpAddress, representing an IP address as
// a constant string, can be used in Pkt4/Pkt6 evaluation.
// (The actual packet is not used)
TEST_F(TokenTest, ipaddress) {
    TokenPtr bad4;
    TokenPtr bad6;
    TokenPtr ip4;
    TokenPtr ip6;

    // Bad IP addresses
    ASSERT_NO_THROW(bad4.reset(new TokenIpAddress("10.0.0.0.1")));
    ASSERT_NO_THROW(bad6.reset(new TokenIpAddress(":::")));

    // IP addresses
    ASSERT_NO_THROW(ip4.reset(new TokenIpAddress("10.0.0.1")));
    EXPECT_EQ(0, ip4->getLabel());
    ASSERT_NO_THROW(ip6.reset(new TokenIpAddress("2001:db8::1")));

    // Make sure that tokens can be evaluated without exceptions.
    testEvaluate(ip4, *pkt4_, values_);
    testEvaluate(ip6, *pkt6_, values_);
    testEvaluate(bad4, *pkt4_, values_);
    testEvaluate(bad6, *pkt6_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(4, values_.size());

    // Check bad addresses (they pushed '' on the value stack)
    EXPECT_EQ(0, values_.top().size());
    values_.pop();
    EXPECT_EQ(0, values_.top().size());
    values_.pop();

    // Check IPv6 address
    uint8_t expected6[] = { 0x20, 1, 0xd, 0xb8, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0, 0, 1 };
    EXPECT_EQ(16, values_.top().size());
    EXPECT_EQ(0, memcmp(expected6, &values_.top()[0], 16));
    values_.pop();

    // Check IPv4 address
    uint8_t expected4[] = { 10, 0, 0, 1 };
    EXPECT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(expected4, &values_.top()[0], 4));

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_IPADDRESS", "Pushing IPAddress 0x0A000001", pkt4_->getLabel());
    addString("EVAL_DEBUG_IPADDRESS", "Pushing IPAddress 0x20010DB8000000000000000000000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_IPADDRESS", "Pushing IPAddress 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_IPADDRESS", "Pushing IPAddress 0x", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that a TokenIpAddressToText, representing an IP address as
// a string, can be used in Pkt4/Pkt6 evaluation.
// (The actual packet is not used)
TEST_F(TokenTest, addressToText) {
    TokenPtr address((new TokenIpAddressToText()));
    EXPECT_EQ(0, address->getLabel());
    std::vector<uint8_t> bytes;

    std::string value = "10.0.0.1";
    values_.push(value);

    // Invalid data size fails.
    EXPECT_THROW(address->evaluate(*pkt4_, values_), EvalTypeError);

    bytes = IOAddress(value).toBytes();
    values_.push(std::string(bytes.begin(), bytes.end()));

    testEvaluate(address, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());

    value = "2001:db8::1";
    bytes = IOAddress(value).toBytes();
    values_.push(std::string(bytes.begin(), bytes.end()));

    testEvaluate(address, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(2, values_.size());

    values_.push(std::string());
    testEvaluate(address, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(3, values_.size());

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check IPv6 address
    EXPECT_EQ(11, values_.top().size());
    EXPECT_EQ("2001:db8::1", values_.top());
    values_.pop();

    // Check IPv4 address
    EXPECT_EQ(8, values_.top().size());
    EXPECT_EQ("10.0.0.1", values_.top());
    values_.pop();

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_IPADDRESSTOTEXT", "Pushing IPAddress 10.0.0.1", pkt4_->getLabel());
    addString("EVAL_DEBUG_IPADDRESSTOTEXT", "Pushing IPAddress 2001:db8::1", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that a TokenIntToText, representing an integer as a string,
// can be used in Pkt4/Pkt6 evaluation.
// (The actual packet is not used)
TEST_F(TokenTest, integerToText) {
    TokenPtr int8token((new TokenInt8ToText()));
    EXPECT_EQ(0, int8token->getLabel());
    TokenPtr int16token((new TokenInt16ToText()));
    EXPECT_EQ(0, int16token->getLabel());
    TokenPtr int32token((new TokenInt32ToText()));
    EXPECT_EQ(0, int32token->getLabel());
    TokenPtr uint8token((new TokenUInt8ToText()));
    EXPECT_EQ(0, uint8token->getLabel());
    TokenPtr uint16token((new TokenUInt16ToText()));
    EXPECT_EQ(0, uint16token->getLabel());
    TokenPtr uint32token((new TokenUInt32ToText()));
    EXPECT_EQ(0, uint32token->getLabel());

    std::vector<uint8_t> bytes;
    std::string value = "0123456789";

    // Invalid data size fails.
    values_.push(value);
    EXPECT_THROW(int8token->evaluate(*pkt4_, values_), EvalTypeError);
    values_.push(value);
    EXPECT_THROW(int16token->evaluate(*pkt4_, values_), EvalTypeError);
    values_.push(value);
    EXPECT_THROW(int32token->evaluate(*pkt4_, values_), EvalTypeError);
    values_.push(value);
    EXPECT_THROW(uint8token->evaluate(*pkt4_, values_), EvalTypeError);
    values_.push(value);
    EXPECT_THROW(uint16token->evaluate(*pkt4_, values_), EvalTypeError);
    values_.push(value);
    EXPECT_THROW(uint32token->evaluate(*pkt4_, values_), EvalTypeError);

    uint64_t data = -1;

    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&data)), sizeof(int8_t)));

    testEvaluate(int8token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(1, values_.size());

    int16_t i16 = 0;
    memcpy(&i16, &data, sizeof(int16_t));
    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&i16)), sizeof(int16_t)));

    testEvaluate(int16token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(2, values_.size());

    int32_t i32 = 0;
    memcpy(&i32, &data, sizeof(int32_t));
    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&i32)), sizeof(int32_t)));

    testEvaluate(int32token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(3, values_.size());

    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&data)), sizeof(uint8_t)));

    testEvaluate(uint8token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(4, values_.size());

    uint16_t ui16 = 0;
    memcpy(&ui16, &data, sizeof(uint16_t));
    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&ui16)), sizeof(uint16_t)));

    testEvaluate(uint16token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(5, values_.size());

    uint32_t ui32 = 0;
    memcpy(&ui32, &data, sizeof(uint32_t));
    values_.push(
        std::string(const_cast<const char*>(reinterpret_cast<char*>(&ui32)), sizeof(uint32_t)));

    testEvaluate(uint32token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(6, values_.size());

    value = "";

    values_.push(value);
    testEvaluate(int8token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(7, values_.size());

    values_.push(value);
    testEvaluate(int16token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(8, values_.size());

    values_.push(value);
    testEvaluate(int32token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(9, values_.size());

    values_.push(value);
    testEvaluate(uint8token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(10, values_.size());

    values_.push(value);
    testEvaluate(uint16token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(11, values_.size());

    values_.push(value);
    testEvaluate(uint32token, *pkt4_, values_);

    // Check that the evaluation put its value on the values stack.
    ASSERT_EQ(12, values_.size());

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check empty data
    EXPECT_EQ(0, values_.top().size());
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Check uint32
    EXPECT_EQ(10, values_.top().size());
    EXPECT_EQ("4294967295", values_.top());
    values_.pop();

    // Check uint16
    EXPECT_EQ(5, values_.top().size());
    EXPECT_EQ("65535", values_.top());
    values_.pop();

    // Check uint8
    EXPECT_EQ(3, values_.top().size());
    EXPECT_EQ("255", values_.top());
    values_.pop();

    // Check int32
    EXPECT_EQ(2, values_.top().size());
    EXPECT_EQ("-1", values_.top());
    values_.pop();

    // Check int16
    EXPECT_EQ(2, values_.top().size());
    EXPECT_EQ("-1", values_.top());
    values_.pop();

    // Check int8
    EXPECT_EQ(2, values_.top().size());
    EXPECT_EQ("-1", values_.top());
    values_.pop();

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_INT8TOTEXT", "Pushing Int8 -1", pkt4_->getLabel());
    addString("EVAL_DEBUG_INT16TOTEXT", "Pushing Int16 -1", pkt4_->getLabel());
    addString("EVAL_DEBUG_INT32TOTEXT", "Pushing Int32 -1", pkt4_->getLabel());
    addString("EVAL_DEBUG_UINT8TOTEXT", "Pushing UInt8 255", pkt4_->getLabel());
    addString("EVAL_DEBUG_UINT16TOTEXT", "Pushing UInt16 65535", pkt4_->getLabel());
    addString("EVAL_DEBUG_UINT32TOTEXT", "Pushing UInt32 4294967295", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an option value is able to extract
// the option from an IPv4 packet and properly store the option's value.
TEST_F(TokenTest, optionString4) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
    EXPECT_EQ(0, found->getLabel());
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));

    // This should evaluate to the content of the option 100 (i.e. "hundred4")
    testEvaluate(found, *pkt4_, values_);

    // This should evaluate to "" as there is no option 101.
    testEvaluate(not_found, *pkt4_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed. We should get the empty
    // string first.
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Then the content of the option 100.
    EXPECT_EQ("hundred4", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred4'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value ''", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing option value is able to extract
// the option from an IPv4 packet and properly store its value in a
// hexadecimal format.
TEST_F(TokenTest, optionHexString4) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL)));
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL)));

    // This should evaluate to the content of the option 100 (i.e. "hundred4")
    testEvaluate(found, *pkt4_, values_);

    // This should evaluate to "" as there is no option 101.
    testEvaluate(not_found, *pkt4_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed. We should get the empty
    // string first.
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Then the content of the option 100.
    EXPECT_EQ("hundred4", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 0x68756E6472656434",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 0x", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an option value is able to check
// the existence of the option from an IPv4 packet.
TEST_F(TokenTest, optionExistsString4) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS)));
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS)));

    testEvaluate(found, *pkt4_, values_);
    testEvaluate(not_found, *pkt4_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed.
    EXPECT_EQ("false", values_.top());
    values_.pop();
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 'false'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an option value is able to extract
// the option from an IPv6 packet and properly store the option's value.
TEST_F(TokenTest, optionString6) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));

    // This should evaluate to the content of the option 100 (i.e. "hundred6")
    testEvaluate(found, *pkt6_, values_);

    // This should evaluate to "" as there is no option 101.
    testEvaluate(not_found, *pkt6_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed. We should get the empty
    // string first.
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Then the content of the option 100.
    EXPECT_EQ("hundred6", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred6'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value ''", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an option value is able to extract
// the option from an IPv6 packet and properly store its value in hexadecimal
// format.
TEST_F(TokenTest, optionHexString6) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL)));
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL)));

    // This should evaluate to the content of the option 100 (i.e. "hundred6")
    testEvaluate(found, *pkt6_, values_);

    // This should evaluate to "" as there is no option 101.
    testEvaluate(not_found, *pkt6_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed. We should get the empty
    // string first.
    EXPECT_EQ("", values_.top());
    values_.pop();

    // Then the content of the option 100.
    EXPECT_EQ("hundred6", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 0x68756E6472656436",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 0x", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an option value is able to check
// the existence of the option from an IPv6 packet.
TEST_F(TokenTest, optionExistsString6) {
    TokenPtr found;
    TokenPtr not_found;

    // The packets we use have option 100 with a string in them.
    ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS)));
    ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS)));

    testEvaluate(found, *pkt6_, values_);
    testEvaluate(not_found, *pkt6_, values_);

    // There should be 2 values evaluated.
    ASSERT_EQ(2, values_.size());

    // This is a stack, so the pop order is inversed.
    EXPECT_EQ("false", values_.top());
    values_.pop();
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'true'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 'false'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the existing relay4 option can be found.
TEST_F(TokenTest, relay4Option) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();

    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should be found and relay4[13] should evaluate to the
    // content of that sub-option, i.e. "thirteen"
    EXPECT_EQ("thirteen", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 13 with value 'thirteen'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the code properly handles cases when
// there is a RAI option, but there's no requested sub-option.
TEST_F(TokenTest, relay4OptionNoSuboption) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();

    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(15, TokenOption::TEXTUAL)));
    EXPECT_EQ(0, t_->getLabel());

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should NOT be found (there is no sub-option 15),
    // so the expression should evaluate to ""
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 15 with value ''", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the code properly handles cases when
// there's no RAI option at all.
TEST_F(TokenTest, relay4OptionNoRai) {

    // We didn't call insertRelay4Option(), so there's no RAI option.

    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should NOT be found (there is no sub-option 13),
    // so the expression should evaluate to ""
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 13 with value ''", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that only the RAI is searched for the requested
// sub-option.
TEST_F(TokenTest, relay4RAIOnly) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();

    // Add options 13 and 70 to the packet.
    OptionPtr opt13(new OptionString(Option::V4, 13, "THIRTEEN"));
    OptionPtr opt70(new OptionString(Option::V4, 70, "SEVENTY"));
    pkt4_->addOption(opt13);
    pkt4_->addOption(opt70);

    // The situation is as follows:
    // Packet:
    //  - option 13 (containing "THIRTEEN")
    //  - option 82 (rai)
    //      - option 1 (containing "one")
    //      - option 13 (containing "thirteen")

    // Let's try to get option 13. It should get the one from RAI
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("thirteen", values_.top());

    // Try to get option 1. It should get the one from RAI
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("one", values_.top());

    // Try to get option 70. It should fail, as there's no such
    // sub option in RAI.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("", values_.top());

    // Try to check option 1. It should return "true"
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::EXISTS)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Try to check option 70. It should return "false"
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::EXISTS)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 13 with value 'thirteen'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'one'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 70 with value ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 70 with value 'false'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if we can properly extract an option
// from relay encapsulations.  Our packet has two relay
// encapsulations.  Both include a common option with the
// original message (option 100) and both include their
// own option (101 and 102).  We attempt to extract the
// options and compare them to expected values.  We also
// try to extract an option from an encapsulation
// that doesn't exist (level 2), this should result in an empty
// string.
TEST_F(TokenTest, relay6Option) {
    // We start by adding a set of relay encapsulations to the
    // basic v6 packet.
    addRelay6Encapsulations();

    // Then we work our way through the set of choices
    // Level 0 both options it has and the check that
    // the checking for an option it doesn't have results
    // in an empty string.
    verifyRelay6Option(0, 100, TokenOption::TEXTUAL, "hundred.zero");
    verifyRelay6Option(0, 100, TokenOption::EXISTS, "true");
    verifyRelay6Option(0, 101, TokenOption::TEXTUAL, "hundredone.zero");
    verifyRelay6Option(0, 102, TokenOption::TEXTUAL, "");
    verifyRelay6Option(0, 102, TokenOption::EXISTS, "false");

    // Level 1, again both options it has and the one for level 0
    verifyRelay6Option(1, 100, TokenOption::TEXTUAL, "hundred.one");
    verifyRelay6Option(1, 101, TokenOption::TEXTUAL, "");
    verifyRelay6Option(1, 102, TokenOption::TEXTUAL, "hundredtwo.one");

    // Level 2, no encapsulation so no options
    verifyRelay6Option(2, 100, TokenOption::TEXTUAL, "");

    // Level -1, the same as level 1
    verifyRelay6Option(-1, 100, TokenOption::TEXTUAL, "hundred.one");
    verifyRelay6Option(-1, 101, TokenOption::TEXTUAL, "");
    verifyRelay6Option(-1, 102, TokenOption::TEXTUAL, "hundredtwo.one");

    // Level -2, the same as level 0
    verifyRelay6Option(-2, 100, TokenOption::TEXTUAL, "hundred.zero");
    verifyRelay6Option(-2, 100, TokenOption::EXISTS, "true");
    verifyRelay6Option(-2, 101, TokenOption::TEXTUAL, "hundredone.zero");
    verifyRelay6Option(-2, 102, TokenOption::TEXTUAL, "");
    verifyRelay6Option(-2, 102, TokenOption::EXISTS, "false");

    // Level -3, no encapsulation so no options
    verifyRelay6Option(-3, 100, TokenOption::TEXTUAL, "");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred.zero'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'true'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 'hundredone.zero'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value 'false'", pkt6_->getLabel());

    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred.one'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value 'hundredtwo.one'",
              pkt6_->getLabel());

    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value ''", pkt6_->getLabel());

    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred.one'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value 'hundredtwo.one'",
              pkt6_->getLabel());

    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'hundred.zero'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value 'true'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 101 with value 'hundredone.zero'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 102 with value 'false'", pkt6_->getLabel());

    addString("EVAL_DEBUG_OPTION", "Pushing option 100 with value ''", pkt6_->getLabel());

    EXPECT_TRUE(checkFile());
}

// Verifies that relay6 option requires DHCPv6
TEST_F(TokenTest, relay6OptionError) {
    // Create a relay6 option token
    ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(0, 13, TokenOption::TEXTUAL)));
    EXPECT_EQ(0, t_->getLabel());

    // A DHCPv6 packet is required
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
}

// Verifies that DHCPv4 packet metadata can be extracted.
TEST_F(TokenTest, pkt4MetaData) {
    pkt4_->setIface("eth0");
    pkt4_->setLocalAddr(IOAddress("10.0.0.1"));
    pkt4_->setRemoteAddr(IOAddress("10.0.0.2"));

    // Check interface (expect eth0)
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE)));
    EXPECT_EQ(0, t_->getLabel());
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ("eth0", values_.top());

    // Check source (expect 10.0.0.2)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    vector<uint8_t> a2 = IOAddress("10.0.0.2").toBytes();
    ASSERT_EQ(a2.size(), values_.top().size());
    EXPECT_EQ(0, memcmp(&a2[0], &values_.top()[0], a2.size()));

    // Check destination (expect 10.0.0.1)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    vector<uint8_t> a1 = IOAddress("10.0.0.1").toBytes();
    ASSERT_EQ(a1.size(), values_.top().size());
    EXPECT_EQ(0, memcmp(&a1[0], &values_.top()[0], a1.size()));

    // Check length (expect 249)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    uint32_t length = htonl(static_cast<uint32_t>(pkt4_->len()));
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4));

    // Unknown metadata type fails
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100))));
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data iface with value eth0", pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data src with value 0x0A000002",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data dst with value 0x0A000001",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data len with value 0x000000F9",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Verifies that DHCPv6 packet metadata can be extracted.
TEST_F(TokenTest, pkt6MetaData) {
    pkt6_->setIface("eth0");
    pkt6_->setLocalAddr(IOAddress("ff02::1:2"));
    pkt6_->setRemoteAddr(IOAddress("fe80::1234"));

    // Check interface (expect eth0)
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE)));
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ("eth0", values_.top());

    // Check source (expect fe80::1234)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC)));
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ(16, values_.top().size());
    EXPECT_EQ(0xfe, static_cast<uint8_t>(values_.top()[0]));
    EXPECT_EQ(0x80, static_cast<uint8_t>(values_.top()[1]));
    for (unsigned i = 2; i < 14; ++i) {
        EXPECT_EQ(0, values_.top()[i]);
    }
    EXPECT_EQ(0x12, values_.top()[14]);
    EXPECT_EQ(0x34, values_.top()[15]);

    // Check destination (expect ff02::1:2)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST)));
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    vector<uint8_t> ma = IOAddress("ff02::1:2").toBytes();
    ASSERT_EQ(ma.size(), values_.top().size());
    EXPECT_EQ(0, memcmp(&ma[0], &values_.top()[0], ma.size()));

    // Check length (expect 16)
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN)));
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    uint32_t length = htonl(static_cast<uint32_t>(pkt6_->len()));
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4));

    // Unknown meta data type fails
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100))));
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data iface with value eth0", pkt6_->getLabel());
    addString("EVAL_DEBUG_PKT",
              "Pushing PKT meta data src with value 0xFE800000000000000000000000001234",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_PKT",
              "Pushing PKT meta data dst with value 0xFF020000000000000000000000010002",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_PKT", "Pushing PKT meta data len with value 0x00000010",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Verifies if the DHCPv4 packet fields can be extracted.
TEST_F(TokenTest, pkt4Fields) {
    pkt4_->setGiaddr(IOAddress("192.0.2.1"));
    pkt4_->setCiaddr(IOAddress("192.0.2.2"));
    pkt4_->setYiaddr(IOAddress("192.0.2.3"));
    pkt4_->setSiaddr(IOAddress("192.0.2.4"));

    // We're setting hardware address to uncommon (7 bytes rather than 6 and
    // hardware type 123) HW address. We'll use it in hlen and htype checks.
    HWAddrPtr hw(new HWAddr(HWAddr::fromText("01:02:03:04:05:06:07", 123)));
    pkt4_->setHWAddr(hw);

    // Check hardware address field.
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CHADDR)));
    EXPECT_EQ(0, t_->getLabel());
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    uint8_t expected_hw[] = { 1, 2, 3, 4, 5, 6, 7 };
    ASSERT_EQ(7, values_.top().size());
    EXPECT_EQ(0, memcmp(expected_hw, &values_.top()[0], 7));

    // Check hlen value field.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ(4, values_.top().size());
    uint32_t expected_hlen = htonl(7);
    EXPECT_EQ(0, memcmp(&expected_hlen, &values_.top()[0], 4));

    // Check htype value.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HTYPE)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ(4, values_.top().size());
    uint32_t expected_htype = htonl(123);
    EXPECT_EQ(0, memcmp(&expected_htype, &values_.top()[0], 4));

    // Check giaddr value.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::GIADDR)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    uint8_t expected_addr[] = { 192, 0, 2, 1 };
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));

    // Check ciaddr value.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CIADDR)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    expected_addr[3] = 2;
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));

    // Check yiaddr value.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::YIADDR)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    expected_addr[3] = 3;
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));

    // Check siaddr value.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::SIADDR)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    expected_addr[3] = 4;
    ASSERT_EQ(4, values_.top().size());
    EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));

    // Check msgtype.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::MSGTYPE)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ(4, values_.top().size());
    string exp_msgtype = encode(DHCPDISCOVER);
    EXPECT_EQ(0, memcmp(&exp_msgtype[0], &values_.top()[0], 4));

    // Check transaction-id
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::TRANSID)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    ASSERT_EQ(4, values_.top().size());
    string exp_transid = encode(12345);
    EXPECT_EQ(0, memcmp(&exp_transid[0], &values_.top()[0], 4));

    // Check a DHCPv6 packet throws.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN)));
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // Unknown field fails
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt4(static_cast<TokenPkt4::FieldType>(100))));
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field mac with value 0x01020304050607",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field hlen with value 0x00000007",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field htype with value 0x0000007B",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field giaddr with value 0xC0000201",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field ciaddr with value 0xC0000202",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field yiaddr with value 0xC0000203",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field siaddr with value 0xC0000204",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field msgtype with value 0x00000001",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_PKT4", "Pushing PKT4 field transid with value 0x00003039",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Verifies if the DHCPv6 packet fields can be extracted.
TEST_F(TokenTest, pkt6Fields) {
    // The default test creates a v6 DHCPV6_SOLICIT packet with a
    // transaction id of 12345.

    // Check the message type
    ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::MSGTYPE)));
    EXPECT_EQ(0, t_->getLabel());
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    uint32_t expected = htonl(1);
    EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));

    // Check the transaction id field
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID)));
    testEvaluate(t_, *pkt6_, values_);
    ASSERT_EQ(1, values_.size());
    expected = htonl(12345);
    EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));

    // Check that working with a v4 packet generates an error
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID)));
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Unknown field fails
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenPkt6(static_cast<TokenPkt6::FieldType>(100))));
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_PKT6", "Pushing PKT6 field msgtype with value 0x00000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_PKT6", "Pushing PKT6 field transid with value 0x00003039",
              pkt6_->getLabel());

    EXPECT_TRUE(checkFile());
}

// This test checks if we can properly extract the link and peer
// address fields from relay encapsulations.  Our packet has
// two relay encapsulations.  We attempt to extract the two
// fields from both of the encapsulations and compare them.
// We also try to extract one of the fields from an encapsulation
// that doesn't exist (level 2), this should result in an empty
// string.
TEST_F(TokenTest, relay6Field) {
    // Values for the address results
    uint8_t zeroaddr[] = { 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0 };
    uint8_t linkaddr[] = { 0, 1, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 1 };
    uint8_t peeraddr[] = { 0, 1, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 2 };

    // We start by adding a set of relay encapsulations to the
    // basic v6 packet.
    addRelay6Encapsulations();

    // Then we work our way through the set of choices
    // Level 0 both link and peer address should be 0::0
    verifyRelay6Eval(0, TokenRelay6Field::LINKADDR, 16, zeroaddr);
    verifyRelay6Eval(0, TokenRelay6Field::PEERADDR, 16, zeroaddr);

    // Level 1 link and peer should have different non-zero addresses
    verifyRelay6Eval(1, TokenRelay6Field::LINKADDR, 16, linkaddr);
    verifyRelay6Eval(1, TokenRelay6Field::PEERADDR, 16, peeraddr);

    // Level 2 has no encapsulation so the address should be zero length
    verifyRelay6Eval(2, TokenRelay6Field::LINKADDR, 0, zeroaddr);

    // Level -1 is the same as level 1
    verifyRelay6Eval(-1, TokenRelay6Field::LINKADDR, 16, linkaddr);
    verifyRelay6Eval(-1, TokenRelay6Field::PEERADDR, 16, peeraddr);

    // Level -2 is the same as level 0
    verifyRelay6Eval(-2, TokenRelay6Field::LINKADDR, 16, zeroaddr);
    verifyRelay6Eval(-2, TokenRelay6Field::PEERADDR, 16, zeroaddr);

    // Level -3 has no encapsulation so the address should be zero length
    verifyRelay6Eval(-3, TokenRelay6Field::LINKADDR, 0, zeroaddr);

    // Lets check that the layout of the address returned by the
    // token matches that of the TokenIpAddress
    TokenPtr trelay;
    TokenPtr taddr;
    TokenPtr tequal;
    ASSERT_NO_THROW(trelay.reset(new TokenRelay6Field(1, TokenRelay6Field::LINKADDR)));
    EXPECT_EQ(0, trelay->getLabel());
    ASSERT_NO_THROW(taddr.reset(new TokenIpAddress("1::1")));
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));

    testEvaluate(trelay, *pkt6_, values_);
    testEvaluate(taddr, *pkt6_, values_);
    testEvaluate(tequal, *pkt6_, values_);

    // We should have a single value on the stack and it should be "true"
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // be tidy
    clearStack();

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field linkaddr nest 0 with value "
              "0x00000000000000000000000000000000",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field peeraddr nest 0 with value "
              "0x00000000000000000000000000000000",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field linkaddr nest 1 with value "
              "0x00010000000000000000000000000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field peeraddr nest 1 with value "
              "0x00010000000000000000000000000002",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6_RANGE", "Pushing PKT6 relay field linkaddr nest 2 with value 0x",
              pkt6_->getLabel());

    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field linkaddr nest -1 with value "
              "0x00010000000000000000000000000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field peeraddr nest -1 with value "
              "0x00010000000000000000000000000002",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field linkaddr nest -2 with value "
              "0x00000000000000000000000000000000",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field peeraddr nest -2 with value "
              "0x00000000000000000000000000000000",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_RELAY6_RANGE", "Pushing PKT6 relay field linkaddr nest -3 with value 0x",
              pkt6_->getLabel());

    addString("EVAL_DEBUG_RELAY6",
              "Pushing PKT6 relay field linkaddr nest 1 with value "
              "0x00010000000000000000000000000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_IPADDRESS", "Pushing IPAddress 0x00010000000000000000000000000001",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_EQUAL",
              "Popping 0x00010000000000000000000000000001 and 0x00010000000000000000000000000001 "
              "pushing result 'true'",
              pkt6_->getLabel());

    EXPECT_TRUE(checkFile());
}

// This test checks some error cases for a relay6 field token
TEST_F(TokenTest, relay6FieldError) {
    // Create a valid relay6 field token
    ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(0, TokenRelay6Field::LINKADDR)));
    EXPECT_EQ(0, t_->getLabel());

    // a DHCPv6 packet is required
    ASSERT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // No test for unknown field as it is not (yet) checked?!
}

// This test checks if a token representing an == operator is able to
// compare two values (with incorrectly built stack).
TEST_F(TokenTest, optionEqualInvalid) {

    ASSERT_NO_THROW(t_.reset(new TokenEqual()));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: There's not enough values on the stack. == is an operator that
    // takes two parameters. There are 0 on the stack.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: One value is still not enough.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
}

// This test checks if a token representing an == operator is able to
// compare two different values.
TEST_F(TokenTest, optionEqualFalse) {

    ASSERT_NO_THROW(t_.reset(new TokenEqual()));

    values_.push("foo");
    values_.push("bar");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single value that represents
    // result of "foo" == "bar" comparison.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_EQUAL", "Popping 0x626172 and 0x666F6F pushing result 'false'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an == operator is able to
// compare two identical values.
TEST_F(TokenTest, optionEqualTrue) {

    ASSERT_NO_THROW(t_.reset(new TokenEqual()));

    values_.push("foo");
    values_.push("foo");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single value that represents
    // result of "foo" == "foo" comparison.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_EQUAL", "Popping 0x666F6F and 0x666F6F pushing result 'true'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing a substring request
// throws an exception if there aren't enough values on the stack.
// The stack from the top is: length, start, string.
// The actual packet is not used.
TEST_F(TokenTest, substringNotEnoughValues) {
    ASSERT_NO_THROW(t_.reset(new TokenSubstring()));
    EXPECT_EQ(0, t_->getLabel());

    // Substring requires three values on the stack, try
    // with 0, 1 and 2 all should throw an exception
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("0");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // Three should work
    values_.push("0");
    testEvaluate(t_, *pkt4_, values_);

    // As we had an empty string to start with we should have an empty
    // one after the evaluate
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING_EMPTY",
              "Popping length 0, start 0, string 0x pushing result 0x", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test getting the whole string in different ways
TEST_F(TokenTest, substringWholeString) {
    // Get the whole string
    verifySubstringEval("foobar", "0", "6", "foobar");

    // Get the whole string with "all"
    verifySubstringEval("foobar", "0", "all", "foobar");

    // Get the whole string with an extra long number
    verifySubstringEval("foobar", "0", "123456", "foobar");

    // Get the whole string counting from the back
    verifySubstringEval("foobar", "-6", "all", "foobar");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 6, start 0, string 0x666F6F626172 pushing result 0x666F6F626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start 0, string 0x666F6F626172 pushing result 0x666F6F626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 123456, start 0, string 0x666F6F626172 pushing result 0x666F6F626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start -6, string 0x666F6F626172 pushing result 0x666F6F626172",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test getting a suffix, in this case the last 3 characters
TEST_F(TokenTest, substringTrailer) {
    verifySubstringEval("foobar", "3", "3", "bar");
    verifySubstringEval("foobar", "3", "all", "bar");
    verifySubstringEval("foobar", "-3", "all", "bar");
    verifySubstringEval("foobar", "-3", "123", "bar");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 3, start 3, string 0x666F6F626172 pushing result 0x626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start 3, string 0x666F6F626172 pushing result 0x626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start -3, string 0x666F6F626172 pushing result 0x626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 123, start -3, string 0x666F6F626172 pushing result 0x626172",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test getting the middle of the string in different ways
TEST_F(TokenTest, substringMiddle) {
    verifySubstringEval("foobar", "1", "4", "ooba");
    verifySubstringEval("foobar", "-5", "4", "ooba");
    verifySubstringEval("foobar", "-1", "-4", "ooba");
    verifySubstringEval("foobar", "5", "-4", "ooba");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 1, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start -5, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start -1, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start 5, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test getting the last letter in different ways
TEST_F(TokenTest, substringLastLetter) {
    verifySubstringEval("foobar", "5", "all", "r");
    verifySubstringEval("foobar", "5", "1", "r");
    verifySubstringEval("foobar", "5", "5", "r");
    verifySubstringEval("foobar", "-1", "all", "r");
    verifySubstringEval("foobar", "-1", "1", "r");
    verifySubstringEval("foobar", "-1", "5", "r");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start 5, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 1, start 5, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 5, start 5, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length all, start -1, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 1, start -1, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 5, start -1, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test we get only what is available if we ask for a longer string
TEST_F(TokenTest, substringLength) {
    // Test off the front
    verifySubstringEval("foobar", "0", "-4", "");
    verifySubstringEval("foobar", "1", "-4", "f");
    verifySubstringEval("foobar", "2", "-4", "fo");
    verifySubstringEval("foobar", "3", "-4", "foo");

    // and the back
    verifySubstringEval("foobar", "3", "4", "bar");
    verifySubstringEval("foobar", "4", "4", "ar");
    verifySubstringEval("foobar", "5", "4", "r");
    verifySubstringEval("foobar", "6", "4", "");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start 0, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start 1, string 0x666F6F626172 pushing result 0x66",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start 2, string 0x666F6F626172 pushing result 0x666F",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length -4, start 3, string 0x666F6F626172 pushing result 0x666F6F",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 3, string 0x666F6F626172 pushing result 0x626172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 4, string 0x666F6F626172 pushing result 0x6172",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 5, string 0x666F6F626172 pushing result 0x72",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length 4, start 6, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test that we get nothing if the starting position is out of the string
TEST_F(TokenTest, substringStartingPosition) {
    // Off the front
    verifySubstringEval("foobar", "-7", "1", "");
    verifySubstringEval("foobar", "-7", "-11", "");
    verifySubstringEval("foobar", "-7", "all", "");

    // and the back
    verifySubstringEval("foobar", "6", "1", "");
    verifySubstringEval("foobar", "6", "-11", "");
    verifySubstringEval("foobar", "6", "all", "");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length 1, start -7, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length -11, start -7, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length all, start -7, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length 1, start 6, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length -11, start 6, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING_RANGE",
              "Popping length all, start 6, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Check what happens if we use strings that aren't numbers for start or length
// We should return the empty string
TEST_F(TokenTest, substringBadParams) {
    verifySubstringEval("foobar", "0ick", "all", "", true);
    verifySubstringEval("foobar", "ick0", "all", "", true);
    verifySubstringEval("foobar", "ick", "all", "", true);
    verifySubstringEval("foobar", "0", "ick", "", true);
    verifySubstringEval("foobar", "0", "0ick", "", true);
    verifySubstringEval("foobar", "0", "ick0", "", true);
    verifySubstringEval("foobar", "0", "allaboard", "", true);

    // These should result in a throw which should generate it's own
    // log entry
}

// lastly check that we don't get anything if the string is empty or
// we don't ask for any characters from it.
TEST_F(TokenTest, substringReturnEmpty) {
    verifySubstringEval("", "0", "all", "");
    verifySubstringEval("foobar", "0", "0", "");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING_EMPTY",
              "Popping length all, start 0, string 0x pushing result 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 0, start 0, string 0x666F6F626172 pushing result 0x",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Check if we can use the substring and equal tokens together
// We put the result on the stack first then the substring values
// then evaluate the substring which should leave the original
// result on the bottom with the substring result on next.
// Evaluating the equals should produce true for the first
// and false for the second.
// throws an exception if there aren't enough values on the stack.
// The stack from the top is: length, start, string.
// The actual packet is not used.
TEST_F(TokenTest, substringEquals) {
    TokenPtr tequal;

    ASSERT_NO_THROW(t_.reset(new TokenSubstring()));
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));

    // The final expected value
    values_.push("ooba");

    // The substring values
    values_.push("foobar");
    values_.push("1");
    values_.push("4");
    testEvaluate(t_, *pkt4_, values_);

    // we should have two values on the stack
    ASSERT_EQ(2, values_.size());

    // next the equals eval
    testEvaluate(tequal, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // get rid of the result
    values_.pop();

    // and try it again but with a bad final value
    // The final expected value
    values_.push("foob");

    // The substring values
    values_.push("foobar");
    values_.push("1");
    values_.push("4");
    testEvaluate(t_, *pkt4_, values_);

    // we should have two values on the stack
    ASSERT_EQ(2, values_.size());

    // next the equals eval
    testEvaluate(tequal, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 1, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_EQUAL", "Popping 0x6F6F6261 and 0x6F6F6261 pushing result 'true'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUBSTRING",
              "Popping length 4, start 1, string 0x666F6F626172 pushing result 0x6F6F6261",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_EQUAL", "Popping 0x6F6F6261 and 0x666F6F62 pushing result 'false'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing a concat request
// throws an exception if there aren't enough values on the stack.
// The actual packet is not used.
TEST_F(TokenTest, concat) {
    ASSERT_NO_THROW(t_.reset(new TokenConcat()));
    EXPECT_EQ(0, t_->getLabel());

    // Concat requires two values on the stack, try
    // with 0 and 1 both should throw an exception
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // Two should work
    values_.push("bar");
    testEvaluate(t_, *pkt4_, values_);

    // Check the result
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("foobar", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_CONCAT", "Popping 0x626172 and 0x666F6F pushing 0x666F6F626172",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing a hexstring request
// throws an exception if there aren't enough values on the stack.
// The actual packet is not used.
TEST_F(TokenTest, tohexstring) {
    ASSERT_NO_THROW(t_.reset(new TokenToHexString()));
    EXPECT_EQ(0, t_->getLabel());

    // Hexstring requires two values on the stack, try
    // with 0 and 1 both should throw an exception
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // Two should work
    values_.push("-");
    testEvaluate(t_, *pkt4_, values_);

    // Check the result
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("66-6f-6f", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_TOHEXSTRING",
              "Popping binary value 0x666F6F and separator -, pushing result 66-6f-6f",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an ifelse is able
// to select the branch following the condition.
TEST_F(TokenTest, ifElse) {
    ASSERT_NO_THROW(t_.reset(new TokenIfElse()));
    EXPECT_EQ(0, t_->getLabel());

    // Ifelse requires three values on the stack, try
    // with 0, 1 and 2 all should throw an exception
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("bar");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // The condition must be a boolean
    values_.push("bar");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Check if what it returns
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenIfElse()));
    values_.push("true");
    values_.push("foo");
    values_.push("bar");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("foo", values_.top());

    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenIfElse()));
    values_.push("false");
    values_.push("foo");
    values_.push("bar");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("bar", values_.top());
}

// This test checks if a token representing a not is able to
// negate a boolean value (with incorrectly built stack).
TEST_F(TokenTest, operatorNotInvalid) {

    ASSERT_NO_THROW(t_.reset(new TokenNot()));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: The stack is empty.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: The top value is not a boolean
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
}

// This test checks if a token representing a not operator is able to
// negate a boolean value.
TEST_F(TokenTest, operatorNot) {

    ASSERT_NO_THROW(t_.reset(new TokenNot()));

    values_.push("true");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be the negation of the value.
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Double negation is identity.
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_NOT", "Popping 'true' pushing 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_NOT", "Popping 'false' pushing 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an and is able to
// conjugate two values (with incorrectly built stack).
TEST_F(TokenTest, operatorAndInvalid) {

    ASSERT_NO_THROW(t_.reset(new TokenAnd()));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: There's not enough values on the stack. and is an operator that
    // takes two parameters. There are 0 on the stack.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: One value is still not enough.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 3: The two values must be logical
    values_.push("true");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Swap the 2 values
    values_.push("true");
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
}

// This test checks if a token representing an and operator is able to
// conjugate false with another logical
TEST_F(TokenTest, operatorAndFalse) {

    ASSERT_NO_THROW(t_.reset(new TokenAnd()));

    values_.push("true");
    values_.push("false");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single "false" value
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // After true and false, check false and true
    values_.push("true");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // And false and false
    values_.push("false");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_AND", "Popping 'false' and 'true' pushing 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_AND", "Popping 'true' and 'false' pushing 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_AND", "Popping 'false' and 'false' pushing 'false'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an and is able to
// conjugate two true values.
TEST_F(TokenTest, operatorAndTrue) {

    ASSERT_NO_THROW(t_.reset(new TokenAnd()));

    values_.push("true");
    values_.push("true");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single "true" value
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_AND", "Popping 'true' and 'true' pushing 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an or is able to
// combine two values (with incorrectly built stack).
TEST_F(TokenTest, operatorOrInvalid) {

    ASSERT_NO_THROW(t_.reset(new TokenOr()));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: There's not enough values on the stack. or is an operator that
    // takes two parameters. There are 0 on the stack.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: One value is still not enough.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 3: The two values must be logical
    values_.push("true");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);

    // Swap the 2 values
    values_.push("true");
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
}

// This test checks if a token representing an or is able to
// conjugate two false values.
TEST_F(TokenTest, operatorOrFalse) {

    ASSERT_NO_THROW(t_.reset(new TokenOr()));

    values_.push("false");
    values_.push("false");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single "false" value
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OR", "Popping 'false' and 'false' pushing 'false'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks if a token representing an == operator is able to
// conjugate true with another logical
TEST_F(TokenTest, operatorOrTrue) {

    ASSERT_NO_THROW(t_.reset(new TokenOr()));

    values_.push("false");
    values_.push("true");
    testEvaluate(t_, *pkt4_, values_);

    // After evaluation there should be a single "true" value
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // After false or true, checks true or false
    values_.push("false");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // And true or true
    values_.push("true");
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_OR", "Popping 'true' and 'false' pushing 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OR", "Popping 'false' and 'true' pushing 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OR", "Popping 'true' and 'true' pushing 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies client class membership
TEST_F(TokenTest, member) {

    ASSERT_NO_THROW(t_.reset(new TokenMember("foo")));
    EXPECT_EQ(0, t_->getLabel());

    testEvaluate(t_, *pkt4_, values_);

    // the packet has no classes so false was left on the stack
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());
    values_.pop();

    // Add bar and retry
    pkt4_->addClass("bar");
    testEvaluate(t_, *pkt4_, values_);

    // the packet has a class but it is not foo
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());
    values_.pop();

    // Add foo and retry
    pkt4_->addClass("foo");
    testEvaluate(t_, *pkt4_, values_);

    // Now the packet is in the foo class
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());
}

// This test verifies if expression vendor[4491].exists works properly in DHCPv4.
TEST_F(TokenTest, vendor4SpecificVendorExists) {
    ASSERT_NO_THROW(t_.reset(new TokenVendor(Option::V4, 4491,
                                             TokenOption::EXISTS)));
    EXPECT_EQ(0, t_->getLabel());

    // Case 1: no option, should evaluate to false
    testVendorExists(Option::V4, 4491, 0, "false");

    // Case 2: option present, but uses different enterprise-id, should fail
    testVendorExists(Option::V4, 4491, 1234, "false");

    // Case 3: option present and has matching enterprise-id, should succeed
    testVendorExists(Option::V4, 4491, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 125 missing, pushing result 'false'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies if expression vendor[4491].exists works properly in DHCPv6.
TEST_F(TokenTest, vendor6SpecificVendorExists) {
    // Case 1: no option, should evaluate to false
    testVendorExists(Option::V6, 4491, 0, "false");

    // Case 2: option present, but uses different enterprise-id, should fail
    testVendorExists(Option::V6, 4491, 1234, "false");

    // Case 3: option present and has matching enterprise-id, should succeed
    testVendorExists(Option::V6, 4491, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 17 missing, pushing result 'false'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

/// Test if expression vendor[*].exists works properly for DHCPv4.
TEST_F(TokenTest, vendor4AnyVendorExists) {
    // Case 1: no option, should evaluate to false
    testVendorExists(Option::V4, 0, 0, "false");

    // Case 2: option present with vendor-id 1234, should succeed
    testVendorExists(Option::V4, 0, 1234, "true");

    // Case 3: option present with vendor-id 4491, should succeed
    testVendorExists(Option::V4, 0, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 125 missing, pushing result 'false'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 1234 found, pushing result 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test if expression vendor[*].exists works properly for DHCPv6.
TEST_F(TokenTest, vendor6AnyVendorExists) {
    // Case 1: no option, should evaluate to false
    testVendorExists(Option::V6, 0, 0, "false");

    // Case 2: option present with vendor-id 1234, should succeed
    testVendorExists(Option::V6, 0, 1234, "true");

    // Case 3: option present with vendor-id 4491, should succeed
    testVendorExists(Option::V6, 0, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 17 missing, pushing result 'false'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 1234 found, pushing result 'true'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test if expression vendor[*].enterprise works properly for DHCPv4.
TEST_F(TokenTest, vendor4enterprise) {
    // Case 1: No option present, should return empty string
    testVendorEnterprise(Option::V4, 0, "");

    // Case 2: Option with vendor-id 1234, should return "1234"
    testVendorEnterprise(Option::V4, 1234, encode(1234));

    // Case 3: Option with vendor-id set to maximum value, should still
    // be able to handle it
    testVendorEnterprise(Option::V4, 4294967295, encode(4294967295));

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 125 missing, pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID", "Pushing enterprise-id 1234 as result 0x000004D2",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID",
              "Pushing enterprise-id 4294967295 as result 0xFFFFFFFF", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test if expression vendor[*].enterprise works properly for DHCPv6.
TEST_F(TokenTest, vendor6enterprise) {
    // Case 1: No option present, should return empty string
    testVendorEnterprise(Option::V6, 0, "");

    // Case 2: Option with vendor-id 1234, should return "1234"
    testVendorEnterprise(Option::V6, 1234, encode(1234));

    // Case 3: Option with vendor-id set to maximum value, should still
    // be able to handle it
    testVendorEnterprise(Option::V6, 4294967295, encode(4294967295));

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 17 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID", "Pushing enterprise-id 1234 as result 0x000004D2",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID",
              "Pushing enterprise-id 4294967295 as result 0xFFFFFFFF", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This one tests "vendor[4491].option[1].exists" expression. There are so many
// wonderful ways in which this could fail: the option could not be there,
// it could have different enterprise-id, may not have suboption 1. Or may
// have the suboption with valid type, but enterprise may be different.
TEST_F(TokenTest, vendor4SuboptionExists) {
    // Case 1: expression vendor[4491].option[1].exists, no option present
    testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::EXISTS, "false");

    // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
    // no suboptions, expected result "false"
    testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::EXISTS, "false");

    // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
    // suboption 1, expected result "false"
    testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::EXISTS, "false");

    // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
    // suboption 2, expected result "false"
    testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::EXISTS, "false");

    // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
    // suboption 1, expected result "true"
    testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::EXISTS, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 125 missing, pushing result 'false'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This is similar to the previous one, but tests vendor[4491].option[1].exists
// for DHCPv6.
TEST_F(TokenTest, vendor6SuboptionExists) {
    // Case 1: expression vendor[4491].option[1].exists, no option present
    testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::EXISTS, "false");

    // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
    // no suboptions, expected result "false"
    testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::EXISTS, "false");

    // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
    // suboption 1, expected result "false"
    testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::EXISTS, "false");

    // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
    // suboption 2, expected result "false"
    testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::EXISTS, "false");

    // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
    // suboption 1, expected result "true"
    testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::EXISTS, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 17 missing, pushing result 'false'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 'true'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies if vendor[4491].option[1].hex expression properly returns
// value of said sub-option or empty string if desired option is not present.
// This test is for DHCPv4.
TEST_F(TokenTest, vendor4SuboptionHex) {
    // Case 1: no option present, should return empty string
    testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, "");

    // Case 2: option with vendor-id = 1234, no suboptions, expected result ""
    testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, "");

    // Case 3: option with vendor-id = 1234, suboption 1, expected result ""
    testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, "");

    // Case 4: option with vendor-id = 4491, suboption 2, expected result ""
    testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, "");

    // Case 5: option with vendor-id = 4491, suboption 1, expected result content
    // of the option
    testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 125 missing, pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 0x616C706861", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies if vendor[4491].option[1].hex expression properly returns
// value of said sub-option or empty string if desired option is not present.
// This test is for DHCPv4.
TEST_F(TokenTest, vendor6SuboptionHex) {
    // Case 1: no option present, should return empty string
    testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, "");

    // Case 2: option with vendor-id = 1234, no suboptions, expected result ""
    testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, "");

    // Case 3: option with vendor-id = 1234, suboption 1, expected result ""
    testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, "");

    // Case 4: option with vendor-id = 4491, suboption 2, expected result ""
    testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, "");

    // Case 5: option with vendor-id = 4491, suboption 1, expected result content
    // of the option
    testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_NO_OPTION", "Option with code 17 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 0x", pkt6_->getLabel());
    addString("EVAL_DEBUG_OPTION", "Pushing option 1 with value 0x616C706861", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies that "vendor-class[4491].exists" expression can be used
// in DHCPv4.
TEST_F(TokenTest, vendorClass4SpecificVendorExists) {
    ASSERT_NO_THROW(t_.reset(new TokenVendorClass(Option::V4, 4491,
                                                  TokenVendor::ENTERPRISE_ID)));
    EXPECT_EQ(0, t_->getLabel());

    // Case 1: no option present, should fail
    testVendorClassExists(Option::V4, 4491, 0, "false");

    // Case 2: option exists, but has different vendor-id (1234), should fail
    testVendorClassExists(Option::V4, 4491, 1234, "false");

    // Case 3: option exists and has matching vendor-id, should succeed
    testVendorClassExists(Option::V4, 4491, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies that "vendor-class[4491].exists" expression can be used
// in DHCPv6.
TEST_F(TokenTest, vendorClass6SpecificVendorExists) {
    // Case 1: no option present, should fail
    testVendorClassExists(Option::V6, 4491, 0, "false");

    // Case 2: option exists, but has different vendor-id (1234), should fail
    testVendorClassExists(Option::V6, 4491, 1234, "false");

    // Case 3: option exists and has matching vendor-id, should succeed
    testVendorClassExists(Option::V6, 4491, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 16 missing, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies that "vendor-class[*].exists" can be used in DHCPv4
// and it matches a vendor class option with any vendor-id.
TEST_F(TokenTest, vendorClass4AnyVendorExists) {
    // Case 1: no option present, should fail
    testVendorClassExists(Option::V4, 0, 0, "false");

    // Case 2: option exists, should succeed, regardless of the vendor-id
    testVendorClassExists(Option::V4, 0, 1234, "true");

    // Case 3: option exists, should succeed, regardless of the vendor-id
    testVendorClassExists(Option::V4, 0, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result 'false'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 1234 found, pushing result 'true'", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies that "vendor-class[*].exists" can be used in DHCPv6
// and it matches a vendor class option with any vendor-id.
TEST_F(TokenTest, vendorClass6AnyVendorExists) {
    // Case 1: no option present, should fail
    testVendorClassExists(Option::V6, 0, 0, "false");

    // Case 2: option exists, should succeed, regardless of the vendor-id
    testVendorClassExists(Option::V6, 0, 1234, "true");

    // Case 3: option exists, should succeed, regardless of the vendor-id
    testVendorClassExists(Option::V6, 0, 4491, "true");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 16 missing, pushing result 'false'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 1234 found, pushing result 'true'", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS",
              "Option with enterprise-id 4491 found, pushing result 'true'", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test if expression "vendor-class.enterprise" works properly for DHCPv4.
TEST_F(TokenTest, vendorClass4enterprise) {
    // Case 1: No option present, should return empty string
    testVendorClassEnterprise(Option::V4, 0, "");

    // Case 2: Option with vendor-id 1234, should return "1234"
    testVendorClassEnterprise(Option::V4, 1234, encode(1234));

    // Case 3: Option with vendor-id set to maximum value, should still
    // be able to handle it
    testVendorClassEnterprise(Option::V4, 4294967295, encode(4294967295));

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID",
              "Pushing enterprise-id 1234 as result 0x000004D2", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID",
              "Pushing enterprise-id 4294967295 as result 0xFFFFFFFF", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test if expression "vendor-class.enterprise" works properly for DHCPv6.
TEST_F(TokenTest, vendorClass6enterprise) {
    // Case 1: No option present, should return empty string
    testVendorClassEnterprise(Option::V6, 0, "");

    // Case 2: Option with vendor-id 1234, should return "1234"
    testVendorClassEnterprise(Option::V6, 1234, encode(1234));

    // Case 3: Option with vendor-id set to maximum value, should still
    // be able to handle it.
    testVendorClassEnterprise(Option::V6, 4294967295, encode(4294967295));

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION", "Option with code 16 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID",
              "Pushing enterprise-id 1234 as result 0x000004D2", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID",
              "Pushing enterprise-id 4294967295 as result 0xFFFFFFFF", pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test that expression "vendor-class[4491].data" is able to retrieve content
// of the first tuple of the vendor-class option in DHCPv4.
TEST_F(TokenTest, vendorClass4SpecificVendorData) {
    // Case 1: Expression looks for vendor-id 4491, data[0], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V4, 4491, 0, 0, 0, "");

    // Case 2: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 1234 and no data, expected result is empty string.
    testVendorClassData(Option::V4, 4491, 0, 1234, 0, "");

    // Case 3: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 4491 and no data, expected result is empty string.
    // Note that vendor option in v4 always have at least one data chunk, even though
    // it may be empty. The OptionVendor code was told to not create any special
    // tuples, but it creates one empty on its own. So the code finds that one
    // tuple and extracts its content (an empty string).
    testVendorClassData(Option::V4, 4491, 0, 4491, 0, "");

    // Case 4: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string
    testVendorClassData(Option::V4, 4491, 0, 1234, 1, "");

    // Case 5: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V4, 4491, 0, 4491, 1, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test that expression "vendor-class[4491].data" is able to retrieve content
// of the first tuple of the vendor-class option in DHCPv6.
TEST_F(TokenTest, vendorClass6SpecificVendorData) {
    // Case 1: Expression looks for vendor-id 4491, data[0], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V6, 4491, 0, 0, 0, "");

    // Case 2: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 1234 and no data, expected result is empty string.
    testVendorClassData(Option::V6, 4491, 0, 1234, 0, "");

    // Case 3: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 4491 and no data, expected result is empty string
    testVendorClassData(Option::V6, 4491, 0, 4491, 0, "");

    // Case 4: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string
    testVendorClassData(Option::V6, 4491, 0, 1234, 1, "");

    // Case 5: Expression looks for vendor-id 4491, data[0], there is
    // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V6, 4491, 0, 4491, 1, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION", "Option with code 16 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 0, but option with enterprise-id 4491 has only 0 data "
              "tuple(s), pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test that expression "vendor-class[*].data" is able to retrieve content
// of the first tuple of the vendor-class option in DHCPv4.
TEST_F(TokenTest, vendorClass4AnyVendorData) {
    // Case 1: Expression looks for any vendor-id (0), data[0], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V4, 0, 0, 0, 0, "");

    // Case 2: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 1234 and no data (one empty tuple), expected
    // result is empty string.
    testVendorClassData(Option::V4, 0, 0, 1234, 0, "");

    // Case 3: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 4491 and no data (one empty tuple), expected
    // result is empty string.
    testVendorClassData(Option::V4, 0, 0, 4491, 0, "");

    // Case 4: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 1234 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V4, 0, 0, 1234, 1, "alpha");

    // Case 5: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V4, 0, 0, 4491, 1, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Test that expression "vendor-class[*].data" is able to retrieve content
// of the first tuple of the vendor-class option in DHCPv6.
TEST_F(TokenTest, vendorClass6AnyVendorData) {
    // Case 1: Expression looks for any vendor-id (0), data[0], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V6, 0, 0, 0, 0, "");

    // Case 2: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 1234 and no data, expected result is empty string.
    testVendorClassData(Option::V6, 0, 0, 1234, 0, "");

    // Case 3: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 4491 and no data, expected result is empty string
    testVendorClassData(Option::V6, 0, 0, 4491, 0, "");

    // Case 4: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 1234 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V6, 0, 0, 1234, 1, "alpha");

    // Case 5: Expression looks for any vendor-id (0), data[0], there is
    // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
    // content of that data ("alpha")
    testVendorClassData(Option::V6, 0, 0, 4491, 1, "alpha");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION", "Option with code 16 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 0, but option with enterprise-id 1234 has only 0 data "
              "tuple(s), pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 0, but option with enterprise-id 4491 has only 0 data "
              "tuple(s), pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 0 (out of 1 received) in vendor class found, pushing result 'alpha'",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies if expression vendor-class[4491].data[3] is able to access
// the tuple specified by index. This is a DHCPv4 test.
TEST_F(TokenTest, vendorClass4DataIndex) {
    // Case 1: Expression looks for vendor-id 4491, data[3], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V4, 4491, 3, 0, 0, "");

    // Case 2: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 1234 and no data, expected result is empty string.
    testVendorClassData(Option::V4, 4491, 3, 1234, 0, "");

    // Case 3: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491 and no data, expected result is empty string
    testVendorClassData(Option::V4, 4491, 3, 4491, 0, "");

    // Case 4: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string.
    testVendorClassData(Option::V4, 4491, 3, 1234, 1, "");

    // Case 5: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491, but has only 3 data tuples, expected
    // result is empty string.
    testVendorClassData(Option::V4, 4491, 3, 4491, 3, "");

    // Case 6: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491 and 5 data tuples, expected result is
    // content of that tuple ("gamma")
    testVendorClassData(Option::V4, 4491, 3, 4491, 5, "gamma");

    // Case 6: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 1234 and 5 data tuples, expected result is
    // empty string, because vendor-id does not match.
    testVendorClassData(Option::V4, 4491, 3, 1234, 5, "");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION",
              "Option with code 124 missing, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 3, but option with enterprise-id 4491 has only 1 data "
              "tuple(s), pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 3, but option with enterprise-id 4491 has only 3 data "
              "tuple(s), pushing result ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 3 (out of 5 received) in vendor class found, pushing result 'gamma'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test verifies if expression vendor-class[4491].data[3] is able to access
// the tuple specified by index. This is a DHCPv6 test.
TEST_F(TokenTest, vendorClass6DataIndex) {
    // Case 1: Expression looks for vendor-id 4491, data[3], there is no
    // vendor-class option at all, expected result is empty string.
    testVendorClassData(Option::V6, 4491, 3, 0, 0, "");

    // Case 2: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 1234 and no data, expected result is empty string.
    testVendorClassData(Option::V6, 4491, 3, 1234, 0, "");

    // Case 3: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491 and no data, expected result is empty string
    testVendorClassData(Option::V6, 4491, 3, 4491, 0, "");

    // Case 4: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 1234 and 5 data tuples, expected result is empty string.
    testVendorClassData(Option::V6, 4491, 3, 1234, 5, "");

    // Case 5: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491, but has only 3 data tuples, expected
    // result is empty string.
    testVendorClassData(Option::V6, 4491, 3, 4491, 3, "");

    // Case 6: Expression looks for vendor-id 4491, data[3], there is
    // vendor-class with vendor-id 4491 and 5 data tuples, expected result is
    // content of that tuple ("gamma")
    testVendorClassData(Option::V6, 4491, 3, 4491, 5, "gamma");

    // Check if the logged messages are correct.
    addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION", "Option with code 16 missing, pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 3, but option with enterprise-id 4491 has only 0 data "
              "tuple(s), pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH",
              "Was looking for 4491, option had 1234, pushing result ''", pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND",
              "Requested data index 3, but option with enterprise-id 4491 has only 3 data "
              "tuple(s), pushing result ''",
              pkt6_->getLabel());
    addString("EVAL_DEBUG_VENDOR_CLASS_DATA",
              "Data 3 (out of 5 received) in vendor class found, pushing result 'gamma'",
              pkt6_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the existing RAI -sunoption can be found.
TEST_F(TokenTest, subOption) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();

    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 13, TokenOption::TEXTUAL)));
    EXPECT_EQ(0, t_->getLabel());

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should be found and option[82].option[13] should evaluate
    // to thecontent of that sub-option, i.e. "thirteen"
    EXPECT_EQ("thirteen", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 13 with value 'thirteen'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the code properly handles cases when
// there is a RAI option, but there's no requested sub-option.
TEST_F(TokenTest, subOptionNoSubOption) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();


    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 15, TokenOption::TEXTUAL)));

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should NOT be found (there is no sub-option 15),
    // so the expression should evaluate to ""
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 15 with value ''",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that the code properly handles cases when
// there's no RAI option at all.
TEST_F(TokenTest, subOptionNoOption) {

    // We didn't call insertRelay4Option(), so there's no RAI option.

    // Creating the token should be safe.
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 13, TokenOption::TEXTUAL)));

    // We should be able to evaluate it.
    testEvaluate(t_, *pkt4_, values_);

    // we should have one value on the stack
    ASSERT_EQ(1, values_.size());

    // The option should NOT be found (there is no option 82),
    // so the expression should evaluate to ""
    EXPECT_EQ("", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUB_OPTION_NO_OPTION",
              "Requested option 82 sub-option 13, but the parent option is not present, pushing "
              "result ''",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// This test checks that only the requested parent is searched for
// the requested sub-option.
TEST_F(TokenTest, subOptionOptionOnly) {

    // Insert relay option with sub-options 1 and 13
    insertRelay4Option();

    // Add options 13 and 70 to the packet.
    OptionPtr opt13(new OptionString(Option::V4, 13, "THIRTEEN"));
    OptionPtr opt70(new OptionString(Option::V4, 70, "SEVENTY"));
    pkt4_->addOption(opt13);
    pkt4_->addOption(opt70);

    // The situation is as follows:
    // Packet:
    //  - option 13 (containing "THIRTEEN")
    //  - option 82 (rai)
    //      - option 1 (containing "one")
    //      - option 13 (containing "thirteen")

    // Let's try to get option 13. It should get the one from RAI
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 13, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("thirteen", values_.top());

    // Try to get option 1. It should get the one from RAI
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 1, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("one", values_.top());

    // Try to get option 70. It should fail, as there's no such
    // sub option in RAI.
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 70, TokenOption::TEXTUAL)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("", values_.top());

    // Try to check option 1. It should return "true"
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 1, TokenOption::EXISTS)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("true", values_.top());

    // Try to check option 70. It should return "false"
    clearStack();
    ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, 70, TokenOption::EXISTS)));
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_EQ(1, values_.size());
    EXPECT_EQ("false", values_.top());

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 13 with value 'thirteen'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 1 with value 'one'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 70 with value ''",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 1 with value 'true'",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SUB_OPTION", "Pushing option 82 sub-option 70 with value 'false'",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Checks if various values can be represented as integer tokens
TEST_F(TokenTest, integer) {
    ASSERT_NO_THROW(t_.reset(new TokenInteger(0)));
    EXPECT_EQ(0, t_->getLabel());

    testInteger(encode(0), 0);
    testInteger(encode(6), 6);
    testInteger(encode(255), 255);
    testInteger(encode(256), 256);
    testInteger(encode(1410), 1410);
    testInteger(encode(4294967295), 4294967295);
}

// Verify TokenSplit::eval, single delimiter.
TEST_F(TokenTest, split) {
    ASSERT_NO_THROW(t_.reset(new TokenSplit()));
    EXPECT_EQ(0, t_->getLabel());

    // Get the whole string
    std::string input(".two.three..five.");
    std::string delims(".");

    // Empty input string should yield empty result.
    verifySplitEval("", delims, "1", "");

    // Empty delimiters string should yield original string result.
    verifySplitEval(input, "", "1", input);

    // Field number less than one should yield empty result.
    verifySplitEval(input, delims, "0", "");

    // Now get each field in succession.
    verifySplitEval(input, delims, "1", "");
    verifySplitEval(input, delims, "2", "two");
    verifySplitEval(input, delims, "3", "three");
    verifySplitEval(input, delims, "4", "");
    verifySplitEval(input, delims, "5", "five");
    verifySplitEval(input, delims, "6", "");

    // Too large of a field should yield empty result.
    verifySplitEval(input, delims, "7", "");

    // A string without delimiters returns as field 1.
    verifySplitEval("just_one", delims, "1", "just_one");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison
    addString("EVAL_DEBUG_SPLIT_EMPTY", "Popping field 1, delimiters ., string , pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT_DELIM_EMPTY",
              "Popping field 1, delimiters , string .two.three..five., pushing result "
              "0x2E74776F2E74687265652E2E666976652E",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE",
              "Popping field 0, delimiters ., string .two.three..five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 1, delimiters ., string .two.three..five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 2, delimiters ., string .two.three..five., pushing result 0x74776F",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 3, delimiters ., string .two.three..five., pushing result "
              "0x7468726565",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 4, delimiters ., string .two.three..five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 5, delimiters ., string .two.three..five., pushing result 0x66697665",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 6, delimiters ., string .two.three..five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE",
              "Popping field 7, delimiters ., string .two.three..five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 1, delimiters ., string just_one, pushing result 0x6A7573745F6F6E65",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Verify TokenSplit::eval with more than one delimiter.
TEST_F(TokenTest, splitMultipleDelims) {
    // Get the whole string
    std::string input(".two:three.:five.");
    std::string delims(".:");

    // Empty input string should yield empty result.
    verifySplitEval("", delims, "1", "");

    // Too small of a field should yield empty result.
    verifySplitEval(input, delims, "0", "");

    // Now get each field in succession.
    verifySplitEval(input, delims, "1", "");
    verifySplitEval(input, delims, "2", "two");
    verifySplitEval(input, delims, "3", "three");
    verifySplitEval(input, delims, "4", "");
    verifySplitEval(input, delims, "5", "five");
    verifySplitEval(input, delims, "6", "");

    // Too large of a field should yield empty result.
    verifySplitEval(input, delims, "7", "");

    // A string without delimiters returns as field 1.
    verifySplitEval("just_one", delims, "1", "just_one");

    // Check that the debug output was correct.  Add the strings
    // to the test vector in the class and then call checkFile
    // for comparison

    addString("EVAL_DEBUG_SPLIT_EMPTY",
              "Popping field 1, delimiters .:, string , pushing result 0x", pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE",
              "Popping field 0, delimiters .:, string .two:three.:five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 1, delimiters .:, string .two:three.:five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 2, delimiters .:, string .two:three.:five., pushing result 0x74776F",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 3, delimiters .:, string .two:three.:five., pushing result "
              "0x7468726565",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 4, delimiters .:, string .two:three.:five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 5, delimiters .:, string .two:three.:five., pushing result 0x66697665",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 6, delimiters .:, string .two:three.:five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE",
              "Popping field 7, delimiters .:, string .two:three.:five., pushing result 0x",
              pkt4_->getLabel());
    addString("EVAL_DEBUG_SPLIT",
              "Popping field 1, delimiters .:, string just_one, pushing result 0x6A7573745F6F6E65",
              pkt4_->getLabel());
    EXPECT_TRUE(checkFile());
}

// Verify TokenMatch raises an error on invalid regular expression.
TEST_F(TokenTest, invalidRegEx) {
    try {
        TokenMatch t("[bad");
        ADD_FAILURE() << "expected exception, got none";
    } catch (const EvalParseError& ex) {
        string msg(ex.what());
        string expected = "invalid regular expression '[bad': ";
        EXPECT_EQ(expected, msg.substr(0, expected.size()))
            << "expected to start with " << expected
            << "\nbut got: " << msg;
    } catch (const exception& ex) {
        ADD_FAILURE() << "unexpected expection: " << ex.what();
    }
}

// Verify TokenMatch works as expected.
TEST_F(TokenTest, match) {
    ASSERT_NO_THROW(t_.reset(new TokenMatch("foo")));
    EXPECT_EQ(0, t_->getLabel());

    testMatch("foo", "foo", true);
    // Full match is required.
    testMatch("foo", "foobar", false);
    testMatch("^foo", "foo", true);
    testMatch("foo$", "foo", true);
    testMatch("^foo$", "foo", true);
    testMatch("foo.*", "foo", true);
    testMatch("foo.*", "foobar", true);
    // Case sensitive.
    testMatch("foo", "Foo", false);
    testMatch("Foo", "foo", false);
    // Contain is a subcase.
    testMatch(".*foo.*", "foo", true);
    testMatch(".*foo.*", "foobar", true);
    testMatch(".*foo.*", "barfoo", true);
    testMatch(".*foo.*", "foofoofoo", true);
    // Character range.
    testMatch("[fo]{3}", "foo", true);
    testMatch("[abfor]{6}", "foobar", true);
    testMatch("[abfo]{6}", "foobar", false);
    testMatch("[^xyz]+", "foobar", true);
    // Group.
    testMatch("f(.)\\1bar", "foobar", true);
    // Classes.
    testMatch("[[:alpha:]]{1,9}", "Foobar", true);
    testMatch("[[:alpha:]]{1,9}", "FoObar", true);
    testMatch("[[:alpha:]]{1,9}", "Fo0bar", false);
}

// Verify TokenLabel.
TEST_F(TokenTest, label) {
    // 0 is not a valid label.
    ASSERT_THROW(t_.reset(new TokenLabel(0)), EvalParseError);

    // Evaluation does and uses nothing.
    ASSERT_NO_THROW(t_.reset(new TokenLabel(123)));
    EXPECT_EQ(123, t_->getLabel());
    testEvaluate(t_, *pkt4_, values_);
    ASSERT_TRUE(values_.empty());
}

// Verify TokenBranch.
TEST_F(TokenTest, branch) {
    // 0 is not a valid branch.
    ASSERT_THROW(t_.reset(new TokenBranch(0)), EvalParseError);

    // Evaluation does and uses nothing.
    ASSERT_NO_THROW(t_.reset(new TokenBranch(123)));
    EXPECT_EQ(0, t_->getLabel());
    unsigned next(0);
    ASSERT_NO_THROW(next = t_->evaluate(*pkt4_, values_));
    EXPECT_EQ(123, next);
    ASSERT_TRUE(values_.empty());
}

// Verify TokenPopOrBranchTrue.
TEST_F(TokenTest, popOrBranchTrue) {
    // 0 is not a valid branch.
    ASSERT_THROW(t_.reset(new TokenPopOrBranchTrue(0)), EvalParseError);

    ASSERT_NO_THROW(t_.reset(new TokenPopOrBranchTrue(123)));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: The stack is empty.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: The top value is not a boolen.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // CASE 3: The top value is true.
    clearStack(false);
    values_.push("true");
    ASSERT_EQ(1, values_.size());
    unsigned next(0);
    ASSERT_NO_THROW(next = t_->evaluate(*pkt4_, values_));
    EXPECT_EQ(123, next);
    ASSERT_EQ(1, values_.size());
    string result = values_.top();
    EXPECT_EQ("true", result);

    // CASE 4: The top value is false.
    clearStack(false);
    values_.push("false");
    ASSERT_EQ(1, values_.size());
    testEvaluate(t_, *pkt6_, values_);
    EXPECT_TRUE(values_.empty());
}

// Verify TokenPopOrBranchFalse.
TEST_F(TokenTest, popOrBranchFalse) {
    // 0 is not a valid branch.
    ASSERT_THROW(t_.reset(new TokenPopOrBranchFalse(0)), EvalParseError);

    ASSERT_NO_THROW(t_.reset(new TokenPopOrBranchFalse(123)));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: The stack is empty.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: The top value is not a boolen.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // CASE 3: The top value is true.
    clearStack(false);
    values_.push("true");
    ASSERT_EQ(1, values_.size());
    testEvaluate(t_, *pkt6_, values_);
    EXPECT_TRUE(values_.empty());

    // CASE 4: The top value is false.
    clearStack(false);
    values_.push("false");
    ASSERT_EQ(1, values_.size());
    unsigned next(0);
    ASSERT_NO_THROW(next = t_->evaluate(*pkt4_, values_));
    EXPECT_EQ(123, next);
    ASSERT_EQ(1, values_.size());
    string result = values_.top();
    EXPECT_EQ("false", result);
}

// Verify TokenPopAndBranchFalse.
TEST_F(TokenTest, popAndBranchFalse) {
    // 0 is not a valid branch.
    ASSERT_THROW(t_.reset(new TokenPopAndBranchFalse(0)), EvalParseError);

    ASSERT_NO_THROW(t_.reset(new TokenPopAndBranchFalse(123)));
    EXPECT_EQ(0, t_->getLabel());

    // CASE 1: The stack is empty.
    EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);

    // CASE 2: The top value is not a boolen.
    values_.push("foo");
    EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);

    // CASE 3: The top value is true.
    clearStack(false);
    values_.push("true");
    ASSERT_EQ(1, values_.size());
    testEvaluate(t_, *pkt6_, values_);
    EXPECT_TRUE(values_.empty());

    // CASE 4: The top value is false.
    clearStack(false);
    values_.push("false");
    ASSERT_EQ(1, values_.size());
    unsigned next(0);
    ASSERT_NO_THROW(next = t_->evaluate(*pkt4_, values_));
    EXPECT_EQ(123, next);
    EXPECT_TRUE(values_.empty());
}

}