1 // Copyright 2020 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/service.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_rpc/internal/method.h"
19
20 namespace pw::rpc {
21
22 class ServiceTestHelper {
23 public:
FindMethod(Service & service,uint32_t id)24 static const internal::Method* FindMethod(Service& service, uint32_t id) {
25 return service.FindMethod(id);
26 }
27 };
28
29 namespace {
30
InvokeIt(const internal::CallContext &,const internal::Packet &)31 void InvokeIt(const internal::CallContext&, const internal::Packet&) {}
32
33 class ServiceTestMethod : public internal::Method {
34 public:
ServiceTestMethod(uint32_t id,char the_value)35 constexpr ServiceTestMethod(uint32_t id, char the_value)
36 : internal::Method(id, InvokeIt), value(the_value) {}
37
38 char value; // Add a member so the class is larger than the base Method.
39 };
40
41 class ServiceTestMethodUnion : public internal::MethodUnion {
42 public:
ServiceTestMethodUnion(ServiceTestMethod && method)43 constexpr ServiceTestMethodUnion(ServiceTestMethod&& method)
44 : impl_({.service_test = method}) {}
45
method() const46 constexpr const internal::Method& method() const { return impl_.method; }
47
48 private:
49 union {
50 internal::Method method;
51 ServiceTestMethod service_test;
52 } impl_;
53 };
54
55 class TestService : public Service {
56 public:
TestService()57 constexpr TestService() : Service(0xabcd, kMethods) {}
58
59 static constexpr std::array<ServiceTestMethodUnion, 3> kMethods = {
60 ServiceTestMethod(123, 'a'),
61 ServiceTestMethod(456, 'b'),
62 ServiceTestMethod(789, 'c'),
63 };
64 };
65
TEST(Service,MultipleMethods_FindMethod_Present)66 TEST(Service, MultipleMethods_FindMethod_Present) {
67 TestService service;
68 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 123),
69 &TestService::kMethods[0].method());
70 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 456),
71 &TestService::kMethods[1].method());
72 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 789),
73 &TestService::kMethods[2].method());
74 }
75
TEST(Service,MultipleMethods_FindMethod_NotPresent)76 TEST(Service, MultipleMethods_FindMethod_NotPresent) {
77 TestService service;
78 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 0), nullptr);
79 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 457), nullptr);
80 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 999), nullptr);
81 }
82
83 class EmptyTestService : public Service {
84 public:
EmptyTestService()85 constexpr EmptyTestService() : Service(0xabcd, kMethods) {}
86 static constexpr std::array<ServiceTestMethodUnion, 0> kMethods = {};
87 };
88
TEST(Service,NoMethods_FindMethod_NotPresent)89 TEST(Service, NoMethods_FindMethod_NotPresent) {
90 EmptyTestService service;
91 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 123), nullptr);
92 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 456), nullptr);
93 EXPECT_EQ(ServiceTestHelper::FindMethod(service, 789), nullptr);
94 }
95
96 } // namespace
97 } // namespace pw::rpc
98