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

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

Issue 229683005: Validate MessageHeader before using (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove debug printfs Created 6 years, 7 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
(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 <algorithm>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11
12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
13 #include "mojo/public/cpp/test_support/test_support.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 if (names[i].size() >= suffix.size() &&
26 names[i].substr(0, prefix.size()) == prefix &&
27 names[i].substr(names[i].size() - suffix.size()) == suffix)
28 tests.push_back(names[i].substr(0, names[i].size() - suffix.size()));
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 if (size != size_read)
62 return false;
63 // Result files are new-line delimited text files. Remove any CRs.
64 result->erase(std::remove(result->begin(), result->end(), '\r'),
65 result->end());
66 return true;
67 }
68
69 std::string GetPath(const std::string& root, const std::string& suffix) {
70 return "mojo/public/interfaces/bindings/tests/data/" + root + suffix;
71 }
72
73 void RunValidationTest(const std::string& root, std::string (*func)(Message*)) {
74 std::vector<uint8_t> data;
75 ASSERT_TRUE(ReadDataFile(GetPath(root, ".data"), &data));
76
77 std::string expected;
78 ASSERT_TRUE(ReadResultFile(GetPath(root, ".expected"), &expected));
79
80 Message message;
81 message.AllocUninitializedData(static_cast<uint32_t>(data.size()));
82 memcpy(message.mutable_data(), &data[0], data.size());
83
84 std::string result = func(&message);
85 EXPECT_EQ(expected, result) << "failed test: " << root;
86 }
87
88 class DummyMessageReceiver : public MessageReceiver {
89 public:
90 virtual bool Accept(Message* message) MOJO_OVERRIDE {
91 return true; // Any message is OK.
92 }
93 virtual bool AcceptWithResponder(Message* message,
94 MessageReceiver* responder) MOJO_OVERRIDE {
95 assert(false);
96 return false;
97 }
98 };
99
100 std::string DumpMessageHeader(Message* message) {
101 DummyMessageReceiver not_reached_receiver;
102 internal::MessageHeaderValidator validator(&not_reached_receiver);
103 bool rv = validator.Accept(message);
104 if (!rv)
105 return "ERROR\n";
106
107 std::ostringstream os;
108 os << "num_bytes: " << message->header()->num_bytes << "\n"
109 << "num_fields: " << message->header()->num_fields << "\n"
110 << "name: " << message->header()->name << "\n"
111 << "flags: " << message->header()->flags << "\n";
112 return os.str();
113 }
114
115 TEST(ValidationTest, TestAll) {
116 std::vector<std::string> names =
117 EnumerateSourceRootRelativeDirectory(GetPath("", ""));
118
119 std::vector<std::string> header_tests =
120 GetMatchingTests(names, "validate_header_");
121
122 for (size_t i = 0; i < header_tests.size(); ++i)
123 RunValidationTest(header_tests[i], &DumpMessageHeader);
124 }
125
126 } // namespace
127 } // namespace test
128 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/message_header_validator.cc ('k') | mojo/public/cpp/test_support/lib/test_support.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698