| 128 | |
| 129 | |
| 130 | class TestECAlgorithm: |
| 131 | def test_key_from_pem(self): |
| 132 | assert not ECKey(private_key, ALGORITHMS.ES256).is_public() |
| 133 | |
| 134 | def test_to_pem(self): |
| 135 | key = ECKey(private_key, ALGORITHMS.ES256) |
| 136 | assert not key.is_public() |
| 137 | assert normalize_pem(get_pem_for_key(key)) == normalize_pem(private_key.strip()) |
| 138 | |
| 139 | public_pem = key.public_key().to_pem() |
| 140 | assert ECKey(public_pem, ALGORITHMS.ES256).is_public() |
| 141 | |
| 142 | @pytest.mark.parametrize("Backend,ExceptionType", _backend_exception_types()) |
| 143 | def test_key_too_short(self, Backend, ExceptionType): |
| 144 | key = Backend(TOO_SHORT_PRIVATE_KEY, ALGORITHMS.ES512) |
| 145 | with pytest.raises(ExceptionType): |
| 146 | key.sign(b"foo") |
| 147 | |
| 148 | def test_get_public_key(self): |
| 149 | key = ECKey(private_key, ALGORITHMS.ES256) |
| 150 | pubkey = key.public_key() |
| 151 | pubkey2 = pubkey.public_key() |
| 152 | assert pubkey == pubkey2 |
| 153 | |
| 154 | def test_string_secret(self): |
| 155 | key = "secret" |
| 156 | with pytest.raises(JOSEError): |
| 157 | ECKey(key, ALGORITHMS.ES256) |
| 158 | |
| 159 | def test_object(self): |
| 160 | key = object() |
| 161 | with pytest.raises(JOSEError): |
| 162 | ECKey(key, ALGORITHMS.ES256) |
| 163 | |
| 164 | def test_invalid_algorithm(self): |
| 165 | with pytest.raises(JWKError): |
| 166 | ECKey(private_key, "nonexistent") |
| 167 | |
| 168 | with pytest.raises(JWKError): |
| 169 | ECKey({"kty": "bla"}, ALGORITHMS.ES256) |
| 170 | |
| 171 | def test_EC_jwk(self): |
| 172 | key = { |
| 173 | "kty": "EC", |
| 174 | "kid": "bilbo.baggins@hobbiton.example", |
| 175 | "use": "sig", |
| 176 | "crv": "P-521", |
| 177 | "x": "AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt", |
| 178 | "y": "AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1", |
| 179 | "d": "AAhRON2r9cqXX1hg-RoI6R1tX5p2rUAYdmpHZoC1XNM56KtscrX6zbKipQrCW9CGZH3T4ubpnoTKLDYJ_fF3_rJt", |
| 180 | } |
| 181 | |
| 182 | assert not ECKey(key, ALGORITHMS.ES512).is_public() |
| 183 | |
| 184 | del key["d"] |
| 185 | |
| 186 | # We are now dealing with a public key. |
| 187 | assert ECKey(key, ALGORITHMS.ES512).is_public() |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…