Example #1
0
 def test_initialization(self):
     self.assertEqual(bitvectify(2, 8), Constant(2, 8))
     self.assertEqual(bitvectify("x", 8), Variable("x", 8))
     self.assertEqual(bitvectify(Term(width=8), 8), Term(width=8))
Example #2
0
 def test_invalid_args(self):
     with self.assertRaises(AssertionError):
         Variable(["v", "a", "r"], 8)
     with self.assertRaises(AssertionError):
         Variable(000, 8)
Example #3
0
    def test_comparisons(self):
        x, y = Variable("x", 8), Variable("y", 8)
        x9, z = Variable("x", 9), Variable("z", 9)

        self.assertTrue((x != y) & (x != x9) & (x != z))
        self.assertEqual(x, Variable("x", 8))
Example #4
0
    def test_python_operators(self):
        x = Variable("x", 8)
        y = Variable("y", 8)

        self.assertEqual(BvNot(x), ~x)

        expr = x
        expr &= y
        self.assertEqual(BvAnd(x, y), x & y)
        self.assertEqual(x & y, expr)

        expr = x
        expr |= y
        self.assertEqual(BvOr(x, y), x | y)
        self.assertEqual(x | y, expr)

        expr = x
        expr ^= y
        self.assertEqual(BvXor(x, y), x ^ y)
        self.assertEqual(x ^ y, expr)

        self.assertEqual(BvUlt(x, y), (x < y))

        self.assertEqual(BvUle(x, y), (x <= y))

        self.assertEqual(BvUgt(x, y), (x > y))

        self.assertEqual(BvUge(x, y), (x >= y))

        expr = x
        expr <<= y
        self.assertEqual(BvShl(x, y), x << y)
        self.assertEqual(x << y, expr)

        expr = x
        expr >>= y
        self.assertEqual(BvLshr(x, y), x >> y)
        self.assertEqual(x >> y, expr)

        self.assertEqual(Extract(x, 4, 2), x[4:2])
        self.assertEqual(Extract(x, 4, 4), x[4:4])
        self.assertEqual(Extract(x, 7, 1), x[:1])
        self.assertEqual(Extract(x, 6, 0), x[6:])

        self.assertEqual(BvNeg(x), -x)

        expr = x
        expr += y
        self.assertEqual(BvAdd(x, y), x + y)
        self.assertEqual(x + y, expr)

        expr = x
        expr -= y
        self.assertEqual(BvSub(x, y), x - y)
        self.assertEqual(x - y, expr)

        expr = x
        expr *= y
        self.assertEqual(BvMul(x, y), x * y)
        self.assertEqual(x * y, expr)

        expr = x
        expr /= y
        self.assertEqual(BvUdiv(x, y), x / y)
        self.assertEqual(x / y, expr)

        expr = x
        expr %= y
        self.assertEqual(BvUrem(x, y), x % y)
        self.assertEqual(x % y, expr)
Example #5
0
 def test_variable_properties(self):
     self._simple_properties(Variable("x", 8))
Example #6
0
    def test_invalid_operations(self):
        x = Variable("x", 8)
        y = Variable("y", 9)
        b = Variable("b", 1)
        max_value = (2 ** 8) - 1

        with self.assertRaises(TypeError):
            x ** 2
        with self.assertRaises(TypeError):
            x // 2
        with self.assertRaises(TypeError):
            abs(x)

        for op in simple_op:
            with self.assertRaises((AssertionError, TypeError)):
                op()
            with self.assertRaises((AssertionError, TypeError)):
                op(x, -1)  # invalid range
            with self.assertRaises((AssertionError, TypeError)):
                op(x, max_value + 1)  # invalid range
            with self.assertRaises((AssertionError, TypeError)):
                op(2, 3)  # at least 1 Term
            with self.assertRaises((AssertionError, TypeError)):
                op(x, y)  # != width
            with self.assertRaises((AssertionError, TypeError)):
                op(x)  # invalid # of args
            with self.assertRaises((AssertionError, TypeError)):
                op(x, x, x)

        for op in unary_op:
            with self.assertRaises((AssertionError, TypeError)):
                op()
            with self.assertRaises((AssertionError, TypeError)):
                op(-1)
            with self.assertRaises((AssertionError, TypeError)):
                op(max_value + 1)
            with self.assertRaises((AssertionError, TypeError)):
                op(1)
            with self.assertRaises((AssertionError, TypeError)):
                op(x, x)
            with self.assertRaises((AssertionError, TypeError)):
                op(x, x, x)

        for op in [ZeroExtend, Repeat]:
            with self.assertRaises((AssertionError, TypeError)):
                op()
            with self.assertRaises((AssertionError, TypeError)):
                op(x, -1)

        with self.assertRaises((AssertionError, TypeError)):
            Concat(x, -1)
        with self.assertRaises((AssertionError, TypeError)):
            Concat(x, 1)

        with self.assertRaises((AssertionError, TypeError)):
            Extract(x, 0, 1)
        with self.assertRaises((AssertionError, TypeError)):
            Extract(x, x, 1)

        with self.assertRaises((AssertionError, TypeError)):
            Ite(b, x, y)
        with self.assertRaises((AssertionError, TypeError)):
            Ite(x, x, x)
        with self.assertRaises((AssertionError, TypeError)):
            Ite(0, x, x)
Example #7
0
    def test_creation(self):
        self.assertEqual(self.func(0, 0), (0x00, 0x00))

        with self.assertRaises(TypeError):
            self.func(Variable("x", 8), Variable("y", 8))
Example #8
0
    def test_creation(self):
        self.assertEqual(self.cipher([0], [0]), (0x01, ))

        with self.assertRaises(TypeError):
            self.cipher([Variable("x", 8)], [Variable("y", 8)])