11"""
22Tests for Core Deterministic Encoding.
33"""
4- import cbor2
54
5+ import cbor2
66import pytest
77
88from cwt import COSE , COSEKey
9- from cwt .const import COSE_ALGORITHMS_SIGNATURE , COSE_ALGORITHMS_MAC
9+ from cwt .const import COSE_ALGORITHMS_MAC , COSE_ALGORITHMS_SIGNATURE
1010from cwt .utils import sort_keys_for_deterministic_encoding
1111
12+
1213class TestDeterministicEncoding :
1314 """
1415 Tests for Core Deterministic Encoding
1516 """
1617
1718 def test_deterministically_sorted_dict (self ):
1819 expected = {}
19- expected [0 ] = 0 # Reserved
20- expected [1 ] = 0 # alg
21- expected [4 ] = 0 # kid
22- expected [5 ] = 0 # IV
23- expected [7 ] = 0 # counter signature
24- expected [11 ] = 0 # Countersignature version 2
25- expected [12 ] = 0 # Countersignature0 version 2
26- expected [15 ] = 0 # CWT Claims
27- expected [33 ] = 0 # x5chain
28- expected [- 1 ] = 0 # (max of COSE Header Algorithm Parameters resistry)
29- expected [- 65536 ] = 0 # (min of COSE Header Algorithm Parameters resistry)
30- expected [- 65537 ] = 0 # (max of Reserved for Private Use)
20+ expected [0 ] = 0 # Reserved
21+ expected [1 ] = 0 # alg
22+ expected [4 ] = 0 # kid
23+ expected [5 ] = 0 # IV
24+ expected [7 ] = 0 # counter signature
25+ expected [11 ] = 0 # Countersignature version 2
26+ expected [12 ] = 0 # Countersignature0 version 2
27+ expected [15 ] = 0 # CWT Claims
28+ expected [33 ] = 0 # x5chain
29+ expected [- 1 ] = 0 # (max of COSE Header Algorithm Parameters resistry)
30+ expected [- 65536 ] = 0 # (min of COSE Header Algorithm Parameters resistry)
31+ expected [- 65537 ] = 0 # (max of Reserved for Private Use)
3132
3233 d = {}
3334 d [4 ] = 0
@@ -59,25 +60,24 @@ def test_deterministic_cose_sign_binary(self):
5960
6061 # create unsorted protected header
6162 p = {}
62- p ["kid" ] = sign_jwk ["kid" ] # 4
63- p ["alg" ] = sign_jwk ["alg" ] # 1
63+ p ["kid" ] = sign_jwk ["kid" ] # 4
64+ p ["alg" ] = sign_jwk ["alg" ] # 1
6465
6566 ctx = COSE .new (deterministic_header = True )
6667 encoded = ctx .encode_and_sign (
67- payload = b'a' ,
68+ payload = b"a" ,
6869 key = sign_key ,
6970 protected = p ,
7071 )
7172 encoded_p = cbor2 .loads (encoded ).value [0 ]
7273
7374 sorted_p = {}
74- sorted_p [1 ] = COSE_ALGORITHMS_SIGNATURE [sign_jwk ["alg" ]] # -7 for ES256
75- sorted_p [4 ] = str .encode (sign_jwk ["kid" ]) # b'11'
75+ sorted_p [1 ] = COSE_ALGORITHMS_SIGNATURE [sign_jwk ["alg" ]] # -7 for ES256
76+ sorted_p [4 ] = str .encode (sign_jwk ["kid" ]) # b'11'
7677 expected_p = cbor2 .dumps (sorted_p )
7778
7879 assert expected_p == encoded_p
7980
80-
8181 @pytest .mark .parametrize (
8282 "alg" ,
8383 [
@@ -92,12 +92,12 @@ def test_deterministic_cose_mac_binary(self, alg):
9292
9393 # create unsorted protected header
9494 p = {}
95- p ["kid" ] = "01" # 4
96- p ["alg" ] = alg # 1
95+ p ["kid" ] = "01" # 4
96+ p ["alg" ] = alg # 1
9797
9898 ctx = COSE .new (deterministic_header = True )
9999 encoded = ctx .encode_and_mac (
100- payload = b'a' ,
100+ payload = b"a" ,
101101 key = mac_key ,
102102 protected = p ,
103103 )
0 commit comments