Skip to content

Commit f40f1a8

Browse files
committed
add test
1 parent 43b506e commit f40f1a8

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149

150150
#[proptest]
151151
fn proof_to_bytes_from_bytes_test(
152-
#[strategy(0..20usize)] _size: usize, #[any(#_size)] p1: UnitVectorProof,
152+
#[strategy(0..5usize)] _size: usize, #[any(#_size)] p1: UnitVectorProof,
153153
) {
154154
let bytes = p1.to_bytes();
155155
let p2 = UnitVectorProof::from_bytes(&bytes, p1.size()).unwrap();

rust/catalyst-voting/src/txs/v1/decoding.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,63 @@ impl Tx {
152152
})
153153
}
154154
}
155+
156+
#[cfg(test)]
157+
mod tests {
158+
use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, ProptestConfig, Strategy};
159+
use test_strategy::proptest;
160+
161+
use super::*;
162+
use crate::SecretKey;
163+
164+
impl Arbitrary for Tx {
165+
type Parameters = ();
166+
type Strategy = BoxedStrategy<Self>;
167+
168+
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
169+
any::<([u8; 32], u8, Vote, SecretKey)>()
170+
.prop_map(|(vote_plan_id, proposal_index, vote, s)| {
171+
Tx {
172+
vote_plan_id,
173+
proposal_index,
174+
vote,
175+
public_key: s.public_key(),
176+
}
177+
})
178+
.boxed()
179+
}
180+
}
181+
182+
impl Arbitrary for Vote {
183+
type Parameters = ();
184+
type Strategy = BoxedStrategy<Self>;
185+
186+
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
187+
any::<bool>()
188+
.prop_flat_map(|b| {
189+
if b {
190+
any::<u8>().prop_map(Vote::Public).boxed()
191+
} else {
192+
any::<(u8, u8)>()
193+
.prop_flat_map(|(s1, s2)| {
194+
any_with::<(EncryptedVote, VoterProof)>((s1.into(), s2.into()))
195+
.prop_map(|(v, p)| Vote::Private(v, p))
196+
})
197+
.boxed()
198+
}
199+
})
200+
.boxed()
201+
}
202+
}
203+
204+
#[proptest(ProptestConfig::with_cases(1))]
205+
#[allow(clippy::indexing_slicing)]
206+
fn tx_to_bytes_from_bytes_test(t1: Tx) {
207+
let bytes = t1.to_bytes();
208+
// verify correctness serializing tx size field
209+
let size = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
210+
assert_eq!(size as usize, bytes.len() - 4);
211+
let t2 = Tx::from_bytes(&bytes).unwrap();
212+
assert_eq!(t1, t2);
213+
}
214+
}

rust/catalyst-voting/src/txs/v1/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
};
1111

1212
/// A v1 (Jörmungandr) transaction struct
13+
#[derive(Debug, Clone, PartialEq, Eq)]
1314
pub struct Tx {
1415
/// Vote plan id
1516
vote_plan_id: [u8; 32],
@@ -22,25 +23,10 @@ pub struct Tx {
2223
}
2324

2425
/// Vote struct
26+
#[derive(Debug, Clone, PartialEq, Eq)]
2527
pub enum Vote {
2628
/// Public voting choice
2729
Public(u8),
2830
/// Private (encrypted) voting choice
2931
Private(EncryptedVote, VoterProof),
3032
}
31-
32-
// #[cfg(test)]
33-
// mod tests {
34-
// use proptest::prelude::{Arbitrary, BoxedStrategy};
35-
36-
// use super::*;
37-
38-
// impl Arbitrary for Tx {
39-
// type Parameters = ();
40-
// type Strategy = BoxedStrategy<Self>;
41-
42-
// fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
43-
44-
// }
45-
// }
46-
// }

rust/catalyst-voting/src/vote_protocol/voter/decoding.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ mod tests {
8989

9090
#[proptest]
9191
fn encrypted_vote_to_bytes_from_bytes_test(
92-
#[strategy(0..20usize)] _size: usize, #[any(#_size)] vote1: EncryptedVote,
92+
#[strategy(0..5usize)] _size: usize, #[any(#_size)] vote1: EncryptedVote,
9393
) {
94-
println!("{}", vote1.size());
9594
let bytes = vote1.to_bytes();
9695
assert_eq!(bytes.len(), vote1.bytes_size());
9796
let vote2 = EncryptedVote::from_bytes(&bytes, vote1.size()).unwrap();

0 commit comments

Comments
 (0)