Skip to content

Commit f11c1d6

Browse files
committed
Add fixed-width-serde integration tests
Add a `tests` directory. Add `serde` tests for the recently added fixed width binary serialization code. Please note, serialization is only fixed width when serialized with the `bincode` crate.
1 parent e0fa21b commit f11c1d6

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ rand = "0.6"
5050
rand_core = "0.4"
5151
serde_test = "1.0"
5252
bitcoin_hashes = "0.10"
53+
bincode = "1.3.3"
54+
cbor = "0.4.1"
5355

5456
[target.wasm32-unknown-unknown.dev-dependencies]
5557
wasm-bindgen-test = "0.3"

tests/serde.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#![cfg(feature = "serde")]
2+
3+
extern crate bincode;
4+
extern crate cbor;
5+
extern crate secp256k1;
6+
7+
use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey};
8+
#[cfg(feature = "global-context")]
9+
use secp256k1::{Secp256k1, KeyPair};
10+
11+
// Arbitrary key data.
12+
13+
static SK_BYTES: [u8; 32] = [
14+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
15+
0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23,
16+
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31,
17+
0x0f, 0x10, 0x1f, 0xa0, 0xa9, 0xaa, 0xaf, 0xff,
18+
];
19+
20+
static PK_BYTES: [u8; 33] = [
21+
0x02,
22+
0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f,
23+
0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d,
24+
0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54,
25+
0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66,
26+
];
27+
28+
static XONLY_PK_BYTES: [u8; 32] = [
29+
0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f,
30+
0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d,
31+
0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54,
32+
0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66,
33+
];
34+
35+
fn secret_key() -> SecretKey {
36+
SecretKey::from_slice(&SK_BYTES).expect("failed to create sk from slice")
37+
}
38+
39+
// Our current serde serialization implementation is only guaranteed to be fixed
40+
// width for bincode. https://docs.rs/bincode/latest/bincode/index.html
41+
#[test]
42+
fn bincode_secret_key() {
43+
let sk = secret_key();
44+
let ser = bincode::serialize(&sk).unwrap();
45+
46+
assert_eq!(ser, SK_BYTES);
47+
}
48+
49+
#[test]
50+
fn bincode_public_key() {
51+
let pk = PublicKey::from_slice(&PK_BYTES).expect("failed to create pk from slice");
52+
let ser = bincode::serialize(&pk).unwrap();
53+
54+
assert_eq!(ser, &PK_BYTES as &[u8])
55+
}
56+
57+
#[test]
58+
#[cfg(feature = "global-context")]
59+
fn bincode_key_pair() {
60+
let secp = Secp256k1::new();
61+
let kp = KeyPair::from_seckey_slice(&secp, &SK_BYTES).expect("failed to create keypair");
62+
let ser = bincode::serialize(&kp).unwrap();
63+
64+
assert_eq!(ser, SK_BYTES);
65+
}
66+
67+
#[test]
68+
fn bincode_x_only_public_key() {
69+
let pk = XOnlyPublicKey::from_slice(&XONLY_PK_BYTES).expect("failed to create xonly pk from slice");
70+
let ser = bincode::serialize(&pk).unwrap();
71+
72+
assert_eq!(ser, XONLY_PK_BYTES);
73+
}
74+
75+
// cbor adds an additional byte of metadata to certain byte values (byte_value < 24).
76+
#[test]
77+
fn cbor() {
78+
let sk = secret_key();
79+
80+
let mut e = cbor::Encoder::from_memory();
81+
e.encode(sk.as_ref()).unwrap();
82+
83+
// 52 because there are 22 bytes in the key for which cbor adds metadata.
84+
assert_eq!(e.as_bytes().len(), 52);
85+
}

0 commit comments

Comments
 (0)