Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Tests for message_util.""" |
| 6 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 7 | import os |
| 8 | |
| 9 | from chromite.api import message_util |
| 10 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
| 11 | from chromite.lib import cros_test_lib |
| 12 | |
| 13 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 14 | class JsonSerializerTest(cros_test_lib.TestCase): |
| 15 | """Tests for the JSON serializer.""" |
| 16 | |
| 17 | def test_serialization(self): |
| 18 | """Test json format serialization/deserialization.""" |
| 19 | serializer = message_util.JsonSerializer() |
| 20 | |
| 21 | # Build a message. |
| 22 | msg = build_api_test_pb2.TestRequestMessage() |
| 23 | msg.id = 'foo' |
| 24 | |
| 25 | # Round trip the message contents through the serializer. |
| 26 | serialized = serializer.serialize(msg) |
| 27 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 28 | serializer.deserialize(serialized, deserialized) |
| 29 | |
| 30 | # Create an identical message manually. |
| 31 | source = '{"id":"foo"}' |
| 32 | deserialized2 = build_api_test_pb2.TestRequestMessage() |
| 33 | serializer.deserialize(source, deserialized2) |
| 34 | |
| 35 | # Make sure the round tripped data is equal, and matches the manually |
| 36 | # constructed version. |
| 37 | self.assertEqual(deserialized, msg) |
| 38 | self.assertEqual(deserialized, deserialized2) |
| 39 | |
| 40 | |
| 41 | class BinarySerializerTest(cros_test_lib.TestCase): |
| 42 | """Tests for the binary serializer.""" |
| 43 | |
| 44 | def test_serialization(self): |
| 45 | """Test binary format serialization/deserialization.""" |
| 46 | serializer = message_util.BinarySerializer() |
| 47 | |
| 48 | # Build a message. |
| 49 | msg = build_api_test_pb2.TestRequestMessage() |
| 50 | msg.id = 'foo' |
| 51 | |
| 52 | # Round trip the message contents through the serializer. |
| 53 | serialized = serializer.serialize(msg) |
| 54 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 55 | serializer.deserialize(serialized, deserialized) |
| 56 | |
| 57 | # Make sure the round tripped data is unchanged. |
| 58 | self.assertEqual(msg, deserialized) |
| 59 | |
| 60 | |
| 61 | class MessageHandlerTest(cros_test_lib.TempDirTestCase): |
| 62 | """MessageHandler tests.""" |
| 63 | |
| 64 | def test_binary_serialization(self): |
| 65 | """Test binary serialization/deserialization.""" |
| 66 | msg_path = os.path.join(self.tempdir, 'proto') |
| 67 | |
| 68 | # Use the message handler configured in the module to avoid config drift. |
| 69 | handler = message_util.get_message_handler(msg_path, |
| 70 | message_util.FORMAT_BINARY) |
| 71 | |
| 72 | msg = build_api_test_pb2.TestRequestMessage() |
| 73 | msg.id = 'foo' |
| 74 | |
| 75 | # Round trip the data. |
| 76 | self.assertNotExists(msg_path) |
| 77 | handler.write_from(msg) |
| 78 | self.assertExists(msg_path) |
| 79 | |
| 80 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 81 | handler.read_into(deserialized) |
| 82 | |
| 83 | # Make sure the data has not mutated. |
| 84 | self.assertEqual(msg, deserialized) |
| 85 | |
| 86 | def test_json_serialization(self): |
| 87 | """Test json serialization/deserialization.""" |
| 88 | msg_path = os.path.join(self.tempdir, 'proto') |
| 89 | |
| 90 | # Use the message handler configured in the module to avoid config drift. |
| 91 | handler = message_util.get_message_handler(msg_path, |
| 92 | message_util.FORMAT_JSON) |
| 93 | |
| 94 | msg = build_api_test_pb2.TestRequestMessage() |
| 95 | msg.id = 'foo' |
| 96 | |
| 97 | # Round trip the data. |
| 98 | self.assertNotExists(msg_path) |
| 99 | handler.write_from(msg) |
| 100 | self.assertExists(msg_path) |
| 101 | |
| 102 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 103 | handler.read_into(deserialized) |
| 104 | |
| 105 | # Make sure the data has not mutated. |
| 106 | self.assertEqual(msg, deserialized) |