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
// 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 <eval/evaluate.h>
#include <eval/eval_context.h>
#include <eval/token.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option_string.h>

#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.

using namespace std;
using namespace isc::dhcp;

namespace {

/// @brief Test fixture for testing evaluation.
///
/// This class provides several convenience objects to be used during testing
/// of the evaluation of classification expressions.
class EvaluateTest : public ::testing::Test {
public:

    /// @brief Initializes Pkt4,Pkt6 and options that can be useful for
    ///        evaluation tests.
    EvaluateTest() {
        e_.clear();

        result_ = false;

        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_);
    }

    Expression e_; ///< An expression

    bool result_; ///< A decision

    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

    /// @todo: Add more option types here
};

// This checks the empty expression: it should raise EvalBadStack
// when evaluated with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, empty4) {<--- syntax error
    ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack);
}

// This checks the empty expression: it should raise EvalBadStack
// when evaluated with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, empty6) {
    ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack);
}

// This checks the { "false" } expression: it should return false
// when evaluated with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, false4) {
    TokenPtr tfalse;
    ASSERT_NO_THROW(tfalse.reset(new TokenString("false")));
    e_.push_back(tfalse);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_FALSE(result_);
}

// This checks the { "false" } expression: it should return false
// when evaluated with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, false6) {
    TokenPtr tfalse;
    ASSERT_NO_THROW(tfalse.reset(new TokenString("false")));
    e_.push_back(tfalse);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// This checks the { "true" } expression: it should return true
// when evaluated with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, true4) {
    TokenPtr ttrue;
    ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
    e_.push_back(ttrue);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
}

// This checks the { "true" } expression: it should return true
// when evaluated with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, true6) {
    TokenPtr ttrue;
    ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
    e_.push_back(ttrue);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_TRUE(result_);
}

// This checks the evaluation must lead to "false" or "true"
// with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, bad4) {
    TokenPtr bad;
    ASSERT_NO_THROW(bad.reset(new TokenString("bad")));
    e_.push_back(bad);
    ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalTypeError);
}

// This checks the evaluation must lead to "false" or "true"
// with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, bad6) {
    TokenPtr bad;
    ASSERT_NO_THROW(bad.reset(new TokenString("bad")));
    e_.push_back(bad);
    ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalTypeError);
}

// This checks the evaluation must leave only one value on the stack
// with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, two4) {
    TokenPtr ttrue;
    ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
    e_.push_back(ttrue);
    e_.push_back(ttrue);
    ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack);
}

// This checks the evaluation must leave only one value on the stack
// with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, two6) {
    TokenPtr ttrue;
    ASSERT_NO_THROW(ttrue.reset(new TokenString("true")));
    e_.push_back(ttrue);
    e_.push_back(ttrue);
    ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack);
}

// A more complex test evaluated with a Pkt4. (The actual packet is not used)
TEST_F(EvaluateTest, compare4) {
    TokenPtr tfoo;
    TokenPtr tbar;
    TokenPtr tequal;

    ASSERT_NO_THROW(tfoo.reset(new TokenString("foo")));
    e_.push_back(tfoo);
    ASSERT_NO_THROW(tbar.reset(new TokenString("bar")));
    e_.push_back(tbar);
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
    e_.push_back(tequal);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_FALSE(result_);
}

// A more complex test evaluated with a Pkt6. (The actual packet is not used)
TEST_F(EvaluateTest, compare6) {
    TokenPtr tfoo;
    TokenPtr tbar;
    TokenPtr tequal;

    ASSERT_NO_THROW(tfoo.reset(new TokenString("foo")));
    e_.push_back(tfoo);
    ASSERT_NO_THROW(tbar.reset(new TokenString("bar")));
    e_.push_back(tbar);
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
    e_.push_back(tequal);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// A test using option existence
TEST_F(EvaluateTest, exists) {
    TokenPtr toption;

    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::EXISTS)));
    e_.push_back(toption);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_TRUE(result_);
}

// A test using option non-existence
TEST_F(EvaluateTest, dontExists) {
    TokenPtr toption;

    ASSERT_NO_THROW(toption.reset(new TokenOption(101, TokenOption::EXISTS)));
    e_.push_back(toption);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_FALSE(result_);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// A test using packets.
TEST_F(EvaluateTest, packet) {
    TokenPtr toption;
    TokenPtr tstring;
    TokenPtr tequal;

    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL)));
    e_.push_back(toption);
    ASSERT_NO_THROW(tstring.reset(new TokenString("hundred4")));
    e_.push_back(tstring);
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
    e_.push_back(tequal);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// A test which compares option value represented in hexadecimal format.
