Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
fix: resolve clippy errors of non-mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and dirvine committed Oct 6, 2020
1 parent 37a7ee6 commit 94eda60
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ pub(crate) struct SignableView<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{consensus, rng, section};
use crate::{consensus, crypto::Keypair, rng, section};
use std::iter;

#[test]
fn extend_proof_chain() {
let mut rng = rng::new();

let full_id = FullId::gen(&mut rng);
let keypair = Keypair::generate(&mut rng);

let sk0 = consensus::test_utils::gen_secret_key(&mut rng);
let pk0 = sk0.public_key();
Expand All @@ -394,7 +394,7 @@ mod tests {

let variant = Variant::NodeApproval(elders_info);
let message = Message::single_src(
&full_id,
&keypair,
DstLocation::Direct,
variant,
Some(full_proof_chain.slice(1..)),
Expand Down
17 changes: 9 additions & 8 deletions src/section/elders_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

#[cfg(test)]
use crate::{id::FullId, rng::MainRng};
use crate::{crypto::Keypair, rng::MainRng};
use crate::{peer::Peer, Prefix, XorName};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -84,7 +84,8 @@ pub(crate) fn gen_elders_info(
rng: &mut MainRng,
prefix: Prefix,
count: usize,
) -> (EldersInfo, Vec<FullId>) {
) -> (EldersInfo, Vec<Keypair>) {
use crate::crypto::name;
use rand::Rng;
use std::net::SocketAddr;

Expand All @@ -94,21 +95,21 @@ pub(crate) fn gen_elders_info(
SocketAddr::from((ip, port))
}

let mut full_ids: Vec<_> = (0..count).map(|_| FullId::gen(rng)).collect();
let mut keypairs: Vec<_> = (0..count).map(|_| Keypair::generate(rng)).collect();

// Clippy false positive - https://github.com/rust-lang/rust-clippy/issues/5754
// (note the issue is closed, but it probably hasn't been merged into stable yet)
#[allow(clippy::unnecessary_sort_by)]
full_ids.sort_by(|lhs, rhs| lhs.public_id().name().cmp(rhs.public_id().name()));
keypairs.sort_by(|lhs, rhs| name(&lhs.public).cmp(&name(&rhs.public)));

let elders = full_ids
let elders = keypairs
.iter()
.map(|full_id| {
.map(|keypair| {
let addr = gen_socket_addr(rng);
let peer = Peer::new(*full_id.public_id(), addr);
let peer = Peer::new(name(&keypair.public), addr);
(*peer.name(), peer)
})
.collect();

(EldersInfo::new(elders, prefix), full_ids)
(EldersInfo::new(elders, prefix), keypairs)
}
46 changes: 23 additions & 23 deletions src/section/shared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ mod test {
use super::*;
use crate::{
consensus,
id::{FullId, Peer, PublicId},
crypto::{keypair_within_range, name, Keypair},
peer::Peer,
rng::{self, MainRng},
section::EldersInfo,
};
Expand All @@ -447,7 +448,7 @@ mod test {
collections::{BTreeMap, HashMap},
str::FromStr,
};
use xor_name::Prefix;
use xor_name::{Prefix, XorName};

// Note: The following tests were move over from the former `chain` module.

Expand All @@ -460,29 +461,29 @@ mod test {
fn gen_section_info(
rng: &mut MainRng,
gen: SecInfoGen,
) -> (EldersInfo, HashMap<PublicId, FullId>) {
) -> (EldersInfo, HashMap<XorName, Keypair>) {
match gen {
SecInfoGen::New(prefix, n) => {
let mut full_ids = HashMap::new();
let mut keypairs = HashMap::new();
let mut members = BTreeMap::new();
for _ in 0..n {
let some_id = FullId::within_range(rng, &prefix.range_inclusive());
let some_keypair = keypair_within_range(rng, &prefix.range_inclusive());
let peer_addr = ([127, 0, 0, 1], 9999).into();
let pub_id = *some_id.public_id();
let _ = members.insert(*pub_id.name(), Peer::new(pub_id, peer_addr));
let _ = full_ids.insert(*some_id.public_id(), some_id);
let name = name(&some_keypair.public);
let _ = members.insert(name, Peer::new(name, peer_addr));
let _ = keypairs.insert(name, some_keypair);
}
(EldersInfo::new(members, prefix), full_ids)
(EldersInfo::new(members, prefix), keypairs)
}
SecInfoGen::Add(info) => {
let mut members = info.elders.clone();
let some_id = FullId::within_range(rng, &info.prefix.range_inclusive());
let some_keypair = keypair_within_range(rng, &info.prefix.range_inclusive());
let peer_addr = ([127, 0, 0, 1], 9999).into();
let pub_id = *some_id.public_id();
let _ = members.insert(*pub_id.name(), Peer::new(pub_id, peer_addr));
let mut full_ids = HashMap::new();
let _ = full_ids.insert(pub_id, some_id);
(EldersInfo::new(members, info.prefix), full_ids)
let name = name(&some_keypair.public);
let _ = members.insert(name, Peer::new(name, peer_addr));
let mut keypairs = HashMap::new();
let _ = keypairs.insert(name, some_keypair);
(EldersInfo::new(members, info.prefix), keypairs)
}
SecInfoGen::Remove(info) => {
let elders = info.elders.clone();
Expand All @@ -493,17 +494,17 @@ mod test {

fn add_neighbour_elders_info(
state: &mut SharedState,
our_id: &PublicId,
our_id: &XorName,
neighbour_info: Proven<EldersInfo>,
) {
assert!(
!neighbour_info.value.prefix.matches(our_id.name()),
!neighbour_info.value.prefix.matches(our_id),
"Only add neighbours."
);
let _ = state.sections.update_neighbour_info(neighbour_info);
}

fn gen_state<T>(rng: &mut MainRng, sections: T) -> (SharedState, PublicId, bls::SecretKey)
fn gen_state<T>(rng: &mut MainRng, sections: T) -> (SharedState, XorName, bls::SecretKey)
where
T: IntoIterator<Item = (Prefix, usize)>,
{
Expand All @@ -512,14 +513,13 @@ mod test {
for (prefix, size) in sections {
let (info, ids) = gen_section_info(rng, SecInfoGen::New(prefix, size));
if our_id.is_none() {
our_id = ids.values().next().cloned();
our_id = ids.keys().next().cloned();
}

section_members.push(info);
}

let our_id = our_id.expect("our id");
let our_pub_id = *our_id.public_id();
let our_pub_id = our_id.expect("our id");
let mut sections_iter = section_members.into_iter();

let sk = consensus::test_utils::gen_secret_key(rng);
Expand All @@ -537,7 +537,7 @@ mod test {
(state, our_pub_id, sk)
}

fn gen_00_state(rng: &mut MainRng) -> (SharedState, PublicId, bls::SecretKey) {
fn gen_00_state(rng: &mut MainRng) -> (SharedState, XorName, bls::SecretKey) {
let elder_size: usize = 7;
gen_state(
rng,
Expand Down Expand Up @@ -572,7 +572,7 @@ mod test {
state
.sections
.get(&Prefix::from_str("00").unwrap())
.map(|info| info.elders.contains_key(our_id.name())),
.map(|info| info.elders.contains_key(&our_id)),
Some(true)
);
assert_eq!(state.sections.get(&Prefix::from_str("").unwrap()), None);
Expand Down
10 changes: 6 additions & 4 deletions tests/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@
mod utils;

use anyhow::{Error, Result};
use ed25519_dalek::Keypair;
use futures::future::join_all;
use sn_routing::{
event::{Connected, Event},
rng::MainRng,
FullId, Node,
Node,
};
use utils::*;
use xor_name::XorName;

#[tokio::test]
async fn test_genesis_node() -> Result<()> {
let full_id = FullId::gen(&mut MainRng::default());
let keypair = Keypair::generate(&mut MainRng::default());
let pub_key = keypair.public;
let (node, mut event_stream) = TestNodeBuilder::new(None)
.first()
.full_id(full_id.clone())
.keypair(keypair)
.create()
.await?;

assert_eq!(*full_id.public_id(), node.id().await);
assert_eq!(pub_key, node.public_key().await);

assert_next_event!(event_stream, Event::Connected(Connected::First));
assert_next_event!(event_stream, Event::PromotedToElder);
Expand Down
7 changes: 4 additions & 3 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
#![allow(unused)]

use anyhow::{bail, format_err, Error, Result};
use ed25519_dalek::Keypair;
use futures::future;
use itertools::Itertools;
use sn_routing::{
event::{Connected, Event},
log_ident, EventStream, FullId, NetworkParams, Node, NodeConfig, TransportConfig, MIN_AGE,
log_ident, EventStream, NetworkParams, Node, NodeConfig, TransportConfig, MIN_AGE,
};
use std::{
collections::{BTreeSet, HashSet},
Expand Down Expand Up @@ -83,8 +84,8 @@ impl<'a> TestNodeBuilder {
self
}

pub fn full_id(mut self, full_id: FullId) -> Self {
self.config.full_id = Some(full_id);
pub fn keypair(mut self, keypair: Keypair) -> Self {
self.config.keypair = Some(keypair);
self
}

Expand Down

0 comments on commit 94eda60

Please sign in to comment.