github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/message_test.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package p2p
    19  
    20  import (
    21  	"bytes"
    22  	"crypto/rand"
    23  	"testing"
    24  )
    25  
    26  // Size of Ethereum signature, for instance
    27  const randomDataSize = 65
    28  const fuzzingIterations = 1_000
    29  
    30  func randomData() ([]byte, error) {
    31  	data := make([]byte, randomDataSize)
    32  	_, err := rand.Read(data)
    33  	return data, err
    34  }
    35  
    36  func roundTrip(t *testing.T) {
    37  	var out bytes.Buffer
    38  	conn := newProtobufWireWriter(&out)
    39  	conn2 := newProtobufWireReader(&out)
    40  
    41  	data, err := randomData()
    42  	if err != nil {
    43  		t.Fatalf("Can't generate random data portion: %v", err)
    44  	}
    45  
    46  	msg := transportMsg{
    47  		topic: "test",
    48  		data:  data,
    49  	}
    50  	err = msg.writeTo(conn)
    51  	if err != nil {
    52  		t.Fatalf("Can't write data into conn: %v", err)
    53  	}
    54  
    55  	var msg2 transportMsg
    56  	err = msg2.readFrom(conn2)
    57  	if err != nil {
    58  		t.Fatalf("Can't read data from conn2: %v", err)
    59  	}
    60  
    61  	if !bytes.Equal(msg.data, msg2.data) {
    62  		t.Fatalf("Data wasn't properly recovered: original %x != recovered %x", msg.data, msg2.data)
    63  	}
    64  }
    65  
    66  func TestTransportMessageFuzzing(t *testing.T) {
    67  	for i := 0; i < fuzzingIterations; i++ {
    68  		roundTrip(t)
    69  	}
    70  }
    71  
    72  func TestTransportMessagePiping(t *testing.T) {
    73  	var out bytes.Buffer
    74  	conn := newProtobufWireWriter(&out)
    75  	conn2 := newProtobufWireReader(&out)
    76  
    77  	msgs := []*transportMsg{
    78  		{
    79  			id:         1,
    80  			statusCode: 1,
    81  			topic:      "topic1",
    82  			msg:        "msg1",
    83  			data:       []byte("data1"),
    84  		},
    85  		{
    86  			id:         2,
    87  			statusCode: 2,
    88  			topic:      "topic2",
    89  			msg:        "msg2",
    90  			data:       []byte("data2"),
    91  		},
    92  		{
    93  			id:         3,
    94  			statusCode: 3,
    95  			topic:      "topic3",
    96  			msg:        "msg3",
    97  			data:       []byte("data3"),
    98  		},
    99  		{
   100  			id:         4,
   101  			statusCode: 4,
   102  			topic:      "topic4",
   103  			msg:        "msg4",
   104  			data:       []byte("data4"),
   105  		},
   106  		{
   107  			id:         5,
   108  			statusCode: 5,
   109  			topic:      "topic5",
   110  			msg:        "msg5",
   111  			data:       []byte("data5"),
   112  		},
   113  	}
   114  
   115  	for _, msg := range msgs {
   116  		err := msg.writeTo(conn)
   117  		if err != nil {
   118  			t.Fatalf("msg.writeTo() error: %v", err)
   119  		}
   120  	}
   121  
   122  	for _, refMsg := range msgs {
   123  		msg := new(transportMsg)
   124  		err := msg.readFrom(conn2)
   125  		if err != nil {
   126  			t.Fatalf("msg.readFrom() error: %v", err)
   127  		}
   128  
   129  		if !(msg.id == refMsg.id &&
   130  			msg.statusCode == refMsg.statusCode &&
   131  			msg.topic == refMsg.topic &&
   132  			msg.msg == refMsg.msg &&
   133  			bytes.Compare(msg.data, refMsg.data) == 0) {
   134  			t.Fatal("channel transport messages are not equal!")
   135  		}
   136  	}
   137  
   138  }