-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -24,3 +24,6 @@ serde = "1.0" | |
serde_derive = "1.0" | ||
flatbuffers = "0.6.0" | ||
|
||
[dev-dependencies] | ||
crypto = { path = "../crypto" } | ||
|
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,2 @@ | ||
mod test_notifier; | ||
mod test_verifier; |
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,88 @@ | ||
use crate::notifier::Notifier; | ||
use ckb_core::alert::AlertBuilder; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn test_notice_alerts_by_version() { | ||
let mut notifier = Notifier::new("0.1.0".into()); | ||
let alert1 = Arc::new( | ||
AlertBuilder::default() | ||
.id(1) | ||
.max_version(Some("0.2.0".into())) | ||
.min_version(Some("0.1.0".into())) | ||
.build(), | ||
); | ||
let alert2 = Arc::new( | ||
AlertBuilder::default() | ||
.id(2) | ||
.max_version(Some("0.0.2".into())) | ||
.build(), | ||
); | ||
notifier.add(alert1); | ||
notifier.add(alert2); | ||
assert_eq!(notifier.received_alerts().len(), 2); | ||
assert_eq!(notifier.noticed_alerts().len(), 1); | ||
assert_eq!(notifier.noticed_alerts()[0].id, 1); | ||
} | ||
|
||
#[test] | ||
fn test_received_alerts() { | ||
let mut notifier = Notifier::new("0.1.0".into()); | ||
let alert1 = Arc::new( | ||
AlertBuilder::default() | ||
.id(1) | ||
.max_version(Some("0.2.0".into())) | ||
.min_version(Some("0.1.0".into())) | ||
.build(), | ||
); | ||
let dup_alert1 = Arc::new(AlertBuilder::default().id(1).build()); | ||
notifier.add(Arc::clone(&alert1)); | ||
assert!(notifier.has_received(1)); | ||
notifier.add(dup_alert1); | ||
assert_eq!(notifier.received_alerts().len(), 1); | ||
assert_eq!(notifier.received_alerts()[0].hash(), alert1.hash()); | ||
} | ||
|
||
#[test] | ||
fn test_cancel_alert() { | ||
let mut notifier = Notifier::new("0.1.0".into()); | ||
let alert1 = Arc::new( | ||
AlertBuilder::default() | ||
.id(1) | ||
.max_version(Some("0.2.0".into())) | ||
.min_version(Some("0.1.0".into())) | ||
.build(), | ||
); | ||
let cancel_alert1 = Arc::new(AlertBuilder::default().id(2).cancel(1).build()); | ||
notifier.add(Arc::clone(&alert1)); | ||
assert!(notifier.has_received(1)); | ||
notifier.add(Arc::clone(&cancel_alert1)); | ||
assert!(notifier.has_received(1)); | ||
assert!(notifier.has_received(2)); | ||
assert_eq!(notifier.received_alerts().len(), 1); | ||
assert_eq!(notifier.noticed_alerts().len(), 1); | ||
assert_eq!(notifier.received_alerts()[0].hash(), cancel_alert1.hash()); | ||
} | ||
|
||
#[test] | ||
fn test_clear_expired_alerts() { | ||
let mut notifier = Notifier::new("0.1.0".into()); | ||
let notice_until = 1_561_084_974_000; | ||
let before_expired_time = notice_until - 1000; | ||
let after_expired_time = notice_until + 1000; | ||
let alert1 = Arc::new( | ||
AlertBuilder::default() | ||
.id(1) | ||
.notice_until(notice_until) | ||
.build(), | ||
); | ||
notifier.add(Arc::clone(&alert1)); | ||
notifier.clear_expired_alerts(before_expired_time); | ||
assert!(notifier.has_received(1)); | ||
assert_eq!(notifier.received_alerts().len(), 1); | ||
assert_eq!(notifier.noticed_alerts().len(), 1); | ||
notifier.clear_expired_alerts(after_expired_time); | ||
assert!(!notifier.has_received(1)); | ||
assert_eq!(notifier.received_alerts().len(), 0); | ||
assert_eq!(notifier.noticed_alerts().len(), 0); | ||
} |
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,34 @@ | ||
use crate::config::Config; | ||
use crate::verifier::Verifier; | ||
use ckb_core::alert::AlertBuilder; | ||
use crypto::secp::Generator; | ||
use jsonrpc_types::JsonBytes; | ||
|
||
#[test] | ||
fn test_veirifer() { | ||
let keypairs: Vec<_> = (0..3) | ||
.into_iter() | ||
.map(move |_| Generator::new().random_keypair()) | ||
.collect::<Result<Vec<_>, _>>() | ||
.expect("random keypair"); | ||
let config = Config { | ||
signatures_threshold: 2, | ||
public_keys: keypairs | ||
.iter() | ||
.map(|(_, pubkey)| JsonBytes::from_vec(pubkey.serialize())) | ||
.collect(), | ||
}; | ||
let verifier = Verifier::new(config); | ||
let mut alert = AlertBuilder::default().id(1).build(); | ||
let hash = alert.hash(); | ||
let signatures = keypairs | ||
.iter() | ||
.map(|(privkey, _)| privkey.sign_recoverable(&hash)) | ||
.collect::<Result<Vec<_>, _>>() | ||
.expect("sign alert"); | ||
alert.signatures = signatures | ||
.into_iter() | ||
.map(|s| s.serialize().into()) | ||
.collect(); | ||
assert!(verifier.verify_signatures(&alert).is_ok()); | ||
} |