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