• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15/* eslint-env browser, jasmine */
16import 'jasmine';
17
18import {
19  PacketType,
20  RpcPacket,
21} from 'packet_proto_tspb/packet_proto_tspb_pb/pw_rpc/internal/packet_pb';
22import {Status} from '@pigweed/pw_status';
23
24import * as packets from './packets';
25
26function addTestData(packet: RpcPacket) {
27  const payload = new RpcPacket();
28  payload.setStatus(321);
29  packet.setChannelId(1);
30  packet.setServiceId(2);
31  packet.setMethodId(3);
32  packet.setPayload(payload.serializeBinary());
33}
34
35describe('Packets', () => {
36  beforeEach(() => {});
37
38  it('encodeRequest sets packet fields', () => {
39    const goldenRequest = new RpcPacket();
40    goldenRequest.setType(PacketType.REQUEST);
41    addTestData(goldenRequest);
42
43    const dataPacket = new RpcPacket();
44    dataPacket.setStatus(321);
45    const data = packets.encodeRequest([1, 2, 3], dataPacket);
46    const packet = RpcPacket.deserializeBinary(data);
47
48    expect(packet.toObject()).toEqual(goldenRequest.toObject());
49  });
50
51  it('encodeResponse sets packet fields', () => {
52    const goldenResponse = new RpcPacket();
53    goldenResponse.setType(PacketType.RESPONSE);
54    addTestData(goldenResponse);
55
56    const dataPacket = new RpcPacket();
57    dataPacket.setStatus(321);
58    const data = packets.encodeResponse([1, 2, 3], dataPacket);
59    const packet = RpcPacket.deserializeBinary(data);
60
61    expect(packet.toObject()).toEqual(goldenResponse.toObject());
62  });
63
64  it('encodesClientError sets packet fields', () => {
65    const packet = new RpcPacket();
66    packet.setType(PacketType.REQUEST);
67    addTestData(packet);
68    const data = packets.encodeClientError(packet, Status.NOT_FOUND);
69    const errorPacket = RpcPacket.deserializeBinary(data);
70
71    const golden = new RpcPacket();
72    golden.setType(PacketType.CLIENT_ERROR);
73    golden.setChannelId(1);
74    golden.setServiceId(2);
75    golden.setMethodId(3);
76    golden.setStatus(Status.NOT_FOUND);
77
78    expect(errorPacket.toObject()).toEqual(golden.toObject());
79  });
80
81  it('encodeCancel sets packet fields', () => {
82    const goldenCancel = new RpcPacket();
83    goldenCancel.setType(PacketType.CLIENT_ERROR);
84    goldenCancel.setStatus(Status.CANCELLED);
85    goldenCancel.setChannelId(1);
86    goldenCancel.setServiceId(2);
87    goldenCancel.setMethodId(3);
88
89    const data = packets.encodeCancel([1, 2, 3]);
90    const packet = RpcPacket.deserializeBinary(data);
91    expect(packet.toObject()).toEqual(goldenCancel.toObject());
92  });
93
94  it('decode with serialized request returns request', () => {
95    const request = new RpcPacket();
96    request.setType(PacketType.REQUEST);
97    addTestData(request);
98
99    expect(request.toObject()).toEqual(
100      packets.decode(request.serializeBinary()).toObject()
101    );
102  });
103
104  it('forServer correctly handles RESPONSE and REQUEST types', () => {
105    const request = new RpcPacket();
106    request.setType(PacketType.REQUEST);
107    const response = new RpcPacket();
108    response.setType(PacketType.RESPONSE);
109
110    expect(packets.forServer(request)).toBeTrue();
111    expect(packets.forServer(response)).toBeFalse();
112  });
113});
114