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