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 #include "pw_router/static_router.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_router/egress_function.h"
19
20 namespace pw::router {
21 namespace {
22
23 struct BasicPacket {
24 static constexpr uint32_t kMagic = 0x8badf00d;
25
BasicPacketpw::router::__anon8714275d0111::BasicPacket26 constexpr BasicPacket(uint32_t addr, uint64_t data)
27 : magic(kMagic), address(addr), payload(data) {}
28
datapw::router::__anon8714275d0111::BasicPacket29 ConstByteSpan data() const { return std::as_bytes(std::span(this, 1)); }
30
31 uint32_t magic;
32 uint32_t address;
33 uint64_t payload;
34 };
35
36 class BasicPacketParser : public PacketParser {
37 public:
BasicPacketParser()38 constexpr BasicPacketParser() : packet_(nullptr) {}
39
Parse(pw::ConstByteSpan packet)40 bool Parse(pw::ConstByteSpan packet) final {
41 packet_ = reinterpret_cast<const BasicPacket*>(packet.data());
42 return packet_->magic == BasicPacket::kMagic;
43 }
44
GetDestinationAddress() const45 std::optional<uint32_t> GetDestinationAddress() const final {
46 PW_DCHECK_NOTNULL(packet_);
47 return packet_->address;
48 }
49
50 private:
51 const BasicPacket* packet_;
52 };
53
__anon8714275d0202(ConstByteSpan) 54 EgressFunction GoodEgress(+[](ConstByteSpan) { return OkStatus(); });
__anon8714275d0302(ConstByteSpan) 55 EgressFunction BadEgress(+[](ConstByteSpan) {
56 return Status::ResourceExhausted();
57 });
58
TEST(StaticRouter,RoutePacket_RoutesToAnEgress)59 TEST(StaticRouter, RoutePacket_RoutesToAnEgress) {
60 BasicPacketParser parser;
61 constexpr StaticRouter::Route routes[] = {{1, GoodEgress}, {2, BadEgress}};
62 StaticRouter router(parser, std::span(routes));
63
64 EXPECT_EQ(router.RoutePacket(BasicPacket(1, 0xdddd).data()), OkStatus());
65 EXPECT_EQ(router.RoutePacket(BasicPacket(2, 0xdddd).data()),
66 Status::Unavailable());
67 }
68
TEST(StaticRouter,RoutePacket_ReturnsParserError)69 TEST(StaticRouter, RoutePacket_ReturnsParserError) {
70 BasicPacketParser parser;
71 constexpr StaticRouter::Route routes[] = {{1, GoodEgress}, {2, BadEgress}};
72 StaticRouter router(parser, std::span(routes));
73
74 BasicPacket bad_magic(1, 0xdddd);
75 bad_magic.magic = 0x1badda7a;
76 EXPECT_EQ(router.RoutePacket(bad_magic.data()), Status::DataLoss());
77 }
78
TEST(StaticRouter,RoutePacket_ReturnsNotFoundOnInvalidRoute)79 TEST(StaticRouter, RoutePacket_ReturnsNotFoundOnInvalidRoute) {
80 BasicPacketParser parser;
81 constexpr StaticRouter::Route routes[] = {{1, GoodEgress}, {2, BadEgress}};
82 StaticRouter router(parser, std::span(routes));
83
84 EXPECT_EQ(router.RoutePacket(BasicPacket(42, 0xdddd).data()),
85 Status::NotFound());
86 }
87
TEST(StaticRouter,RoutePacket_TracksNumberOfDrops)88 TEST(StaticRouter, RoutePacket_TracksNumberOfDrops) {
89 BasicPacketParser parser;
90 constexpr StaticRouter::Route routes[] = {{1, GoodEgress}, {2, BadEgress}};
91 StaticRouter router(parser, std::span(routes));
92
93 // Good
94 EXPECT_EQ(router.RoutePacket(BasicPacket(1, 0xdddd).data()), OkStatus());
95
96 // Egress error
97 EXPECT_EQ(router.RoutePacket(BasicPacket(2, 0xdddd).data()),
98 Status::Unavailable());
99
100 // Parser error
101 BasicPacket bad_magic(1, 0xdddd);
102 bad_magic.magic = 0x1badda7a;
103 EXPECT_EQ(router.RoutePacket(bad_magic.data()), Status::DataLoss());
104
105 // Good
106 EXPECT_EQ(router.RoutePacket(BasicPacket(1, 0xdddd).data()), OkStatus());
107
108 // Bad route
109 EXPECT_EQ(router.RoutePacket(BasicPacket(42, 0xdddd).data()),
110 Status::NotFound());
111
112 EXPECT_EQ(router.dropped_packets(), 3u);
113 }
114
115 } // namespace
116 } // namespace pw::router
117