tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IdnaTestV2-compare.py (2581B)


      1 # This script can be used to compare IdnaTestV2.json across revisions, with some manual labor. It is
      2 # used primarily for review and to add to IdnaTestV2-removed.json, to ensure we never lose a test.
      3 # (IdnaTestV2.json is the output of IdnaTestV2-parser.py.)
      4 
      5 import argparse
      6 import json
      7 
      8 def compute_differences():
      9    data_old = None
     10    data_new = None
     11    with open("IdnaTestV2-old.json", "r") as file_handle:
     12        data_old = json.load(file_handle)
     13 
     14    with open("IdnaTestV2-new.json", "r") as file_handle:
     15        data_new = json.load(file_handle)
     16 
     17    added_tests = []
     18    changed_tests = []
     19    removed_tests = []
     20 
     21    for old_test in data_old:
     22        if isinstance(old_test, str):
     23            continue
     24        found = None
     25        for new_test in data_new:
     26            if isinstance(new_test, str):
     27                continue
     28            if old_test["input"] == new_test["input"]:
     29                found = new_test
     30                break
     31 
     32        if not found:
     33            # We now exclude ? as it's a forbidden domain code point. This check can be removed in the future.
     34            if "?" not in old_test["input"]:
     35                removed_tests.append(old_test)
     36        # For changed tests we only care about parsing no longer succeeding.
     37        elif old_test["output"] != found["output"] and old_test["output"]:
     38            changed_tests.append({ "input": old_test["input"], "output_old": old_test["output"], "output_new": found["output"] })
     39 
     40    for new_test in data_new:
     41        if isinstance(new_test, str):
     42            continue
     43        found = False
     44        for old_test in data_old:
     45            if isinstance(old_test, str):
     46                continue
     47            if new_test["input"] == old_test["input"]:
     48                found = True
     49                break
     50        if not found:
     51            added_tests.append(new_test)
     52 
     53    return { "added": added_tests, "changed": changed_tests, "removed": removed_tests }
     54 
     55 def main():
     56    parser = argparse.ArgumentParser(epilog="Thanks for caring about IDNA!")
     57    group = parser.add_mutually_exclusive_group(required=True)
     58    group.add_argument("--differences", action="store_true", help="Output the differences")
     59    group.add_argument("--removed", action="store_true", help="Output the removed tests only")
     60    args = parser.parse_args()
     61 
     62    differences = compute_differences()
     63 
     64    output = None
     65 
     66    if args.differences:
     67        output = differences
     68    elif args.removed:
     69        output = differences["removed"]
     70 
     71    print(json.dumps(output, sort_keys=True, allow_nan=False, indent=2, separators=(',', ': ')))
     72 
     73 main()