OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stdio.h> | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "mojo/public/c/system/macros.h" | |
12 #include "mojo/public/cpp/bindings/interface_impl.h" | |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
14 #include "mojo/public/cpp/bindings/lib/connector.h" | |
15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
17 #include "mojo/public/cpp/bindings/lib/router.h" | |
18 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
19 #include "mojo/public/cpp/bindings/message.h" | |
20 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" | |
21 #include "mojo/public/cpp/environment/environment.h" | |
22 #include "mojo/public/cpp/system/core.h" | |
23 #include "mojo/public/cpp/test_support/test_support.h" | |
24 #include "mojo/public/cpp/utility/run_loop.h" | |
25 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom
.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 | |
28 namespace mojo { | |
29 namespace test { | |
30 namespace { | |
31 | |
32 template <typename T> | |
33 void Append(std::vector<uint8_t>* data_vector, T data) { | |
34 size_t pos = data_vector->size(); | |
35 data_vector->resize(pos + sizeof(T)); | |
36 memcpy(&(*data_vector)[pos], &data, sizeof(T)); | |
37 } | |
38 | |
39 bool TestInputParser(const std::string& input, | |
40 bool expected_result, | |
41 const std::vector<uint8_t>& expected_data, | |
42 size_t expected_num_handles) { | |
43 std::vector<uint8_t> data; | |
44 size_t num_handles; | |
45 std::string error_message; | |
46 | |
47 bool result = | |
48 ParseValidationTestInput(input, &data, &num_handles, &error_message); | |
49 if (expected_result) { | |
50 if (result && error_message.empty() && expected_data == data && | |
51 expected_num_handles == num_handles) { | |
52 return true; | |
53 } | |
54 | |
55 // Compare with an empty string instead of checking |error_message.empty()|, | |
56 // so that the message will be printed out if the two are not equal. | |
57 EXPECT_EQ(std::string(), error_message); | |
58 EXPECT_EQ(expected_data, data); | |
59 EXPECT_EQ(expected_num_handles, num_handles); | |
60 return false; | |
61 } | |
62 | |
63 EXPECT_FALSE(error_message.empty()); | |
64 return !result && !error_message.empty(); | |
65 } | |
66 | |
67 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names, | |
68 const std::string& prefix) { | |
69 const std::string suffix = ".data"; | |
70 std::vector<std::string> tests; | |
71 for (size_t i = 0; i < names.size(); ++i) { | |
72 if (names[i].size() >= suffix.size() && | |
73 names[i].substr(0, prefix.size()) == prefix && | |
74 names[i].substr(names[i].size() - suffix.size()) == suffix) | |
75 tests.push_back(names[i].substr(0, names[i].size() - suffix.size())); | |
76 } | |
77 return tests; | |
78 } | |
79 | |
80 bool ReadFile(const std::string& path, std::string* result) { | |
81 FILE* fp = OpenSourceRootRelativeFile(path.c_str()); | |
82 if (!fp) { | |
83 ADD_FAILURE() << "File not found: " << path; | |
84 return false; | |
85 } | |
86 fseek(fp, 0, SEEK_END); | |
87 size_t size = static_cast<size_t>(ftell(fp)); | |
88 if (size == 0) { | |
89 result->clear(); | |
90 fclose(fp); | |
91 return true; | |
92 } | |
93 fseek(fp, 0, SEEK_SET); | |
94 result->resize(size); | |
95 size_t size_read = fread(&result->at(0), 1, size, fp); | |
96 fclose(fp); | |
97 return size == size_read; | |
98 } | |
99 | |
100 bool ReadAndParseDataFile(const std::string& path, | |
101 std::vector<uint8_t>* data, | |
102 size_t* num_handles) { | |
103 std::string input; | |
104 if (!ReadFile(path, &input)) | |
105 return false; | |
106 | |
107 std::string error_message; | |
108 if (!ParseValidationTestInput(input, data, num_handles, &error_message)) { | |
109 ADD_FAILURE() << error_message; | |
110 return false; | |
111 } | |
112 | |
113 return true; | |
114 } | |
115 | |
116 bool ReadResultFile(const std::string& path, std::string* result) { | |
117 if (!ReadFile(path, result)) | |
118 return false; | |
119 | |
120 // Result files are new-line delimited text files. Remove any CRs. | |
121 result->erase(std::remove(result->begin(), result->end(), '\r'), | |
122 result->end()); | |
123 | |
124 // Remove trailing LFs. | |
125 size_t pos = result->find_last_not_of('\n'); | |
126 if (pos == std::string::npos) | |
127 result->clear(); | |
128 else | |
129 result->resize(pos + 1); | |
130 | |
131 return true; | |
132 } | |
133 | |
134 std::string GetPath(const std::string& root, const std::string& suffix) { | |
135 return "mojo/public/interfaces/bindings/tests/data/validation/" + root + | |
136 suffix; | |
137 } | |
138 | |
139 // |message| should be a newly created object. | |
140 bool ReadTestCase(const std::string& test, | |
141 Message* message, | |
142 std::string* expected) { | |
143 std::vector<uint8_t> data; | |
144 size_t num_handles; | |
145 if (!ReadAndParseDataFile(GetPath(test, ".data"), &data, &num_handles) || | |
146 !ReadResultFile(GetPath(test, ".expected"), expected)) { | |
147 return false; | |
148 } | |
149 | |
150 message->AllocUninitializedData(static_cast<uint32_t>(data.size())); | |
151 if (!data.empty()) | |
152 memcpy(message->mutable_data(), &data[0], data.size()); | |
153 message->mutable_handles()->resize(num_handles); | |
154 | |
155 return true; | |
156 } | |
157 | |
158 void RunValidationTests(const std::string& prefix, | |
159 MessageReceiver* test_message_receiver) { | |
160 std::vector<std::string> names = | |
161 EnumerateSourceRootRelativeDirectory(GetPath("", "")); | |
162 std::vector<std::string> tests = GetMatchingTests(names, prefix); | |
163 | |
164 for (size_t i = 0; i < tests.size(); ++i) { | |
165 Message message; | |
166 std::string expected; | |
167 ASSERT_TRUE(ReadTestCase(tests[i], &message, &expected)); | |
168 | |
169 std::string result; | |
170 mojo::internal::ValidationErrorObserverForTesting observer; | |
171 mojo_ignore_result(test_message_receiver->Accept(&message)); | |
172 if (observer.last_error() == mojo::internal::VALIDATION_ERROR_NONE) | |
173 result = "PASS"; | |
174 else | |
175 result = mojo::internal::ValidationErrorToString(observer.last_error()); | |
176 | |
177 EXPECT_EQ(expected, result) << "failed test: " << tests[i]; | |
178 } | |
179 } | |
180 | |
181 class DummyMessageReceiver : public MessageReceiver { | |
182 public: | |
183 bool Accept(Message* message) override { | |
184 return true; // Any message is OK. | |
185 } | |
186 }; | |
187 | |
188 class ValidationTest : public testing::Test { | |
189 public: | |
190 ~ValidationTest() override {} | |
191 | |
192 private: | |
193 Environment env_; | |
194 }; | |
195 | |
196 class ValidationIntegrationTest : public ValidationTest { | |
197 public: | |
198 ValidationIntegrationTest() : test_message_receiver_(nullptr) {} | |
199 | |
200 ~ValidationIntegrationTest() override {} | |
201 | |
202 void SetUp() override { | |
203 ScopedMessagePipeHandle tester_endpoint; | |
204 ASSERT_EQ(MOJO_RESULT_OK, | |
205 CreateMessagePipe(nullptr, &tester_endpoint, &testee_endpoint_)); | |
206 test_message_receiver_ = | |
207 new TestMessageReceiver(this, tester_endpoint.Pass()); | |
208 } | |
209 | |
210 void TearDown() override { | |
211 delete test_message_receiver_; | |
212 test_message_receiver_ = nullptr; | |
213 | |
214 // Make sure that the other end receives the OnConnectionError() | |
215 // notification. | |
216 PumpMessages(); | |
217 } | |
218 | |
219 MessageReceiver* test_message_receiver() { return test_message_receiver_; } | |
220 | |
221 ScopedMessagePipeHandle testee_endpoint() { return testee_endpoint_.Pass(); } | |
222 | |
223 private: | |
224 class TestMessageReceiver : public MessageReceiver { | |
225 public: | |
226 TestMessageReceiver(ValidationIntegrationTest* owner, | |
227 ScopedMessagePipeHandle handle) | |
228 : owner_(owner), connector_(handle.Pass()) {} | |
229 ~TestMessageReceiver() override {} | |
230 | |
231 bool Accept(Message* message) override { | |
232 bool rv = connector_.Accept(message); | |
233 owner_->PumpMessages(); | |
234 return rv; | |
235 } | |
236 | |
237 public: | |
238 ValidationIntegrationTest* owner_; | |
239 mojo::internal::Connector connector_; | |
240 }; | |
241 | |
242 void PumpMessages() { loop_.RunUntilIdle(); } | |
243 | |
244 RunLoop loop_; | |
245 TestMessageReceiver* test_message_receiver_; | |
246 ScopedMessagePipeHandle testee_endpoint_; | |
247 }; | |
248 | |
249 class IntegrationTestInterface1Client : public IntegrationTestInterface1 { | |
250 public: | |
251 ~IntegrationTestInterface1Client() override {} | |
252 | |
253 void Method0(BasicStructPtr param0) override {} | |
254 }; | |
255 | |
256 class IntegrationTestInterface1Impl | |
257 : public InterfaceImpl<IntegrationTestInterface1> { | |
258 public: | |
259 ~IntegrationTestInterface1Impl() override {} | |
260 | |
261 void Method0(BasicStructPtr param0) override {} | |
262 }; | |
263 | |
264 TEST_F(ValidationTest, InputParser) { | |
265 { | |
266 // The parser, as well as Append() defined above, assumes that this code is | |
267 // running on a little-endian platform. Test whether that is true. | |
268 uint16_t x = 1; | |
269 ASSERT_EQ(1, *(reinterpret_cast<char*>(&x))); | |
270 } | |
271 { | |
272 // Test empty input. | |
273 std::string input; | |
274 std::vector<uint8_t> expected; | |
275 | |
276 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
277 } | |
278 { | |
279 // Test input that only consists of comments and whitespaces. | |
280 std::string input = " \t // hello world \n\r \t// the answer is 42 "; | |
281 std::vector<uint8_t> expected; | |
282 | |
283 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
284 } | |
285 { | |
286 std::string input = | |
287 "[u1]0x10// hello world !! \n\r \t [u2]65535 \n" | |
288 "[u4]65536 [u8]0xFFFFFFFFFFFFFFFF 0 0Xff"; | |
289 std::vector<uint8_t> expected; | |
290 Append(&expected, static_cast<uint8_t>(0x10)); | |
291 Append(&expected, static_cast<uint16_t>(65535)); | |
292 Append(&expected, static_cast<uint32_t>(65536)); | |
293 Append(&expected, static_cast<uint64_t>(0xffffffffffffffff)); | |
294 Append(&expected, static_cast<uint8_t>(0)); | |
295 Append(&expected, static_cast<uint8_t>(0xff)); | |
296 | |
297 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
298 } | |
299 { | |
300 std::string input = "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40"; | |
301 std::vector<uint8_t> expected; | |
302 Append(&expected, -static_cast<int64_t>(0x800)); | |
303 Append(&expected, static_cast<int8_t>(-128)); | |
304 Append(&expected, static_cast<int16_t>(0)); | |
305 Append(&expected, static_cast<int32_t>(-40)); | |
306 | |
307 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
308 } | |
309 { | |
310 std::string input = "[b]00001011 [b]10000000 // hello world\r [b]00000000"; | |
311 std::vector<uint8_t> expected; | |
312 Append(&expected, static_cast<uint8_t>(11)); | |
313 Append(&expected, static_cast<uint8_t>(128)); | |
314 Append(&expected, static_cast<uint8_t>(0)); | |
315 | |
316 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
317 } | |
318 { | |
319 std::string input = "[f]+.3e9 [d]-10.03"; | |
320 std::vector<uint8_t> expected; | |
321 Append(&expected, +.3e9f); | |
322 Append(&expected, -10.03); | |
323 | |
324 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
325 } | |
326 { | |
327 std::string input = "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar"; | |
328 std::vector<uint8_t> expected; | |
329 Append(&expected, static_cast<uint32_t>(14)); | |
330 Append(&expected, static_cast<uint8_t>(0)); | |
331 Append(&expected, static_cast<uint64_t>(9)); | |
332 Append(&expected, static_cast<uint8_t>(0)); | |
333 | |
334 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
335 } | |
336 { | |
337 std::string input = "// This message has handles! \n[handles]50 [u8]2"; | |
338 std::vector<uint8_t> expected; | |
339 Append(&expected, static_cast<uint64_t>(2)); | |
340 | |
341 EXPECT_TRUE(TestInputParser(input, true, expected, 50)); | |
342 } | |
343 | |
344 // Test some failure cases. | |
345 { | |
346 const char* error_inputs[] = {"/ hello world", | |
347 "[u1]x", | |
348 "[u2]-1000", | |
349 "[u1]0x100", | |
350 "[s2]-0x8001", | |
351 "[b]1", | |
352 "[b]1111111k", | |
353 "[dist4]unmatched", | |
354 "[anchr]hello [dist8]hello", | |
355 "[dist4]a [dist4]a [anchr]a", | |
356 "[dist4]a [anchr]a [dist4]a [anchr]a", | |
357 "0 [handles]50", | |
358 nullptr}; | |
359 | |
360 for (size_t i = 0; error_inputs[i]; ++i) { | |
361 std::vector<uint8_t> expected; | |
362 if (!TestInputParser(error_inputs[i], false, expected, 0)) | |
363 ADD_FAILURE() << "Unexpected test result for: " << error_inputs[i]; | |
364 } | |
365 } | |
366 } | |
367 | |
368 TEST_F(ValidationTest, Conformance) { | |
369 DummyMessageReceiver dummy_receiver; | |
370 mojo::internal::FilterChain validators(&dummy_receiver); | |
371 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
372 validators.Append<ConformanceTestInterface::RequestValidator_>(); | |
373 | |
374 RunValidationTests("conformance_", validators.GetHead()); | |
375 } | |
376 | |
377 TEST_F(ValidationTest, NotImplemented) { | |
378 DummyMessageReceiver dummy_receiver; | |
379 mojo::internal::FilterChain validators(&dummy_receiver); | |
380 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
381 validators.Append<ConformanceTestInterface::RequestValidator_>(); | |
382 | |
383 RunValidationTests("not_implemented_", validators.GetHead()); | |
384 } | |
385 | |
386 TEST_F(ValidationIntegrationTest, InterfacePtr) { | |
387 // Test that InterfacePtr<X> applies the correct validators and they don't | |
388 // conflict with each other: | |
389 // - MessageHeaderValidator | |
390 // - X::Client::RequestValidator_ | |
391 // - X::ResponseValidator_ | |
392 | |
393 IntegrationTestInterface1Client interface1_client; | |
394 IntegrationTestInterface2Ptr interface2_ptr = | |
395 MakeProxy<IntegrationTestInterface2>(testee_endpoint().Pass()); | |
396 interface2_ptr.set_client(&interface1_client); | |
397 interface2_ptr.internal_state()->router_for_testing()->EnableTestingMode(); | |
398 | |
399 RunValidationTests("integration_", test_message_receiver()); | |
400 } | |
401 | |
402 TEST_F(ValidationIntegrationTest, InterfaceImpl) { | |
403 // Test that InterfaceImpl<X> applies the correct validators and they don't | |
404 // conflict with each other: | |
405 // - MessageHeaderValidator | |
406 // - X::RequestValidator_ | |
407 // - X::Client::ResponseValidator_ | |
408 | |
409 // |interface1_impl| will delete itself when the pipe is closed. | |
410 IntegrationTestInterface1Impl* interface1_impl = | |
411 BindToPipe(new IntegrationTestInterface1Impl(), testee_endpoint().Pass()); | |
412 interface1_impl->internal_router()->EnableTestingMode(); | |
413 | |
414 RunValidationTests("integration_", test_message_receiver()); | |
415 } | |
416 | |
417 } // namespace | |
418 } // namespace test | |
419 } // namespace mojo | |
OLD | NEW |