TEST_F(EvaluateTest, optionHex) {
    TokenPtr toption;
    TokenPtr tstring;
    TokenPtr tequal;

    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::HEXADECIMAL)));
    e_.push_back(toption);
    ASSERT_NO_THROW(tstring.reset(new TokenString("hundred4")));
    e_.push_back(tstring);
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
    e_.push_back(tequal);

    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// A test using substring on an option.
TEST_F(EvaluateTest, complex) {
    TokenPtr toption;
    TokenPtr tstart;
    TokenPtr tlength;
    TokenPtr tsubstring;
    TokenPtr tstring;
    TokenPtr tequal;

    // Get the option, i.e., "hundred[46]"
    ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL)));
    e_.push_back(toption);

    // Get substring("hundred[46]", 0, 7), i.e., "hundred"
    ASSERT_NO_THROW(tstart.reset(new TokenString("0")));
    e_.push_back(tstart);
    ASSERT_NO_THROW(tlength.reset(new TokenString("7")));
    e_.push_back(tlength);
    ASSERT_NO_THROW(tsubstring.reset(new TokenSubstring()));
    e_.push_back(tsubstring);

    // Compare with "hundred"
    ASSERT_NO_THROW(tstring.reset(new TokenString("hundred")));
    e_.push_back(tstring);
    ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
    e_.push_back(tequal);

    // Should return true for v4 and v6 packets
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_TRUE(result_);
}

/// @brief Generic class for parsing expressions and evaluating them.
///
/// The main purpose of this class is to provide a generic interface to the
/// eval library, so everything (expression parsing and then evaluation for
/// given packets) can be done in one simple call.
///
/// These tests may be somewhat redundant to other more specialized tests, but
/// the idea here is to mass produce tests that are trivial to write.
class ExpressionsTest : public EvaluateTest {
public:

    /// @brief Checks if expression can be parsed and evaluated to bool
    ///
    /// There are skeleton packets created in pkt4_ and pkt6_. Make sure you
    /// tweak them as needed before calling this method.
    ///
    /// @param u universe (V4 or V6)
    /// @param expr expression to be parsed
    /// @param exp_result expected result (true or false)
    void testExpression(const Option::Universe& u, const std::string& expr,
                        const bool exp_result) {

        EvalContext eval(u);
        bool result = false;
        bool parsed = false;

        EXPECT_NO_THROW(parsed = eval.parseString(expr))
            << " while parsing expression " << expr;
        EXPECT_TRUE(parsed) << " for expression " << expr;

        switch (u) {
        case Option::V4:
            ASSERT_NO_THROW(result = evaluateBool(eval.expression_, *pkt4_))
                << " for expression " << expr;
            break;
        case Option::V6:
            ASSERT_NO_THROW(result = evaluateBool(eval.expression_, *pkt6_))
                << " for expression " << expr;
            break;
        }

        EXPECT_EQ(exp_result, result) << " for expression " << expr;
    }

    /// @brief Checks if expression can be parsed and evaluated to string
    ///
    /// There are skeleton packets created in pkt4_ and pkt6_. Make sure you
    /// tweak them as needed before calling this method.
    ///
    /// @param u universe (V4 or V6)
    /// @param expr expression to be parsed
    /// @param exp_result expected result (string)
    void testExpressionString(const Option::Universe& u, const std::string& expr,
                              const std::string& exp_result) {

        EvalContext eval(u);
        string result;
        bool parsed = false;

        EXPECT_NO_THROW(parsed = eval.parseString(expr, EvalContext::PARSER_STRING))
            << " while parsing expression " << expr;
        EXPECT_TRUE(parsed) << " for expression " << expr;

        switch (u) {
        case Option::V4:
            ASSERT_NO_THROW(result = evaluateString(eval.expression_, *pkt4_))
                << " for expression " << expr;
            break;
        case Option::V6:
            ASSERT_NO_THROW(result = evaluateString(eval.expression_, *pkt6_))
                << " for expression " << expr;
            break;
        }

        EXPECT_EQ(exp_result, result) << " for expression " << expr;
    }

    /// @brief Checks that specified expression throws expected exception.
    ///
    /// @tparam ex exception type expected to be thrown
    /// @param expr expression to be evaluated
    template<typename ex>
    void testExpressionNegative(const std::string& expr,
                                const Option::Universe& u = Option::V4,
                                EvalContext::ParserType type = EvalContext::PARSER_BOOL) {
        EvalContext eval(u);

        EXPECT_THROW(eval.parseString(expr, type), ex) << "while parsing expression "
                                                 << expr;
    }
};

