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

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

Issue 307353009: Mojo cpp bindings: report the reason of validation failure. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
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 <sstream> 8 #include <sstream>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" 12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
13 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
13 #include "mojo/public/cpp/test_support/test_support.h" 14 #include "mojo/public/cpp/test_support/test_support.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace mojo { 17 namespace mojo {
17 namespace test { 18 namespace test {
18 namespace { 19 namespace {
19 20
21 std::string ValidationErrorToResultString(internal::ValidationError error) {
22 std::string result = internal::ValidationErrorToString(error);
23 result.push_back('\n');
24 return result;
25 }
26
20 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names, 27 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names,
21 const std::string& prefix) { 28 const std::string& prefix) {
22 const std::string suffix = ".data"; 29 const std::string suffix = ".data";
23 std::vector<std::string> tests; 30 std::vector<std::string> tests;
24 for (size_t i = 0; i < names.size(); ++i) { 31 for (size_t i = 0; i < names.size(); ++i) {
25 if (names[i].size() >= suffix.size() && 32 if (names[i].size() >= suffix.size() &&
26 names[i].substr(0, prefix.size()) == prefix && 33 names[i].substr(0, prefix.size()) == prefix &&
27 names[i].substr(names[i].size() - suffix.size()) == suffix) 34 names[i].substr(names[i].size() - suffix.size()) == suffix)
28 tests.push_back(names[i].substr(0, names[i].size() - suffix.size())); 35 tests.push_back(names[i].substr(0, names[i].size() - suffix.size()));
29 } 36 }
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 99 }
93 100
94 class DummyMessageReceiver : public MessageReceiver { 101 class DummyMessageReceiver : public MessageReceiver {
95 public: 102 public:
96 virtual bool Accept(Message* message) MOJO_OVERRIDE { 103 virtual bool Accept(Message* message) MOJO_OVERRIDE {
97 return true; // Any message is OK. 104 return true; // Any message is OK.
98 } 105 }
99 }; 106 };
100 107
101 std::string DumpMessageHeader(Message* message) { 108 std::string DumpMessageHeader(Message* message) {
109 internal::ValidationErrorObserverForTesting observer;
102 DummyMessageReceiver not_reached_receiver; 110 DummyMessageReceiver not_reached_receiver;
103 internal::MessageHeaderValidator validator(&not_reached_receiver); 111 internal::MessageHeaderValidator validator(&not_reached_receiver);
104 bool rv = validator.Accept(message); 112 bool rv = validator.Accept(message);
105 if (!rv) 113 if (!rv) {
106 return "ERROR\n"; 114 EXPECT_NE(internal::VALIDATION_ERROR_NONE, observer.last_error());
115 return ValidationErrorToResultString(observer.last_error());
116 }
107 117
108 std::ostringstream os; 118 std::ostringstream os;
109 os << "num_bytes: " << message->header()->num_bytes << "\n" 119 os << "num_bytes: " << message->header()->num_bytes << "\n"
110 << "num_fields: " << message->header()->num_fields << "\n" 120 << "num_fields: " << message->header()->num_fields << "\n"
111 << "name: " << message->header()->name << "\n" 121 << "name: " << message->header()->name << "\n"
112 << "flags: " << message->header()->flags << "\n"; 122 << "flags: " << message->header()->flags << "\n";
113 return os.str(); 123 return os.str();
114 } 124 }
115 125
116 TEST(ValidationTest, TestAll) { 126 TEST(ValidationTest, TestAll) {
117 std::vector<std::string> names = 127 std::vector<std::string> names =
118 EnumerateSourceRootRelativeDirectory(GetPath("", "")); 128 EnumerateSourceRootRelativeDirectory(GetPath("", ""));
119 129
120 std::vector<std::string> header_tests = 130 std::vector<std::string> header_tests =
121 GetMatchingTests(names, "validate_header_"); 131 GetMatchingTests(names, "validate_header_");
122 132
123 for (size_t i = 0; i < header_tests.size(); ++i) 133 for (size_t i = 0; i < header_tests.size(); ++i)
124 RunValidationTest(header_tests[i], &DumpMessageHeader); 134 RunValidationTest(header_tests[i], &DumpMessageHeader);
125 } 135 }
126 136
127 } // namespace 137 } // namespace
128 } // namespace test 138 } // namespace test
129 } // namespace mojo 139 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698