Skip to content

Commit 7f38495

Browse files
committed
testdata+test: add pure tlv onion test
Add a TestTLVPayloadOnion test that tests the newer spec test vector which tests the construction of an onion in the case where all the hops are using the TLV version payload.
1 parent f30c0a7 commit 7f38495

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

sphinx_test.go

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"os"
910
"reflect"
1011
"testing"
@@ -750,8 +751,14 @@ func TestSphinxHopVariableSizedPayloads(t *testing.T) {
750751
}
751752
}
752753

753-
// testMultiFrameFileName is the name of the multi-frame onion test file.
754-
const testMultiFrameFileName = "testdata/onion-test-multi-frame.json"
754+
const (
755+
// testMultiFrameFileName is the name of the multi-frame onion test
756+
// file.
757+
testMultiFrameFileName = "testdata/onion-test-multi-frame.json"
758+
759+
// testTLVFileName is the name of the tlv-payload-only onion test file.
760+
testTLVFileName = "testdata/onion-test.json"
761+
)
755762

756763
type jsonHop struct {
757764
Type string `json:"type"`
@@ -796,6 +803,85 @@ func jsonTypeToPayloadType(jsonType string) PayloadType {
796803
}
797804
}
798805

806+
// TestTLVPayloadOnion tests the construction of an onion where all the payloads
807+
// are of the TLV type.
808+
func TestTLVPayloadOnion(t *testing.T) {
809+
t.Parallel()
810+
811+
// First, we'll read out the raw JSON file at the target location.
812+
jsonBytes, err := os.ReadFile(testTLVFileName)
813+
require.NoError(t, err)
814+
815+
// Once we have the raw file, we'll unpack it into our jsonTestCase
816+
// struct defined above.
817+
testCase := &jsonTestCase{}
818+
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
819+
820+
// Next, we'll populate a new OnionHop using the information included
821+
// in this test case.
822+
var route PaymentPath
823+
for i, hop := range testCase.Generate.Hops {
824+
pubKeyBytes, err := hex.DecodeString(hop.Pubkey)
825+
require.NoError(t, err)
826+
827+
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
828+
require.NoError(t, err)
829+
830+
// The test has already encoded the length of the TLV payloads,
831+
// so to make it compatible with our PaymentPath, we just
832+
// extract the payload.
833+
payload, err := hex.DecodeString(hop.Payload)
834+
require.NoError(t, err)
835+
836+
var (
837+
bufReader = bytes.NewReader(payload)
838+
b [8]byte
839+
)
840+
varInt, err := ReadVarInt(bufReader, &b)
841+
require.NoError(t, err)
842+
843+
payloadSize := uint32(varInt)
844+
require.NoError(t, err)
845+
846+
hopPayload := make([]byte, payloadSize)
847+
_, err = io.ReadFull(bufReader, hopPayload)
848+
require.NoError(t, err)
849+
850+
route[i] = OnionHop{
851+
NodePub: *pubKey,
852+
HopPayload: HopPayload{
853+
Type: PayloadTLV,
854+
Payload: hopPayload,
855+
},
856+
}
857+
}
858+
859+
finalPacket, err := hex.DecodeString(testCase.Onion)
860+
require.NoError(t, err)
861+
862+
sessionKeyBytes, err := hex.DecodeString(testCase.Generate.SessionKey)
863+
require.NoError(t, err)
864+
865+
assocData, err := hex.DecodeString(testCase.Generate.AssociatedData)
866+
require.NoError(t, err)
867+
868+
// With all the required data assembled, we'll craft a new packet.
869+
sessionKey, _ := btcec.PrivKeyFromBytes(sessionKeyBytes)
870+
pkt, err := NewOnionPacket(
871+
&route, sessionKey, assocData, DeterministicPacketFiller,
872+
)
873+
require.NoError(t, err)
874+
875+
var b bytes.Buffer
876+
require.NoError(t, pkt.Encode(&b))
877+
878+
// Finally, we expect that our packet matches the packet included in
879+
// the spec's test vectors.
880+
require.Equalf(t, b.Bytes(), finalPacket, "final packet does not "+
881+
"match expected BOLT 4 packet, want: %s, got %s",
882+
hex.EncodeToString(finalPacket), hex.EncodeToString(b.Bytes()))
883+
}
884+
799885
// TestVariablePayloadOnion tests that if we construct a packet that contains a
800886
// mix of the old and new payload format, that we match the version that's
801887
// included in the spec.

