Skip to content

Commit f1bf81b

Browse files
committed
better leaf op test coverage
1 parent 11aabc0 commit f1bf81b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

go/ops.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func ApplyLeafOp(op *LeafOp, key []byte, value []byte) ([]byte, error) {
6060
}
6161
data := append(op.Prefix, pkey...)
6262
data = append(data, pvalue...)
63+
fmt.Printf("data: %X\n", data)
6364
return doHash(op.Hash, data)
6465
}
6566

@@ -139,6 +140,6 @@ func encodeVarintProto(l int) []byte {
139140
res = append(res, uint8(l&0x7f|0x80))
140141
l >>= 7
141142
}
142-
res = append(res, uint8(1))
143+
res = append(res, uint8(l))
143144
return res
144145
}

go/ops_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ func TestLeafOp(t *testing.T) {
2424
// echo -n foobar | sha256sum
2525
expected: fromHex("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"),
2626
},
27+
"hash foobaz, sha-512": {
28+
op: &LeafOp{
29+
Hash: HashOp_SHA512,
30+
// no prehash, no length prefix
31+
},
32+
key: []byte("foo"),
33+
value: []byte("baz"),
34+
// echo -n foobaz | sha512sum
35+
expected: fromHex("4f79f191298ec7461d60136c60f77c2ae8ddd85dbf6168bb925092d51bfb39b559219b39ae5385ba04946c87f64741385bef90578ea6fe6dac85dbf7ad3f79e1"),
36+
},
37+
"hash foobar (different break)": {
38+
op: &LeafOp{
39+
Hash: HashOp_SHA256,
40+
// no prehash, no length prefix
41+
},
42+
key: []byte("f"),
43+
value: []byte("oobar"),
44+
// echo -n foobar | sha256sum
45+
expected: fromHex("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"),
46+
},
2747
"requires key": {
2848
op: &LeafOp{
2949
Hash: HashOp_SHA256,
@@ -40,6 +60,33 @@ func TestLeafOp(t *testing.T) {
4060
value: []byte("bar"),
4161
isErr: true,
4262
},
63+
"hash with length prefix": {
64+
op: &LeafOp{
65+
Hash: HashOp_SHA256,
66+
Length: LengthOp_VAR_PROTO,
67+
// no prehash, no length prefix
68+
},
69+
// echo -n food | xxs -ps
70+
// and manually compute length byte
71+
key: []byte("food"), // 04666f6f64
72+
value: []byte("some longer text"), // 10736f6d65206c6f6e6765722074657874
73+
// echo -n 04666f6f6410736f6d65206c6f6e6765722074657874 | xxd -r -p | sha256sum
74+
expected: fromHex("b68f5d298e915ae1753dd333da1f9cf605411a5f2e12516be6758f365e6db265"),
75+
},
76+
"hash with prehash and length prefix": {
77+
op: &LeafOp{
78+
Hash: HashOp_SHA256,
79+
Length: LengthOp_VAR_PROTO,
80+
PrehashValue: HashOp_SHA256,
81+
// no prehash, no length prefix
82+
},
83+
key: []byte("food"), // 04666f6f64
84+
// TODO: this is hash, then length....
85+
// echo -n yet another long string | sha256sum
86+
value: []byte("yet another long string"), // 20a48c2d4f67b9f80374938535285ed285819d8a5a8fc1fccd1e3244e437cf290d
87+
// echo -n 04666f6f6420a48c2d4f67b9f80374938535285ed285819d8a5a8fc1fccd1e3244e437cf290d | xxd -r -p | sha256sum
88+
expected: fromHex("87e0483e8fb624aef2e2f7b13f4166cda485baa8e39f437c83d74c94bedb148f"),
89+
},
4390
}
4491

4592
for name, tc := range cases {

0 commit comments

Comments
 (0)