Skip to content

Commit 7356705

Browse files
committed
Added a test for create/destroy functions
1 parent 75784ec commit 7356705

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,15 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
681681
mod tests {
682682
use rand::{RngCore, thread_rng};
683683
use std::str::FromStr;
684+
use std::marker::PhantomData;
684685

685686
use key::{SecretKey, PublicKey};
686687
use super::from_hex;
687688
use super::constants;
688689
use super::{Secp256k1, Signature, Message};
689690
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
691+
use ffi;
692+
use context::*;
690693

691694
macro_rules! hex {
692695
($hex:expr) => ({
@@ -696,6 +699,35 @@ mod tests {
696699
});
697700
}
698701

702+
703+
#[test]
704+
fn test_manual_create_destroy() {
705+
let ctx_full = unsafe { ffi::secp256k1_context_create(AllPreallocated::FLAGS) };
706+
let ctx_sign = unsafe { ffi::secp256k1_context_create(SignOnlyPreallocated::FLAGS) };
707+
let ctx_vrfy = unsafe { ffi::secp256k1_context_create(VerifyOnlyPreallocated::FLAGS) };
708+
709+
let buf: *mut [u8] = &mut [0u8;0] as _;
710+
let full: Secp256k1<AllPreallocated> = Secp256k1{ctx: ctx_full, phantom: PhantomData, buf};
711+
let sign: Secp256k1<SignOnlyPreallocated> = Secp256k1{ctx: ctx_sign, phantom: PhantomData, buf};
712+
let vrfy: Secp256k1<VerifyOnlyPreallocated> = Secp256k1{ctx: ctx_vrfy, phantom: PhantomData, buf};
713+
714+
let (sk, pk) = full.generate_keypair(&mut thread_rng());
715+
let msg = Message::from_slice(&[2u8; 32]).unwrap();
716+
// Try signing
717+
assert_eq!(sign.sign(&msg, &sk), full.sign(&msg, &sk));
718+
let sig = full.sign(&msg, &sk);
719+
720+
// Try verifying
721+
assert!(vrfy.verify(&msg, &sig, &pk).is_ok());
722+
assert!(full.verify(&msg, &sig, &pk).is_ok());
723+
724+
drop(full);drop(sign);drop(vrfy);
725+
726+
unsafe { ffi::secp256k1_context_destroy(ctx_vrfy) };
727+
unsafe { ffi::secp256k1_context_destroy(ctx_sign) };
728+
unsafe { ffi::secp256k1_context_destroy(ctx_full) };
729+
}
730+
699731
#[test]
700732
fn test_preallocation() {
701733
let mut buf_ful = vec![0u8; Secp256k1::preallocate_size()];

0 commit comments

Comments
 (0)