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 <sstream> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
12 #include "mojo/public/cpp/test_support/test_support.h" | |
13 #include "mojo/public/interfaces/bindings/tests/validation_interfaces.mojom.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace mojo { | |
17 namespace test { | |
18 namespace { | |
19 | |
20 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names, | |
21 const std::string& prefix) { | |
22 const std::string suffix = ".data"; | |
23 std::vector<std::string> tests; | |
24 for (size_t i = 0; i < names.size(); ++i) { | |
25 size_t root_length = names[i].size() - suffix.size(); | |
26 if (names[i].find(prefix) == 0 && | |
27 names[i].find(suffix) == root_length) | |
28 tests.push_back(names[i].substr(0, root_length)); | |
29 } | |
30 return tests; | |
31 } | |
32 | |
33 bool ReadDataFile(const std::string& path, std::vector<uint8_t>* result) { | |
34 FILE* fp = OpenSourceRootRelativeFile(path.c_str()); | |
35 if (!fp) { | |
36 ADD_FAILURE() << "File not found: " << path; | |
37 return false; | |
38 } | |
39 for (;;) { | |
40 unsigned int value; | |
41 int rv = fscanf(fp, "%x", &value); | |
42 if (rv != 1) | |
43 break; | |
44 result->push_back(static_cast<uint8_t>(value & 0xFF)); | |
45 } | |
46 bool error = ferror(fp); | |
47 fclose(fp); | |
48 return !error; | |
49 } | |
50 | |
51 bool ReadResultFile(const std::string& path, std::string* result) { | |
52 FILE* fp = OpenSourceRootRelativeFile(path.c_str()); | |
53 if (!fp) | |
54 return false; | |
55 fseek(fp, 0, SEEK_END); | |
56 size_t size = static_cast<size_t>(ftell(fp)); | |
57 fseek(fp, 0, SEEK_SET); | |
58 result->resize(size); | |
59 size_t size_read = fread(&result->at(0), 1, size, fp); | |
60 fclose(fp); | |
61 return size == size_read; | |
62 } | |
63 | |
64 std::string GetPath(const std::string& root, const std::string& suffix) { | |
65 return "mojo/public/interfaces/bindings/tests/data/" + root + suffix; | |
66 } | |
67 | |
68 void RunValidationTest(const std::string& root, std::string (*func)(Message*)) { | |
69 std::vector<uint8_t> data; | |
70 ASSERT_TRUE(ReadDataFile(GetPath(root, ".data"), &data)); | |
71 | |
72 std::string expected; | |
73 ASSERT_TRUE(ReadResultFile(GetPath(root, ".expected"), &expected)); | |
74 | |
75 Message message; | |
76 message.AllocUninitializedData(data.size()); | |
77 memcpy(message.mutable_data(), &data[0], data.size()); | |
78 | |
79 std::string result = func(&message); | |
80 EXPECT_EQ(expected, result); | |
yzshen1
2014/04/27 22:49:38
Does it make sense to also output |root| to help p
darin (slow to review)
2014/04/29 06:31:45
Done.
| |
81 } | |
82 | |
83 class DummyMessageReceiver : public MessageReceiver { | |
84 public: | |
85 virtual bool Accept(Message* message) MOJO_OVERRIDE { | |
86 return true; // Any message is OK. | |
87 } | |
88 virtual bool AcceptWithResponder(Message* message, | |
89 MessageReceiver* responder) MOJO_OVERRIDE { | |
90 assert(false); | |
91 return false; | |
92 } | |
93 }; | |
94 | |
95 std::string DumpMessageHeader(Message* message) { | |
96 DummyMessageReceiver not_reached_receiver; | |
97 internal::MessageHeaderValidator validator(¬_reached_receiver); | |
98 bool rv = validator.Accept(message); | |
99 if (!rv) | |
100 return "ERROR\n"; | |
101 | |
102 std::ostringstream os; | |
103 os << "num_bytes: " << message->header()->num_bytes << "\n" | |
104 << "num_fields: " << message->header()->num_fields << "\n" | |
105 << "name: " << message->header()->name << "\n" | |
106 << "flags: " << message->header()->flags << "\n"; | |
107 return os.str(); | |
108 } | |
109 | |
110 TEST(ValidationTest, TestAll) { | |
111 std::vector<std::string> names = | |
112 EnumerateSourceRootRelativeDirectory(GetPath("", "")); | |
113 | |
114 std::vector<std::string> header_tests = | |
115 GetMatchingTests(names, "validate_header_"); | |
116 | |
117 for (size_t i = 0; i < header_tests.size(); ++i) | |
118 RunValidationTest(header_tests[i], &DumpMessageHeader); | |
119 } | |
120 | |
121 } // namespace | |
122 } // namespace test | |
123 } // namespace mojo | |
OLD | NEW |