|
| 1 | +""" |
| 2 | +Test support for the 'errors' field in MobileDocument. |
| 3 | +
|
| 4 | +ISO 18013-5 specifies that when status != 0, documents may contain |
| 5 | +an 'errors' field describing which elements were not available. |
| 6 | +""" |
| 7 | + |
| 8 | +from pymdoccbor.mdoc.verifier import MobileDocument |
| 9 | +from pymdoccbor.mdoc.issuer import MdocCborIssuer |
| 10 | +from pymdoccbor.tests.micov_data import MICOV_DATA |
| 11 | +from pymdoccbor.tests.pkey import PKEY |
| 12 | +from pymdoccbor.tests.cert_data import CERT_DATA |
| 13 | + |
| 14 | + |
| 15 | +def test_mobile_document_with_errors_field(): |
| 16 | + """Test that MobileDocument accepts an 'errors' field.""" |
| 17 | + mdoc = MdocCborIssuer( |
| 18 | + private_key=PKEY, |
| 19 | + alg="ES256", |
| 20 | + cert_info=CERT_DATA |
| 21 | + ) |
| 22 | + mdoc.new( |
| 23 | + data=MICOV_DATA, |
| 24 | + doctype="org.micov.medical.1", |
| 25 | + validity={ |
| 26 | + "issuance_date": "2024-12-31", |
| 27 | + "expiry_date": "2050-12-31" |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + document = mdoc.signed["documents"][0] |
| 32 | + |
| 33 | + # Add errors field (simulating status 20 - elements not present) |
| 34 | + document['errors'] = { |
| 35 | + 'org.micov.medical.1': { |
| 36 | + 'missing_element': 1 # Error code for element not present |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + # Should not raise TypeError |
| 41 | + doc = MobileDocument(**document) |
| 42 | + |
| 43 | + assert doc.doctype == "org.micov.medical.1" |
| 44 | + assert doc.errors is not None |
| 45 | + assert isinstance(doc.errors, dict) |
| 46 | + |
| 47 | + |
| 48 | +def test_mobile_document_without_errors_field(): |
| 49 | + """Test that MobileDocument works without 'errors' field (backward compatibility).""" |
| 50 | + mdoc = MdocCborIssuer( |
| 51 | + private_key=PKEY, |
| 52 | + alg="ES256", |
| 53 | + cert_info=CERT_DATA |
| 54 | + ) |
| 55 | + mdoc.new( |
| 56 | + data=MICOV_DATA, |
| 57 | + doctype="org.micov.medical.1", |
| 58 | + validity={ |
| 59 | + "issuance_date": "2024-12-31", |
| 60 | + "expiry_date": "2050-12-31" |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + document = mdoc.signed["documents"][0] |
| 65 | + |
| 66 | + # No errors field |
| 67 | + doc = MobileDocument(**document) |
| 68 | + |
| 69 | + assert doc.doctype == "org.micov.medical.1" |
| 70 | + assert doc.errors == {} # Should default to empty dict |
| 71 | + |
| 72 | + |
| 73 | +def test_mobile_document_dump_with_errors(): |
| 74 | + """Test that dump() includes errors field when present.""" |
| 75 | + mdoc = MdocCborIssuer( |
| 76 | + private_key=PKEY, |
| 77 | + alg="ES256", |
| 78 | + cert_info=CERT_DATA |
| 79 | + ) |
| 80 | + mdoc.new( |
| 81 | + data=MICOV_DATA, |
| 82 | + doctype="org.micov.medical.1", |
| 83 | + validity={ |
| 84 | + "issuance_date": "2024-12-31", |
| 85 | + "expiry_date": "2050-12-31" |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + document = mdoc.signed["documents"][0] |
| 90 | + |
| 91 | + # Add errors field |
| 92 | + errors_data = { |
| 93 | + 'org.micov.medical.1': { |
| 94 | + 'missing_element': 1 |
| 95 | + } |
| 96 | + } |
| 97 | + document['errors'] = errors_data |
| 98 | + |
| 99 | + doc = MobileDocument(**document) |
| 100 | + dump = doc.dump() |
| 101 | + |
| 102 | + assert dump |
| 103 | + assert isinstance(dump, bytes) |
| 104 | + |
| 105 | + # Decode and verify errors field is present |
| 106 | + import cbor2 |
| 107 | + decoded = cbor2.loads(dump) |
| 108 | + # The dump is wrapped in a CBORTag, so we need to access .value |
| 109 | + if hasattr(decoded, 'value'): |
| 110 | + decoded = decoded.value |
| 111 | + |
| 112 | + assert 'errors' in decoded |
| 113 | + assert decoded['errors'] == errors_data |
| 114 | + |
| 115 | + |
| 116 | +def test_mobile_document_dump_without_errors(): |
| 117 | + """Test that dump() works without errors field (backward compatibility).""" |
| 118 | + mdoc = MdocCborIssuer( |
| 119 | + private_key=PKEY, |
| 120 | + alg="ES256", |
| 121 | + cert_info=CERT_DATA |
| 122 | + ) |
| 123 | + mdoc.new( |
| 124 | + data=MICOV_DATA, |
| 125 | + doctype="org.micov.medical.1", |
| 126 | + validity={ |
| 127 | + "issuance_date": "2024-12-31", |
| 128 | + "expiry_date": "2050-12-31" |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + document = mdoc.signed["documents"][0] |
| 133 | + doc = MobileDocument(**document) |
| 134 | + |
| 135 | + dump = doc.dump() |
| 136 | + |
| 137 | + assert dump |
| 138 | + assert isinstance(dump, bytes) |
| 139 | + |
| 140 | + # Decode and verify errors field is NOT present |
| 141 | + import cbor2 |
| 142 | + decoded = cbor2.loads(dump) |
| 143 | + if hasattr(decoded, 'value'): |
| 144 | + decoded = decoded.value |
| 145 | + |
| 146 | + # errors field should not be in dump if it's empty |
| 147 | + assert 'errors' not in decoded |
0 commit comments