OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <iostream> | |
6 #include <cstdio> | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/command_line.h" | |
10 #include "base/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" | |
16 | |
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"; | |
25 | |
26 const char* kSwitches[] = { kSwitchErrorOnUnknownField, | |
stevenjb
2013/01/23 17:22:00
nit: first switch on newline is more common and ma
pneubeck (no reviews)
2013/01/24 13:35:02
Done.
| |
27 kSwitchErrorOnWrongRecommended, | |
28 kSwitchErrorOnMissingField, | |
29 kSwitchManagedOnc, | |
30 kSwitchUserPolicy, | |
31 kSwitchDevicePolicy, | |
32 kSwitchUserImport }; | |
stevenjb
2013/01/23 17:22:00
} on separate line
pneubeck (no reviews)
2013/01/24 13:35:02
Done.
| |
33 | |
34 // Return codes. | |
35 enum ReturnCode { | |
36 kStatusValid = 0, | |
37 kStatusWarnings = 1, | |
38 kStatusInvalid = 2, | |
39 kStatusJsonError = 3, | |
40 kStatusArgumentError = 4, | |
41 }; | |
42 | |
43 const char kToplevelConfiguration[] = "ToplevelConfiguration"; | |
44 const char kNetworkConfiguration[] = "NetworkConfiguration"; | |
45 const char kCertificate[] = "Certificate"; | |
46 const char* kTypes[] = { kToplevelConfiguration, | |
47 kNetworkConfiguration, | |
48 kCertificate }; | |
stevenjb
2013/01/23 17:22:00
Same nits here
pneubeck (no reviews)
2013/01/24 13:35:02
Done.
| |
49 | |
50 void PrintHelp() { | |
51 fprintf(stderr, | |
52 "Usage:\n" | |
53 " onc_validator [OPTION]... [TYPE] onc_file\n" | |
54 "\n" | |
55 "Valid TYPEs are:\n"); | |
56 for (size_t i = 0; i < arraysize(kTypes); ++i) | |
57 fprintf(stderr, " %s\n", kTypes[i]); | |
58 | |
59 fprintf(stderr, | |
60 "\n" | |
61 "Valid OPTIONs are:\n"); | |
62 for (size_t i = 0; i < arraysize(kSwitches); ++i) | |
63 fprintf(stderr, " --%s\n", kSwitches[i]); | |
64 | |
65 fprintf(stderr, | |
66 "\n" | |
67 "Exist status is one of:\n" | |
stevenjb
2013/01/23 17:22:00
Exit?
pneubeck (no reviews)
2013/01/24 13:35:02
Done.
| |
68 " %i File is valid without warnings.\n" | |
69 " %i File is valid with warnings,\n" | |
70 " i.e. there were errors which were degraded to warnings.\n" | |
71 " %i File is invalid.\n" | |
72 " %i File couldn't be read or is not a valid JSON dictionary.\n" | |
73 " %i Some command line arguments are wrong.\n", | |
74 kStatusValid, | |
75 kStatusWarnings, | |
76 kStatusInvalid, | |
77 kStatusJsonError, | |
78 kStatusArgumentError); | |
79 } | |
80 | |
81 scoped_ptr<base::DictionaryValue> ReadDictionary(std::string filename) { | |
82 FilePath path(filename); | |
83 JSONFileValueSerializer serializer(path); | |
84 serializer.set_allow_trailing_comma(true); | |
85 | |
86 base::DictionaryValue* dict = NULL; | |
87 | |
88 std::string json_error; | |
89 base::Value* value = serializer.Deserialize(NULL, &json_error); | |
90 if (!value) { | |
91 LOG(ERROR) << "Couldn't json-deserialize file '" << filename | |
92 << "': " << json_error; | |
93 return make_scoped_ptr(dict); | |
94 } | |
95 | |
96 if (!value->GetAsDictionary(&dict)) { | |
97 LOG(ERROR) << "File '" << filename | |
98 << "' does not contain a dictionary as expected, but type " | |
99 << value->GetType(); | |
100 } | |
101 | |
102 return make_scoped_ptr(dict); | |
103 } | |
104 | |
105 int main(int argc, const char* argv[]) { | |
106 CommandLine::Init(argc, argv); | |
107 | |
108 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
109 CommandLine::StringVector args = command_line.GetArgs(); | |
110 if (args.size() != 2) { | |
111 PrintHelp(); | |
112 return kStatusArgumentError; | |
113 } | |
114 | |
115 scoped_ptr<base::DictionaryValue> onc_object = ReadDictionary(args[1]); | |
116 | |
117 if (!onc_object) | |
118 return kStatusJsonError; | |
119 | |
120 chromeos::onc::Validator validator( | |
121 command_line.HasSwitch(kSwitchErrorOnUnknownField), | |
122 command_line.HasSwitch(kSwitchErrorOnWrongRecommended), | |
123 command_line.HasSwitch(kSwitchErrorOnMissingField), | |
124 command_line.HasSwitch(kSwitchManagedOnc)); | |
125 | |
126 if (command_line.HasSwitch(kSwitchUserPolicy)) | |
127 validator.SetOncSource(chromeos::onc::ONC_SOURCE_USER_POLICY); | |
128 else if (command_line.HasSwitch(kSwitchDevicePolicy)) | |
129 validator.SetOncSource(chromeos::onc::ONC_SOURCE_DEVICE_POLICY); | |
130 else if (command_line.HasSwitch(kSwitchUserImport)) | |
131 validator.SetOncSource(chromeos::onc::ONC_SOURCE_USER_IMPORT); | |
132 | |
133 std::string type_arg(args[0]); | |
134 const chromeos::onc::OncValueSignature* signature = NULL; | |
135 if (type_arg == kToplevelConfiguration) { | |
136 signature = &chromeos::onc::kToplevelConfigurationSignature; | |
137 } else if (type_arg == kNetworkConfiguration) { | |
138 signature = &chromeos::onc::kNetworkConfigurationSignature; | |
139 } else if (type_arg == kCertificate) { | |
140 signature = &chromeos::onc::kCertificateSignature; | |
141 } else { | |
142 LOG(ERROR) << "Unknown ONC type '" << type_arg << "'"; | |
143 return kStatusArgumentError; | |
144 } | |
145 | |
146 chromeos::onc::Validator::Result result; | |
147 validator.ValidateAndRepairObject(signature, *onc_object, &result); | |
148 | |
149 switch(result) { | |
150 case chromeos::onc::Validator::VALID: | |
151 return kStatusValid; | |
152 case chromeos::onc::Validator::VALID_WITH_WARNINGS: | |
153 return kStatusWarnings; | |
154 case chromeos::onc::Validator::INVALID: | |
155 return kStatusInvalid; | |
156 default: | |
157 CHECK(false); | |
158 } | |
159 } | |
OLD | NEW |