-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidate_quality_assurance.py
237 lines (203 loc) · 7.01 KB
/
validate_quality_assurance.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/env python3.9
import os
import util
import argparse
import sys
import zlib
import re
import zxing
import json
from datetime import datetime
from pyzbar.pyzbar import decode
from PIL import Image
from base45 import b45decode
from cbor2 import loads
from cose.messages import Sign1Message
from classes.SchemaValidator import SchemaValidator
from classes.SignatureValidator import SignatureValidator
from classes.TrustList import TrustList, UnknownKidError
# Initialize components
SCHEMA_VALIDATOR = SchemaValidator.create()
SIGNATURE_VALIDATOR = SignatureValidator(TrustList.load("trustlist.json"))
CLI_PARSER = argparse.ArgumentParser()
# Global state
SCRIPT_NAME = 'validate_quality_assurance.py'
ALLOWED_EXT = ['.PNG']
SCHEMA_FILE_PATH = 'DGC.combined-schema.json'
# Various flags which need to be turned into
VERBOSE = True
VALID_IF_ANY_VALID_CASE_FOUND = True
EXPECTED_FAILURES = 0
def validate(path):
results = dict()
for country in countries:
validate_country(path, country, results)
return results
def date_time_serializer(obj):
# It's sad that this is needed, but hey!
if isinstance(obj, datetime):
return obj.isoformat()
return None
def unpack_qr_text(qr_text):
compressed_bytes = b45decode(qr_text[4:])
cose_bytes = zlib.decompress(compressed_bytes)
cose_message = Sign1Message.decode(cose_bytes)
cbor_message = loads(cose_message.payload)
print(cbor_message)
payload_object = cbor_message[-260][1]
return {
"COSE": cose_bytes.hex(),
"CBOR": cbor_message,
"COSE_MESSAGE": cose_message,
"PAYLOAD_OBJECT": payload_object,
"PAYLOAD_JSON": json.dumps(payload_object, default=date_time_serializer)
}
def validate_country(path, country, results):
print(f"Validating {country}..")
results[country] = dict()
results[country]["passed"] = list()
results[country]["failed"] = list()
results[country]["skipped"] = list()
# try:
files = [f for f in util.walk_path(path, country)
if os.path.splitext(f)[1].upper() in ALLOWED_EXT]
if len(files) == 0:
results[country]["valid"] = False
results[country]["exception"] = "No test cases found."
return
for file in files:
validate_png(country, file, results)
if len(results[country]["failed"]) > 0:
results[country]["valid"] = False
else:
results[country]["valid"] = True
def get_version(file):
pattern = re.compile("\\d\\.\\d\\.\\d")
result = pattern.search(file)
return result.group(0)
def read_qr_zxing(file):
reader = zxing.BarCodeReader()
barcode = reader.decode(file)
return barcode.raw
def read_qr_pyzbar(file):
barcode = decode(Image.open(file))[0]
return barcode.data.decode("utf-8")
def validate_png(country, file, results):
print(f" File: {file}")
unpacked = dict()
try:
version = get_version(file)
qr_data = read_qr_pyzbar(file)
if qr_data is None or qr_data == '':
results[country]["failed"].append({
"file": file,
"exception": "Unable to read QR",
"json": "",
"cbor": "",
"cose_msg": ""
})
return
compressed_bytes = b45decode(qr_data[4:])
cose_payload = zlib.decompress(compressed_bytes)
unpacked = unpack_qr_text(qr_data)
result_schema_val = SCHEMA_VALIDATOR.validate_dcc(unpacked["PAYLOAD_OBJECT"], version)
result_sig_val = SIGNATURE_VALIDATOR.validate(cose_payload)
if result_schema_val["valid"] and result_sig_val["valid"]:
results[country]["passed"].append(file)
else:
results[country]["failed"].append({
"file": file,
"schema": result_schema_val,
"signature": result_sig_val,
"json": unpacked["PAYLOAD_JSON"]
})
except UnknownKidError as e:
result = {
"file": file,
"exception": e
}
results[country]["failed"].append(result)
except Exception as e:
#
result = {
"file": file,
"exception": e
}
if "PAYLOAD_JSON" in unpacked:
result["json"] = unpacked["PAYLOAD_JSON"]
if "CBOR" in unpacked:
result["cbor"] = unpacked["CBOR"]
if "CBOR" in unpacked:
result["cose_msg"] = unpacked["COSE_MESSAGE"]
results[country]["failed"].append(result)
def info(message):
if VERBOSE:
print(message)
CLI_PARSER.add_argument(
'--repo',
type=str,
help='Path to the repository containing the test cases',
default="..\\dcc-quality-assurance")
CLI_PARSER.add_argument(
'--countries',
type=str,
nargs="?",
help='Optional string containing a comma-separated list of countries,' +
' e.g. "NL,DE,ES"')
args = CLI_PARSER.parse_args()
if args.repo is None:
CLI_PARSER.print_help()
sys.exit()
root_directory = args.repo
if args.countries is None:
print(root_directory)
countries = util.list_country_directories(root_directory)
else:
countries = args.countries.split(",")
print(f"Starting validation of the following countries: {countries}")
validation_results = validate(root_directory)
print("Validation complete.")
print("Validation results:")
for c in sorted(validation_results.keys()):
total_passed = len(validation_results[c]["passed"])
total_skipped = len(validation_results[c]["skipped"])
TOTAL_FAILED = len(validation_results[c]["failed"])
if validation_results[c]["valid"]:
print(
f" {c} ✅ | passed {total_passed} failed {TOTAL_FAILED} skipped" +
" {total_skipped}.")
else:
print(
f" {c} ❌ | passed {total_passed} failed {TOTAL_FAILED} skipped" +
" {total_skipped}.")
print()
TOTAL_FAILED = 0
for c in sorted(validation_results.keys()):
TOTAL_FAILED = TOTAL_FAILED + len(validation_results[c]["failed"])
if not validation_results[c]["valid"]:
print("Error details")
print()
for e in validation_results[c]["failed"]:
print(f"{ c }")
print(f" {e['file']}")
if "exception" in e:
print(" General error:")
print(f" { e['exception'] }")
if "schema" in e and not e["schema"]["valid"]:
print(" Schema error:")
for se in e["schema"]["errors"]:
print(f' { se["error"] }')
if "signature" in e and not e["signature"]["valid"]:
print(" Signature error:")
print(f" { e['signature']['error']['message'] }")
if "json" in e:
print(" JSON:")
print(f" {e['json']}")
if "cbor" in e:
print(" CBOR:")
print(f" {e['cbor']}")
if "cose_msg" in e:
print(" COSE message:")
print(f" {e['cose_msg']}")
if TOTAL_FAILED == 0:
print("Everything succeeded!")