1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <Hello.client.h>
18 #include <MockHello.client.h>
19
20 #include <google/protobuf/util/message_differencer.h>
21
22 #include <nos/MockNuggetClient.h>
23
24 #include <gtest/gtest.h>
25
26 using ::google::protobuf::util::MessageDifferencer;
27
28 using ::nos::MockNuggetClient;
29 using ::nos::generator::test::EmptyRequest;
30 using ::nos::generator::test::EmptyResponse;
31 using ::nos::generator::test::GreetRequest;
32 using ::nos::generator::test::GreetResponse;
33 using ::nos::generator::test::Hello;
34 using ::nos::generator::test::IHello;
35 using ::nos::generator::test::MockHello;
36
37 using ::testing::_;
38 using ::testing::DoAll;
39 using ::testing::Eq;
40 using ::testing::Return;
41 using ::testing::SetArgPointee;
42
43 MATCHER_P(ProtoMessageEq, msg, "") { return MessageDifferencer::Equals(arg, msg); }
44
45 // Check the message is the same rather than the encoded bytes as different
46 // bytes could decode to the same message.
47 MATCHER_P(DecodesToProtoMessage, msg, "Vector does not decode to correct message") {
48 typename std::remove_const<decltype(msg)>::type decoded;
49 return decoded.ParseFromArray(arg.data(), arg.size())
50 && MessageDifferencer::Equals(decoded, msg);
51 }
52
53 // The method's ID is based on the order of declaration in the service.
TEST(GeneratedServiceClientTest,MethodsHaveCorrectIds)54 TEST(GeneratedServiceClientTest, MethodsHaveCorrectIds) {
55 MockNuggetClient client;
56 Hello service{client};
57
58 EXPECT_CALL(client, CallApp(APP_ID_TEST, 2, _, _)).WillOnce(Return(APP_SUCCESS));
59 EXPECT_CALL(client, CallApp(APP_ID_TEST, 0, _, _)).WillOnce(Return(APP_SUCCESS));
60 EXPECT_CALL(client, CallApp(APP_ID_TEST, 1, _, _)).WillOnce(Return(APP_SUCCESS));
61
62 EmptyRequest request;
63 EmptyResponse response;
64
65 service.Third(request, &response);
66 service.First(request, &response);
67 service.Second(request, &response);
68 }
69
70 // Both request and response messages are exchanged successfully.
TEST(GeneratedServiceClientTest,DataSuccessfullyExchanged)71 TEST(GeneratedServiceClientTest, DataSuccessfullyExchanged) {
72 MockNuggetClient client;
73 Hello service{client};
74
75 GreetRequest request;
76 request.set_who("Tester");
77 request.set_age(78);
78
79 GreetResponse response;
80 response.set_greeting("Hello, Tester age 78");
81
82 std::vector<uint8_t> responseBytes(response.ByteSize());
83 ASSERT_TRUE(response.SerializeToArray(responseBytes.data(), responseBytes.size()));
84
85 EXPECT_CALL(client, CallApp(_, _, DecodesToProtoMessage(request), _))
86 .WillOnce(DoAll(SetArgPointee<3>(responseBytes), Return(APP_SUCCESS)));
87
88 GreetResponse real_response;
89 EXPECT_THAT(service.Greet(request, &real_response), Eq(APP_SUCCESS));
90 EXPECT_THAT(real_response, ProtoMessageEq(response));
91 }
92
93 // The response can be ignored but the request is still passed on.
TEST(GeneratedServiceClientTest,NullptrResponseBufferIgnoresResponseData)94 TEST(GeneratedServiceClientTest, NullptrResponseBufferIgnoresResponseData) {
95 MockNuggetClient client;
96 Hello service{client};
97
98 GreetRequest request;
99 request.set_who("Silence");
100 request.set_age(10);
101
102 EXPECT_CALL(client, CallApp(_, _, DecodesToProtoMessage(request), Eq(nullptr)))
103 .WillOnce(Return(APP_SUCCESS));
104
105 EXPECT_THAT(service.Greet(request, nullptr), Eq(APP_SUCCESS));
106 }
107
108 // Errors from the chip should be forwarded to the caller without decoding the
109 // response
TEST(GeneratedServiceClientTest,AppErrorsPropagatedWithoutResponseDecode)110 TEST(GeneratedServiceClientTest, AppErrorsPropagatedWithoutResponseDecode) {
111 MockNuggetClient client;
112 Hello service{client};
113
114 GreetResponse response;
115 response.set_greeting("Ignore me");
116
117 std::vector<uint8_t> responseBytes(response.ByteSize());
118 ASSERT_TRUE(response.SerializeToArray(responseBytes.data(), responseBytes.size()));
119
120 EXPECT_CALL(client, CallApp(_, _, _, _))
121 .WillOnce(DoAll(SetArgPointee<3>(responseBytes), Return(APP_ERROR_BOGUS_ARGS)));
122
123 GreetRequest request;
124 GreetResponse real_response;
125 EXPECT_THAT(service.Greet(request, &real_response), Eq(APP_ERROR_BOGUS_ARGS));
126 EXPECT_THAT(real_response, ProtoMessageEq(GreetResponse{}));
127 }
128
129 // Invalid encoding of a response message results in an RPC error
TEST(GeneratedServiceClientTest,GarbledResponseIsRpcFailure)130 TEST(GeneratedServiceClientTest, GarbledResponseIsRpcFailure) {
131 MockNuggetClient client;
132 Hello service{client};
133
134 std::vector<uint8_t> garbledResponse{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
135 EXPECT_CALL(client, CallApp(_, _, _, _))
136 .WillOnce(DoAll(SetArgPointee<3>(garbledResponse), Return(APP_SUCCESS)));
137
138 GreetRequest request;
139 GreetResponse response;
140 EXPECT_THAT(service.Greet(request, &response), Eq(APP_ERROR_RPC));
141 }
142
143 // Sending too much data will fail before beginning a transaction with the chip.
TEST(GeneratedServiceClientTest,RequestLargerThanBuffer)144 TEST(GeneratedServiceClientTest, RequestLargerThanBuffer) {
145 MockNuggetClient client;
146 Hello service{client};
147
148 EXPECT_CALL(client, CallApp(_, _, _, _)).Times(0);
149
150 GreetRequest request;
151 request.set_who("This is far too long for the buffer so should fail");
152
153 GreetResponse response;
154 EXPECT_THAT(service.Greet(request, &response), Eq(APP_ERROR_TOO_MUCH));
155 }
156
157 // Example using generate service mocks.
TEST(GeneratedServiceClientTest,CanUseGeneratedMocks)158 TEST(GeneratedServiceClientTest, CanUseGeneratedMocks) {
159 MockHello mockService;
160
161 GreetResponse response;
162 constexpr auto mockGreeting = "I made this up";
163 response.set_greeting(mockGreeting);
164
165 EXPECT_CALL(mockService, Greet(_, _))
166 .WillOnce(DoAll(SetArgPointee<1>(response), Return(APP_SUCCESS)));
167
168 // Use as an instance of the service to mimic a real test
169 IHello& service = mockService;
170 GreetRequest request;
171 GreetResponse real_response;
172 EXPECT_THAT(service.Greet(request, &real_response), Eq(APP_SUCCESS));
173 EXPECT_THAT(real_response, ProtoMessageEq(response));
174 }
175