// This is a quick way to check if certain expressions are valid or not and
// whether the whole expression makes sense. This particular test checks if
// integers can be used properly in expressions. There are many places where
// integers are used. This particular test checks if pkt6.msgtype returns
// something that can be compared with integers.
//
// For basic things we can take advantage of the skeleton packets created in
// EvaluateTest constructors: The packet type is DISCOVER in DHCPv4 and
// SOLICIT in DHCPv6. There is one option added with code 100 and content
// being either "hundred4" or "hundred6" depending on the universe.

// Tests if pkt6.msgtype returns something that can be compared with integers.
TEST_F(ExpressionsTest, expressionsInteger1) {
    testExpression(Option::V6, "pkt6.msgtype == 1", true);
    testExpression(Option::V6, "pkt6.msgtype == 2", false);

    testExpression(Option::V6, "pkt6.msgtype == 0x00000001", true);
    testExpression(Option::V6, "pkt6.msgtype == 0x00000002", false);
}

// Tests if pkt6.transid returns something that can be compared with integers.
TEST_F(ExpressionsTest, expressionsInteger2) {
    testExpression(Option::V6, "pkt6.transid == 0", false);
    testExpression(Option::V6, "pkt6.transid == 12345", true);
    testExpression(Option::V6, "pkt6.transid == 12346", false);
}

// Tests if pkt4.transid returns something that can be compared with integers.
TEST_F(ExpressionsTest, expressionsInteger3) {
    testExpression(Option::V4, "pkt4.transid == 0", false);
    testExpression(Option::V4, "pkt4.transid == 12345", true);
    testExpression(Option::V4, "pkt4.transid == 12346", false);
}

// Tests if integers can be compared with integers.
TEST_F(ExpressionsTest, expressionsInteger4) {
    testExpression(Option::V6, "0 == 0", true);
    testExpression(Option::V6, "2 == 3", false);
}

// Tests if pkt4.hlen and pkt4.htype return values that can be compared with integers.
TEST_F(ExpressionsTest, expressionsPkt4Hlen) {

    // By default there's no hardware set up. The default Pkt4 constructor
    // creates HWAddr(), which has hlen=0 and htype set to HTYPE_ETHER.
    testExpression(Option::V4, "pkt4.hlen == 0", true);
    testExpression(Option::V4, "pkt4.htype == 1", true);

    // Ok, let's initialize the hardware address to something plausible.
    const size_t hwaddr_len = 6;
    const uint16_t expected_htype = 123;
    std::vector<uint8_t> hw(hwaddr_len,0);
    for (int i = 0; i < hwaddr_len; i++) {
        hw[i] = i + 1;
    }
    pkt4_->setHWAddr(expected_htype, hwaddr_len, hw);

    testExpression(Option::V4, "pkt4.hlen == 0", false);
    testExpression(Option::V4, "pkt4.hlen == 5", false);
    testExpression(Option::V4, "pkt4.hlen == 6", true);
    testExpression(Option::V4, "pkt4.hlen == 7", false);

    testExpression(Option::V4, "pkt4.htype == 0", false);
    testExpression(Option::V4, "pkt4.htype == 122", false);
    testExpression(Option::V4, "pkt4.htype == 123", true);
    testExpression(Option::V4, "pkt4.htype == 124", false);

    testExpression(Option::V4, "pkt4.mac == 0x010203040506", true);
}

// Test if expressions message type can be detected in Pkt4.
// It also doubles as a check for integer comparison here.
TEST_F(ExpressionsTest, expressionsPkt4type) {

    // We can inspect the option content directly, but
    // it requires knowledge of the option type and its format.
    testExpression(Option::V4, "option[53].hex == 0x0", false);
    testExpression(Option::V4, "option[53].hex == 0x1", true);
    testExpression(Option::V4, "option[53].hex == 0x2", false);

    // It's easier to simply use the pkt4.msgtype
    testExpression(Option::V4, "pkt4.msgtype == 0", false);
    testExpression(Option::V4, "pkt4.msgtype == 1", true);
    testExpression(Option::V4, "pkt4.msgtype == 2", false);
}

// This tests if inappropriate values (negative, too large) are
// rejected, but extreme values still allowed for uint32_t are ok.
TEST_F(ExpressionsTest, invalidIntegers) {

    // These are the extreme uint32_t values that still should be accepted.
    testExpression(Option::V4, "4294967295 == 0", false);

    // Negative integers should be rejected.
    testExpressionNegative<EvalParseError>("4294967295 == -1");

    // Oops, one too much.
    testExpressionNegative<EvalParseError>("4294967296 == 0");
}

