Ejemplo n.º 1
0
 def test_get_bit_dump_string_zero_bits(self):
     """
     Test that reading zero bits from the stream as a bit dump string 
     results in getting the empty string: \"\".
     """
     bitstream = BitStream()
     self.assertEquals(bitstream.get_bit_dump_string(0), "")
     
     # Store some bits in the stream.
     bitstream.put_bit_dump_string("0001110101") # 10 bits
     
     bitstream.seek(0)
     self.assertEquals(bitstream.get_bit_dump_string(0), "")
Ejemplo n.º 2
0
    def test_get_bit_dump_string_zero_bits(self):
        """
        Test that reading zero bits from the stream as a bit dump string 
        results in getting the empty string: \"\".
        """
        bitstream = BitStream()
        self.assertEquals(bitstream.get_bit_dump_string(0), "")

        # Store some bits in the stream.
        bitstream.put_bit_dump_string("0001110101")  # 10 bits

        bitstream.seek(0)
        self.assertEquals(bitstream.get_bit_dump_string(0), "")
Ejemplo n.º 3
0
    def test_put_bitstream_copy_self(self):
        """
        Test using the put_bitstream_copy method with the same BitStream object 
        as origin and destination.
        """
        bitstream = BitStream()

        # Generate a random string of bits, ie: ('0' | '1')*
        num_bits = 50
        bits = ""
        for i in range(0, num_bits):
            bits += random.choice(
                ('0', '1'))  # inefficient, but ok for a test.

        # Put those bits in the BitStream...
        bitstream.put_bit_dump_string(bits)

        # ... copy the bitstream into itself at any point:
        bitstream.seek(random.randint(0, 50))
        bitstream.put_bitstream_copy(bitstream)

        # Check that the bitstream was unchanged by the previous operation:
        # (overwriting data with the same data is the same as doing nothing,
        # except that the current position is changed to the end of the stream)
        self.assertEquals(bitstream.get_length(), num_bits)
        self.assertEquals(bitstream.get_current_pos(), num_bits)
        bitstream.seek(0)
        read_bits = bitstream.get_bit_dump_string(bitstream.get_length())
        self.assertEqual(read_bits, bits)
Ejemplo n.º 4
0
 def test_put_bitstream_copy_self(self):
     """
     Test using the put_bitstream_copy method with the same BitStream object 
     as origin and destination.
     """
     bitstream = BitStream()
     
     # Generate a random string of bits, ie: ('0' | '1')*
     num_bits = 50        
     bits = ""
     for i in range(0,num_bits):
         bits += random.choice(('0','1')) # inefficient, but ok for a test.
         
     # Put those bits in the BitStream...
     bitstream.put_bit_dump_string(bits)
     
     # ... copy the bitstream into itself at any point:
     bitstream.seek(random.randint(0,50))
     bitstream.put_bitstream_copy(bitstream)
     
     # Check that the bitstream was unchanged by the previous operation:
     # (overwriting data with the same data is the same as doing nothing,
     # except that the current position is changed to the end of the stream)
     self.assertEquals(bitstream.get_length(),num_bits)
     self.assertEquals(bitstream.get_current_pos(),num_bits)
     bitstream.seek(0)
     read_bits = bitstream.get_bit_dump_string(bitstream.get_length())
     self.assertEqual(read_bits, bits)
Ejemplo n.º 5
0
    def test_multiformat_write_multiformat_read(self):
        """
        This test writes numeric, byte and string data to a stream and then 
        reads the whole stream as binary, hex and base64 data, ensuring that 
        the output is the expected one in each case. 
        """
        # Write a number, 2 bytes and a string
        bitstream = BitStream()
        bitstream.put_num(10438341575639894917, 64)
        bitstream.put_byte(230)
        bitstream.put_byte(191)
        bitstream.put_string("ÄäÜüßTestЯБГДЖЙŁĄŻStringĘĆŃŚŹてす" \
                                 "とアイウエオカキク4234ケコサシスセソタチツテ")

        # Read in binary, hex and base64 formats
        expected_bits = \
            "1001000011011100011100000101101110111100001101010000101110000101" \
            "1110011010111111110000111000010011000011101001001100001110011100" \
            "1100001110111100110000111001111101010100011001010111001101110100" \
            "1101000010101111110100001001000111010000100100111101000010010100" \
            "1101000010010110110100001001100111000101100000011100010010000100" \
            "1100010110111011010100110111010001110010011010010110111001100111" \
            "1100010010011000110001001000011011000101100000111100010110011010" \
            "1100010110111001111000111000000110100110111000111000000110011001" \
            "1110001110000001101010001110111110111101101100011110111110111101" \
            "1011001011101111101111011011001111101111101111011011010011101111" \
            "1011110110110101111011111011110110110110111011111011110110110111" \
            "1110111110111101101110000011010000110010001100110011010011101111" \
            "1011110110111001111011111011110110111010111011111011110110111011" \
            "1110111110111101101111001110111110111101101111011110111110111101" \
            "1011111011101111101111011011111111101111101111101000000011101111" \
            "1011111010000001111011111011111010000010111011111011111010000011"
        expected_hex = \
            "90dc705bbc350b85e6bfc384c3a4c39cc3bcc39f54657374d0afd091d093d094" \
            "d096d099c581c484c5bb537472696e67c498c486c583c59ac5b9e381a6e38199" \
            "e381a8efbdb1efbdb2efbdb3efbdb4efbdb5efbdb6efbdb7efbdb834323334ef" \
            "bdb9efbdbaefbdbbefbdbcefbdbdefbdbeefbdbfefbe80efbe81efbe82efbe83"
        expected_base64 = \
            "kNxwW7w1C4Xmv8OEw6TDnMO8w59UZXN00K/QkdCT0JTQltCZxYHEhMW7U3RyaW5n" \
            "xJjEhsWDxZrFueOBpuOBmeOBqO+9se+9su+9s++9tO+9te+9tu+9t++9uDQyMzTv" \
            "vbnvvbrvvbvvvbzvvb3vvb7vvb/vvoDvvoHvvoLvvoM="

        bitstream.seek(0)
        self.assertEquals( \
                    bitstream.get_bit_dump_string(bitstream.get_length()),
                    expected_bits)
        bitstream.seek(0)
        self.assertEquals(
            bitstream.get_hex(bitstream.get_length()).lower(), expected_hex)
        bitstream.seek(0)
        self.assertEquals(bitstream.get_base64(bitstream.get_length()),
                          expected_base64)
