• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2020 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Tests creating pw_rpc client."""
16
17import unittest
18
19from pw_status import Status
20
21from pw_rpc.internal.packet_pb2 import PacketType, RpcPacket
22from pw_rpc import packets
23
24_TEST_REQUEST = RpcPacket(type=PacketType.REQUEST,
25                          channel_id=1,
26                          service_id=2,
27                          method_id=3,
28                          payload=RpcPacket(status=321).SerializeToString())
29
30
31class PacketsTest(unittest.TestCase):
32    """Tests for packet encoding and decoding."""
33    def test_encode_request(self):
34        data = packets.encode_request((1, 2, 3), RpcPacket(status=321))
35        packet = RpcPacket()
36        packet.ParseFromString(data)
37
38        self.assertEqual(_TEST_REQUEST, packet)
39
40    def test_encode_response(self):
41        response = RpcPacket(type=PacketType.RESPONSE,
42                             channel_id=1,
43                             service_id=2,
44                             method_id=3,
45                             payload=RpcPacket(status=321).SerializeToString())
46
47        data = packets.encode_response((1, 2, 3), RpcPacket(status=321))
48        packet = RpcPacket()
49        packet.ParseFromString(data)
50
51        self.assertEqual(response, packet)
52
53    def test_encode_cancel(self):
54        data = packets.encode_cancel((9, 8, 7))
55
56        packet = RpcPacket()
57        packet.ParseFromString(data)
58
59        self.assertEqual(
60            packet,
61            RpcPacket(type=PacketType.CLIENT_ERROR,
62                      channel_id=9,
63                      service_id=8,
64                      method_id=7,
65                      status=Status.CANCELLED.value))
66
67    def test_encode_client_error(self):
68        data = packets.encode_client_error(_TEST_REQUEST, Status.NOT_FOUND)
69
70        packet = RpcPacket()
71        packet.ParseFromString(data)
72
73        self.assertEqual(
74            packet,
75            RpcPacket(type=PacketType.CLIENT_ERROR,
76                      channel_id=1,
77                      service_id=2,
78                      method_id=3,
79                      status=Status.NOT_FOUND.value))
80
81    def test_decode(self):
82        self.assertEqual(_TEST_REQUEST,
83                         packets.decode(_TEST_REQUEST.SerializeToString()))
84
85    def test_for_server(self):
86        self.assertTrue(packets.for_server(_TEST_REQUEST))
87
88        self.assertFalse(
89            packets.for_server(
90                RpcPacket(type=PacketType.RESPONSE,
91                          channel_id=1,
92                          service_id=2,
93                          method_id=3,
94                          payload=RpcPacket(status=321).SerializeToString())))
95
96
97if __name__ == '__main__':
98    unittest.main()
99