Skip to content

Commit 41ff4a6

Browse files
committed
sphinx: add variable sized EOB spec JSON tests
1 parent 7f17f54 commit 41ff4a6

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

sphinx_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,5 +772,143 @@ func TestSphinxHopVariableSizedPayloads(t *testing.T) {
772772
}
773773
}
774774
}
775+
776+
// testFileName is the name of the multi-frame onion test file.
777+
const testFileName = "testdata/onion-test-multi-frame.json"
778+
779+
type jsonHop struct {
780+
Type string `json:"type"`
781+
782+
Pubkey string `json:"pubkey"`
783+
784+
Payload string `json:"payload"`
785+
}
786+
787+
type payloadTestCase struct {
788+
SessionKey string `json:"session_key"`
789+
790+
AssociatedData string `json:"associated_data"`
791+
792+
Hops []jsonHop `json:"hops"`
793+
}
794+
795+
type jsonTestCase struct {
796+
Comment string `json:"comment"`
797+
798+
Generate payloadTestCase `json:"generate"`
799+
800+
Onion string `json:"onion"`
801+
802+
Decode []string `json:"decode"`
803+
}
804+
805+
// jsonTypeToPayloadType maps the JSON payload type to our concrete PayloadType
806+
// type.
807+
func jsonTypeToPayloadType(jsonType string) PayloadType {
808+
switch jsonType {
809+
case "raw":
810+
fallthrough
811+
case "tlv":
812+
return PayloadTLV
813+
814+
case "legacy":
815+
return PayloadLegacy
816+
817+
default:
818+
panic(fmt.Sprintf("unknown payload type: %v", jsonType))
819+
}
820+
}
821+
822+
// TestVariablePayloadOnion tests that if we construct a packet that contains a
823+
// mix of the old and new payload format, that we match the version that's
824+
// included in the spec.
825+
func TestVariablePayloadOnion(t *testing.T) {
826+
t.Parallel()
827+
828+
// First, we'll read out the raw JSOn file at the target location.
829+
jsonBytes, err := ioutil.ReadFile(testFileName)
830+
if err != nil {
831+
t.Fatalf("unable to read json file: %v", err)
832+
}
833+
834+
// Once we have the raw file, we'll unpack it into our jsonTestCase
835+
// struct defined above.
836+
testCase := &jsonTestCase{}
837+
if err := json.Unmarshal(jsonBytes, testCase); err != nil {
838+
t.Fatalf("unable to parse spec json file: %v", err)
839+
}
840+
841+
// Next, we'll populate a new OnionHop using the information included
842+
// in this test case.
843+
var route PaymentPath
844+
for i, hop := range testCase.Generate.Hops {
845+
pubKeyBytes, err := hex.DecodeString(hop.Pubkey)
846+
if err != nil {
847+
t.Fatalf("unable to decode pubkey: %v", err)
848+
}
849+
pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
850+
if err != nil {
851+
t.Fatalf("unable to parse BOLT 4 pubkey #%d: %v", i, err)
852+
}
853+
854+
payload, err := hex.DecodeString(hop.Payload)
855+
if err != nil {
856+
t.Fatalf("unable to decode payload: %v", err)
857+
}
858+
859+
payloadType := jsonTypeToPayloadType(hop.Type)
860+
route[i] = OnionHop{
861+
NodePub: *pubKey,
862+
HopPayload: HopPayload{
863+
Type: payloadType,
864+
Payload: payload,
865+
},
866+
}
867+
868+
if payloadType == PayloadLegacy {
869+
route[i].HopPayload.Payload = append(
870+
[]byte{0x00}, route[i].HopPayload.Payload...,
871+
)
872+
873+
route[i].HopPayload.Payload = append(
874+
route[i].HopPayload.Payload,
875+
bytes.Repeat([]byte{0x00}, NumPaddingBytes)...,
876+
)
877+
}
878+
}
879+
880+
finalPacket, err := hex.DecodeString(testCase.Onion)
881+
if err != nil {
882+
t.Fatalf("unable to decode packet: %v", err)
883+
}
884+
885+
sessionKeyBytes, err := hex.DecodeString(testCase.Generate.SessionKey)
886+
if err != nil {
887+
t.Fatalf("unable to generate session key: %v", err)
888+
}
889+
890+
associatedData, err := hex.DecodeString(testCase.Generate.AssociatedData)
891+
if err != nil {
892+
t.Fatalf("unable to decode AD: %v", err)
893+
}
894+
895+
// With all the required data assembled, we'll craft a new packet.
896+
sessionKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), sessionKeyBytes)
897+
pkt, err := NewOnionPacket(&route, sessionKey, associatedData)
898+
if err != nil {
899+
t.Fatalf("unable to construct onion packet: %v", err)
900+
}
901+
902+
var b bytes.Buffer
903+
if err := pkt.Encode(&b); err != nil {
904+
t.Fatalf("unable to decode onion packet: %v", err)
905+
}
906+
907+
// Finally, we expect that our packet matches the packet included in
908+
// the spec's test vectors.
909+
if bytes.Compare(b.Bytes(), finalPacket) != 0 {
910+
t.Fatalf("final packet does not match expected BOLT 4 packet, "+
911+
"want: %s, got %s", hex.EncodeToString(finalPacket),
912+
hex.EncodeToString(b.Bytes()))
775913
}
776914
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"comment": "A testcase for a variable length hop_payload. The third payload is 256 bytes long.",
3+
"generate": {
4+
"session_key": "4141414141414141414141414141414141414141414141414141414141414141",
5+
"associated_data": "4242424242424242424242424242424242424242424242424242424242424242",
6+
"hops": [
7+
{
8+
"type": "legacy",
9+
"pubkey": "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
10+
"payload": "0000000000000000000000000000000000000000"
11+
},
12+
{
13+
"type": "tlv",
14+
"pubkey": "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
15+
"payload": "0101010101010101000000000000000100000001"
16+
},
17+
{
18+
"type": "raw",
19+
"pubkey": "027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007",
20+
"payload": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
21+
},
22+
{
23+
"type": "tlv",
24+
"pubkey": "032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991",
25+
"payload": "0303030303030303000000000000000300000003"
26+
},
27+
{
28+
"type": "legacy",
29+
"pubkey": "02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145",
30+
"payload": "0404040404040404000000000000000400000004"
31+
}
32+
]
33+
},
34+
"onion": "0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a7181924e4b6c645f2b6eecc821084ec9b16dfb9d2e7622d4c14db4fc5ecdfc07eac50f7d61ab590531cf08000178a333a347f8b4072e7d3ae44f4f309e150b49886d3f044cd6462be389c830784aba767682923c8683404aaf9a8e8d7178977d7094a1ae549f89338c0777551f874159eb42d3a59fb9285ad4e24883f27de23942ec966611e99bee1cee503455be9e8e642cef6cef7b9864130f692283f8a973d47a8f1c1726b6e59969385975c766e35737c8d76388b64f748ee7943ffb0e2ee45c57a1abc40762ae598723d21bd184e2b338f68ebff47219357bd19cd7e01e2337b806ef4d717888e129e59cd3dc31e6201ccb2fd6d7499836f37a993262468bcb3a4dcd03a22818aca49c6b7b9b8e9e870045631d8e039b066ff86e0d1b7291f71cefa7264c70404a8e538b566c17ccc5feab231401e6c08a01bd5edfc1aa8e3e533b96e82d1f91118d508924b923531929aea889fcdf050597c681185f336b1da63b0939aa2b7c50b21b5eb7b6ad66c81fab98a3cdf73f658149e7e9ced4edde5d38c9b8f92e16f6b4ab13d7fca6a0e4ecc9f9de611a90da6e99c39551094c56e3196f282c5dffd9fc4b2fc12f3bca8e6fe47eb45fbdd3be21a8a8d200797eae3c9a0497132f92410d804977408494dff49dd3d8bce248e0b74fd9e6f0f7102c25ddfa02bd9ad9f746abbfa337ef811d5345a9e16b60de1767b209645ba40bd1f9a5f75bc04feca9b27c5554be4fe83fac2cb83aa447a817bb85ae966c68b420063833fada375e2f515965e687a45699632902672c654d1d18d7bcbf55e8fa57f63f2da449f8e1e606e8722df081e5f193fc4179feb99ad22819afdeef211f7c54afdba92aeef0c00b7bc2b65a4813c01f907a8377585708f2d4c940a25328e585714c8ded0a9a4d7a6de1027c1cb7a0198cd3db68b58c0704dfd0cfbe624e9cd18cc0ae5d96697bb476708b9ee0403d211e64e0d5a7683a7a9a140c02f0ff1c6e67a302941b4052bdea8a63e70a3ad62c5b89c698f1fd3c7685cb49705096cad702d02d93bcb1c27a409f4c9bddec001205ca4a2740f19b50900be81c7e847f1a863deea8d35701f1355cad8db57b1d4eb2ab4e29587734785abfb46ddede71928213d7d089dfdeda052827f459f1688cc0935bd47e7bcec27427c8376dcce7e22699567c0d145f8a7db33f6758815f1f15f9f7a9760dec4f34ae095edda4c64e9735bdd029c4e32c2ee31ba47ec5e6bdb97813d52dbd15b4e0b7a2c7f790ae64104d99f38c127f0a093288fa34144adb16b8968d4fa7656fcec99de8503dd46d3b03620a71c7cd085364abd30dccf7fbda25a1cdc102600149c9af1c97aa0372cd2e1909f28ac5c686f432b310e79528c9b8b9e8f314c1e74621ce6308ad2278b81d460892e0d9dd38b7c76d58be6dfd10ae7583ee1e7ef5b3f6f78dc60af0950df1b00cc55b6d178ba2e476bea0eaeef49323b83f05804159e7aef4eed4cc60dd07be76f067dfd0bcfb0b806b69ba921336a20c43c832d0cab8fa3ddeb29e3bf07b0d98a112eb07802756235a49d44a8b82a950d84e95e01971f0e106ccb337f07384e21620e0ad39e16ed9edca123226cf55ac44f449eeb53e38a7f27d101806e4823e4efcc887414240ee6826c4a5cb1c6443ad36ebf905a435c1d9054e54173911b17b5b40f60b3d9fd5f12eac54ca1e20191f5f18544d5fd3d665e9bcef96fb44b76110aa64d9db4c86c9513cbdad546538e8aec521fbe83ceac5e74a15629f1ed0b870a1d0d1e5680b6d6100d1bdd9a2ba3be3772f71854cb12ae9b3afd87cda738dcd107fe56a15f1877450cd0e",
35+
"decode": [
36+
"4141414141414141414141414141414141414141414141414141414141414141",
37+
"4242424242424242424242424242424242424242424242424242424242424242",
38+
"4343434343434343434343434343434343434343434343434343434343434343",
39+
"4444444444444444444444444444444444444444444444444444444444444444",
40+
"4545454545454545454545454545454545454545454545454545454545454545"
41+
]
42+
}

0 commit comments

Comments
 (0)