# File covered: evlMgr.c

set pathToTestFiles "./tests"

if {[lsearch [namespace children] ::tcltest] == -1} {
  package require tcltest
  namespace import -force ::tcltest::*
}

proc check { testId result expectedResult pktString mgrString } {
if { $result != $expectedResult } {
  puts "Test $testId failed"
  puts "\tPkt=\t$pktString"
  puts "\tRules=\t$mgrString"
  puts "\tExpected result=$expectedResult, actual result=$result"
} else {
  puts "Test $testId passed"
}
}

set testId 0

# set pkt [pktCreateFromString "srcip:192.168.1.1; destip:1.2.3.4; ipflags:MF; ipoptions:EOOL; offset:1234; ttl:255; protocol:tcp; srcport:80; destport:80; seqnum:100; acknum:100; tcpflag:fin; size:100; content:\"foo\"; depth:0; content:\"bar\"; depth:4; mesg:\"a message\";"]

set rText {
  tcp any any -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:0; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-1 {simple warmup check} \
                      $pText  $rText  {0} ]
 
set rText {
  var HN 0.0.0.0
  tcp HN any -> any any () route:eth0;
  tcp !HN any -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:0; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-2 {ip var define} \
                      $pText  $rText  {0} ]
                                                                           
set rText {
  var HN 0.0.0.0
  var PN 0
  tcp HN PN -> any any () route:eth0;
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:0; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-3 {ip, port var define} \
                      $pText  $rText  {0} ]
           

set rText {
  var HN 0.0.0.0,1.1.1.1,!1.2.3.4
  var PN 0:80
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
  tcp HN PN -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-4 {ip, port var define} \
                      $pText  $rText  {2} ]

set rText {
  var HN 0.0.0.0,1.1.1.1,!1.2.3.4
  var PN 0:79
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
  tcp HN PN -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-5 {ip, port var define} \
                      $pText  $rText  {} ]

set rText [format "%s%s" "include $pathToTestFiles/evlMgr.test.include.1.tcl" \
{ 
  var PN 0:79
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
  tcp HN PN -> any any () route:eth0;
}
]


set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-6 {ip, port var define} \
                      $pText  $rText  {} ]

set rText [format "%s%s%s" "include $pathToTestFiles/evlMgr.test.include.1.tcl\n" \
  "include $pathToTestFiles/evlMgr.test.include.2.tcl\n"  \
{
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
  tcp HN PN -> any any () route:eth0; 
} 
]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-7 {includes, comments} \
                      $pText  $rText  {} ]

set rText {
  var HN 0.1.2.4
  var PN 80
  var HN 0.0.0.0
  tcp !HN !PN -> any any () route:eth0;
  icmp !HN !PN -> any any () route:eth0;
  tcp HN PN -> any any () route:eth0;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-8 {duplicated var, last define wins} \
                      $pText  $rText  {2} ]


set rText [format "%s%s" "include $pathToTestFiles/evlMgr.test.include.3.tcl" \
  { 
   # below is rule number 1, since rule 0 is in an included file
   tcp !HN !PN -> any any () route:eth0;
   icmp !HN !PN -> any any () route:eth0;
   tcp HN !PN -> any any () route:eth0;
  }
]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-9 {nested includes} \
                      $pText  $rText  {3} ]


set rText {
  tcp any any -> any any () \
  		route:eth0;
  tcp any !80 -> any any () \
  		route:eth0;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-10 {split lines} \
                      $pText  $rText  {0} ]


set rText {
  tcp any any -> any any () \
  		route:eth0;
  tcp any !80 -> any any () \
  		route:eth0;
  tcp any any -> any any () \
  		route:eth1;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-11 {split lines, dup rules} \
                      $pText  $rText  {0,2} ]