Ejemplo n.º 6
0
 def test_multiformat_write_multiformat_read(self):
     """
     This test writes numeric, byte and string data to a stream and then 
     reads the whole stream as binary, hex and base64 data, ensuring that 
     the output is the expected one in each case. 
     """
     # Write a number, 2 bytes and a string
     bitstream = BitStream()
     bitstream.put_num(10438341575639894917, 64)
     bitstream.put_byte(230)
     bitstream.put_byte(191)
     bitstream.put_string("ÄäÜüßTestЯБГДЖЙŁĄŻStringĘĆŃŚŹてす" \
                              "とアイウエオカキク4234ケコサシスセソタチツテ")
     
     # Read in binary, hex and base64 formats
     expected_bits = \
         "1001000011011100011100000101101110111100001101010000101110000101" \
         "1110011010111111110000111000010011000011101001001100001110011100" \
         "1100001110111100110000111001111101010100011001010111001101110100" \
         "1101000010101111110100001001000111010000100100111101000010010100" \
         "1101000010010110110100001001100111000101100000011100010010000100" \
         "1100010110111011010100110111010001110010011010010110111001100111" \
         "1100010010011000110001001000011011000101100000111100010110011010" \
         "1100010110111001111000111000000110100110111000111000000110011001" \
         "1110001110000001101010001110111110111101101100011110111110111101" \
         "1011001011101111101111011011001111101111101111011011010011101111" \
         "1011110110110101111011111011110110110110111011111011110110110111" \
         "1110111110111101101110000011010000110010001100110011010011101111" \
         "1011110110111001111011111011110110111010111011111011110110111011" \
         "1110111110111101101111001110111110111101101111011110111110111101" \
         "1011111011101111101111011011111111101111101111101000000011101111" \
         "1011111010000001111011111011111010000010111011111011111010000011"
     expected_hex = \
         "90dc705bbc350b85e6bfc384c3a4c39cc3bcc39f54657374d0afd091d093d094" \
         "d096d099c581c484c5bb537472696e67c498c486c583c59ac5b9e381a6e38199" \
         "e381a8efbdb1efbdb2efbdb3efbdb4efbdb5efbdb6efbdb7efbdb834323334ef" \
         "bdb9efbdbaefbdbbefbdbcefbdbdefbdbeefbdbfefbe80efbe81efbe82efbe83"
     expected_base64 = \
         "kNxwW7w1C4Xmv8OEw6TDnMO8w59UZXN00K/QkdCT0JTQltCZxYHEhMW7U3RyaW5n" \
         "xJjEhsWDxZrFueOBpuOBmeOBqO+9se+9su+9s++9tO+9te+9tu+9t++9uDQyMzTv" \
         "vbnvvbrvvbvvvbzvvb3vvb7vvb/vvoDvvoHvvoLvvoM="
         
     bitstream.seek(0)
     self.assertEquals( \
                 bitstream.get_bit_dump_string(bitstream.get_length()),
                 expected_bits)
     bitstream.seek(0)
     self.assertEquals(bitstream.get_hex(bitstream.get_length()).lower(),
                       expected_hex)
     bitstream.seek(0)
     self.assertEquals(bitstream.get_base64(bitstream.get_length()),
                       expected_base64)
Ejemplo n.º 7
0
 def test_bit_dump_string_basic(self):
     """
     This method tests put_bit_dump_string's and get_bit_dump_string's 
     basic functionality.
     """
     bitstream = BitStream()
     
     # Generate a random string of bits, ie: ('0' | '1')*
     num_bits = 50        
     bits = ""
     for i in range(0,num_bits):
         bits += random.choice(('0','1')) # inefficient, but ok for a test.
         
     # Put those bits in the BitStream...
     bitstream.put_bit_dump_string(bits)
     
     # ...and get them back
     bitstream.seek(0)
     read_bits = bitstream.get_bit_dump_string(len(bits))
     
     # Check that the bits were recovered correctly
     self.assertEqual(read_bits, bits)
Ejemplo n.º 8
0
    def test_bit_dump_string_basic(self):
        """
        This method tests put_bit_dump_string's and get_bit_dump_string's 
        basic functionality.
        """
        bitstream = BitStream()

        # Generate a random string of bits, ie: ('0' | '1')*
        num_bits = 50
        bits = ""
        for i in range(0, num_bits):
            bits += random.choice(
                ('0', '1'))  # inefficient, but ok for a test.

        # Put those bits in the BitStream...
        bitstream.put_bit_dump_string(bits)

        # ...and get them back
        bitstream.seek(0)
        read_bits = bitstream.get_bit_dump_string(len(bits))

        # Check that the bits were recovered correctly
        self.assertEqual(read_bits, bits)