Beispiel #1
0
 def testMinus(self):
     h1 = headerspace(1)
     h1.add_hs(wildcard_create_from_string("1001xxxx"))
     h2 = headerspace(1)
     h2.add_hs(wildcard_create_from_string("100xx000"))
     h1.minus(h2)
     self.assertEqual(h1.count(),3)
Beispiel #2
0
 def testRW1(self):
     tf = TF(1)
     tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], "10011111", "01100000"))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("1001xxxx"))
     result = tf.T(hs, 1)
     self.assertEqual(len(result), 1)
     self.assert_(wildcard_is_equal(result[0][0].hs_list[0], wildcard_create_from_string("1111xxxx")))
Beispiel #3
0
 def testSelfDiff(self):
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     h.diff_hs(wildcard_create_from_string("100xxx00"))
     h.diff_hs(wildcard_create_from_string("1xxxx111"))
     h.self_diff()
     self.assertEqual(h.count(),5)
     self.assertEqual(h.count_diff(),0)
Beispiel #4
0
 def testCreate(self):
     '''
     Test if creating a headerspace object creates correct number of 
     bytearrays inside.
     '''
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     self.assertEqual(h.count(),2)
Beispiel #5
0
 def testComplement(self):
     '''
     Test if complement correctly handles diffs
     '''
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.diff_hs(wildcard_create_from_string("100xx000"))
     h.complement()
     self.assertEqual(h.count(),5)
Beispiel #6
0
 def testRW1(self):
     tf = TF(1)
     tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \
                                             "10011111", "01100000"))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("1001xxxx"))
     result = tf.T(hs, 1)
     self.assertEqual(len(result), 1) 
     self.assert_(wildcard_is_equal(result[0][0].hs_list[0],\
                                    wildcard_create_from_string("1111xxxx")))
Beispiel #7
0
 def testIntersect1(self):
     '''
     Test intersect with a bytearray
     '''
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     h.diff_hs(wildcard_create_from_string("100xx000"))
     h.diff_hs(wildcard_create_from_string("1xxx1x11"))
     h.intersect(wildcard_create_from_string("xxxxx011"))
     self.assertEqual(h.count(),2)
     self.assertEqual(h.count_diff(),2)
Beispiel #8
0
 def testInverse(self):
     tf = TF(1)
     tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], "10011111", "01100000"))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("111xxxxx"))
     hs.diff_hs(wildcard_create_from_string("1110xxxx"))
     result = tf.T_inv(hs, 2)
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0][0].count(), 1)
     self.assertEqual(result[0][0].count_diff(), 1)
     self.assert_(wildcard_is_equal(result[0][0].hs_list[0], wildcard_create_from_string("10xxxxxx")))
     self.assert_(wildcard_is_equal(result[0][0].hs_diff[0][0], wildcard_create_from_string("10x0xxxx")))
Beispiel #9
0
 def testDependency(self):
     tf = TF(1)
     tf.add_fwd_rule(TF.create_standard_rule([1], "10xxxxxx", [2], None, None))
     tf.add_rewrite_rule(TF.create_standard_rule([1], "1xxxxxxx", [3], "00111111", "10000000", "", []))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("xxxxxxxx"))
     result = tf.T(hs, 1)
     self.assertEqual(len(result), 2, "Expecting both rules to be matched")
     self.assertTrue(
         wildcard_is_equal(result[1][0].hs_list[0], wildcard_create_from_string("10xxxxxx")),
         "unexpected second byte array",
     )
Beispiel #10
0
 def testDependency(self):
     tf = TF(1)
     tf.add_fwd_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \
                                             None, None))
     tf.add_rewrite_rule(TF.create_standard_rule([1], "1xxxxxxx", [3], "00111111", "10000000","",[]))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("xxxxxxxx"))
     result = tf.T(hs, 1)
     self.assertEqual(len(result), 2, "Expecting both rules to be matched")
     self.assertTrue(wildcard_is_equal(
                                      result[1][0].hs_list[0],\
                                      wildcard_create_from_string("10xxxxxx"),\
                                      ), \
                     "unexpected second byte array")
Beispiel #11
0
 def testDiffHS(self):
     '''
     Test the diff (lazy subtraction):
     1) adding a diff before having anything doesn't add any diff to hs
     2) adding a diff actually adds correct number of diff bytearrays
     3) adding a new bytearray that has intersection with a previously added
     diff doesn't add that diff to the new bytearray.
     '''
     h = headerspace(1)
     h.diff_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     self.assertEqual(h.count_diff(),0)
     h.diff_hs(wildcard_create_from_string("1xxx1111"))
     self.assertEqual(h.count_diff(),2)
     h.add_hs(wildcard_create_from_string("xxxxxx11"))
     self.assertEqual(h.count_diff(),2)
