| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "mojo/public/cpp/bindings/tests/validation_util.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" |
| 10 #include "mojo/public/cpp/test_support/test_support.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace test { |
| 15 namespace validation_util { |
| 16 |
| 17 std::string GetValidationDataPath(const std::string& root, |
| 18 const std::string& suffix) { |
| 19 return "mojo/public/interfaces/bindings/tests/data/validation/" + root + |
| 20 suffix; |
| 21 } |
| 22 |
| 23 bool ReadFile(const std::string& path, std::string* result) { |
| 24 FILE* fp = OpenSourceRootRelativeFile(path.c_str()); |
| 25 if (!fp) { |
| 26 ADD_FAILURE() << "File not found: " << path; |
| 27 return false; |
| 28 } |
| 29 fseek(fp, 0, SEEK_END); |
| 30 size_t size = static_cast<size_t>(ftell(fp)); |
| 31 if (size == 0) { |
| 32 result->clear(); |
| 33 fclose(fp); |
| 34 return true; |
| 35 } |
| 36 fseek(fp, 0, SEEK_SET); |
| 37 result->resize(size); |
| 38 size_t size_read = fread(&result->at(0), 1, size, fp); |
| 39 fclose(fp); |
| 40 return size == size_read; |
| 41 } |
| 42 |
| 43 bool ReadAndParseDataFile(const std::string& path, |
| 44 std::vector<uint8_t>* data, |
| 45 size_t* num_handles) { |
| 46 std::string input; |
| 47 if (!ReadFile(path, &input)) |
| 48 return false; |
| 49 |
| 50 std::string error_message; |
| 51 if (!ParseValidationTestInput(input, data, num_handles, &error_message)) { |
| 52 ADD_FAILURE() << error_message; |
| 53 return false; |
| 54 } |
| 55 |
| 56 return true; |
| 57 } |
| 58 |
| 59 bool ReadResultFile(const std::string& path, std::string* result) { |
| 60 if (!ReadFile(path, result)) |
| 61 return false; |
| 62 |
| 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 |
| 67 // Remove trailing LFs. |
| 68 size_t pos = result->find_last_not_of('\n'); |
| 69 if (pos == std::string::npos) |
| 70 result->clear(); |
| 71 else |
| 72 result->resize(pos + 1); |
| 73 |
| 74 return true; |
| 75 } |
| 76 |
| 77 bool ReadTestCase(const std::string& test_name, |
| 78 std::vector<uint8_t>* data, |
| 79 size_t* num_handles, |
| 80 std::string* expected) { |
| 81 if (!ReadAndParseDataFile(GetValidationDataPath(test_name, ".data"), data, |
| 82 num_handles) || |
| 83 !ReadResultFile(GetValidationDataPath(test_name, ".expected"), |
| 84 expected)) { |
| 85 return false; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 std::vector<std::string> GetMatchingTests(const std::string& prefix) { |
| 92 std::vector<std::string> names = EnumerateSourceRootRelativeDirectory( |
| 93 validation_util::GetValidationDataPath("", "")); |
| 94 const std::string suffix = ".data"; |
| 95 std::vector<std::string> tests; |
| 96 for (size_t i = 0; i < names.size(); ++i) { |
| 97 if (names[i].size() >= suffix.size() && |
| 98 names[i].substr(0, prefix.size()) == prefix && |
| 99 names[i].substr(names[i].size() - suffix.size()) == suffix) |
| 100 tests.push_back(names[i].substr(0, names[i].size() - suffix.size())); |
| 101 } |
| 102 return tests; |
| 103 } |
| 104 |
| 105 } // namespace validation_util |
| 106 } // namespace test |
| 107 } // namespace mojo |
| OLD | NEW |