-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from dajiaji/add-test-for-mac
Add test for MAC.
- Loading branch information
Showing
14 changed files
with
169 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# pylint: disable=R0201, R0904, W0621 | ||
# R0201: Method could be a function | ||
# R0904: Too many public methods | ||
# W0621: Redefined outer name | ||
|
||
""" | ||
Tests for CWT. | ||
""" | ||
# import cbor2 | ||
import pytest | ||
|
||
# from secrets import token_bytes | ||
|
||
from cwt import CWT, claims, cose_key, VerifyError | ||
|
||
# from .utils import key_path | ||
|
||
|
||
class TestCWT: | ||
""" | ||
Tests for CWT. | ||
""" | ||
|
||
def test_cwt_constructor(self): | ||
"""""" | ||
c = CWT() | ||
assert isinstance(c, CWT) | ||
|
||
def test_cwt_encode_and_mac_with_default_alg(self): | ||
"""""" | ||
c = CWT() | ||
key = cose_key.from_symmetric_key("mysecretpassword") | ||
token = c.encode_and_mac( | ||
{1: "https://as.example", 2: "someone", 7: b"123"}, key | ||
) | ||
decoded = c.decode(token, key) | ||
assert 1 in decoded and decoded[1] == "https://as.example" | ||
assert 2 in decoded and decoded[2] == "someone" | ||
assert 2 in decoded and decoded[7] == b"123" | ||
|
||
@pytest.mark.parametrize( | ||
"alg", | ||
[ | ||
"HMAC 256/64", | ||
"HMAC 256/256", | ||
"HMAC 384/384", | ||
"HMAC 512/512", | ||
], | ||
) | ||
def test_cwt_encode_and_mac_with_valid_alg(self, alg): | ||
"""""" | ||
c = CWT() | ||
key = cose_key.from_symmetric_key("mysecretpassword", alg=alg) | ||
token = c.encode_and_mac( | ||
{1: "https://as.example", 2: "someone", 7: b"123"}, key | ||
) | ||
decoded = c.decode(token, key) | ||
assert 1 in decoded and decoded[1] == "https://as.example" | ||
assert 2 in decoded and decoded[2] == "someone" | ||
assert 2 in decoded and decoded[7] == b"123" | ||
|
||
def test_cwt_decode_with_invalid_mac_key(self): | ||
"""""" | ||
c = CWT() | ||
key = cose_key.from_symmetric_key("mysecretpassword") | ||
token = c.encode_and_mac( | ||
{1: "https://as.example", 2: "someone", 7: b"123"}, key | ||
) | ||
wrong_key = cose_key.from_symmetric_key("xxxxxxxxxx") | ||
with pytest.raises(VerifyError) as err: | ||
res = c.decode(token, wrong_key) | ||
pytest.fail("decode should be fail: res=%s" % vars(res)) | ||
assert "Failed to compare digest" in str(err.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# pylint: disable=R0201, R0904, W0621 | ||
# R0201: Method could be a function | ||
# R0904: Too many public methods | ||
# W0621: Redefined outer name | ||
|
||
""" | ||
Tests for KeyBuilder. | ||
""" | ||
import pytest | ||
|
||
# from secrets import token_bytes | ||
|
||
from cwt import COSEKey, KeyBuilder | ||
# from .utils import key_path | ||
|
||
|
||
class TestKeyBuilder: | ||
""" | ||
Tests for KeyBuilder. | ||
""" | ||
|
||
def test_key_builder_constructor(self): | ||
"""""" | ||
c = KeyBuilder() | ||
assert isinstance(c, KeyBuilder) | ||
|
||
@pytest.mark.parametrize( | ||
"alg", | ||
[ | ||
"HMAC 256/64", | ||
"HMAC 256/256", | ||
"HMAC 384/384", | ||
"HMAC 512/512", | ||
], | ||
) | ||
def test_cwt_encode_and_mac_with_valid_alg(self, alg): | ||
"""""" | ||
kb = KeyBuilder() | ||
k = kb.from_symmetric_key("mysecretpassword", alg=alg) | ||
assert isinstance(k, COSEKey) | ||
|
||
def test_key_builder_from_symmetric_key_with_invalid_alg(self): | ||
"""""" | ||
kb = KeyBuilder() | ||
with pytest.raises(ValueError) as err: | ||
res = kb.from_symmetric_key("mysecretpassword", alg="xxx") | ||
pytest.fail("from_symmetric_key should be fail: res=%s" % vars(res)) | ||
assert "Unsupported or unknown alg" in str(err.value) |