Skip to content

Commit 3ca7f49

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 bf9f556 commit 3ca7f49

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ rand = "0.8"
5050
rand_core = "0.6"
5151
serde_test = "1.0"
5252
bitcoin_hashes = "0.10"
53+
bincode = "1.3.3"
54+
55+
# cbor does not build on WASM, we use it in a single trivial test (an example of when
56+
# fixed-width-serde breaks down). Just run the test when on an x86_64 machine.
57+
[target.'cfg(target_arch = "x86_64")'.dependencies]
58+
cbor = "0.4.1"
5359

5460
[target.wasm32-unknown-unknown.dev-dependencies]
5561
wasm-bindgen-test = "0.3"

tests/serde.rs

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

0 commit comments

Comments
 (0)