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

Unified Diff: mojo/public/cpp/bindings/tests/validation_util.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, 5 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/tests/validation_util.cc
diff --git a/mojo/public/cpp/bindings/tests/validation_util.cc b/mojo/public/cpp/bindings/tests/validation_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6b013564318be151288e0c79073b2e85a5d4afc6
--- /dev/null
+++ b/mojo/public/cpp/bindings/tests/validation_util.cc
@@ -0,0 +1,107 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/cpp/bindings/tests/validation_util.h"
+
+#include <stdio.h>
+
+#include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
+#include "mojo/public/cpp/test_support/test_support.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace test {
+namespace validation_util {
+
+std::string GetValidationDataPath(const std::string& root,
+ const std::string& suffix) {
+ return "mojo/public/interfaces/bindings/tests/data/validation/" + root +
+ suffix;
+}
+
+bool ReadFile(const std::string& path, std::string* result) {
+ FILE* fp = OpenSourceRootRelativeFile(path.c_str());
+ if (!fp) {
+ ADD_FAILURE() << "File not found: " << path;
+ return false;
+ }
+ fseek(fp, 0, SEEK_END);
+ size_t size = static_cast<size_t>(ftell(fp));
+ if (size == 0) {
+ result->clear();
+ fclose(fp);
+ return true;
+ }
+ fseek(fp, 0, SEEK_SET);
+ result->resize(size);
+ size_t size_read = fread(&result->at(0), 1, size, fp);
+ fclose(fp);
+ return size == size_read;
+}
+
+bool ReadAndParseDataFile(const std::string& path,
+ std::vector<uint8_t>* data,
+ size_t* num_handles) {
+ std::string input;
+ if (!ReadFile(path, &input))
+ return false;
+
+ std::string error_message;
+ if (!ParseValidationTestInput(input, data, num_handles, &error_message)) {
+ ADD_FAILURE() << error_message;
+ return false;
+ }
+
+ return true;
+}
+
+bool ReadResultFile(const std::string& path, std::string* result) {
+ if (!ReadFile(path, result))
+ return false;
+
+ // Result files are new-line delimited text files. Remove any CRs.
+ result->erase(std::remove(result->begin(), result->end(), '\r'),
+ result->end());
+
+ // Remove trailing LFs.
+ size_t pos = result->find_last_not_of('\n');
+ if (pos == std::string::npos)
+ result->clear();
+ else
+ result->resize(pos + 1);
+
+ return true;
+}
+
+bool ReadTestCase(const std::string& test_name,
+ std::vector<uint8_t>* data,
+ size_t* num_handles,
+ std::string* expected) {
+ if (!ReadAndParseDataFile(GetValidationDataPath(test_name, ".data"), data,
+ num_handles) ||
+ !ReadResultFile(GetValidationDataPath(test_name, ".expected"),
+ expected)) {
+ return false;
+ }
+
+ return true;
+}
+
+std::vector<std::string> GetMatchingTests(const std::string& prefix) {
+ std::vector<std::string> names = EnumerateSourceRootRelativeDirectory(
+ validation_util::GetValidationDataPath("", ""));
+ const std::string suffix = ".data";
+ std::vector<std::string> tests;
+ for (size_t i = 0; i < names.size(); ++i) {
+ if (names[i].size() >= suffix.size() &&
+ names[i].substr(0, prefix.size()) == prefix &&
+ names[i].substr(names[i].size() - suffix.size()) == suffix)
+ tests.push_back(names[i].substr(0, names[i].size() - suffix.size()));
+ }
+ return tests;
+}
+
+} // namespace validation_util
+} // namespace test
+} // namespace mojo
« no previous file with comments | « mojo/public/cpp/bindings/tests/validation_util.h ('k') | mojo/public/tools/bindings/mojom_tool/bin/linux64/generators/c.sha1 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698