Beispiel #12
0
 def testInverse(self):
     tf = TF(1)
     tf.add_rewrite_rule(TF.create_standard_rule([1], "10xxxxxx", [2], \
                                             "10011111", "01100000"))
     hs = headerspace(1)
     hs.add_hs(wildcard_create_from_string("111xxxxx"))
     hs.diff_hs(wildcard_create_from_string("1110xxxx"))
     result = tf.T_inv(hs, 2)
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0][0].count(),1)
     self.assertEqual(result[0][0].count_diff(),1)
     self.assert_(wildcard_is_equal(result[0][0].hs_list[0],\
                                    wildcard_create_from_string("10xxxxxx"),\
                                    ))
     self.assert_(wildcard_is_equal(result[0][0].hs_diff[0][0],\
                                    wildcard_create_from_string("10x0xxxx"),\
                                    ))
Beispiel #13
0
 def add_entry(self, srcStr, dstStr):
     '''
     Add a new entry (with src and dst IP addesses). 
     @srcStr: source IP string. E.g., 10.130.127.1 or 10.130.127.1/24
     @dstStr: destination IP string. E.g., 10.130.127.2 or 10.130.127.2/22
     '''
     if "/" not in srcStr:
         self.src.add_hs(
             uw.wildcard_create_from_int(self.ip2long(srcStr), 4))
     else:
         self.src.add_hs(
             uw.wildcard_create_from_string(self.ip2wildcard(srcStr)))
     if "/" not in dstStr:
         self.dst.add_hs(
             uw.wildcard_create_from_int(self.ip2long(dstStr), 4))
     else:
         self.dst.add_hs(
             uw.wildcard_create_from_string(self.ip2wildcard(dstStr)))
Beispiel #14
0
 def testCopy(self):
     '''
     Test if copy works correctly
     Adding new stuff on the original hs, doesn't affect copied hs.
     '''
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     h.diff_hs(wildcard_create_from_string("100x0000"))
     h.diff_hs(wildcard_create_from_string("1xxx1111"))
     hcpy = h.copy()
     self.assertEqual(h.count(),hcpy.count())
     self.assertEqual(h.count_diff(),hcpy.count_diff())
     h.add_hs(wildcard_create_from_string("100100xx"))
     self.assertEqual(h.count(),3)
     self.assertEqual(h.count_diff(),3)
     self.assertEqual(hcpy.count(),2)
     self.assertEqual(h.count_diff(),3)        
Beispiel #15
0
 def testContainedIn(self):
     h1 = headerspace(1)
     h1.add_hs(wildcard_create_from_string("1001xxxx"))
     h1.diff_hs(wildcard_create_from_string("1xxxx111"))
     h2 = headerspace(1)
     h2.add_hs(wildcard_create_from_string("1001xxxx"))
     h2.add_hs(wildcard_create_from_string("11xxxx11"))
     h2.diff_hs(wildcard_create_from_string("100xxx00"))
     h2.diff_hs(wildcard_create_from_string("1xxxx111"))
     self.assertTrue(h1.is_contained_in(h2))
     self.assertFalse(h2.is_contained_in(h1))
Beispiel #16
0
 def testIntersect2(self):
     '''
     Test intersect with a headerspace
     '''
     h = headerspace(1)
     h.add_hs(wildcard_create_from_string("1001xxxx"))
     h.add_hs(wildcard_create_from_string("11xxxx11"))
     h.diff_hs(wildcard_create_from_string("100xx000"))
     h.diff_hs(wildcard_create_from_string("1xxx1x11"))
     other = headerspace(1)
     other.add_hs(wildcard_create_from_string("10xxxxx1"))
     other.diff_hs(wildcard_create_from_string("10010xxx"))
     h.intersect(other)
     self.assertEqual(other.count(),1)
     self.assertEqual(other.count_diff(),1)
     self.assertEqual(h.count(),1)
     self.assertEqual(h.count_diff(),2)
Beispiel #17
0
    Copyright 2012, Stanford University. This file is licensed under GPL v2 plus
    a special exception, as described in included LICENSE_EXCEPTION.txt.
    
Created on Jun 7, 2012

@author: Peyman Kazemian
'''

import headerspace.hs as hs
import utils.wildcard as uw

# Creating a header space object of length 8 bits (1 byte)
hsl = hs.headerspace(4)

# Adding some wildcard expressions to the headerspace object
hsl.add_hs(uw.wildcard_create_from_string("00001010100000100111111100000011"))
hsl.add_hs(uw.wildcard_create_from_int(175668993, 4))  # 10.120.127.1
hsl.add_hs(uw.wildcard_create_from_string("000010101000001001111111xxxxxxxx"))
print "original HS is\n", hsl, "\n---------"

# Removing some wildcard expressions from the headerspace object
#hsl.diff_hs(uw.wildcard_create_from_string("1010011x"))
#hsl.diff_hs(uw.wildcard_create_from_string("1010xxx0"))
#print "New HS is\n",hsl,"\n---------"

# Intersecting this headerspace with some wildcard expression
hsl.intersect(
    uw.wildcard_create_from_string("00001010100000101000000000000001"))
print "After intersection HS is\n", hsl, "\n---------"

# Forcing the subtraction to be computed