set rText {
  var HN \
  192.14.1.1,\
2.2.1.1,0.0.0.0
  tcp any any -> any any () \
  		route:eth0;
  tcp any !80 -> any any () \
  		route:eth0;
  tcp any any -> any any () \
  		route:eth1;
  tcp HN any -> any any () route:eth1;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:0.0.0.0; }

lappend TESTS [  list evlMgr-12 {split lines, dup rules} \
                      $pText  $rText  {0,2,3} ]

set rText {
  var HN \
  192.14.1.1,\
2.2.1.1,0.0.0.0
  var EN HN, 1.1.1.1
  tcp HN any -> EN any () route:eth1;
  tcp any !80 -> any any () \
  		route:eth0;
  tcp any any -> any any () \
  		route:eth1;
  tcp any any -> any any () \
  		route:eth0;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-13 {split lines, dup rules, nested defines } \
                      $pText  $rText  {0,2,3} ]


set rText {
  var HN 0.0.0.0
  var A !HN
  var B HN
  var C A,1.1.1.1
  tcp HN any -> A any () route:eth1;
  tcp HN any -> B any () route:eth1;
  tcp HN any -> C any () route:eth1;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-14 {nested defines } \
                      $pText  $rText  {0,2} ]


set rText {
  var HN 0.0.0.0
  var A !HN
  var B HN
  var C A,B,1.1.1.1
  tcp HN any -> A any () route:eth1;
  tcp HN any -> B any () route:eth1;
  tcp HN any -> C any () route:eth1;
}
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-15 {nested defines } \
                      $pText  $rText  {0,2} ]


set rText [format "%s\n%s" "include $pathToTestFiles/evlMgr.test.include.4.tcl" \
		{ tcp HN any -> any any () route:eth1; } ]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-16 {large defined list (100 elements)} \
                      $pText  $rText {0} ]

set rText [format "%s\n%s" "include $pathToTestFiles/evlMgr.test.include.5.tcl" \
 	{ tcp HN any -> any any () route:eth1; } ]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-17 {very large defined list (1000 elements)} \
                      $pText  $rText {0} ]

set rText [format "%s\n%s" "include $pathToTestFiles/evlMgr.test.include.6.tcl" \
		{ tcp HN any -> any any () route:eth1; } ]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

lappend TESTS [  list evlMgr-18 {very very large defined list (10000 elements)} \
                      $pText  $rText {} ]


set rText [format "%s\n%s" "include $pathToTestFiles/evlMgr.test.include.7.tcl" \
		{ tcp HN any -> any any () route:eth1; } ]
 
set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }

# this is a very slow test, commented out
# lappend TESTS [  list evlMgr-19 {very very very large defined list (100000 elements)} \
#                       $pText  $rText {} ]

# this is a very slow test, commented out
# set rText [format "%s\n%s" "include $pathToTestFiles/evlMgr.test.include.8.tcl" \
# 		{ tcp HN any -> any any () route:eth1; } ]
 
# set pText { protocol:tcp; srcport:80; srcip:0.0.0.0; destip:1.1.1.1; }
# 
# lappend TESTS [  list evlMgr-20 {very very very very very large defined list (1000000 elements)} \
#                       $pText  $rText {} ]




### 

set totalCount 0
set runCount 0

foreach aTest $TESTS {
  set totalCount [expr $totalCount + 1 ]
  set testId [lindex $aTest 0]
  set testLabel [lindex $aTest 1]
  if { 1 || [regexp {\-8} $testId ] } {
    set runCount [expr $runCount + 1 ]
    puts "Running $testId\t($testLabel)"

    set pktText [lindex $aTest 2]
    set mgrText [lindex $aTest 3]
    set expectedResult [lindex $aTest 4]

    set pkt [createPktFromText $pktText]
    set mgr [evlBuildManagerFromText $mgrText]

    test $testId $testLabel {evlComputeRuleSetForEthSTRING $mgr $pkt}  $expectedResult
  }
}

stop_profiler
puts "Completed running $runCount tests out of $totalCount possible tests"