testdata/onion-test.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"comment": "A testcase for a variable length hop_payload. The third payload is 275 bytes long.",
3+
"generate": {
4+
"session_key": "4141414141414141414141414141414141414141414141414141414141414141",
5+
"associated_data": "4242424242424242424242424242424242424242424242424242424242424242",
6+
"hops": [
7+
{
8+
"pubkey": "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
9+
"payload": "1202023a98040205dc06080000000000000001"
10+
},
11+
{
12+
"pubkey": "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
13+
"payload": "52020236b00402057806080000000000000002fd02013c0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f"
14+
},
15+
{
16+
"pubkey": "027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
17+
"payload": "12020230d4040204e206080000000000000003"
18+
},
19+
{
20+
"pubkey": "032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
21+
"payload": "1202022710040203e806080000000000000004"
22+
},
23+
{
24+
"pubkey": "02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
25+
"payload": "fd011002022710040203e8082224a33562c54507a9334e79f0dc4f17d407e6d7c61f0e2f3d0d38599502f617042710fd012de02a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a"
26+
}
27+
]
28+
},
29+
"onion": "0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619f7f3416a5aa36dc7eeb3ec6d421e9615471ab870a33ac07fa5d5a51df0a8823aabe3fea3f90d387529d4f72837f9e687230371ccd8d263072206dbed0234f6505e21e282abd8c0e4f5b9ff8042800bbab065036eadd0149b37f27dde664725a49866e052e809d2b0198ab9610faa656bbf4ec516763a59f8f42c171b179166ba38958d4f51b39b3e98706e2d14a2dafd6a5df808093abfca5aeaaca16eded5db7d21fb0294dd1a163edf0fb445d5c8d7d688d6dd9c541762bf5a5123bf9939d957fe648416e88f1b0928bfa034982b22548e1a4d922690eecf546275afb233acf4323974680779f1a964cfe687456035cc0fba8a5428430b390f0057b6d1fe9a8875bfa89693eeb838ce59f09d207a503ee6f6299c92d6361bc335fcbf9b5cd44747aadce2ce6069cfdc3d671daef9f8ae590cf93d957c9e873e9a1bc62d9640dc8fc39c14902d49a1c80239b6c5b7fd91d05878cbf5ffc7db2569f47c43d6c0d27c438abff276e87364deb8858a37e5a62c446af95d8b786eaf0b5fcf78d98b41496794f8dcaac4eef34b2acfb94c7e8c32a9e9866a8fa0b6f2a06f00a1ccde569f97eec05c803ba7500acc96691d8898d73d8e6a47b8f43c3d5de74458d20eda61474c426359677001fbd75a74d7d5db6cb4feb83122f133206203e4e2d293f838bf8c8b3a29acb321315100b87e80e0edb272ee80fda944e3fb6084ed4d7f7c7d21c69d9da43d31a90b70693f9b0cc3eac74c11ab8ff655905688916cfa4ef0bd04135f2e50b7c689a21d04e8e981e74c6058188b9b1f9dfc3eec6838e9ffbcf22ce738d8a177c19318dffef090cee67e12de1a3e2a39f61247547ba5257489cbc11d7d91ed34617fcc42f7a9da2e3cf31a94a210a1018143173913c38f60e62b24bf0d7518f38b5bab3e6a1f8aeb35e31d6442c8abb5178efc892d2e787d79c6ad9e2fc271792983fa9955ac4d1d84a36c024071bc6e431b625519d556af38185601f70e29035ea6a09c8b676c9d88cf7e05e0f17098b584c4168735940263f940033a220f40be4c85344128b14beb9e75696db37014107801a59b13e89cd9d2258c169d523be6d31552c44c82ff4bb18ec9f099f3bf0e5b1bb2ba9a87d7e26f98d294927b600b5529c47e04d98956677cbcee8fa2b60f49776d8b8c367465b7c626da53700684fb6c918ead0eab8360e4f60edd25b4f43816a75ecf70f909301825b512469f8389d79402311d8aecb7b3ef8599e79485a4388d87744d899f7c47ee644361e17040a7958c8911be6f463ab6a9b2afacd688ec55ef517b38f1339efc54487232798bb25522ff4572ff68567fe830f92f7b8113efce3e98c3fffbaedce4fd8b50e41da97c0c08e423a72689cc68e68f752a5e3a9003e64e35c957ca2e1c48bb6f64b05f56b70b575ad2f278d57850a7ad568c24a4d32a3d74b29f03dc125488bc7c637da582357f40b0a52d16b3b40bb2c2315d03360bc24209e20972c200566bcf3bbe5c5b0aedd83132a8a4d5b4242ba370b6d67d9b67eb01052d132c7866b9cb502e44796d9d356e4e3cb47cc527322cd24976fe7c9257a2864151a38e568ef7a79f10d6ef27cc04ce382347a2488b1f404fdbf407fe1ca1c9d0d5649e34800e25e18951c98cae9f43555eef65fee1ea8f15828807366c3b612cd5753bf9fb8fced08855f742cddd6f765f74254f03186683d646e6f09ac2805586c7cf11998357cafc5df3f285329366f475130c928b2dceba4aa383758e7a9d20705c4bb9db619e2992f608a1ba65db254bb389468741d0502e2588aeb54390ac600c19af5c8e61383fc1bebe0029e4474051e4ef908828db9cca13277ef65db3fd47ccc2179126aaefb627719f421e20",
30+
"decode": [
31+
"4141414141414141414141414141414141414141414141414141414141414141",
32+
"4242424242424242424242424242424242424242424242424242424242424242",
33+
"4343434343434343434343434343434343434343434343434343434343434343",
34+
"4444444444444444444444444444444444444444444444444444444444444444",
35+
"4545454545454545454545454545454545454545454545454545454545454545"
36+
]
37+
}

0 commit comments

Comments
 (0)