summaryrefslogtreecommitdiffstats
path: root/chromeos/network/onc/onc_test_utils.cc
blob: 56e5bc6302d7fab51049072a464874f2e4b95596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2012 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 "chromeos/network/onc/onc_test_utils.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/chromeos_test_utils.h"

namespace chromeos {
namespace onc {
namespace test_utils {

namespace {

// The name of the component directory to get the test data from.
const char kNetworkComponentDirectory[] = "network";

}  // namespace

std::string ReadTestData(const std::string& filename) {
  base::FilePath path;
  if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
                                             filename,
                                             &path)) {
    NOTREACHED() << "Unable to get test data path for "
                 << kNetworkComponentDirectory << "/" << filename;
    return "";
  }
  std::string result;
  base::ReadFileToString(path, &result);
  return result;
}

scoped_ptr<base::DictionaryValue> ReadTestDictionary(
    const std::string& filename) {
  base::DictionaryValue* dict = NULL;
  base::FilePath path;
  if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
                                             filename,
                                             &path)) {
    NOTREACHED() << "Unable to get test dictionary path for "
                 << kNetworkComponentDirectory << "/" << filename;
    return make_scoped_ptr(dict);
  }

  JSONFileValueSerializer serializer(path);
  serializer.set_allow_trailing_comma(true);

  std::string error_message;
  base::Value* content = serializer.Deserialize(NULL, &error_message);
  CHECK(content != NULL) << "Couldn't json-deserialize file '"
                         << filename << "': " << error_message;

  CHECK(content->GetAsDictionary(&dict))
      << "File '" << filename
      << "' does not contain a dictionary as expected, but type "
      << content->GetType();
  return make_scoped_ptr(dict);
}

::testing::AssertionResult Equals(const base::Value* expected,
                                  const base::Value* actual) {
  CHECK(expected != NULL);
  if (actual == NULL)
    return ::testing::AssertionFailure() << "Actual value pointer is NULL";

  if (expected->Equals(actual))
    return ::testing::AssertionSuccess() << "Values are equal";

  return ::testing::AssertionFailure() << "Values are unequal.\n"
                                       << "Expected value:\n" << *expected
                                       << "Actual value:\n" << *actual;
}

}  // namespace test_utils
}  // namespace onc
}  // namespace chromeos