OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 Google Inc. All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 # |
| 30 # Inspector protocol validator. |
| 31 # |
| 32 # Tests that subsequent protocol changes are not breaking backwards compatibilit
y. |
| 33 # Following violations are reported: |
| 34 # |
| 35 # - Domain has been removed |
| 36 # - Command has been removed |
| 37 # - Required command parameter was added or changed from optional |
| 38 # - Required response parameter was removed or changed to optional |
| 39 # - Event has been removed |
| 40 # - Required event parameter was removed or changed to optional |
| 41 # - Parameter type has changed. |
| 42 # |
| 43 # For the parameters with composite types the above checks are also applied |
| 44 # recursively to every property of the type. |
| 45 # |
| 46 # Adding --show_changes to the command line prints out a list of valid public AP
I changes. |
| 47 |
| 48 import copy |
| 49 import os.path |
| 50 import optparse |
| 51 import sys |
| 52 |
| 53 try: |
| 54 import json |
| 55 except ImportError: |
| 56 import simplejson as json |
| 57 |
| 58 |
| 59 def list_to_map(items, key): |
| 60 result = {} |
| 61 for item in items: |
| 62 if "experimental" not in item and "hidden" not in item: |
| 63 result[item[key]] = item |
| 64 return result |
| 65 |
| 66 |
| 67 def named_list_to_map(container, name, key): |
| 68 if name in container: |
| 69 return list_to_map(container[name], key) |
| 70 return {} |
| 71 |
| 72 |
| 73 def removed(reverse): |
| 74 if reverse: |
| 75 return "added" |
| 76 return "removed" |
| 77 |
| 78 |
| 79 def required(reverse): |
| 80 if reverse: |
| 81 return "optional" |
| 82 return "required" |
| 83 |
| 84 |
| 85 def compare_schemas(d_1, d_2, reverse): |
| 86 errors = [] |
| 87 domains_1 = copy.deepcopy(d_1) |
| 88 domains_2 = copy.deepcopy(d_2) |
| 89 types_1 = normalize_types_in_schema(domains_1) |
| 90 types_2 = normalize_types_in_schema(domains_2) |
| 91 |
| 92 domains_by_name_1 = list_to_map(domains_1, "domain") |
| 93 domains_by_name_2 = list_to_map(domains_2, "domain") |
| 94 |
| 95 for name in domains_by_name_1: |
| 96 domain_1 = domains_by_name_1[name] |
| 97 if name not in domains_by_name_2: |
| 98 errors.append("%s: domain has been %s" % (name, removed(reverse))) |
| 99 continue |
| 100 compare_domains(domain_1, domains_by_name_2[name], types_1, types_2, err
ors, reverse) |
| 101 return errors |
| 102 |
| 103 |
| 104 def compare_domains(domain_1, domain_2, types_map_1, types_map_2, errors, revers
e): |
| 105 domain_name = domain_1["domain"] |
| 106 commands_1 = named_list_to_map(domain_1, "commands", "name") |
| 107 commands_2 = named_list_to_map(domain_2, "commands", "name") |
| 108 for name in commands_1: |
| 109 command_1 = commands_1[name] |
| 110 if name not in commands_2: |
| 111 errors.append("%s.%s: command has been %s" % (domain_1["domain"], na
me, removed(reverse))) |
| 112 continue |
| 113 compare_commands(domain_name, command_1, commands_2[name], types_map_1,
types_map_2, errors, reverse) |
| 114 |
| 115 events_1 = named_list_to_map(domain_1, "events", "name") |
| 116 events_2 = named_list_to_map(domain_2, "events", "name") |
| 117 for name in events_1: |
| 118 event_1 = events_1[name] |
| 119 if name not in events_2: |
| 120 errors.append("%s.%s: event has been %s" % (domain_1["domain"], name
, removed(reverse))) |
| 121 continue |
| 122 compare_events(domain_name, event_1, events_2[name], types_map_1, types_
map_2, errors, reverse) |
| 123 |
| 124 |
| 125 def compare_commands(domain_name, command_1, command_2, types_map_1, types_map_2
, errors, reverse): |
| 126 context = domain_name + "." + command_1["name"] |
| 127 |
| 128 params_1 = named_list_to_map(command_1, "parameters", "name") |
| 129 params_2 = named_list_to_map(command_2, "parameters", "name") |
| 130 # Note the reversed order: we allow removing but forbid adding parameters. |
| 131 compare_params_list(context, "parameter", params_2, params_1, types_map_2, t
ypes_map_1, 0, errors, not reverse) |
| 132 |
| 133 returns_1 = named_list_to_map(command_1, "returns", "name") |
| 134 returns_2 = named_list_to_map(command_2, "returns", "name") |
| 135 compare_params_list(context, "response parameter", returns_1, returns_2, typ
es_map_1, types_map_2, 0, errors, reverse) |
| 136 |
| 137 |
| 138 def compare_events(domain_name, event_1, event_2, types_map_1, types_map_2, erro
rs, reverse): |
| 139 context = domain_name + "." + event_1["name"] |
| 140 params_1 = named_list_to_map(event_1, "parameters", "name") |
| 141 params_2 = named_list_to_map(event_2, "parameters", "name") |
| 142 compare_params_list(context, "parameter", params_1, params_2, types_map_1, t
ypes_map_2, 0, errors, reverse) |
| 143 |
| 144 |
| 145 def compare_params_list(context, kind, params_1, params_2, types_map_1, types_ma
p_2, depth, errors, reverse): |
| 146 for name in params_1: |
| 147 param_1 = params_1[name] |
| 148 if name not in params_2: |
| 149 if "optional" not in param_1: |
| 150 errors.append("%s.%s: required %s has been %s" % (context, name,
kind, removed(reverse))) |
| 151 continue |
| 152 |
| 153 param_2 = params_2[name] |
| 154 if param_2 and "optional" in param_2 and "optional" not in param_1: |
| 155 errors.append("%s.%s: %s %s is now %s" % (context, name, required(re
verse), kind, required(not reverse))) |
| 156 continue |
| 157 type_1 = extract_type(param_1, types_map_1, errors) |
| 158 type_2 = extract_type(param_2, types_map_2, errors) |
| 159 compare_types(context + "." + name, kind, type_1, type_2, types_map_1, t
ypes_map_2, depth, errors, reverse) |
| 160 |
| 161 |
| 162 def compare_types(context, kind, type_1, type_2, types_map_1, types_map_2, depth
, errors, reverse): |
| 163 if depth > 10: |
| 164 return |
| 165 |
| 166 base_type_1 = type_1["type"] |
| 167 base_type_2 = type_2["type"] |
| 168 |
| 169 if base_type_1 != base_type_2: |
| 170 errors.append("%s: %s base type mismatch, '%s' vs '%s'" % (context, kind
, base_type_1, base_type_2)) |
| 171 elif base_type_1 == "object": |
| 172 params_1 = named_list_to_map(type_1, "properties", "name") |
| 173 params_2 = named_list_to_map(type_2, "properties", "name") |
| 174 # If both parameters have the same named type use it in the context. |
| 175 if "id" in type_1 and "id" in type_2 and type_1["id"] == type_2["id"]: |
| 176 type_name = type_1["id"] |
| 177 else: |
| 178 type_name = "<object>" |
| 179 context += " %s->%s" % (kind, type_name) |
| 180 compare_params_list(context, "property", params_1, params_2, types_map_1
, types_map_2, depth + 1, errors, reverse) |
| 181 elif base_type_1 == "array": |
| 182 item_type_1 = extract_type(type_1["items"], types_map_1, errors) |
| 183 item_type_2 = extract_type(type_2["items"], types_map_2, errors) |
| 184 compare_types(context, kind, item_type_1, item_type_2, types_map_1, type
s_map_2, depth + 1, errors, reverse) |
| 185 |
| 186 |
| 187 def extract_type(typed_object, types_map, errors): |
| 188 if "type" in typed_object: |
| 189 result = {"id": "<transient>", "type": typed_object["type"]} |
| 190 if typed_object["type"] == "object": |
| 191 result["properties"] = [] |
| 192 elif typed_object["type"] == "array": |
| 193 result["items"] = typed_object["items"] |
| 194 return result |
| 195 elif "$ref" in typed_object: |
| 196 ref = typed_object["$ref"] |
| 197 if ref not in types_map: |
| 198 errors.append("Can not resolve type: %s" % ref) |
| 199 types_map[ref] = {"id": "<transient>", "type": "object"} |
| 200 return types_map[ref] |
| 201 |
| 202 |
| 203 def normalize_types_in_schema(domains): |
| 204 types = {} |
| 205 for domain in domains: |
| 206 domain_name = domain["domain"] |
| 207 normalize_types(domain, domain_name, types) |
| 208 return types |
| 209 |
| 210 |
| 211 def normalize_types(obj, domain_name, types): |
| 212 if isinstance(obj, list): |
| 213 for item in obj: |
| 214 normalize_types(item, domain_name, types) |
| 215 elif isinstance(obj, dict): |
| 216 for key, value in obj.items(): |
| 217 if key == "$ref" and value.find(".") == -1: |
| 218 obj[key] = "%s.%s" % (domain_name, value) |
| 219 elif key == "id": |
| 220 obj[key] = "%s.%s" % (domain_name, value) |
| 221 types[obj[key]] = obj |
| 222 else: |
| 223 normalize_types(value, domain_name, types) |
| 224 |
| 225 |
| 226 def load_schema(file_name, domains): |
| 227 # pylint: disable=W0613 |
| 228 if not os.path.isfile(file_name): |
| 229 return |
| 230 input_file = open(file_name, "r") |
| 231 json_string = input_file.read() |
| 232 parsed_json = json.loads(json_string) |
| 233 domains += parsed_json["domains"] |
| 234 return parsed_json["version"] |
| 235 |
| 236 |
| 237 def self_test(): |
| 238 def create_test_schema_1(): |
| 239 return [ |
| 240 { |
| 241 "domain": "Network", |
| 242 "types": [ |
| 243 { |
| 244 "id": "LoaderId", |
| 245 "type": "string" |
| 246 }, |
| 247 { |
| 248 "id": "Headers", |
| 249 "type": "object" |
| 250 }, |
| 251 { |
| 252 "id": "Request", |
| 253 "type": "object", |
| 254 "properties": [ |
| 255 {"name": "url", "type": "string"}, |
| 256 {"name": "method", "type": "string"}, |
| 257 {"name": "headers", "$ref": "Headers"}, |
| 258 {"name": "becameOptionalField", "type": "string"}, |
| 259 {"name": "removedField", "type": "string"}, |
| 260 ] |
| 261 } |
| 262 ], |
| 263 "commands": [ |
| 264 { |
| 265 "name": "removedCommand", |
| 266 }, |
| 267 { |
| 268 "name": "setExtraHTTPHeaders", |
| 269 "parameters": [ |
| 270 {"name": "headers", "$ref": "Headers"}, |
| 271 {"name": "mismatched", "type": "string"}, |
| 272 {"name": "becameOptional", "$ref": "Headers"}, |
| 273 {"name": "removedRequired", "$ref": "Headers"}, |
| 274 {"name": "becameRequired", "$ref": "Headers", "optio
nal": True}, |
| 275 {"name": "removedOptional", "$ref": "Headers", "opti
onal": True}, |
| 276 ], |
| 277 "returns": [ |
| 278 {"name": "mimeType", "type": "string"}, |
| 279 {"name": "becameOptional", "type": "string"}, |
| 280 {"name": "removedRequired", "type": "string"}, |
| 281 {"name": "becameRequired", "type": "string", "option
al": True}, |
| 282 {"name": "removedOptional", "type": "string", "optio
nal": True}, |
| 283 ] |
| 284 } |
| 285 ], |
| 286 "events": [ |
| 287 { |
| 288 "name": "requestWillBeSent", |
| 289 "parameters": [ |
| 290 {"name": "frameId", "type": "string", "experimental"
: True}, |
| 291 {"name": "request", "$ref": "Request"}, |
| 292 {"name": "becameOptional", "type": "string"}, |
| 293 {"name": "removedRequired", "type": "string"}, |
| 294 {"name": "becameRequired", "type": "string", "option
al": True}, |
| 295 {"name": "removedOptional", "type": "string", "optio
nal": True}, |
| 296 ] |
| 297 }, |
| 298 { |
| 299 "name": "removedEvent", |
| 300 "parameters": [ |
| 301 {"name": "errorText", "type": "string"}, |
| 302 {"name": "canceled", "type": "boolean", "optional":
True} |
| 303 ] |
| 304 } |
| 305 ] |
| 306 }, |
| 307 { |
| 308 "domain": "removedDomain" |
| 309 } |
| 310 ] |
| 311 |
| 312 def create_test_schema_2(): |
| 313 return [ |
| 314 { |
| 315 "domain": "Network", |
| 316 "types": [ |
| 317 { |
| 318 "id": "LoaderId", |
| 319 "type": "string" |
| 320 }, |
| 321 { |
| 322 "id": "Request", |
| 323 "type": "object", |
| 324 "properties": [ |
| 325 {"name": "url", "type": "string"}, |
| 326 {"name": "method", "type": "string"}, |
| 327 {"name": "headers", "type": "object"}, |
| 328 {"name": "becameOptionalField", "type": "string", "o
ptional": True}, |
| 329 ] |
| 330 } |
| 331 ], |
| 332 "commands": [ |
| 333 { |
| 334 "name": "addedCommand", |
| 335 }, |
| 336 { |
| 337 "name": "setExtraHTTPHeaders", |
| 338 "parameters": [ |
| 339 {"name": "headers", "type": "object"}, |
| 340 {"name": "mismatched", "type": "object"}, |
| 341 {"name": "becameOptional", "type": "object", "option
al": True}, |
| 342 {"name": "addedRequired", "type": "object"}, |
| 343 {"name": "becameRequired", "type": "object"}, |
| 344 {"name": "addedOptional", "type": "object", "optiona
l": True}, |
| 345 ], |
| 346 "returns": [ |
| 347 {"name": "mimeType", "type": "string"}, |
| 348 {"name": "becameOptional", "type": "string", "option
al": True}, |
| 349 {"name": "addedRequired", "type": "string"}, |
| 350 {"name": "becameRequired", "type": "string"}, |
| 351 {"name": "addedOptional", "type": "string", "optiona
l": True}, |
| 352 ] |
| 353 } |
| 354 ], |
| 355 "events": [ |
| 356 { |
| 357 "name": "requestWillBeSent", |
| 358 "parameters": [ |
| 359 {"name": "request", "$ref": "Request"}, |
| 360 {"name": "becameOptional", "type": "string", "option
al": True}, |
| 361 {"name": "addedRequired", "type": "string"}, |
| 362 {"name": "becameRequired", "type": "string"}, |
| 363 {"name": "addedOptional", "type": "string", "optiona
l": True}, |
| 364 ] |
| 365 }, |
| 366 { |
| 367 "name": "addedEvent" |
| 368 } |
| 369 ] |
| 370 }, |
| 371 { |
| 372 "domain": "addedDomain" |
| 373 } |
| 374 ] |
| 375 |
| 376 expected_errors = [ |
| 377 "removedDomain: domain has been removed", |
| 378 "Network.removedCommand: command has been removed", |
| 379 "Network.removedEvent: event has been removed", |
| 380 "Network.setExtraHTTPHeaders.mismatched: parameter base type mismatch, '
object' vs 'string'", |
| 381 "Network.setExtraHTTPHeaders.addedRequired: required parameter has been
added", |
| 382 "Network.setExtraHTTPHeaders.becameRequired: optional parameter is now r
equired", |
| 383 "Network.setExtraHTTPHeaders.removedRequired: required response paramete
r has been removed", |
| 384 "Network.setExtraHTTPHeaders.becameOptional: required response parameter
is now optional", |
| 385 "Network.requestWillBeSent.removedRequired: required parameter has been
removed", |
| 386 "Network.requestWillBeSent.becameOptional: required parameter is now opt
ional", |
| 387 "Network.requestWillBeSent.request parameter->Network.Request.removedFie
ld: required property has been removed", |
| 388 "Network.requestWillBeSent.request parameter->Network.Request.becameOpti
onalField: required property is now optional", |
| 389 ] |
| 390 |
| 391 expected_errors_reverse = [ |
| 392 "addedDomain: domain has been added", |
| 393 "Network.addedEvent: event has been added", |
| 394 "Network.addedCommand: command has been added", |
| 395 "Network.setExtraHTTPHeaders.mismatched: parameter base type mismatch, '
string' vs 'object'", |
| 396 "Network.setExtraHTTPHeaders.removedRequired: required parameter has bee
n removed", |
| 397 "Network.setExtraHTTPHeaders.becameOptional: required parameter is now o
ptional", |
| 398 "Network.setExtraHTTPHeaders.addedRequired: required response parameter
has been added", |
| 399 "Network.setExtraHTTPHeaders.becameRequired: optional response parameter
is now required", |
| 400 "Network.requestWillBeSent.becameRequired: optional parameter is now req
uired", |
| 401 "Network.requestWillBeSent.addedRequired: required parameter has been ad
ded", |
| 402 ] |
| 403 |
| 404 def is_subset(subset, superset, message): |
| 405 for i in range(len(subset)): |
| 406 if subset[i] not in superset: |
| 407 sys.stderr.write("%s error: %s\n" % (message, subset[i])) |
| 408 return False |
| 409 return True |
| 410 |
| 411 def errors_match(expected, actual): |
| 412 return (is_subset(actual, expected, "Unexpected") and |
| 413 is_subset(expected, actual, "Missing")) |
| 414 |
| 415 return (errors_match(expected_errors, |
| 416 compare_schemas(create_test_schema_1(), create_test_sch
ema_2(), False)) and |
| 417 errors_match(expected_errors_reverse, |
| 418 compare_schemas(create_test_schema_2(), create_test_sch
ema_1(), True))) |
| 419 |
| 420 |
| 421 def load_domains_and_baselines(file_name, domains, baseline_domains): |
| 422 version = load_schema(os.path.normpath(file_name), domains) |
| 423 suffix = "-%s.%s.json" % (version["major"], version["minor"]) |
| 424 baseline_file = file_name.replace(".json", suffix) |
| 425 load_schema(os.path.normpath(baseline_file), baseline_domains) |
| 426 return version |
| 427 |
| 428 |
| 429 def main(): |
| 430 if not self_test(): |
| 431 sys.stderr.write("Self-test failed") |
| 432 return 1 |
| 433 |
| 434 cmdline_parser = optparse.OptionParser() |
| 435 cmdline_parser.add_option("--show_changes") |
| 436 cmdline_parser.add_option("--expected_errors") |
| 437 cmdline_parser.add_option("--stamp") |
| 438 arg_options, arg_values = cmdline_parser.parse_args() |
| 439 |
| 440 if len(arg_values) < 1: |
| 441 sys.stderr.write("Usage: %s [--show_changes] <protocol-1> [, <protocol-2
>...]\n" % sys.argv[0]) |
| 442 return 1 |
| 443 |
| 444 domains = [] |
| 445 baseline_domains = [] |
| 446 version = load_domains_and_baselines(arg_values[0], domains, baseline_domain
s) |
| 447 for dependency in arg_values[1:]: |
| 448 load_domains_and_baselines(dependency, domains, baseline_domains) |
| 449 |
| 450 expected_errors = [] |
| 451 if arg_options.expected_errors: |
| 452 expected_errors_file = open(arg_options.expected_errors, "r") |
| 453 expected_errors = json.loads(expected_errors_file.read())["errors"] |
| 454 expected_errors_file.close() |
| 455 |
| 456 errors = compare_schemas(baseline_domains, domains, False) |
| 457 unexpected_errors = [] |
| 458 for i in range(len(errors)): |
| 459 if errors[i] not in expected_errors: |
| 460 unexpected_errors.append(errors[i]) |
| 461 if len(unexpected_errors) > 0: |
| 462 sys.stderr.write(" Compatibility checks FAILED\n") |
| 463 for error in unexpected_errors: |
| 464 sys.stderr.write(" %s\n" % error) |
| 465 return 1 |
| 466 |
| 467 if arg_options.show_changes: |
| 468 changes = compare_schemas(domains, baseline_domains, True) |
| 469 if len(changes) > 0: |
| 470 print " Public changes since %s:" % version |
| 471 for change in changes: |
| 472 print " %s" % change |
| 473 |
| 474 if arg_options.stamp: |
| 475 with open(arg_options.stamp, 'a') as _: |
| 476 pass |
| 477 |
| 478 if __name__ == '__main__': |
| 479 sys.exit(main()) |
OLD | NEW |