• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
Append(std::vector<uint8_t> * data_vector,T data)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 
TestInputParser(const std::string & input,bool expected_result,const std::vector<uint8_t> & expected_data,size_t expected_num_handles)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 = ParseValidationTestInput(input, &data, &num_handles,
48                                          &error_message);
49   if (expected_result) {
50     if (result && error_message.empty() &&
51         expected_data == data && 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 
GetMatchingTests(const std::vector<std::string> & names,const std::string & prefix)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 
ReadFile(const std::string & path,std::string * result)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 
ReadAndParseDataFile(const std::string & path,std::vector<uint8_t> * data,size_t * num_handles)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 
ReadResultFile(const std::string & path,std::string * result)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 
GetPath(const std::string & root,const std::string & suffix)134 std::string GetPath(const std::string& root, const std::string& suffix) {
135   return "mojo/public/interfaces/bindings/tests/data/validation/" +
136       root + suffix;
137 }
138 
139 // |message| should be a newly created object.
ReadTestCase(const std::string & test,Message * message,std::string * expected)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 
RunValidationTests(const std::string & prefix,MessageReceiver * test_message_receiver)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     bool unused MOJO_ALLOW_UNUSED = 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:
Accept(Message * message)183   virtual bool Accept(Message* message) MOJO_OVERRIDE {
184     return true;  // Any message is OK.
185   }
186 };
187 
188 class ValidationIntegrationTest : public testing::Test {
189  public:
ValidationIntegrationTest()190   ValidationIntegrationTest() : test_message_receiver_(NULL) {
191   }
192 
~ValidationIntegrationTest()193   virtual ~ValidationIntegrationTest() {
194   }
195 
SetUp()196   virtual void SetUp() MOJO_OVERRIDE {
197     ScopedMessagePipeHandle tester_endpoint;
198     ASSERT_EQ(MOJO_RESULT_OK,
199               CreateMessagePipe(NULL, &tester_endpoint, &testee_endpoint_));
200     test_message_receiver_ =
201         new TestMessageReceiver(this, tester_endpoint.Pass());
202   }
203 
TearDown()204   virtual void TearDown() MOJO_OVERRIDE {
205     delete test_message_receiver_;
206     test_message_receiver_ = NULL;
207 
208     // Make sure that the other end receives the OnConnectionError()
209     // notification.
210     PumpMessages();
211   }
212 
test_message_receiver()213   MessageReceiver* test_message_receiver() {
214     return test_message_receiver_;
215   }
216 
testee_endpoint()217   ScopedMessagePipeHandle testee_endpoint() {
218     return testee_endpoint_.Pass();
219   }
220 
221  private:
222   class TestMessageReceiver : public MessageReceiver {
223    public:
TestMessageReceiver(ValidationIntegrationTest * owner,ScopedMessagePipeHandle handle)224     TestMessageReceiver(ValidationIntegrationTest* owner,
225                         ScopedMessagePipeHandle handle)
226         : owner_(owner),
227           connector_(handle.Pass()) {
228     }
~TestMessageReceiver()229     virtual ~TestMessageReceiver() {
230     }
231 
Accept(Message * message)232     virtual bool Accept(Message* message) MOJO_OVERRIDE {
233       bool rv = connector_.Accept(message);
234       owner_->PumpMessages();
235       return rv;
236     }
237 
238    public:
239     ValidationIntegrationTest* owner_;
240     mojo::internal::Connector connector_;
241   };
242 
PumpMessages()243   void PumpMessages() {
244     loop_.RunUntilIdle();
245   }
246 
247   Environment env_;
248   RunLoop loop_;
249   TestMessageReceiver* test_message_receiver_;
250   ScopedMessagePipeHandle testee_endpoint_;
251 };
252 
253 class IntegrationTestInterface1Client : public IntegrationTestInterface1 {
254  public:
~IntegrationTestInterface1Client()255   virtual ~IntegrationTestInterface1Client() {
256   }
257 
Method0(BasicStructPtr param0)258   virtual void Method0(BasicStructPtr param0) MOJO_OVERRIDE {
259   }
260 };
261 
262 class IntegrationTestInterface1Impl
263     : public InterfaceImpl<IntegrationTestInterface1> {
264  public:
~IntegrationTestInterface1Impl()265   virtual ~IntegrationTestInterface1Impl() {
266   }
267 
Method0(BasicStructPtr param0)268   virtual void Method0(BasicStructPtr param0) MOJO_OVERRIDE {
269   }
270 
OnConnectionError()271   virtual void OnConnectionError() MOJO_OVERRIDE {
272     delete this;
273   }
274 };
275 
TEST(ValidationTest,InputParser)276 TEST(ValidationTest, InputParser) {
277   {
278     // The parser, as well as Append() defined above, assumes that this code is
279     // running on a little-endian platform. Test whether that is true.
280     uint16_t x = 1;
281     ASSERT_EQ(1, *(reinterpret_cast<char*>(&x)));
282   }
283   {
284     // Test empty input.
285     std::string input;
286     std::vector<uint8_t> expected;
287 
288     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
289   }
290   {
291     // Test input that only consists of comments and whitespaces.
292     std::string input = "    \t  // hello world \n\r \t// the answer is 42   ";
293     std::vector<uint8_t> expected;
294 
295     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
296   }
297   {
298     std::string input = "[u1]0x10// hello world !! \n\r  \t [u2]65535 \n"
299                         "[u4]65536 [u8]0xFFFFFFFFFFFFFFFF 0 0Xff";
300     std::vector<uint8_t> expected;
301     Append(&expected, static_cast<uint8_t>(0x10));
302     Append(&expected, static_cast<uint16_t>(65535));
303     Append(&expected, static_cast<uint32_t>(65536));
304     Append(&expected, static_cast<uint64_t>(0xffffffffffffffff));
305     Append(&expected, static_cast<uint8_t>(0));
306     Append(&expected, static_cast<uint8_t>(0xff));
307 
308     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
309   }
310   {
311     std::string input = "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40";
312     std::vector<uint8_t> expected;
313     Append(&expected, -static_cast<int64_t>(0x800));
314     Append(&expected, static_cast<int8_t>(-128));
315     Append(&expected, static_cast<int16_t>(0));
316     Append(&expected, static_cast<int32_t>(-40));
317 
318     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
319   }
320   {
321     std::string input = "[b]00001011 [b]10000000  // hello world\r [b]00000000";
322     std::vector<uint8_t> expected;
323     Append(&expected, static_cast<uint8_t>(11));
324     Append(&expected, static_cast<uint8_t>(128));
325     Append(&expected, static_cast<uint8_t>(0));
326 
327     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
328   }
329   {
330     std::string input = "[f]+.3e9 [d]-10.03";
331     std::vector<uint8_t> expected;
332     Append(&expected, +.3e9f);
333     Append(&expected, -10.03);
334 
335     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
336   }
337   {
338     std::string input = "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar";
339     std::vector<uint8_t> expected;
340     Append(&expected, static_cast<uint32_t>(14));
341     Append(&expected, static_cast<uint8_t>(0));
342     Append(&expected, static_cast<uint64_t>(9));
343     Append(&expected, static_cast<uint8_t>(0));
344 
345     EXPECT_TRUE(TestInputParser(input, true, expected, 0));
346   }
347   {
348     std::string input = "// This message has handles! \n[handles]50 [u8]2";
349     std::vector<uint8_t> expected;
350     Append(&expected, static_cast<uint64_t>(2));
351 
352     EXPECT_TRUE(TestInputParser(input, true, expected, 50));
353   }
354 
355   // Test some failure cases.
356   {
357     const char* error_inputs[] = {
358       "/ hello world",
359       "[u1]x",
360       "[u2]-1000",
361       "[u1]0x100",
362       "[s2]-0x8001",
363       "[b]1",
364       "[b]1111111k",
365       "[dist4]unmatched",
366       "[anchr]hello [dist8]hello",
367       "[dist4]a [dist4]a [anchr]a",
368       "[dist4]a [anchr]a [dist4]a [anchr]a",
369       "0 [handles]50",
370       NULL
371     };
372 
373     for (size_t i = 0; error_inputs[i]; ++i) {
374       std::vector<uint8_t> expected;
375       if (!TestInputParser(error_inputs[i], false, expected, 0))
376         ADD_FAILURE() << "Unexpected test result for: " << error_inputs[i];
377     }
378   }
379 }
380 
TEST(ValidationTest,Conformance)381 TEST(ValidationTest, Conformance) {
382   DummyMessageReceiver dummy_receiver;
383   mojo::internal::FilterChain validators(&dummy_receiver);
384   validators.Append<mojo::internal::MessageHeaderValidator>();
385   validators.Append<ConformanceTestInterface::RequestValidator_>();
386 
387   RunValidationTests("conformance_", validators.GetHead());
388 }
389 
TEST_F(ValidationIntegrationTest,InterfacePtr)390 TEST_F(ValidationIntegrationTest, InterfacePtr) {
391   // Test that InterfacePtr<X> applies the correct validators and they don't
392   // conflict with each other:
393   //   - MessageHeaderValidator
394   //   - X::Client::RequestValidator_
395   //   - X::ResponseValidator_
396 
397   IntegrationTestInterface1Client interface1_client;
398   IntegrationTestInterface2Ptr interface2_ptr =
399       MakeProxy<IntegrationTestInterface2>(testee_endpoint().Pass());
400   interface2_ptr.set_client(&interface1_client);
401   interface2_ptr.internal_state()->router()->EnableTestingMode();
402 
403   RunValidationTests("integration_", test_message_receiver());
404 }
405 
TEST_F(ValidationIntegrationTest,InterfaceImpl)406 TEST_F(ValidationIntegrationTest, InterfaceImpl) {
407   // Test that InterfaceImpl<X> applies the correct validators and they don't
408   // conflict with each other:
409   //   - MessageHeaderValidator
410   //   - X::RequestValidator_
411   //   - X::Client::ResponseValidator_
412 
413   // |interface1_impl| will delete itself when the pipe is closed.
414   IntegrationTestInterface1Impl* interface1_impl =
415       BindToPipe(new IntegrationTestInterface1Impl(), testee_endpoint().Pass());
416   interface1_impl->internal_state()->router()->EnableTestingMode();
417 
418   RunValidationTests("integration_", test_message_receiver());
419 }
420 
421 }  // namespace
422 }  // namespace test
423 }  // namespace mojo
424