// Tests whether expressions can be evaluated to a string.
TEST_F(ExpressionsTest, evaluateString) {

    // Check that content of the options is returned properly.
    testExpressionString(Option::V4, "option[100].hex", "hundred4");
    testExpressionString(Option::V6, "option[100].hex", "hundred6");

    // Check that content of non-existing option returns empty string.
    testExpressionString(Option::V4, "option[200].hex", "");
    testExpressionString(Option::V6, "option[200].hex", "");

    testExpressionNegative<EvalParseError>("pkt4.msgtype == 1", Option::V4,
                                           EvalContext::PARSER_STRING);
    testExpressionNegative<EvalParseError>("pkt6.msgtype == 1", Option::V6,
                                           EvalContext::PARSER_STRING);

    // Check that ifelse works as expecting (it was added explicitly for
    // the string evaluation).
    testExpressionString(Option::V4,
                         "ifelse(option[100].exists,'foo','bar')", "foo");
    testExpressionString(Option::V4,
                         "ifelse(option[200].exists,'foo','bar')", "bar");

    // Check that ifelse can be chained.
    testExpressionString(Option::V4,
                         "ifelse(option[200].exists,option[200].hex,"
                                 "ifelse(option[100].exists,"
                                         "option[100].hex,'none?'))",
                         "hundred4");

    // Check that hexstring works as expecting.
    testExpressionString(Option::V4, "hexstring(0x1234,':')", "12:34");
    testExpressionString(Option::V4, "hexstring(0x56789a,'-')", "56-78-9a");
    testExpressionString(Option::V4, "hexstring(0xbcde,'')", "bcde");
    testExpressionString(Option::V4, "hexstring(0xf01234,'..')", "f0..12..34");
}

// Tests the not found label.
TEST_F(ExpressionsTest, notFoundLabel) {
    TokenPtr branch;
    ASSERT_NO_THROW(branch.reset(new TokenBranch(123)));
    e_.push_back(branch);
    ValueStack values;
    ASSERT_THROW(evaluateRaw(e_, *pkt4_, values), EvalBadLabel);

    // Add a different label and a string.
    TokenPtr label;
    ASSERT_NO_THROW(label.reset(new TokenLabel(111)));
    e_.push_back(label);
    TokenPtr foo;
    ASSERT_NO_THROW(foo.reset(new TokenString("foo")));
    e_.push_back(foo);
    ASSERT_THROW(evaluateRaw(e_, *pkt4_, values), EvalBadLabel);
}

// Tests the backward label.
TEST_F(ExpressionsTest, backwardLabel) {
    // Add the label before the branch.
    TokenPtr label;
    ASSERT_NO_THROW(label.reset(new TokenLabel(123)));
    e_.push_back(label);

    TokenPtr branch;
    ASSERT_NO_THROW(branch.reset(new TokenBranch(123)));
    e_.push_back(branch);
    ValueStack values;
    ASSERT_THROW(evaluateRaw(e_, *pkt4_, values), EvalBadLabel);

    // Add a different label and a string.
    TokenPtr label2;
    ASSERT_NO_THROW(label2.reset(new TokenLabel(111)));
    e_.push_back(label2);
    TokenPtr foo;
    ASSERT_NO_THROW(foo.reset(new TokenString("foo")));
    e_.push_back(foo);
    ASSERT_THROW(evaluateRaw(e_, *pkt4_, values), EvalBadLabel);
}

// Tests the found label.
TEST_F(ExpressionsTest, label) {
    TokenPtr branch;
    ASSERT_NO_THROW(branch.reset(new TokenBranch(123)));
    e_.push_back(branch);
    TokenPtr label;
    ASSERT_NO_THROW(label.reset(new TokenLabel(123)));
    e_.push_back(label);
    TokenPtr foo;
    ASSERT_NO_THROW(foo.reset(new TokenString("foo")));
    e_.push_back(foo);
    string result;
    ASSERT_NO_THROW(result = evaluateString(e_, *pkt6_));
    EXPECT_EQ("foo", result);

    // The branch is to the first occurence (of course the parser
    // produces only one).
    e_.push_back(label);
    TokenPtr bar;
    ASSERT_NO_THROW(bar.reset(new TokenString("bar")));
    e_.push_back(bar);
    ValueStack values;
    ASSERT_NO_THROW(evaluateRaw(e_, *pkt4_, values));
    EXPECT_EQ(2, values.size());
}

