onc_test_utils.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2012 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. #include "chromeos/components/onc/onc_test_utils.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/json/json_file_value_serializer.h"
  10. #include "base/json/json_reader.h"
  11. #include "base/logging.h"
  12. #include "base/notreached.h"
  13. #include "base/path_service.h"
  14. #include "base/threading/thread_restrictions.h"
  15. #include "base/values.h"
  16. namespace chromeos {
  17. namespace onc {
  18. namespace test_utils {
  19. namespace {
  20. bool GetTestDataPath(const std::string& filename, base::FilePath* result_path) {
  21. base::ScopedAllowBlockingForTesting allow_io;
  22. base::FilePath path;
  23. if (!base::PathService::Get(base::DIR_SOURCE_ROOT, &path)) {
  24. LOG(FATAL) << "Failed to get the path to root for " << filename;
  25. return false;
  26. }
  27. path = path.Append(FILE_PATH_LITERAL("chromeos"));
  28. path = path.Append(FILE_PATH_LITERAL("components"));
  29. path = path.Append(FILE_PATH_LITERAL("test"));
  30. path = path.Append(FILE_PATH_LITERAL("data"));
  31. path = path.Append(FILE_PATH_LITERAL("onc"));
  32. path = path.Append(FILE_PATH_LITERAL(filename));
  33. if (!base::PathExists(path)) { // We don't want to create this.
  34. LOG(FATAL) << "The file doesn't exist: " << path;
  35. return false;
  36. }
  37. *result_path = path;
  38. return true;
  39. }
  40. } // namespace
  41. std::string ReadTestData(const std::string& filename) {
  42. base::ScopedAllowBlockingForTesting allow_io;
  43. base::FilePath path;
  44. if (!GetTestDataPath(filename, &path)) {
  45. return "";
  46. }
  47. std::string result;
  48. base::ReadFileToString(path, &result);
  49. return result;
  50. }
  51. base::Value ReadTestJson(const std::string& filename) {
  52. base::FilePath path;
  53. if (!GetTestDataPath(filename, &path)) {
  54. LOG(FATAL) << "Unable to get test file path for: " << filename;
  55. return {};
  56. }
  57. JSONFileValueDeserializer deserializer(
  58. path,
  59. base::JSON_PARSE_CHROMIUM_EXTENSIONS | base::JSON_ALLOW_TRAILING_COMMAS);
  60. std::string error_message;
  61. std::unique_ptr<base::Value> result =
  62. deserializer.Deserialize(nullptr, &error_message);
  63. CHECK(result != nullptr) << "Couldn't json-deserialize file: " << filename
  64. << ": " << error_message;
  65. return std::move(*result);
  66. }
  67. base::Value ReadTestDictionaryValue(const std::string& filename) {
  68. base::Value content = ReadTestJson(filename);
  69. CHECK(content.is_dict())
  70. << "File '" << filename
  71. << "' does not contain a dictionary as expected, but type "
  72. << content.type();
  73. return content;
  74. }
  75. ::testing::AssertionResult Equals(const base::Value* expected,
  76. const base::Value* actual) {
  77. CHECK(expected != nullptr);
  78. if (actual == nullptr)
  79. return ::testing::AssertionFailure() << "Actual value pointer is nullptr";
  80. if (*expected == *actual)
  81. return ::testing::AssertionSuccess() << "Values are equal";
  82. return ::testing::AssertionFailure() << "Values are unequal.\n"
  83. << "Expected value:\n"
  84. << *expected << "Actual value:\n"
  85. << *actual;
  86. }
  87. } // namespace test_utils
  88. } // namespace onc
  89. } // namespace chromeos