Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2020 The ChromiumOS Authors |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 15 | """Tests for the JSON serializer.""" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 16 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 17 | def test_serialization(self): |
| 18 | """Test json format serialization/deserialization.""" |
| 19 | serializer = message_util.JsonSerializer() |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 20 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | # Build a message. |
| 22 | msg = build_api_test_pb2.TestRequestMessage() |
| 23 | msg.id = "foo" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | # Create an identical message manually. |
| 31 | source = '{"id":"foo"}' |
| 32 | deserialized2 = build_api_test_pb2.TestRequestMessage() |
| 33 | serializer.deserialize(source, deserialized2) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class BinarySerializerTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | """Tests for the binary serializer.""" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | def test_serialization(self): |
| 45 | """Test binary format serialization/deserialization.""" |
| 46 | serializer = message_util.BinarySerializer() |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | # Build a message. |
| 49 | msg = build_api_test_pb2.TestRequestMessage() |
| 50 | msg.id = "foo" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | # Make sure the round tripped data is unchanged. |
| 58 | self.assertEqual(msg, deserialized) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class MessageHandlerTest(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """MessageHandler tests.""" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def test_binary_serialization(self): |
| 65 | """Test binary serialization/deserialization.""" |
| 66 | msg_path = os.path.join(self.tempdir, "proto") |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | # Use the message handler configured in the module to avoid config drift. |
| 69 | handler = message_util.get_message_handler( |
| 70 | msg_path, message_util.FORMAT_BINARY |
| 71 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | msg = build_api_test_pb2.TestRequestMessage() |
| 74 | msg.id = "foo" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | # Round trip the data. |
| 77 | self.assertNotExists(msg_path) |
| 78 | handler.write_from(msg) |
| 79 | self.assertExists(msg_path) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 82 | handler.read_into(deserialized) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | # Make sure the data has not mutated. |
| 85 | self.assertEqual(msg, deserialized) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | def test_json_serialization(self): |
| 88 | """Test json serialization/deserialization.""" |
| 89 | msg_path = os.path.join(self.tempdir, "proto") |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | # Use the message handler configured in the module to avoid config drift. |
| 92 | handler = message_util.get_message_handler( |
| 93 | msg_path, message_util.FORMAT_JSON |
| 94 | ) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | msg = build_api_test_pb2.TestRequestMessage() |
| 97 | msg.id = "foo" |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | # Round trip the data. |
| 100 | self.assertNotExists(msg_path) |
| 101 | handler.write_from(msg) |
| 102 | self.assertExists(msg_path) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | deserialized = build_api_test_pb2.TestRequestMessage() |
| 105 | handler.read_into(deserialized) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | # Make sure the data has not mutated. |
| 108 | self.assertEqual(msg, deserialized) |