Skip to content

Commit

Permalink
Merge pull request #455 from jpculp/clippy
Browse files Browse the repository at this point in the history
Fix clippy warnings
  • Loading branch information
jpculp authored Apr 26, 2022
2 parents a0968a4 + 648465c commit e756b26
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 54 deletions.
5 changes: 3 additions & 2 deletions olpc-cjson/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ mod tests {
use serde_json::Serializer;
use std::io::Result;

/// Small wrapper around serde_json's json! macro to encode the value as canonical JSON.
/// Small wrapper around the `serde_json` json! macro to encode the value as canonical JSON.
macro_rules! encode {
($($tt:tt)+) => {
(|v: serde_json::Value| -> Result<Vec<u8>> {
Expand All @@ -311,7 +311,7 @@ mod tests {
/// These smoke tests come from securesystemslib, the library used by the TUF reference
/// implementation.
///
/// https://github.com/secure-systems-lab/securesystemslib/blob/f466266014aff529510216b8c2f8c8f39de279ec/tests/test_formats.py#L354-L389
/// `<https://github.com/secure-systems-lab/securesystemslib/blob/f466266014aff529510216b8c2f8c8f39de279ec/tests/test_formats.py#L354-L389>`
#[test]
fn securesystemslib_asserts() -> Result<()> {
assert_eq!(encode!([1, 2, 3])?, b"[1,2,3]");
Expand Down Expand Up @@ -426,6 +426,7 @@ mod tests {

/// This test asserts that the cannonical representation of some real-world data always comes
/// out the same.
#[allow(clippy::unreadable_literal)]
#[test]
fn actual_tuf_signed() {
let encode_result = encode!(
Expand Down
4 changes: 2 additions & 2 deletions tough-kms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ fn pad_signature(mut signature: Vec<u8>, modulus_size_bytes: usize) -> error::Re
#[test]
fn parse_modulus_length_wrong_alg() {
let result = parse_modulus_length_bytes("ECC_SECG_P256K1");
assert!(result.is_err())
assert!(result.is_err());
}

#[test]
fn parse_modulus_length_bad_str() {
let result = parse_modulus_length_bytes("RSA_");
assert!(result.is_err())
assert!(result.is_err());
}

#[test]
Expand Down
24 changes: 8 additions & 16 deletions tough-kms/tests/all_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use serde::{Deserialize, Deserializer};
use std::fs::File;
use std::io::BufReader;
use tough::key_source::KeySource;
use tough::schema::decoded::{Decoded, RsaPem};
use tough::schema::key::Key;
use tough_kms::KmsKeySource;
use tough_kms::KmsSigningAlgorithm::RsassaPssSha256;
Expand All @@ -34,11 +33,6 @@ struct PublicKeyResp {
public_key: bytes::Bytes,
}

#[derive(Deserialize, Debug)]
struct ExpectedPublicKey {
public_key: Decoded<RsaPem>,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
struct SignResp {
#[serde(rename = "Signature")]
Expand Down Expand Up @@ -83,7 +77,7 @@ fn check_tuf_key_success() {
let mock_client = KmsClient::new_with(mock, MockCredentialsProvider, Region::UsEast1);
let kms_key = KmsKeySource {
profile: None,
key_id: key_id.clone(),
key_id,
client: Some(mock_client),
signing_algorithm: RsassaPssSha256,
};
Expand Down Expand Up @@ -175,8 +169,7 @@ fn check_public_key_failure() {
assert_eq!(
format!(
"Failed to get public key for aws-kms:///{} : {}",
key_id.clone(),
error_msg.clone()
key_id, error_msg
),
err.to_string()
);
Expand All @@ -202,7 +195,7 @@ fn check_public_key_missing_algo() {
let mock_client = KmsClient::new_with(mock, MockCredentialsProvider, Region::UsEast1);
let kms_key = KmsKeySource {
profile: None,
key_id: key_id.clone(),
key_id,
client: Some(mock_client),
signing_algorithm: RsassaPssSha256,
};
Expand Down Expand Up @@ -235,7 +228,7 @@ fn check_public_key_unmatch_algo() {
let mock_client = KmsClient::new_with(mock, MockCredentialsProvider, Region::UsEast1);
let kms_key = KmsKeySource {
profile: None,
key_id: key_id.clone(),
key_id,
client: Some(mock_client),
signing_algorithm: RsassaPssSha256,
};
Expand Down Expand Up @@ -292,8 +285,7 @@ fn check_sign_request_failure() {
assert_eq!(
format!(
"Error while signing message for aws-kms:///{} : {}",
key_id.clone(),
error_msg.clone()
key_id, error_msg
),
err.to_string()
);
Expand Down Expand Up @@ -340,7 +332,7 @@ fn check_signature_failure() {
let mock_client = KmsClient::new_with(mock, MockCredentialsProvider, Region::UsEast1);
let kms_key = KmsKeySource {
profile: None,
key_id: key_id.clone(),
key_id,
client: Some(mock_client),
signing_algorithm: RsassaPssSha256,
};
Expand All @@ -360,9 +352,9 @@ fn check_write_ok() {
let key_id = String::from("alias/some_alias");
let kms_key = KmsKeySource {
profile: None,
key_id: key_id.clone(),
key_id,
client: None,
signing_algorithm: RsassaPssSha256,
};
assert_eq!((), kms_key.write("", "").unwrap())
assert!(kms_key.write("", "").is_ok());
}
2 changes: 2 additions & 0 deletions tough/src/editor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod tests {
}

// Make sure we can add targets from different sources
#[allow(clippy::similar_names)]
#[test]
fn add_targets_from_multiple_sources() {
let targets: Signed<Targets> = serde_json::from_str(include_str!(
Expand All @@ -80,6 +81,7 @@ mod tests {
.unwrap();
}

#[allow(clippy::similar_names)]
#[test]
fn clear_targets() {
let targets: Signed<Targets> = serde_json::from_str(include_str!(
Expand Down
2 changes: 1 addition & 1 deletion tough/src/schema/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {

/// Ensure that we can deserialize a root.json file that has hex-encoded ECDSA keys. This uses
/// sigstore's root.json file taken from here:
/// https://sigstore-tuf-root.storage.googleapis.com/2.root.json
/// `<https://sigstore-tuf-root.storage.googleapis.com/2.root.json>`
#[test]
fn ecdsa_hex_encoded_keys() {
assert!(serde_json::from_str::<Signed<Root>>(include_str!(
Expand Down
27 changes: 12 additions & 15 deletions tough/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,10 @@ fn targets_iter_and_map_test() {
length: 0,
hashes: Hashes {
sha256: [0u8].to_vec().into(),
_extra: Default::default(),
_extra: HashMap::default(),
},
custom: Default::default(),
_extra: Default::default(),
custom: HashMap::default(),
_extra: HashMap::default(),
};

// Create a hierarchy of targets/delegations: a -> b -> c
Expand All @@ -1201,13 +1201,13 @@ fn targets_iter_and_map_test() {
TargetName::new("c.txt").unwrap() => nothing.clone(),
},
delegations: None,
_extra: Default::default(),
_extra: HashMap::default(),
},
signatures: vec![],
}),
};
let b_delegations = Delegations {
keys: Default::default(),
keys: HashMap::default(),
roles: vec![c_role],
};
let b_role = DelegatedRole {
Expand All @@ -1225,42 +1225,39 @@ fn targets_iter_and_map_test() {
TargetName::new("b.txt").unwrap() => nothing.clone(),
},
delegations: Some(b_delegations),
_extra: Default::default(),
_extra: HashMap::default(),
},
signatures: vec![],
}),
};
let a_delegations = Delegations {
keys: Default::default(),
keys: HashMap::default(),
roles: vec![b_role],
};
let a = Targets {
spec_version: "".to_string(),
version: NonZeroU64::new(1).unwrap(),
expires: Utc::now(),
targets: hashmap! {
TargetName::new("a.txt").unwrap() => nothing.clone(),
TargetName::new("a.txt").unwrap() => nothing,
},
delegations: Some(a_delegations),
_extra: Default::default(),
_extra: HashMap::default(),
};

// Assert that targets_iter is recursive and thus has a.txt, b.txt and c.txt
assert!(a
.targets_iter()
.map(|(key, _)| key)
.find(|&item| item.raw() == "a.txt")
.is_some());
.any(|item| item.raw() == "a.txt"));
assert!(a
.targets_iter()
.map(|(key, _)| key)
.find(|&item| item.raw() == "b.txt")
.is_some());
.any(|item| item.raw() == "b.txt"));
assert!(a
.targets_iter()
.map(|(key, _)| key)
.find(|&item| item.raw() == "c.txt")
.is_some());
.any(|item| item.raw() == "c.txt"));

// Assert that targets_map is also recursive
let map = a.targets_map();
Expand Down
6 changes: 3 additions & 3 deletions tough/tests/target_path_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn create_root(root_path: &Path, consistent_snapshot: bool) -> Vec<Box<dyn KeySo
path: test_data().join("snakeoil.pem"),
})];

let key_pair = keys.iter().next().unwrap().as_sign().unwrap().tuf_key();
let key_pair = keys.get(0).unwrap().as_sign().unwrap().tuf_key();
let key_id = key_pair.key_id().unwrap();

let empty_keys = RoleKeys {
Expand All @@ -48,15 +48,15 @@ fn create_root(root_path: &Path, consistent_snapshot: bool) -> Vec<Box<dyn KeySo
RoleType::Root => empty_keys.clone(),
RoleType::Snapshot => empty_keys.clone(),
RoleType::Targets => empty_keys.clone(),
RoleType::Timestamp => empty_keys.clone(),
RoleType::Timestamp => empty_keys,
// RoleType::DelegatedTargets => empty_keys.clone(),
},
_extra: HashMap::new(),
},
signatures: Vec::new(),
};

root.signed.keys.insert(key_id.clone(), key_pair.clone());
root.signed.keys.insert(key_id, key_pair);

let signed_root = SignedRole::new(
root.signed.clone(),
Expand Down
2 changes: 1 addition & 1 deletion tuftool/tests/clone_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn assert_all_metadata(metadata_dir: &TempDir) {
"role1.json",
"role2.json",
] {
assert_metadata_match(&metadata_dir, f)
assert_metadata_match(metadata_dir, f)
}
}

Expand Down
10 changes: 5 additions & 5 deletions tuftool/tests/create_repository_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn gen_key(key: &str, root_json: &str) {
fn add_root_key(key: &str, root_json: &str) {
Command::cargo_bin("tuftool")
.unwrap()
.args(&["root", "add-key", root_json, key.clone(), "--role", "root"])
.args(&["root", "add-key", root_json, key, "--role", "root"])
.assert()
.success();
}
Expand Down Expand Up @@ -108,13 +108,13 @@ fn create_repository(root_key: &str, auto_generate: bool) {
let root_json_dir = TempDir::new().unwrap();
let root_json = root_json_dir.path().join("root.json");
initialize_root_json(root_json.to_str().unwrap());
if auto_generate == true {
gen_key(root_key.clone(), root_json.to_str().unwrap());
if auto_generate {
gen_key(root_key, root_json.to_str().unwrap());
} else {
add_root_key(root_key, root_json.to_str().unwrap());
}
add_key_all_role(root_key.clone(), root_json.to_str().unwrap());
sign_root_json(root_key.clone(), root_json.to_str().unwrap());
add_key_all_role(root_key, root_json.to_str().unwrap());
sign_root_json(root_key, root_json.to_str().unwrap());
// Use root.json file to generate metadata using create command.
let timestamp_expiration = Utc::now().checked_add_signed(Duration::days(3)).unwrap();
let timestamp_version: u64 = 1234;
Expand Down
12 changes: 6 additions & 6 deletions tuftool/tests/root_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ fn check_signature_exists(root_json: &str, key_id: Decoded<Hex>) -> bool {
if root.signatures.iter().find(|sig| sig.keyid == key_id) == None {
return false;
}
return true;
true
}

fn get_version(root_json: &str) -> NonZeroU64 {
let root = get_signed_root(root_json);
return root.signed.version;
root.signed.version
}

#[test]
Expand Down Expand Up @@ -263,10 +263,10 @@ fn cross_sign_root() {
new_root_json.to_str().unwrap(),
old_root_key.to_str().unwrap(),
);
assert_eq!(
check_signature_exists(new_root_json.to_str().unwrap(), old_key_id),
true
);
assert!(check_signature_exists(
new_root_json.to_str().unwrap(),
old_key_id,
));
}

//cross-signing new_root.json with invalid key ( key not present in old_root.json )
Expand Down
6 changes: 3 additions & 3 deletions tuftool/tests/update_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn update_with_incorrect_key() {
"-k",
bad_key.to_str().unwrap(),
"--root",
root_json.clone().to_str().unwrap(),
root_json.to_str().unwrap(),
"--metadata-url",
"https://metadata.url.does.not.matter",
"--targets-expires",
Expand Down Expand Up @@ -334,15 +334,15 @@ fn updates_expired_repo(
} else {
cmd.assert()
};
return (
(
assert,
timestamp_expiration,
timestamp_version,
snapshot_expiration,
snapshot_version,
targets_expiration,
targets_version,
);
)
}

#[test]
Expand Down

0 comments on commit e756b26

Please sign in to comment.