def test_string_ascii(self): """ Test basic put_string and get_string behavior, using ASCII strings. """ bitstream = BitStream() string1 = "Test string " # 12 chars/bytes string2 = "using only ASCII characters (0-126):\n\t" # 38 chars/bytes string3 = "Hello World!" # 12 chars/bytes # Store our message in 3 writes bitstream.put_string(string1) bitstream.put_string(string2) bitstream.put_string(string3) # Sanity check: self.assertEquals(bitstream.get_length(), (12 + 38 + 12) * 8) self.assertEquals(bitstream.get_current_pos(), (12 + 38 + 12) * 8) # Retrieve our message in 2 reads bitstream.seek(0) self.assertEquals( bitstream.get_string(29 * 8), # read 29 bytes "Test string using only ASCII ") self.assertEquals( bitstream.get_string(33 * 8), # read 33 bytes "characters (0-126):\n\tHello World!")
def test_get_string_zero_bits(self): """ Test that reading zero bits from the stream as a string results in getting the empty string: \"\". """ bitstream = BitStream() self.assertEquals(bitstream.get_string(0), "") # Store a string in the stream. bitstream.put_string("Hello World!") bitstream.seek(0) self.assertEquals(bitstream.get_string(0), "")
def test_get_string_utf8_read_partial_characters(self): """ Test that it is possible to read "partial" utf-8 characters from the bitstream and that concatenation restores the full character/glyph. """ bitstream = BitStream() string = "てすとアイウエオカキクケコサシスセソタチツテ" # 22 chars, 66 bytes bitstream.put_string(string) bitstream.seek(0) # First read 32 bytes readString = bitstream.get_string(32 * 8) self.assertEquals(readString, "てすとアイウエオカキ\xef\xbd") # Now read the rest readString += bitstream.get_string(34 * 8) self.assertEquals(readString, "てすとアイウエオカキクケコサシスセソタチツテ")
def test_get_string_utf8_read_partial_characters(self): """ Test that it is possible to read "partial" utf-8 characters from the bitstream and that concatenation restores the full character/glyph. """ bitstream = BitStream() string = "てすとアイウエオカキクケコサシスセソタチツテ" # 22 chars, 66 bytes bitstream.put_string(string) bitstream.seek(0) # First read 32 bytes readString = bitstream.get_string(32*8) self.assertEquals(readString,"てすとアイウエオカキ\xef\xbd") # Now read the rest readString += bitstream.get_string(34*8) self.assertEquals(readString,"てすとアイウエオカキクケコサシスセソタチツテ")
def test_put_bitstream_copy(self): """ Test the basic functionality of the put_bitstream_copy method. """ bitstream1 = BitStream() bitstream1.put_string("This is bitstream1") bitstream2 = BitStream() bitstream2.put_string("This is bitstream2") bitstream3 = BitStream() # bitstream3 remains empty bitstream4 = BitStream() bitstream4.put_string("This is bitstream4") # copy the full contents of bitstream2 to the end of bitstream1 bitstream2.seek(0) bitstream1.put_bitstream_copy(bitstream2) self.assertEquals(bitstream2.get_current_pos(), bitstream2.get_length()) # check the contents of bitstream1 bitstream1.seek(0) self.assertEquals(bitstream1.get_string(bitstream1.get_length()), "This is bitstream1This is bitstream2") # copy the full contents of bitstream3 (aka. nothing) to the end of # bitstream4 bitstream3.seek(0) bitstream4.put_bitstream_copy(bitstream3) # check the contents of bitstream4 bitstream4.seek(0) self.assertEquals(bitstream4.get_string(bitstream4.get_length()), "This is bitstream4") # copy the contents of bitstream4 from the position 8 onwards bitstream4.seek(8 * 8) bitstream1.put_bitstream_copy(bitstream4) self.assertEquals(bitstream4.get_current_pos(), bitstream4.get_length()) # check the contents of bitstream1 bitstream1.seek(0) self.assertEquals(bitstream1.get_string(bitstream1.get_length()), "This is bitstream1This is bitstream2bitstream4")
def test_string_character_zero(self): """ Test that we are handling character zero ('\\0') correctly. """ bitstream = BitStream() bitstream.put_string("Evil \0String.") # 13 chars/bytes self.assertEquals(bitstream.get_length(), 13 * 8) bitstream.seek(0) self.assertEquals(bitstream.get_string(bitstream.get_length()), "Evil \0String.")
def test_string_character_zero(self): """ Test that we are handling character zero ('\\0') correctly. """ bitstream = BitStream() bitstream.put_string("Evil \0String.") # 13 chars/bytes self.assertEquals(bitstream.get_length(),13*8) bitstream.seek(0) self.assertEquals(bitstream.get_string(bitstream.get_length()), "Evil \0String.")
def test_put_bitstream_copy(self): """ Test the basic functionality of the put_bitstream_copy method. """ bitstream1 = BitStream() bitstream1.put_string("This is bitstream1") bitstream2 = BitStream() bitstream2.put_string("This is bitstream2") bitstream3 = BitStream() # bitstream3 remains empty bitstream4 = BitStream() bitstream4.put_string("This is bitstream4") # copy the full contents of bitstream2 to the end of bitstream1 bitstream2.seek(0) bitstream1.put_bitstream_copy(bitstream2) self.assertEquals(bitstream2.get_current_pos(), bitstream2.get_length()) # check the contents of bitstream1 bitstream1.seek(0) self.assertEquals(bitstream1.get_string(bitstream1.get_length()), "This is bitstream1This is bitstream2") # copy the full contents of bitstream3 (aka. nothing) to the end of # bitstream4 bitstream3.seek(0) bitstream4.put_bitstream_copy(bitstream3) # check the contents of bitstream4 bitstream4.seek(0) self.assertEquals(bitstream4.get_string(bitstream4.get_length()), "This is bitstream4") # copy the contents of bitstream4 from the position 8 onwards bitstream4.seek(8*8) bitstream1.put_bitstream_copy(bitstream4) self.assertEquals(bitstream4.get_current_pos(), bitstream4.get_length()) # check the contents of bitstream1 bitstream1.seek(0) self.assertEquals(bitstream1.get_string(bitstream1.get_length()), "This is bitstream1This is bitstream2bitstream4")
def test_string_unicode(self): """ Test basic put_string and get_string support for python unicode objects. """ bitstream = BitStream() unicode_string = u"ÄäÜüßЯБГДЖЙŁĄŻĘĆŃŚŹてすとアイウエオカキクケコサシスセソタチツテ" bitstream.put_string(unicode_string) bitstream.seek(0) # Note: the string is read back as a "normal" UTF-8 string, unicode # type information is not stored in the bitstream. self.assertEquals(bitstream.get_string(bitstream.get_length()), "ÄäÜüßЯБГДЖЙŁĄŻĘĆŃŚŹてすとアイウエオカキクケコサシスセソタチツテ")
def test_base64_basic(self): """ This method tests put_base64's and get_base64's basic functionality. """ bitstream = BitStream() # We use the Base64 Test Vectors defined in RFC4648. # http://www.ietf.org/rfc/rfc4648.txt test_vectors = [("",""), ("f","Zg=="), ("fo","Zm8="), ("foo","Zm9v"), ("foob","Zm9vYg=="), ("fooba","Zm9vYmE="), ("foobar","Zm9vYmFy")] # STEP 1: # For each test vector, we write its value to the bitstream as a string # then read it as base64 data. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val)*8 bitstream.put_string(str_val) bitstream.seek(0) self.assertEquals(bitstream.get_base64(vector_bit_length), base64_val) bitstream.seek(0) # NOTE that we are overwriting multiple times our bitstream, this is # also a feature of BitStream we are testing in this test case. # STEP 2: # For each test vector, we write its value to the bitstream as base64 # data, then read it as string *and* base64 data. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val)*8 bitstream.put_base64(base64_val) bitstream.seek(0) self.assertEquals(bitstream.get_string(vector_bit_length), str_val) bitstream.seek(0) self.assertEquals(bitstream.get_base64(vector_bit_length), base64_val) bitstream.seek(0) # STEP 3: # For each test vector, we write its value to a NEW bitstream as base64 # data, and make sure the length of the stream is the expected one. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val)*8 new_bs = BitStream() new_bs.put_base64(base64_val) self.assertEquals(new_bs.get_length(), vector_bit_length) self.assertEquals(new_bs.get_current_pos(), vector_bit_length)
def test_string_ascii(self): """ Test basic put_string and get_string behavior, using ASCII strings. """ bitstream = BitStream() string1 = "Test string " # 12 chars/bytes string2 = "using only ASCII characters (0-126):\n\t" # 38 chars/bytes string3 = "Hello World!" # 12 chars/bytes # Store our message in 3 writes bitstream.put_string(string1) bitstream.put_string(string2) bitstream.put_string(string3) # Sanity check: self.assertEquals(bitstream.get_length(),(12+38+12)*8) self.assertEquals(bitstream.get_current_pos(),(12+38+12)*8) # Retrieve our message in 2 reads bitstream.seek(0) self.assertEquals(bitstream.get_string(29*8), # read 29 bytes "Test string using only ASCII ") self.assertEquals(bitstream.get_string(33*8), # read 33 bytes "characters (0-126):\n\tHello World!")
def test_base64_basic(self): """ This method tests put_base64's and get_base64's basic functionality. """ bitstream = BitStream() # We use the Base64 Test Vectors defined in RFC4648. # http://www.ietf.org/rfc/rfc4648.txt test_vectors = [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] # STEP 1: # For each test vector, we write its value to the bitstream as a string # then read it as base64 data. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val) * 8 bitstream.put_string(str_val) bitstream.seek(0) self.assertEquals(bitstream.get_base64(vector_bit_length), base64_val) bitstream.seek(0) # NOTE that we are overwriting multiple times our bitstream, this is # also a feature of BitStream we are testing in this test case. # STEP 2: # For each test vector, we write its value to the bitstream as base64 # data, then read it as string *and* base64 data. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val) * 8 bitstream.put_base64(base64_val) bitstream.seek(0) self.assertEquals(bitstream.get_string(vector_bit_length), str_val) bitstream.seek(0) self.assertEquals(bitstream.get_base64(vector_bit_length), base64_val) bitstream.seek(0) # STEP 3: # For each test vector, we write its value to a NEW bitstream as base64 # data, and make sure the length of the stream is the expected one. for (str_val, base64_val) in test_vectors: vector_bit_length = len(str_val) * 8 new_bs = BitStream() new_bs.put_base64(base64_val) self.assertEquals(new_bs.get_length(), vector_bit_length) self.assertEquals(new_bs.get_current_pos(), vector_bit_length)
def test_string_utf8(self): """ Test basic put_string and get_string support for UTF-8 strings. """ bitstream = BitStream() string1 = "ÄäÜüß" # 5 chars, 10 bytes string2 = "ЯБГДЖЙŁĄŻĘĆŃŚŹ" # 14 chars, 28 bytes string3 = "てすとアイウエオカキクケコサシスセソタチツテ" # 22 chars, 66 bytes # Store our message in 3 writes bitstream.put_string(string1) bitstream.put_string(string2) bitstream.put_string(string3) # Sanity check: self.assertEquals(bitstream.get_length(), (10 + 28 + 66) * 8) self.assertEquals(bitstream.get_current_pos(), (10 + 28 + 66) * 8) # Retrieve the whole message bitstream.seek(0) self.assertEquals(bitstream.get_string(bitstream.get_length()), "ÄäÜüßЯБГДЖЙŁĄŻĘĆŃŚŹてすとアイウエオカキクケコサシスセソタチツテ")
def test_string_utf8(self): """ Test basic put_string and get_string support for UTF-8 strings. """ bitstream = BitStream() string1 = "ÄäÜüß" # 5 chars, 10 bytes string2 = "ЯБГДЖЙŁĄŻĘĆŃŚŹ" # 14 chars, 28 bytes string3 = "てすとアイウエオカキクケコサシスセソタチツテ" # 22 chars, 66 bytes # Store our message in 3 writes bitstream.put_string(string1) bitstream.put_string(string2) bitstream.put_string(string3) # Sanity check: self.assertEquals(bitstream.get_length(),(10+28+66)*8) self.assertEquals(bitstream.get_current_pos(),(10+28+66)*8) # Retrieve the whole message bitstream.seek(0) self.assertEquals(bitstream.get_string(bitstream.get_length()), "ÄäÜüßЯБГДЖЙŁĄŻĘĆŃŚŹてすとアイウエオカキクケコサシスセソタチツテ")