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