def test_recipients_from_list_with_empty_recipients(self): try: Recipients.from_list( [[cbor2.dumps({1: -10}), { -20: b"aabbccddeefff" }, b"", []]]) except Exception: pytest.fail("from_list() should not fail.")
def test_recipients_constructor_with_recipient_alg_direct(self): key = COSEKey.from_symmetric_key("mysecret", alg="HMAC 256/64", kid="our-secret") r = Recipients([Recipient.new(unprotected={1: -6, 4: b"our-secret"})]) key = r.extract([key]) assert key.kty == 4 assert key.alg == 4 assert key.kid == b"our-secret"
def test_recipients_extract_without_key(self): r = Recipients( [RecipientInterface(unprotected={ 1: -6, 4: b"our-secret" })]) with pytest.raises(ValueError) as err: r.extract([]) pytest.fail("extract() should fail.") assert "key is not found." in str(err.value)
def test_recipients_extract_with_different_kid(self): key = COSEKey.from_symmetric_key("mysecret", alg="HMAC 256/64", kid="our-secret") r = Recipients( [RecipientInterface(unprotected={ 1: -6, 4: b"your-secret" })]) with pytest.raises(ValueError) as err: r.extract([key]) pytest.fail("extract() should fail.") assert "key is not found." in str(err.value)
def test_recipients_extract_without_context(self, material): r = Recipients( [ Recipient.new(unprotected={ "alg": "direct+HKDF-SHA-256", "kid": "02" }, ) ], True, ) with pytest.raises(ValueError) as err: r.extract(keys=[material]) pytest.fail("extract() should fail.") assert "context should be set." in str(err.value)
def test_recipients_from_list_with_recipients(self): try: Recipients.from_list([[ cbor2.dumps({1: -10}), { -20: b"aabbccddeefff" }, b"", [[b"", { 1: -6, 4: b"our-secret" }, b""]], ]]) except Exception: pytest.fail("from_list() should not fail.")
def test_recipients_extract_with_multiple_materials( self, material, context): r1 = Recipient.from_jwk({ "alg": "direct", "kid": "01", }) r2 = Recipient.from_jwk({ "alg": "direct+HKDF-SHA-256", "kid": "02", "salt": "aabbccddeeffgghh", }) rs = Recipients([r1, r2]) key = rs.extract(context=context, keys=[material]) assert key.alg == 10 assert key.kid == b"02"
def test_recipients_extract_with_multiple_keys(self, material): mac_key = COSEKey.from_symmetric_key( bytes.fromhex( "DDDC08972DF9BE62855291A17A1B4CF767C2DC762CB551911893BF7754988B0A286127BFF5D60C4CBC877CAC4BF3BA02C07AD544C951C3CA2FC46B70219BC3DC" ), alg="HS512", ) r1 = Recipient.from_jwk({ "kty": "oct", "alg": "A128KW", "kid": "01", }) r2 = Recipient.from_jwk( { "alg": "direct+HKDF-SHA-256", "kid": "02", "salt": "aabbccddeeffgghh", }, ) r3 = Recipient.from_jwk( { "kty": "oct", "alg": "A128KW", "kid": "03", "k": "hJtXIZ2uSN5kbQfbtTNWbg", }, ) k3 = COSEKey.from_jwk( { "kty": "oct", "alg": "A128KW", "kid": "03", "k": "hJtXIZ2uSN5kbQfbtTNWbg", }, ) r3.apply(mac_key) rs = Recipients([r1, r2, r3]) key = rs.extract(keys=[k3], alg=7) assert key.alg == 7 assert key.kid == b"03"
def test_recipients_from_list_with_invalid_args(self, invalid, msg): with pytest.raises(ValueError) as err: Recipients.from_list(invalid) pytest.fail("extract() should fail.") assert msg in str(err.value)
def test_recipients_extract_with_empty_recipients(self, material, context): r = Recipients([]) with pytest.raises(ValueError) as err: r.extract(context=context, keys=[material]) pytest.fail("extract() should fail.") assert "No recipients." in str(err.value)
def test_recipients_constructor(self): r = Recipients([RecipientInterface()]) assert isinstance(r, Recipients)