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.
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"
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()));
33 bool ReadDataFile(const std::string& path, std::vector<uint8_t>* result) {
34 FILE* fp = OpenSourceRootRelativeFile(path.c_str());
36 ADD_FAILURE() << "File not found: " << path;
41 int rv = fscanf(fp, "%x", &value);
44 result->push_back(static_cast<uint8_t>(value & 0xFF));
46 bool error = ferror(fp);
51 bool ReadResultFile(const std::string& path, std::string* result) {
52 FILE* fp = OpenSourceRootRelativeFile(path.c_str());
55 fseek(fp, 0, SEEK_END);
56 size_t size = static_cast<size_t>(ftell(fp));
58 // Result files should never be empty.
62 fseek(fp, 0, SEEK_SET);
64 size_t size_read = fread(&result->at(0), 1, size, fp);
66 if (size != size_read)
68 // Result files are new-line delimited text files. Remove any CRs.
69 result->erase(std::remove(result->begin(), result->end(), '\r'),
74 std::string GetPath(const std::string& root, const std::string& suffix) {
75 return "mojo/public/interfaces/bindings/tests/data/" + root + suffix;
78 void RunValidationTest(const std::string& root, std::string (*func)(Message*)) {
79 std::vector<uint8_t> data;
80 ASSERT_TRUE(ReadDataFile(GetPath(root, ".data"), &data));
83 ASSERT_TRUE(ReadResultFile(GetPath(root, ".expected"), &expected));
86 message.AllocUninitializedData(static_cast<uint32_t>(data.size()));
88 memcpy(message.mutable_data(), &data[0], data.size());
90 std::string result = func(&message);
91 EXPECT_EQ(expected, result) << "failed test: " << root;
94 class DummyMessageReceiver : public MessageReceiver {
96 virtual bool Accept(Message* message) MOJO_OVERRIDE {
97 return true; // Any message is OK.
99 virtual bool AcceptWithResponder(Message* message,
100 MessageReceiver* responder) MOJO_OVERRIDE {
106 std::string DumpMessageHeader(Message* message) {
107 DummyMessageReceiver not_reached_receiver;
108 internal::MessageHeaderValidator validator(¬_reached_receiver);
109 bool rv = validator.Accept(message);
113 std::ostringstream os;
114 os << "num_bytes: " << message->header()->num_bytes << "\n"
115 << "num_fields: " << message->header()->num_fields << "\n"
116 << "name: " << message->header()->name << "\n"
117 << "flags: " << message->header()->flags << "\n";
121 TEST(ValidationTest, TestAll) {
122 std::vector<std::string> names =
123 EnumerateSourceRootRelativeDirectory(GetPath("", ""));
125 std::vector<std::string> header_tests =
126 GetMatchingTests(names, "validate_header_");
128 for (size_t i = 0; i < header_tests.size(); ++i)
129 RunValidationTest(header_tests[i], &DumpMessageHeader);