// Tests the pop or branch when true / left or.
TEST_F(ExpressionsTest, popOrBranchTrue) {
    // The left or can be implemented as <left><P|BT-L><right><L>.
    // Do the complete table.
    TokenPtr branch;
    ASSERT_NO_THROW(branch.reset(new TokenPopOrBranchTrue(123)));
    TokenPtr label;
    ASSERT_NO_THROW(label.reset(new TokenLabel(123)));
    TokenPtr left;
    TokenPtr right;
    bool result_(false);

    // False or false == false.
    ASSERT_NO_THROW(left.reset(new TokenString("false")));
    ASSERT_NO_THROW(right.reset(new TokenString("false")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_FALSE(result_);
    e_.clear();

    // False or true == true.
    ASSERT_NO_THROW(left.reset(new TokenString("false")));
    ASSERT_NO_THROW(right.reset(new TokenString("true")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    e_.clear();

    // True or any thing == true.
    ASSERT_NO_THROW(left.reset(new TokenString("true")));
    ASSERT_NO_THROW(right.reset(new TokenString("any thing")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    EXPECT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_TRUE(result_);
}

// Tests the pop or branch when false / left and.
TEST_F(ExpressionsTest, popOrBranchFalse) {
    // The left and can be implemented as <left><P|BF-L><right><L>.
    // Do the complete table.
    TokenPtr branch;
    ASSERT_NO_THROW(branch.reset(new TokenPopOrBranchFalse(123)));
    TokenPtr label;
    ASSERT_NO_THROW(label.reset(new TokenLabel(123)));
    TokenPtr left;
    TokenPtr right;
    bool result_(false);

    // True and true == true.
    ASSERT_NO_THROW(left.reset(new TokenString("true")));
    ASSERT_NO_THROW(right.reset(new TokenString("true")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_TRUE(result_);
    e_.clear();

    // True and false == false.
    ASSERT_NO_THROW(left.reset(new TokenString("true")));
    ASSERT_NO_THROW(right.reset(new TokenString("false")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_));
    EXPECT_FALSE(result_);
    e_.clear();

    // False and any thing == false.
    ASSERT_NO_THROW(left.reset(new TokenString("false")));
    ASSERT_NO_THROW(right.reset(new TokenString("any thing")));
    e_.push_back(left);
    e_.push_back(branch);
    e_.push_back(right);
    e_.push_back(label);
    EXPECT_NO_THROW(result_ = evaluateBool(e_, *pkt6_));
    EXPECT_FALSE(result_);
}

// Tests the pop and branch when false / lazy if.
TEST_F(ExpressionsTest, popAndBranchFalse) {
    // The lazy can be implemented as:
    // <test><P&BF-L1><then><B-L2><L1><else><L2>>.
    // Do the complete table.
    TokenPtr brancht;
    ASSERT_NO_THROW(brancht.reset(new TokenPopAndBranchFalse(123)));
    TokenPtr branchu;
    ASSERT_NO_THROW(branchu.reset(new TokenBranch(567)));
    TokenPtr label1;
    ASSERT_NO_THROW(label1.reset(new TokenLabel(123)));
    TokenPtr label2;
    ASSERT_NO_THROW(label2.reset(new TokenLabel(567)));
    TokenPtr test;
    TokenPtr foo;
    ASSERT_NO_THROW(foo.reset(new TokenString("foo")));
    TokenPtr bar;
    ASSERT_NO_THROW(bar.reset(new TokenString("bar")));
    TokenPtr extra;
    ASSERT_NO_THROW(extra.reset(new TokenString("extra token")));
    string result_("");

    // if true then foo else bar == foo
    ASSERT_NO_THROW(test.reset(new TokenString("true")));
    e_.push_back(test);
    e_.push_back(brancht);
    e_.push_back(foo);
    e_.push_back(branchu);
    e_.push_back(label1);
    e_.push_back(bar);
    e_.push_back(extra);
    e_.push_back(label2);
    ASSERT_NO_THROW(result_ = evaluateString(e_, *pkt4_));
    EXPECT_EQ("foo", result_);
    e_.clear();

    // if false then foo else bar == bar
    ASSERT_NO_THROW(test.reset(new TokenString("false")));
    e_.push_back(test);
    e_.push_back(brancht);
    e_.push_back(foo);
    e_.push_back(extra);
    e_.push_back(branchu);
    e_.push_back(label1);
    e_.push_back(bar);
    e_.push_back(label2);
    ASSERT_NO_THROW(result_ = evaluateString(e_, *pkt6_));
    EXPECT_EQ("bar", result_);
}

}