Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / chromeos / tools / onc_validator / onc_validator.cc
blob84a92202c9c40ce8d6f4b435cb6556967de753f3
1 // Copyright (c) 2011 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.
5 #include <cstdio>
6 #include <iostream>
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_file_value_serializer.h"
12 #include "base/logging.h"
13 #include "base/values.h"
14 #include "chromeos/network/onc/onc_signature.h"
15 #include "chromeos/network/onc/onc_validator.h"
17 // Command line switches.
18 const char kSwitchErrorOnUnknownField[] = "error-on-unknown-field";
19 const char kSwitchErrorOnWrongRecommended[] = "error-on-wrong-recommended";
20 const char kSwitchErrorOnMissingField[] = "error-on-missing-field";
21 const char kSwitchManagedOnc[] = "managed-onc";
22 const char kSwitchUserPolicy[] = "user-policy";
23 const char kSwitchDevicePolicy[] = "device-policy";
24 const char kSwitchUserImport[] = "user-import";
26 const char* kSwitches[] = {
27 kSwitchErrorOnUnknownField,
28 kSwitchErrorOnWrongRecommended,
29 kSwitchErrorOnMissingField,
30 kSwitchManagedOnc,
31 kSwitchUserPolicy,
32 kSwitchDevicePolicy,
33 kSwitchUserImport
36 // Return codes.
37 enum ReturnCode {
38 kStatusValid = 0,
39 kStatusWarnings = 1,
40 kStatusInvalid = 2,
41 kStatusJsonError = 3,
42 kStatusArgumentError = 4,
45 const char kToplevelConfiguration[] = "ToplevelConfiguration";
46 const char kNetworkConfiguration[] = "NetworkConfiguration";
47 const char kCertificate[] = "Certificate";
48 const char* kTypes[] = {
49 kToplevelConfiguration,
50 kNetworkConfiguration,
51 kCertificate
54 void PrintHelp() {
55 fprintf(stderr,
56 "Usage:\n"
57 " onc_validator [OPTION]... [TYPE] onc_file\n"
58 "\n"
59 "Valid TYPEs are:\n");
60 for (size_t i = 0; i < arraysize(kTypes); ++i)
61 fprintf(stderr, " %s\n", kTypes[i]);
63 fprintf(stderr,
64 "\n"
65 "Valid OPTIONs are:\n");
66 for (size_t i = 0; i < arraysize(kSwitches); ++i)
67 fprintf(stderr, " --%s\n", kSwitches[i]);
69 fprintf(stderr,
70 "\n"
71 "Exit status is one of:\n"
72 " %i File is valid without warnings.\n"
73 " %i File is valid with warnings,\n"
74 " i.e. there were errors which were degraded to warnings.\n"
75 " %i File is invalid.\n"
76 " %i File couldn't be read or is not a valid JSON dictionary.\n"
77 " %i Some command line arguments are wrong.\n",
78 kStatusValid,
79 kStatusWarnings,
80 kStatusInvalid,
81 kStatusJsonError,
82 kStatusArgumentError);
85 scoped_ptr<base::DictionaryValue> ReadDictionary(std::string filename) {
86 base::FilePath path(filename);
87 JSONFileValueDeserializer deserializer(path);
88 deserializer.set_allow_trailing_comma(true);
90 base::DictionaryValue* dict = NULL;
92 std::string json_error;
93 base::Value* value = deserializer.Deserialize(NULL, &json_error);
94 if (!value) {
95 LOG(ERROR) << "Couldn't json-deserialize file '" << filename
96 << "': " << json_error;
97 return make_scoped_ptr(dict);
100 if (!value->GetAsDictionary(&dict)) {
101 LOG(ERROR) << "File '" << filename
102 << "' does not contain a dictionary as expected, but type "
103 << value->GetType();
106 return make_scoped_ptr(dict);
109 int main(int argc, const char* argv[]) {
110 base::CommandLine::Init(argc, argv);
112 const base::CommandLine& command_line =
113 *base::CommandLine::ForCurrentProcess();
114 base::CommandLine::StringVector args = command_line.GetArgs();
115 if (args.size() != 2) {
116 PrintHelp();
117 return kStatusArgumentError;
120 scoped_ptr<base::DictionaryValue> onc_object = ReadDictionary(args[1]);
122 if (!onc_object)
123 return kStatusJsonError;
125 chromeos::onc::Validator validator(
126 command_line.HasSwitch(kSwitchErrorOnUnknownField),
127 command_line.HasSwitch(kSwitchErrorOnWrongRecommended),
128 command_line.HasSwitch(kSwitchErrorOnMissingField),
129 command_line.HasSwitch(kSwitchManagedOnc));
131 if (command_line.HasSwitch(kSwitchUserPolicy))
132 validator.SetOncSource(::onc::ONC_SOURCE_USER_POLICY);
133 else if (command_line.HasSwitch(kSwitchDevicePolicy))
134 validator.SetOncSource(::onc::ONC_SOURCE_DEVICE_POLICY);
135 else if (command_line.HasSwitch(kSwitchUserImport))
136 validator.SetOncSource(::onc::ONC_SOURCE_USER_IMPORT);
138 std::string type_arg(args[0]);
139 const chromeos::onc::OncValueSignature* signature = NULL;
140 if (type_arg == kToplevelConfiguration) {
141 signature = &chromeos::onc::kToplevelConfigurationSignature;
142 } else if (type_arg == kNetworkConfiguration) {
143 signature = &chromeos::onc::kNetworkConfigurationSignature;
144 } else if (type_arg == kCertificate) {
145 signature = &chromeos::onc::kCertificateSignature;
146 } else {
147 LOG(ERROR) << "Unknown ONC type '" << type_arg << "'";
148 return kStatusArgumentError;
151 chromeos::onc::Validator::Result result;
152 validator.ValidateAndRepairObject(signature, *onc_object, &result);
154 switch(result) {
155 case chromeos::onc::Validator::VALID:
156 return kStatusValid;
157 case chromeos::onc::Validator::VALID_WITH_WARNINGS:
158 return kStatusWarnings;
159 case chromeos::onc::Validator::INVALID:
160 return kStatusInvalid;
161 default:
162 CHECK(false);