1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-11-24 12:59:22 +00:00

Improve test coverage

This commit is contained in:
Nick Hynes 2020-07-16 03:43:53 +00:00
parent 72368980c4
commit 8014d18bf0
No known key found for this signature in database
GPG Key ID: 5B3463E9F1D73C83
7 changed files with 92 additions and 15 deletions

View File

@ -62,8 +62,6 @@ jobs:
override: true
- uses: actions-rs/tarpaulin@v0.1
with:
args: '--all-features'
- uses: codecov/codecov-action@v1
with:

9
.tarpaulin.toml Normal file
View File

@ -0,0 +1,9 @@
[default_coverage]
exclude-files = ["target"]
all-features = true
locked = true
target-dir = "target/tarpaulin"
[report]
out = ["Html", "Xml"]
output-dir = "target/tarpaulin"

View File

@ -1,6 +1,6 @@
[package]
name = "jsonwebkey"
version = "0.1.0"
version = "0.2.0"
authors = ["Nick Hynes <nhynes@nhynes.com>"]
description = "JSON Web Key (JWK) (de)serialization, generation, and conversion."
readme = "README.md"

View File

@ -40,8 +40,6 @@ println!("{:#?}", the_jwk); // looks like `jwt_str` but with reordered fields.
### Using with other crates
*Note:* The following example requires the `jwt-convert` feature.
```rust
#[cfg(all(feature = "generate", feature = "jwt-convert"))] {
extern crate jsonwebtoken as jwt;

View File

@ -54,3 +54,21 @@ impl_key_ops!(
(deriveKey, DERIVE_KEY, 0b01000000),
(deriveBits, DERIVE_BITS, 0b10000000),
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_invalid() {
let result: Result<KeyOps, _> = serde_json::from_str(r#"["unknown"]"#);
assert!(result.is_err());
}
#[test]
fn serialize() {
let ops = KeyOps::SIGN | KeyOps::DERIVE_BITS;
let json = serde_json::to_string(&ops).unwrap();
assert_eq!(json, r#"["sign","deriveBits"]"#)
}
}

View File

@ -193,14 +193,9 @@ impl Key {
}
}
/// Returns true iff this key only contains non-private components.
pub fn is_public(&self) -> bool {
!self.is_private()
}
/// Returns the public part of this key (symmetric keys have no public parts).
pub fn to_public(&self) -> Option<Cow<Self>> {
if self.is_public() {
if !self.is_private() {
return Some(Cow::Borrowed(self));
}
Some(Cow::Owned(match self {
@ -366,7 +361,7 @@ impl Key {
#[cfg(feature = "generate")]
pub fn generate_symmetric(num_bits: usize) -> Self {
use rand::RngCore;
let mut bytes = Vec::with_capacity(num_bits / 8);
let mut bytes = vec![0; num_bits / 8];
rand::thread_rng().fill_bytes(&mut bytes);
Self::Symmetric { key: bytes.into() }
}
@ -506,7 +501,7 @@ const _IMPL_JWT_CONVERSIONS: () = {
impl Key {
/// Returns an `EncodingKey` if the key is private.
pub fn try_to_encoding_key(&self) -> Result<jwt::EncodingKey, ConversionError> {
if self.is_public() {
if !self.is_private() {
return Err(ConversionError::NotPrivate);
}
Ok(match self {

View File

@ -28,6 +28,12 @@ static RSA_JWK_FIXTURE: &str = r#"{
"n": "pCzbcd9kjvg5rfGHdEMWnXo49zbB6FLQ-m0B0BvVp0aojVWYa0xujC-ZP7ZhxByPxyc2PazwFJJi9ivZ_ggRww"
}"#;
#[cfg(feature = "pkcs-convert")]
static OCT_FIXTURE: &str = r#"{
"kty": "oct",
"k": "TdSBZdXL5n39JXlQc7QL3w"
}"#;
#[test]
fn deserialize_es256() {
let jwk = JsonWebKey::from_str(P256_JWK_FIXTURE).unwrap();
@ -347,7 +353,6 @@ J2lmylxUG0M=
#[test]
fn rsa_public_to_pem() {
let jwk = JsonWebKey::from_str(RSA_JWK_FIXTURE).unwrap();
#[rustfmt::skip]
assert_eq!(
jwk.key.to_public().unwrap().to_pem(),
"-----BEGIN PUBLIC KEY-----
@ -357,3 +362,57 @@ MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKQs23HfZI74Oa3xh3RDFp16OPc2wehS
"
);
}
#[cfg(feature = "pkcs-convert")]
#[test]
fn oct_to_pem() {
let jwk = JsonWebKey::from_str(OCT_FIXTURE).unwrap();
assert!(jwk.key.try_to_pem().is_err());
}
#[cfg(feature = "pkcs-convert")]
#[test]
fn oct_to_public() {
let jwk = JsonWebKey::from_str(OCT_FIXTURE).unwrap();
assert!(jwk.key.to_public().is_none());
}
#[cfg(feature = "generate")]
#[test]
fn generate_oct() {
let bits = 56;
match Key::generate_symmetric(bits) {
Key::Symmetric { key } if key.len() == 56 / 8 => {}
k => panic!("`generate_symmetric` generated {:?}", k),
}
}
#[test]
fn ec_is_private() {
let private_jwk = JsonWebKey::from_str(P256_JWK_FIXTURE).unwrap();
assert!(private_jwk.key.is_private());
assert!(!private_jwk.key.to_public().unwrap().is_private());
let mut k: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(P256_JWK_FIXTURE).unwrap();
k.remove("d");
let public_jwk = JsonWebKey::from_str(&serde_json::to_string(&k).unwrap()).unwrap();
assert!(!public_jwk.key.is_private());
assert!(!public_jwk.key.to_public().unwrap().is_private());
}
#[test]
fn rsa_is_private() {
let private_jwk = JsonWebKey::from_str(RSA_JWK_FIXTURE).unwrap();
assert!(private_jwk.key.is_private());
assert!(!private_jwk.key.to_public().unwrap().is_private());
static PUBLIC_RSA_JWK_FIXTURE: &str = r#"{
"kty": "RSA",
"e": "AQAB",
"n": "pCzbcd9kjvg5rfGHdEMWnXo49zbB6FLQ-m0B0BvVp0aojVWYa0xujC-ZP7ZhxByPxyc2PazwFJJi9ivZ_ggRww"
}"#;
let public_jwk = JsonWebKey::from_str(PUBLIC_RSA_JWK_FIXTURE).unwrap();
assert!(!public_jwk.key.is_private());
assert!(!public_jwk.key.to_public().unwrap().is_private());
}