github.com/la5nta/wl2k-go@v0.11.8/transport/ax25/agwpe/frame_test.go (about)

     1  package agwpe
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestHeaderRoundtrip(t *testing.T) {
    11  	tests := []header{
    12  		{},
    13  		{DataLen: 1024},
    14  		{
    15  			From:    callsign{'L', 'A', '5', 'N', 'T', 'A', '-', '1', 0x00, 0x00},
    16  			To:      callsign{'L', 'A', '5', 'N', 'T', 'A', '-', '2', 0x00, 0x00},
    17  			DataLen: 1024,
    18  		},
    19  	}
    20  
    21  	for i, h := range tests {
    22  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    23  			var buf bytes.Buffer
    24  			if _, err := h.WriteTo(&buf); err != nil {
    25  				t.Fatal(err)
    26  			}
    27  			var got header
    28  			if _, err := got.ReadFrom(&buf); err != nil {
    29  				t.Fatal(err)
    30  			}
    31  			if !reflect.DeepEqual(h, got) {
    32  				t.Error("not equal", h, got)
    33  			}
    34  		})
    35  	}
    36  }
    37  
    38  func TestFrameRoundtrip(t *testing.T) {
    39  	tests := []frame{
    40  		{},
    41  		{
    42  			header: header{DataLen: 1},
    43  			Data:   []byte{0x10},
    44  		},
    45  	}
    46  
    47  	for i, f := range tests {
    48  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    49  			var buf bytes.Buffer
    50  			if _, err := f.WriteTo(&buf); err != nil {
    51  				t.Fatal(err)
    52  			}
    53  			var got frame
    54  			if _, err := got.ReadFrom(&buf); err != nil {
    55  				t.Fatal(err)
    56  			}
    57  			if !reflect.DeepEqual(f.header, got.header) {
    58  				t.Error("header not equal")
    59  			}
    60  			if !reflect.DeepEqual(f.Data, got.Data) {
    61  				t.Error("data not equal")
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestFrameDecode(t *testing.T) {
    68  	raw := []byte{
    69  		0x01, 0x00, 0x00, 0x00, 0x4D, 0x00, 0xCF, 0x00, 0x4C, 0x55, 0x37, 0x44, 0x49, 0x44, 0x2D, 0x34,
    70  		0x00, 0x00, 0x4E, 0x4F, 0x44, 0x45, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
    71  		0x00, 0x00, 0x00, 0x00, 0xFF, 0x41, 0x42, 0x52, 0x4F, 0x57, 0x4E,
    72  	}
    73  	expect := frame{
    74  		header: header{
    75  			Port:     1,
    76  			DataKind: 'M',
    77  			PID:      207,
    78  			From:     callsignFromString("LU7DID-4"),
    79  			To:       callsignFromString("NODES"),
    80  			DataLen:  7,
    81  		},
    82  		Data: []byte{0xFF, 0x41, 0x42, 0x52, 0x4F, 0x57, 0x4E},
    83  	}
    84  	got := frame{}
    85  	if _, err := got.ReadFrom(bytes.NewReader(raw)); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	if !reflect.DeepEqual(expect, got) {
    89  		t.Error("got unexpected output")
    90  	}
    91  }