github.com/amazechain/amc@v0.1.3/internal/network/package_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The AmazeChain library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package network
    18  
    19  //func TestHeader(t *testing.T) {
    20  //	var (
    21  //		header  Header
    22  //		msgType message.MessageType
    23  //		msg     []byte
    24  //		buf     *bytes.Buffer
    25  //		payload []byte
    26  //	)
    27  //
    28  //	msgType = message.MsgPingReq
    29  //	msg = []byte("this is a message")
    30  //	buf = new(bytes.Buffer)
    31  //
    32  //	header.Encode(buf, msgType, int32(len(msg)))
    33  //	buf.Write(msg)
    34  //
    35  //	//
    36  //	payload, _ = hexutil.Decode("0x50c402188dcdf09f062a34313244334b6f6f574d716d4c31334b597a5031687a794b43374e72437a35624c335250726163515179635a524150374175463768325f0803125b3059301306072a8648ce3d020106082a8648ce3d030107034200044848ff34cd7de683a77d575281b05000976751ecc205ad246c58a2aabd088c5f12f325dc79cc9f6655712cf6cf9f7493f0a97e7aec4cb8a4ffc75489705f87653a473045022100c4b0fa9f78f2df97776e98f80864cd0aa1eb4fbc8967ade10afca13be892c2b8022047dd6418cb4ff89e2f86e97ec8851da53465951273f69d51f08226d24d8c27e5425c0a0e2f616d632f6170702f312e302e301240356537663532376330386332393664303737363534373466393530326364643338343630373430343535373161633232393236313964653039366564393831351a080a00120410c98706")
    37  //	buf.Reset()
    38  //	buf.Write(payload)
    39  //	// or
    40  //	//payload = buf.Bytes()
    41  //
    42  //	t.Logf("encode message: %s", hexutil.Encode(payload))
    43  //
    44  //	decodeMsgType, remainingLength, err := header.Decode(buf)
    45  //
    46  //	decodeMag, _ := io.ReadAll(buf)
    47  //
    48  //	var protoMessage msg_proto.MessageData
    49  //	if err = proto.Unmarshal(payload, &protoMessage); err != nil {
    50  //		t.Fatalf("can not decode protoMessage err: %s", err)
    51  //	}
    52  //
    53  //	t.Logf("Decode  protoMessage: %v", protoMessage)
    54  //
    55  //	switch decodeMsgType {
    56  //	case message.MsgAppHandshake:
    57  //		var handshakeMessage msg_proto.ProtocolHandshakeMessage
    58  //		if err = proto.Unmarshal(protoMessage.Payload, &handshakeMessage); err != nil {
    59  //			t.Fatalf("can not decode HandshakeMessage err: %s", err)
    60  //		}
    61  //		t.Logf("Decode  HandshakeMessage: %v", handshakeMessage)
    62  //	}
    63  //
    64  //	t.Logf("Decode  message type string:%s, type int:%d, decode len:%d, get len: %d", decodeMsgType, decodeMsgType, remainingLength, len(decodeMag))
    65  //
    66  //	if err != nil {
    67  //		t.Fatalf("can not decode message err: %s", err)
    68  //	}
    69  //
    70  //	if decodeMsgType != msgType {
    71  //		t.Fatalf("decode MsgType err want: %s, get: %s", msgType, decodeMsgType)
    72  //	}
    73  //
    74  //	if remainingLength != int32(len(msg)) {
    75  //		t.Fatalf("decode remainingLength err want: %d, get: %d", int32(len(msg)), remainingLength)
    76  //	}
    77  //
    78  //}