Skip to content

Commit a176698

Browse files
committed
add more tests
1 parent 93d73b1 commit a176698

File tree

10 files changed

+110
-7
lines changed

10 files changed

+110
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/target
22
**/*.rs.bk
33
Cargo.lock
4+
**/reports

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ The standard libraries for building BandChain oracle scripts. Code in this packa
66
- `kit` - standard libraries for BandChain oracle scripts
77
- `ext` - a library is helper package for writing the oracle scripts
88
- `oei` - a library containing functions for querying data from BandChain
9+
10+
## Coverage test
11+
- `owasm-crypto` - cargo tarpaulin --out Html --output-dir reports/crypto --packages owasm-crypto
12+
- `owasm-vm` - cargo tarpaulin --out Html --output-dir reports/vm --packages owasm-vm

packages/crypto/src/ecvrf.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ mod tests {
395395
.unwrap()
396396
)
397397
);
398+
assert_eq!(decode_point(&decode("").unwrap()), Err(CryptoError::invalid_hash_format()));
399+
assert_eq!(
400+
decode_point(&decode("11").unwrap()),
401+
Err(CryptoError::invalid_point_on_curve())
402+
);
398403
}
399404

400405
#[test]
@@ -728,6 +733,11 @@ mod tests {
728733
.unwrap(),
729734
)
730735
);
736+
737+
assert_eq!(
738+
scalar_multiply(&(Mpz::zero(), Mpz::zero()), &Mpz::zero(),),
739+
(Mpz::zero(), Mpz::one(),)
740+
);
731741
}
732742

733743
#[test]
@@ -812,5 +822,16 @@ mod tests {
812822
).unwrap(),
813823
true
814824
);
825+
826+
let zero_vec: Vec<u8> = vec![0; 200];
827+
828+
assert_eq!(
829+
ecvrf_verify(&zero_vec[0..30], &zero_vec[0..1], &zero_vec[0..1]),
830+
Err(CryptoError::invalid_pubkey_format())
831+
);
832+
assert_eq!(
833+
ecvrf_verify(&zero_vec[0..32], &zero_vec[0..1], &zero_vec[0..1]),
834+
Err(CryptoError::invalid_proof_format())
835+
);
815836
}
816837
}

packages/crypto/src/error.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use thiserror::Error;
55

66
pub type CryptoResult<T> = core::result::Result<T, CryptoError>;
77

8-
#[derive(Error, Debug)]
8+
#[derive(Error, Debug, PartialEq)]
99
pub enum CryptoError {
1010
#[error("Crypto error: {msg}")]
1111
GenericErr {
@@ -100,6 +100,15 @@ mod tests {
100100
}
101101
}
102102

103+
#[test]
104+
fn invalid_point_on_curve_works() {
105+
let error = CryptoError::invalid_point_on_curve();
106+
match error {
107+
CryptoError::InvalidPointOnCurve { .. } => {}
108+
_ => panic!("wrong error type!"),
109+
}
110+
}
111+
103112
#[test]
104113
fn invalid_hash_format_works() {
105114
let error = CryptoError::invalid_hash_format();
@@ -126,4 +135,13 @@ mod tests {
126135
_ => panic!("wrong error type!"),
127136
}
128137
}
138+
139+
#[test]
140+
fn code_works() {
141+
assert_eq!(CryptoError::invalid_point_on_curve().code(), 2);
142+
assert_eq!(CryptoError::invalid_hash_format().code(), 3);
143+
assert_eq!(CryptoError::invalid_proof_format().code(), 4);
144+
assert_eq!(CryptoError::invalid_pubkey_format().code(), 5);
145+
assert_eq!(CryptoError::generic_err("test").code(), 10);
146+
}
129147
}

packages/kit/src/ext/cmp.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,50 @@ where
2121
}
2222
}
2323
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_equal() {
31+
let a: f64 = 3.4;
32+
let b: f64 = 3.4;
33+
assert_eq!(fcmp(&a, &b), Ordering::Equal);
34+
}
35+
36+
#[test]
37+
fn test_less() {
38+
let a: f64 = 2.4;
39+
let b: f64 = 3.4;
40+
assert_eq!(fcmp(&a, &b), Ordering::Less);
41+
}
42+
43+
#[test]
44+
fn test_greater() {
45+
let a: f64 = 4.4;
46+
let b: f64 = 3.4;
47+
assert_eq!(fcmp(&a, &b), Ordering::Greater);
48+
}
49+
50+
#[test]
51+
fn test_nan_nan() {
52+
let a: f64 = f64::NAN;
53+
let b: f64 = f64::NAN;
54+
assert_eq!(fcmp(&a, &b), Ordering::Equal);
55+
}
56+
57+
#[test]
58+
fn test_nan_value() {
59+
let a: f64 = f64::NAN;
60+
let b: f64 = 3.4;
61+
assert_eq!(fcmp(&a, &b), Ordering::Greater);
62+
}
63+
64+
#[test]
65+
fn test_value_nan() {
66+
let a: f64 = 3.4;
67+
let b: f64 = f64::NAN;
68+
assert_eq!(fcmp(&a, &b), Ordering::Less);
69+
}
70+
}

packages/vm/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Cache {
8080
}
8181

8282
#[cfg(test)]
83-
mod test {
83+
mod tests {
8484
use super::*;
8585
use std::io::{Read, Write};
8686
use std::process::Command;

packages/vm/src/calls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
}
5454

5555
#[cfg(test)]
56-
mod test {
56+
mod tests {
5757
use crate::cache::CacheOptions;
5858

5959
use super::*;
@@ -159,7 +159,7 @@ mod test {
159159
(i64.const 1)
160160
(i64.const 1)
161161
(i64.const 1048576)
162-
(i64.const 4)
162+
(i64.const 4)
163163
call 0
164164
165165
(local.set $idx (i32.const 0))
@@ -172,7 +172,7 @@ mod test {
172172
)
173173
(func (;"execute": Resolves with result "beeb";))
174174
(memory (export "memory") 17)
175-
(data (i32.const 1048576) "beeb")
175+
(data (i32.const 1048576) "beeb")
176176
(export "prepare" (func 1))
177177
(export "execute" (func 2)))
178178
"#,
@@ -186,6 +186,7 @@ mod test {
186186
}
187187

188188
#[test]
189+
#[cfg(not(tarpaulin))]
189190
fn test_out_of_gas() {
190191
let wasm = wat2wasm(
191192
r#"(module

packages/vm/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn inject_stack_height(module: Module) -> Result<Module, Error> {
112112
}
113113

114114
#[cfg(test)]
115-
mod test {
115+
mod tests {
116116
use super::*;
117117

118118
use assert_matches::assert_matches;

packages/vm/src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ impl Display for Error {
4343
write!(f, "{:?}", self)
4444
}
4545
}
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn fmt_works() {
53+
assert_eq!(format!("{}", Error::NoError), "NoError");
54+
assert_eq!(format!("{}", Error::SpanTooSmallError), "SpanTooSmallError");
55+
}
56+
}

packages/vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
}
159159

160160
#[cfg(test)]
161-
mod test {
161+
mod tests {
162162
use std::{
163163
io::{Read, Write},
164164
process::Command,

0 commit comments

Comments
 (0)