xref: /aosp_15_r20/external/pigweed/pw_rpc/nanopb/serde_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 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 #include "pw_rpc/nanopb/internal/common.h"
16 #include "pw_rpc_test_protos/test.pb.h"
17 #include "pw_unit_test/framework.h"
18 
19 namespace pw::rpc::internal {
20 namespace {
21 
22 constexpr NanopbSerde kTestRequest(pw_rpc_test_TestRequest_fields);
23 constexpr pw_rpc_test_TestRequest kProto{.integer = 3, .status_code = 0};
24 
TEST(NanopbSerde,Encode)25 TEST(NanopbSerde, Encode) {
26   std::byte buffer[32] = {};
27 
28   StatusWithSize result = kTestRequest.Encode(&kProto, buffer);
29   EXPECT_EQ(OkStatus(), result.status());
30   EXPECT_EQ(result.size(), 2u);
31   EXPECT_EQ(buffer[0], std::byte{1} << 3);
32   EXPECT_EQ(buffer[1], std::byte{3});
33 }
34 
TEST(NanopbSerde,Encode_TooSmall)35 TEST(NanopbSerde, Encode_TooSmall) {
36   std::byte buffer[1] = {};
37   EXPECT_EQ(Status::Internal(), kTestRequest.Encode(&kProto, buffer).status());
38 }
39 
TEST(NanopbSerde,EncodedSize)40 TEST(NanopbSerde, EncodedSize) {
41   StatusWithSize result = kTestRequest.EncodedSizeBytes(&kProto);
42   EXPECT_EQ(OkStatus(), result.status());
43   EXPECT_EQ(result.size(), 2u);
44 }
45 
TEST(NanopbSerde,Decode)46 TEST(NanopbSerde, Decode) {
47   constexpr std::byte buffer[]{std::byte{1} << 3, std::byte{3}};
48   pw_rpc_test_TestRequest proto = {};
49 
50   EXPECT_EQ(OkStatus(), kTestRequest.Decode(buffer, &proto));
51 
52   EXPECT_EQ(3, proto.integer);
53   EXPECT_EQ(0u, proto.status_code);
54 }
55 
56 }  // namespace
57 }  // namespace pw::rpc::internal
58