Example #1
0
 def test_subset(self):
     for (start, stop) in [(0, 32), (0, 16), (0, 6), (100, 200), (33, 107)]:
         with self.subTest(start=start, stop=stop):
             data = bytearray(random.getrandbits(8) for _ in range(500))
             vm = VM()
             vm.push(stop)
             vm.push(start)
             vm.push(byterange.frombytes(data))
             byterange.get_subset(vm)
             self.assertEqual(byterange.frombytes(data[start:stop]),
                              vm.stack[0])
Example #2
0
 def test_byterange_keccak256(self):
     for length in [64, 128, 200, 136, 135]:
         data = bytearray(random.getrandbits(8) for _ in range(length))
         vm = VM()
         vm.push(len(data))
         vm.push(byterange.frombytes(data))
         sha3.hash_byterange(vm)
         real_hash = int.from_bytes(eth_utils.crypto.keccak(data),
                                    byteorder="big")
         self.assertEqual(real_hash, vm.stack[0])
Example #3
0
 def test_get8(self):
     data = bytearray(random.getrandbits(8) for _ in range(100))
     br = byterange.frombytes(data)
     for i in range(100):
         with self.subTest(index=i):
             vm = VM()
             vm.push(br)
             vm.push(i)
             vm.dup1()
             byterange.get8(vm)
             self.assertEqual(data[i], vm.stack[0])
Example #4
0
 def test_copy(self):
     indexes = [(0, 32, 0), (0, 16, 0), (0, 32, 32), (0, 6, 0),
                (37, 108, 42)]
     for (source_start, source_end, dest_start) in indexes:
         with self.subTest(source_start=source_start,
                           source_end=source_end,
                           dest_start=dest_start):
             source = bytearray(random.getrandbits(8) for _ in range(500))
             dest = bytearray(random.getrandbits(8) for _ in range(500))
             size = source_end - source_start
             vm = VM()
             vm.push(dest_start)
             vm.push(byterange.frombytes(dest))
             vm.push(source_end)
             vm.push(source_start)
             vm.push(byterange.frombytes(source))
             byterange.copy(vm)
             result = (dest[:dest_start] + source[source_start:source_end] +
                       dest[dest_start + size:])
             self.assertEqual(byterange.frombytes(result), vm.stack[0])
Example #5
0
    def test_pushblock(self):
        data = bytearray(random.getrandbits(8) for _ in range(200))
        vm1 = VM()
        sha3.ctx_new(vm1)
        for v in range(136):
            vm1.push(data[v])
            vm1.swap1()
            sha3.ctx_pushbyte(vm1)

        vm2 = VM()
        vm2.push(0)
        vm2.push(byterange.frombytes(data))
        sha3.byterange_get136(vm2)
        sha3.ctx_new(vm2)
        sha3.ctx_pushblock(vm2)
        self.assertEqual(vm1.stack[0], vm2.stack[0])
Example #6
0
    def test_set8(self):
        data = bytearray(random.getrandbits(8) for _ in range(100))
        br = byterange.frombytes(data)
        update_bytes = random.getrandbits(8)
        for i in range(100):
            with self.subTest(index=i):
                vm = VM()
                vm.push(update_bytes)
                vm.push(i)
                vm.push(br)
                byterange.set_val8(vm)

                solution = bytearray(data)
                solution[i] = update_bytes
                self.assertEqual(solution.hex(),
                                 sized_byterange.tohex([vm.stack[0], 100])[2:])
Example #7
0
 def test_get136(self):
     data = bytearray(random.getrandbits(8) for _ in range(200))
     vm = VM()
     vm.push(0)
     vm.push(byterange.frombytes(data))
     sha3.byterange_get136(vm)
     self.assertEqual(
         vm.stack[0],
         value.Tuple([
             int.from_bytes(data[0:32], byteorder="little"),
             int.from_bytes(data[32:64], byteorder="little"),
             int.from_bytes(data[64:96], byteorder="little"),
             int.from_bytes(data[96:128], byteorder="little"),
             int.from_bytes(data[128:136] +
                            bytearray([0 for x in range(24)]),
                            byteorder="little")
         ]))
Example #8
0
 def test_frombytes(self):
     data = bytearray(random.getrandbits(8) for _ in range(500))
     data2 = bytearray(data)
     if len(data2) % 32 != 0:
         data2 = data2 + b"\0" * (32 - (len(data2) % 32))
     chunks = [
         eth_utils.big_endian_to_int(data2[i:i + 32])
         for i in range(0, len(data2), 32)
     ]
     vm = VM()
     byterange.new(vm)
     for i, chunk in enumerate(chunks):
         vm.push(i * 32)
         vm.push(chunk)
         vm.swap2()
         byterange.set_val(vm)
     self.assertEqual(byterange.frombytes(data), vm.stack[0])