def testFormatPackedIPv4Address(self): """Tests the _FormatPackedIPv4Address function.""" test_format = data_format.BinaryDataFormat() ip_address = test_format._FormatPackedIPv4Address( [0xc0, 0xa8, 0xcc, 0x62]) self.assertEqual(ip_address, '192.168.204.98')
def testReadStructureFromByteStream(self): """Tests the _ReadStructureFromByteStream function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(debug=True, output_writer=output_writer) test_format._ReadStructureFromByteStream( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00', 0, self._POINT3D, 'point3d') # Test with missing byte stream. with self.assertRaises(ValueError): test_format._ReadStructureFromByteStream(None, 0, self._POINT3D, 'point3d') # Test with missing data map type. with self.assertRaises(ValueError): test_format._ReadStructureFromByteStream( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00', 0, None, 'point3d') # Test with data type map that raises an dtfabric.MappingError. data_type_map = ErrorDataTypeMap(None) with self.assertRaises(errors.ParseError): test_format._ReadStructureFromByteStream( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00', 0, data_type_map, 'point3d')
def testReadData(self): """Tests the _ReadData function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(debug=True, output_writer=output_writer) file_object = io.BytesIO( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00') test_format._ReadData(file_object, 0, self._POINT3D_SIZE, 'point3d') # Test with missing file-like object. with self.assertRaises(ValueError): test_format._ReadData(None, 0, self._POINT3D_SIZE, 'point3d') # Test with file-like object with insufficient data. file_object = io.BytesIO( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00') with self.assertRaises(errors.ParseError): test_format._ReadData(file_object, 0, self._POINT3D_SIZE, 'point3d') # Test with file-like object that raises an IOError. file_object = ErrorBytesIO( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00') with self.assertRaises(errors.ParseError): test_format._ReadData(file_object, 0, self._POINT3D_SIZE, 'point3d')
def testFormatPackedIPv6Address(self): """Tests the _FormatPackedIPv6Address function.""" test_format = data_format.BinaryDataFormat() ip_address = test_format._FormatPackedIPv6Address([ 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x83, 0x29]) self.assertEqual(ip_address, '2001:0db8:0000:0000:0000:ff00:0042:8329')
def testDebugPrintText(self): """Tests the _DebugPrintText function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(output_writer=output_writer) test_format._DebugPrintText('Text') expected_output = ['Text'] self.assertEqual(output_writer.output, expected_output)
def testDebugPrintValue(self): """Tests the _DebugPrintValue function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(output_writer=output_writer) test_format._DebugPrintValue('Description', 'Value') expected_output = ['Description\t\t\t\t\t\t\t\t: Value\n'] self.assertEqual(output_writer.output, expected_output)
def testReadStructure(self): """Tests the _ReadStructure function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(debug=True, output_writer=output_writer) file_object = io.BytesIO( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00') test_format._ReadStructure(file_object, 0, self._POINT3D_SIZE, self._POINT3D, 'point3d')
def testDebugPrintData(self): """Tests the _DebugPrintData function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(output_writer=output_writer) data = b'\x00\x01\x02\x03\x04\x05\x06' test_format._DebugPrintData('Description', data) expected_output = [ 'Description:\n', ('0x00000000 00 01 02 03 04 05 06 ' '.......\n\n') ] self.assertEqual(output_writer.output, expected_output)
def testFormatDataInHexadecimal(self): """Tests the _FormatDataInHexadecimal function.""" test_format = data_format.BinaryDataFormat() data = b'\x00\x01\x02\x03\x04\x05\x06' expected_formatted_data = ( '0x00000000 00 01 02 03 04 05 06 ' '.......\n' '\n') formatted_data = test_format._FormatDataInHexadecimal(data) self.assertEqual(formatted_data, expected_formatted_data) data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09' expected_formatted_data = ( '0x00000000 00 01 02 03 04 05 06 07 08 09 ' '..........\n' '\n') formatted_data = test_format._FormatDataInHexadecimal(data) self.assertEqual(formatted_data, expected_formatted_data) data = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' expected_formatted_data = ( '0x00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ' '................\n' '\n') formatted_data = test_format._FormatDataInHexadecimal(data) self.assertEqual(formatted_data, expected_formatted_data) data = ( b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' ) expected_formatted_data = ( '0x00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ' '................\n' '...\n' '0x00000020 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ' '................\n' '\n') formatted_data = test_format._FormatDataInHexadecimal(data) self.assertEqual(formatted_data, expected_formatted_data)
def testReadStructureFromFileObject(self): """Tests the _ReadStructureFromFileObject function.""" output_writer = test_lib.TestOutputWriter() test_format = data_format.BinaryDataFormat(debug=True, output_writer=output_writer) file_object = io.BytesIO( b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00') test_format._ReadStructureFromFileObject(file_object, 0, self._POINT3D, "point3d") file_object = io.BytesIO( b'\x03\x00\x00\x00' b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00' b'\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00' b'\x06\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00') test_format._ReadStructureFromFileObject(file_object, 0, self._SHAPE3D, "shape3d")