Преглед на файлове

test for dup_filter module

Ian Goldberg преди 3 години
родител
ревизия
c02b579d3a
променени са 3 файла, в които са добавени 40 реда и са изтрити 3 реда
  1. 3 3
      src/dup_filter.rs
  2. 2 0
      src/lib.rs
  3. 35 0
      tests/tests.rs

+ 3 - 3
src/dup_filter.rs

@@ -3,13 +3,13 @@
  * the table of seen ids in memory, but a production one would of course
  * use a disk-backed database. */
 
+use std::cmp::Eq;
 use std::collections::HashMap;
 use std::hash::Hash;
-use std::cmp::Eq;
 
 /// Each instance of DupFilter maintains its own independent table of
 /// seen ids.  IdType will typically be Scalar.
-#[derive(Default,Debug)]
+#[derive(Default, Debug)]
 pub struct DupFilter<IdType> {
     seen_table: HashMap<IdType, ()>,
 }
@@ -25,7 +25,7 @@ impl<IdType: Hash + Eq + Copy> DupFilter<IdType> {
     /// As atomically as possible, check to see if the id is in the seen
     /// table, and add it if not.  Return Ok(()) if it was not already
     /// in the table, and Err(()) if it was.
-    pub fn filter(&mut self, id: &IdType) -> Result<(),()> {
+    pub fn filter(&mut self, id: &IdType) -> Result<(), ()> {
         match self.seen_table.insert(*id, ()) {
             None => Ok(()),
             Some(()) => Err(()),

+ 2 - 0
src/lib.rs

@@ -17,6 +17,8 @@ The notation follows that of the paper "Hyphae: Social Secret Sharing"
 #[macro_use]
 extern crate zkp;
 
+pub mod dup_filter;
+
 use sha2::Sha512;
 
 use rand::rngs::OsRng;

+ 35 - 0
tests/tests.rs

@@ -1,5 +1,8 @@
+use lox::dup_filter;
 use lox::BridgeDb;
 
+use curve25519_dalek::scalar::Scalar;
+
 #[test]
 fn test_openinvite() {
     let bdb = BridgeDb::new(20);
@@ -8,3 +11,35 @@ fn test_openinvite() {
     let res = BridgeDb::verify(inv, bdb.pubkey);
     println!("{:?}", res);
 }
+
+#[test]
+fn test_dup_filter() {
+    let mut df1: dup_filter::DupFilter<Scalar> = Default::default();
+    let mut df2: dup_filter::DupFilter<Scalar> = Default::default();
+    let mut rng = rand::thread_rng();
+    let s1 = Scalar::random(&mut rng);
+    let s2 = Scalar::random(&mut rng);
+    let s3 = Scalar::random(&mut rng);
+    let s4 = Scalar::random(&mut rng);
+    let s5 = Scalar::random(&mut rng);
+    // Check basic behaviour
+    assert_eq!(df1.check(&s1), false);
+    assert_eq!(df1.filter(&s1), Ok(()));
+    assert_eq!(df1.check(&s1), true);
+    assert_eq!(df1.filter(&s1), Err(()));
+    // Ensure different instances of DupFilter have different tables
+    assert_eq!(df2.check(&s1), false);
+    assert_eq!(df2.filter(&s1), Ok(()));
+    assert_eq!(df2.filter(&s1), Err(()));
+    assert_eq!(df2.check(&s1), true);
+    // Check multiple ids
+    assert_eq!(df1.check(&s2), false);
+    assert_eq!(df1.filter(&s3), Ok(()));
+    assert_eq!(df1.filter(&s4), Ok(()));
+    assert_eq!(df1.filter(&s3), Err(()));
+    assert_eq!(df1.check(&s1), true);
+    assert_eq!(df1.filter(&s1), Err(()));
+    assert_eq!(df1.filter(&s5), Ok(()));
+    println!("df1 = {:?}", df1);
+    println!("df2 = {:?}", df2);
+}