Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / tests / validation_unittest.cc
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   if (size == 0) {
58     // Result files should never be empty.
59     fclose(fp);
60     return false;
61   }
62   fseek(fp, 0, SEEK_SET);
63   result->resize(size);
64   size_t size_read = fread(&result->at(0), 1, size, fp);
65   fclose(fp);
66   if (size != size_read)
67     return false;
68   // Result files are new-line delimited text files. Remove any CRs.
69   result->erase(std::remove(result->begin(), result->end(), '\r'),
70                 result->end());
71   return true;
72 }
73
74 std::string GetPath(const std::string& root, const std::string& suffix) {
75   return "mojo/public/interfaces/bindings/tests/data/" + root + suffix;
76 }
77
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));
81
82   std::string expected;
83   ASSERT_TRUE(ReadResultFile(GetPath(root, ".expected"), &expected));
84
85   Message message;
86   message.AllocUninitializedData(static_cast<uint32_t>(data.size()));
87   if (!data.empty())
88     memcpy(message.mutable_data(), &data[0], data.size());
89
90   std::string result = func(&message);
91   EXPECT_EQ(expected, result) << "failed test: " << root;
92 }
93
94 class DummyMessageReceiver : public MessageReceiver {
95  public:
96   virtual bool Accept(Message* message) MOJO_OVERRIDE {
97     return true;  // Any message is OK.
98   }
99   virtual bool AcceptWithResponder(Message* message,
100                                    MessageReceiver* responder) MOJO_OVERRIDE {
101     assert(false);
102     return false;
103   }
104 };
105
106 std::string DumpMessageHeader(Message* message) {
107   DummyMessageReceiver not_reached_receiver;
108   internal::MessageHeaderValidator validator(&not_reached_receiver);
109   bool rv = validator.Accept(message);
110   if (!rv)
111     return "ERROR\n";
112
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";
118   return os.str();
119 }
120
121 TEST(ValidationTest, TestAll) {
122   std::vector<std::string> names =
123       EnumerateSourceRootRelativeDirectory(GetPath("", ""));
124
125   std::vector<std::string> header_tests =
126       GetMatchingTests(names, "validate_header_");
127
128   for (size_t i = 0; i < header_tests.size(); ++i)
129     RunValidationTest(header_tests[i], &DumpMessageHeader);
130 }
131
132 }  // namespace
133 }  // namespace test
134 }  // namespace mojo