Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: mojo/public/cpp/bindings/tests/validation_unittest.cc

Issue 2163793002: C bindings: Implement _Validate(), and some pre-requisites (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: address comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdio.h> 5 #include <stdio.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "mojo/public/cpp/bindings/binding.h" 12 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h" 13 #include "mojo/public/cpp/bindings/interface_ptr.h"
14 #include "mojo/public/cpp/bindings/lib/connector.h" 14 #include "mojo/public/cpp/bindings/lib/connector.h"
15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" 15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
16 #include "mojo/public/cpp/bindings/lib/router.h" 16 #include "mojo/public/cpp/bindings/lib/router.h"
17 #include "mojo/public/cpp/bindings/lib/validation_errors.h" 17 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
18 #include "mojo/public/cpp/bindings/message.h" 18 #include "mojo/public/cpp/bindings/message.h"
19 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" 19 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
20 #include "mojo/public/cpp/bindings/tests/validation_util.h"
20 #include "mojo/public/cpp/system/macros.h" 21 #include "mojo/public/cpp/system/macros.h"
21 #include "mojo/public/cpp/system/message_pipe.h" 22 #include "mojo/public/cpp/system/message_pipe.h"
22 #include "mojo/public/cpp/test_support/test_support.h" 23 #include "mojo/public/cpp/test_support/test_support.h"
23 #include "mojo/public/cpp/utility/run_loop.h" 24 #include "mojo/public/cpp/utility/run_loop.h"
24 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom .h" 25 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom .h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 namespace mojo { 28 namespace mojo {
28 29
29 using internal::MessageValidator; 30 using internal::MessageValidator;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 EXPECT_EQ(std::string(), error_message); 63 EXPECT_EQ(std::string(), error_message);
63 EXPECT_EQ(expected_data, data); 64 EXPECT_EQ(expected_data, data);
64 EXPECT_EQ(expected_num_handles, num_handles); 65 EXPECT_EQ(expected_num_handles, num_handles);
65 return false; 66 return false;
66 } 67 }
67 68
68 EXPECT_FALSE(error_message.empty()); 69 EXPECT_FALSE(error_message.empty());
69 return !result && !error_message.empty(); 70 return !result && !error_message.empty();
70 } 71 }
71 72
72 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names,
73 const std::string& prefix) {
74 const std::string suffix = ".data";
75 std::vector<std::string> tests;
76 for (size_t i = 0; i < names.size(); ++i) {
77 if (names[i].size() >= suffix.size() &&
78 names[i].substr(0, prefix.size()) == prefix &&
79 names[i].substr(names[i].size() - suffix.size()) == suffix)
80 tests.push_back(names[i].substr(0, names[i].size() - suffix.size()));
81 }
82 return tests;
83 }
84
85 bool ReadFile(const std::string& path, std::string* result) {
86 FILE* fp = OpenSourceRootRelativeFile(path.c_str());
87 if (!fp) {
88 ADD_FAILURE() << "File not found: " << path;
89 return false;
90 }
91 fseek(fp, 0, SEEK_END);
92 size_t size = static_cast<size_t>(ftell(fp));
93 if (size == 0) {
94 result->clear();
95 fclose(fp);
96 return true;
97 }
98 fseek(fp, 0, SEEK_SET);
99 result->resize(size);
100 size_t size_read = fread(&result->at(0), 1, size, fp);
101 fclose(fp);
102 return size == size_read;
103 }
104
105 bool ReadAndParseDataFile(const std::string& path,
106 std::vector<uint8_t>* data,
107 size_t* num_handles) {
108 std::string input;
109 if (!ReadFile(path, &input))
110 return false;
111
112 std::string error_message;
113 if (!ParseValidationTestInput(input, data, num_handles, &error_message)) {
114 ADD_FAILURE() << error_message;
115 return false;
116 }
117
118 return true;
119 }
120
121 bool ReadResultFile(const std::string& path, std::string* result) {
122 if (!ReadFile(path, result))
123 return false;
124
125 // Result files are new-line delimited text files. Remove any CRs.
126 result->erase(std::remove(result->begin(), result->end(), '\r'),
127 result->end());
128
129 // Remove trailing LFs.
130 size_t pos = result->find_last_not_of('\n');
131 if (pos == std::string::npos)
132 result->clear();
133 else
134 result->resize(pos + 1);
135
136 return true;
137 }
138
139 std::string GetPath(const std::string& root, const std::string& suffix) {
140 return "mojo/public/interfaces/bindings/tests/data/validation/" + root +
141 suffix;
142 }
143
144 // |message| should be a newly created object.
145 bool ReadTestCase(const std::string& test,
146 Message* message,
147 std::string* expected) {
148 std::vector<uint8_t> data;
149 size_t num_handles;
150 if (!ReadAndParseDataFile(GetPath(test, ".data"), &data, &num_handles) ||
151 !ReadResultFile(GetPath(test, ".expected"), expected)) {
152 return false;
153 }
154
155 message->AllocUninitializedData(static_cast<uint32_t>(data.size()));
156 if (!data.empty())
157 memcpy(message->mutable_data(), &data[0], data.size());
158 message->mutable_handles()->resize(num_handles);
159
160 return true;
161 }
162
163 void RunValidationTests(const std::string& prefix, 73 void RunValidationTests(const std::string& prefix,
164 const MessageValidatorList& validators, 74 const MessageValidatorList& validators,
165 MessageReceiver* test_message_receiver) { 75 MessageReceiver* test_message_receiver) {
166 std::vector<std::string> names = 76 std::vector<std::string> tests = validation_util::GetMatchingTests(prefix);
167 EnumerateSourceRootRelativeDirectory(GetPath("", ""));
168 std::vector<std::string> tests = GetMatchingTests(names, prefix);
169 77
170 for (size_t i = 0; i < tests.size(); ++i) { 78 for (size_t i = 0; i < tests.size(); ++i) {
79 std::string expected;
80 std::vector<uint8_t> data;
81 size_t num_handles;
82 ASSERT_TRUE(validation_util::ReadTestCase(tests[i], &data, &num_handles,
83 &expected));
84
171 Message message; 85 Message message;
172 std::string expected; 86 message.AllocUninitializedData(data.size());
173 ASSERT_TRUE(ReadTestCase(tests[i], &message, &expected)); 87 if (!data.empty())
88 memcpy(message.mutable_data(), &data[0], data.size());
89 message.mutable_handles()->resize(num_handles);
174 90
175 std::string actual; 91 std::string actual;
176 auto result = RunValidatorsOnMessage(validators, &message, nullptr); 92 auto result = RunValidatorsOnMessage(validators, &message, nullptr);
177 if (result == ValidationError::NONE) { 93 if (result == ValidationError::NONE) {
178 ignore_result(test_message_receiver->Accept(&message)); 94 ignore_result(test_message_receiver->Accept(&message));
179 actual = "PASS"; 95 actual = "PASS";
180 } else { 96 } else {
181 actual = ValidationErrorToString(result); 97 actual = ValidationErrorToString(result);
182 } 98 }
183 99
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 static_cast<StructWithEnum::EnumWithin>(2))); 445 static_cast<StructWithEnum::EnumWithin>(2)));
530 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( 446 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue(
531 static_cast<StructWithEnum::EnumWithin>(3))); 447 static_cast<StructWithEnum::EnumWithin>(3)));
532 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue( 448 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue(
533 static_cast<StructWithEnum::EnumWithin>(4))); 449 static_cast<StructWithEnum::EnumWithin>(4)));
534 } 450 }
535 451
536 } // namespace 452 } // namespace
537 } // namespace test 453 } // namespace test
538 } // namespace mojo 454 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/validation_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698