• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_transfer/internal/chunk.h"
16 
17 #include "gtest/gtest.h"
18 #include "pw_bytes/array.h"
19 
20 namespace pw::transfer::internal {
21 namespace {
22 
TEST(Chunk,EncodedSizeMatchesEncode)23 TEST(Chunk, EncodedSizeMatchesEncode) {
24   // Use a START chunk to encode a bunch of legacy fields as well.
25   Chunk chunk(ProtocolVersion::kVersionTwo, Chunk::Type::kStart);
26   chunk.set_session_id(42).set_resource_id(7).set_window_end_offset(128);
27 
28   std::array<std::byte, 64> buffer;
29   EXPECT_LT(chunk.EncodedSize(), buffer.size());
30 
31   auto result = chunk.Encode(buffer);
32   ASSERT_EQ(result.status(), OkStatus());
33   EXPECT_EQ(chunk.EncodedSize(), result->size_bytes());
34 }
35 
TEST(Chunk,EncodedSizeGreaterThanBuffer)36 TEST(Chunk, EncodedSizeGreaterThanBuffer) {
37   Chunk chunk(ProtocolVersion::kVersionTwo, Chunk::Type::kParametersRetransmit);
38   chunk.set_session_id(42).set_resource_id(7).set_window_end_offset(128);
39 
40   std::array<std::byte, 8> buffer;
41   EXPECT_GT(chunk.EncodedSize(), buffer.size());
42 
43   auto result = chunk.Encode(buffer);
44   ASSERT_EQ(result.status(), Status::ResourceExhausted());
45 }
46 
TEST(Chunk,EncodedSizeMatchesBuffer)47 TEST(Chunk, EncodedSizeMatchesBuffer) {
48   // 16 bytes for metadata.
49   Chunk chunk(ProtocolVersion::kVersionTwo, Chunk::Type::kStart);
50   chunk.set_session_id(42).set_resource_id(7).set_window_end_offset(128);
51   EXPECT_EQ(chunk.EncodedSize(), 16u);
52 
53   // 2 bytes for payload key & size, leaving 46 for data.
54   constexpr auto kData = bytes::Initialized<46>(0x11);
55   chunk.set_payload(kData);
56 
57   std::array<std::byte, 64> buffer;
58   EXPECT_EQ(chunk.EncodedSize(), buffer.size());
59 
60   auto result = chunk.Encode(buffer);
61   ASSERT_EQ(result.status(), OkStatus());
62   EXPECT_EQ(chunk.EncodedSize(), result->size_bytes());
63 }
64 
65 }  // namespace
66 }  // namespace pw::transfer::internal
67