// Copyright (c) 2013-2016 The btcsuite developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. package appmessage import ( "bytes" "io" "reflect" "testing" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/pkg/errors" "github.com/davecgh/go-spew/spew" ) // mainnetGenesisHash is the hash of the first block in the block DAG for the // main network (genesis block). var mainnetGenesisHash = &externalapi.DomainHash{ 0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25, 0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b, 0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1, 0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63, } // simnetGenesisHash is the hash of the first block in the block DAG for the // simulation test network. var simnetGenesisHash = &externalapi.DomainHash{ 0x9d, 0x89, 0xb0, 0x6e, 0xb3, 0x47, 0xb5, 0x6e, 0xcd, 0x6c, 0x63, 0x99, 0x45, 0x91, 0xd5, 0xce, 0x9b, 0x43, 0x05, 0xc1, 0xa5, 0x5e, 0x2a, 0xda, 0x90, 0x4c, 0xf0, 0x6c, 0x4d, 0x5f, 0xd3, 0x62, } // mainnetGenesisMerkleRoot is the hash of the first transaction in the genesis // block for the main network. var mainnetGenesisMerkleRoot = &externalapi.DomainHash{ 0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a, 0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f, 0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a, 0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b, } var exampleAcceptedIDMerkleRoot = &externalapi.DomainHash{ 0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, 0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87, 0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63, 0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F, } var exampleUTXOCommitment = &externalapi.DomainHash{ 0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, 0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87, 0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63, 0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F, } // TestElementEncoding tests appmessage encode and decode for various element types. This // is mainly to test the "fast" paths in readElement and writeElement which use // type assertions to avoid reflection when possible. func TestElementEncoding(t *testing.T) { tests := []struct { in interface{} // Value to encode buf []byte // Encoded value }{ {int32(1), []byte{0x01, 0x00, 0x00, 0x00}}, {uint32(256), []byte{0x00, 0x01, 0x00, 0x00}}, { int64(65536), []byte{0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, }, { uint64(4294967296), []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, }, { true, []byte{0x01}, }, { false, []byte{0x00}, }, { [4]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, }, { MessageCommand(0x10), []byte{ 0x10, 0x00, 0x00, 0x00, }, }, { [16]byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, }, []byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, }, }, { (*externalapi.DomainHash)(&[externalapi.DomainHashSize]byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, }), []byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, }, }, { ServiceFlag(SFNodeNetwork), []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, }, { KaspaNet(Mainnet), []byte{0x1d, 0xf7, 0xdc, 0x3d}, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Write to appmessage format. var buf bytes.Buffer err := WriteElement(&buf, test.in) if err != nil { t.Errorf("writeElement #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("writeElement #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Read from appmessage format. rbuf := bytes.NewReader(test.buf) val := test.in if reflect.ValueOf(test.in).Kind() != reflect.Ptr { val = reflect.New(reflect.TypeOf(test.in)).Interface() } err = ReadElement(rbuf, val) if err != nil { t.Errorf("readElement #%d error %v", i, err) continue } ival := val if reflect.ValueOf(test.in).Kind() != reflect.Ptr { ival = reflect.Indirect(reflect.ValueOf(val)).Interface() } if !reflect.DeepEqual(ival, test.in) { t.Errorf("readElement #%d\n got: %s want: %s", i, spew.Sdump(ival), spew.Sdump(test.in)) continue } } } // TestElementEncodingErrors performs negative tests against appmessage encode and decode // of various element types to confirm error paths work correctly. func TestElementEncodingErrors(t *testing.T) { type writeElementReflect int32 tests := []struct { in interface{} // Value to encode max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ {int32(1), 0, io.ErrShortWrite, io.EOF}, {uint32(256), 0, io.ErrShortWrite, io.EOF}, {int64(65536), 0, io.ErrShortWrite, io.EOF}, {true, 0, io.ErrShortWrite, io.EOF}, {[4]byte{0x01, 0x02, 0x03, 0x04}, 0, io.ErrShortWrite, io.EOF}, { MessageCommand(10), 0, io.ErrShortWrite, io.EOF, }, { [16]byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, }, 0, io.ErrShortWrite, io.EOF, }, { (*externalapi.DomainHash)(&[externalapi.DomainHashSize]byte{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, }), 0, io.ErrShortWrite, io.EOF, }, {ServiceFlag(SFNodeNetwork), 0, io.ErrShortWrite, io.EOF}, {KaspaNet(Mainnet), 0, io.ErrShortWrite, io.EOF}, // Type with no supported encoding. {writeElementReflect(0), 0, errNoEncodingForType, errNoEncodingForType}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to appmessage format. w := newFixedWriter(test.max) err := WriteElement(w, test.in) if !errors.Is(err, test.writeErr) { t.Errorf("writeElement #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // Decode from appmessage format. r := newFixedReader(test.max, nil) val := test.in if reflect.ValueOf(test.in).Kind() != reflect.Ptr { val = reflect.New(reflect.TypeOf(test.in)).Interface() } err = ReadElement(r, val) if !errors.Is(err, test.readErr) { t.Errorf("readElement #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } } }