github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/socketcan/receiver_test.go (about) 1 package socketcan 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/blueinnovationsgroup/can-go" 9 "gotest.tools/v3/assert" 10 ) 11 12 func TestReceiver_ReceiveFrames_Options(t *testing.T) { 13 testReceive := func(opt ReceiverOption) { 14 input := []byte{ 15 // id---------------> | dlc | padding-------> | data----------------------------------------> | 16 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17 } 18 expected := can.Frame{ID: 0x01, Length: 2, Data: can.Data{0x12, 0x34}} 19 receiver := NewReceiver(io.NopCloser(bytes.NewReader(input)), opt) 20 assert.Assert(t, receiver.Receive(), "expecting 1 CAN frames") 21 assert.NilError(t, receiver.Err()) 22 assert.Assert(t, !receiver.HasErrorFrame()) 23 assert.DeepEqual(t, expected, receiver.Frame()) 24 assert.Assert(t, !receiver.Receive(), "expecting exactly 1 CAN frames") 25 assert.NilError(t, receiver.Err()) 26 } 27 28 // no options 29 testReceive(func(*receiverOpts) {}) 30 31 // frame interceptor 32 run := false 33 intFunc := func(can.Frame) { 34 run = true 35 } 36 testReceive(ReceiverFrameInterceptor(intFunc)) 37 assert.Assert(t, run) 38 } 39 40 func TestReceiver_ReceiveFrames(t *testing.T) { 41 for _, tt := range []struct { 42 msg string 43 input []byte 44 expectedFrames []can.Frame 45 }{ 46 { 47 msg: "no data", 48 input: []byte{}, 49 expectedFrames: []can.Frame{}, 50 }, 51 { 52 msg: "incomplete frame", 53 input: []byte{ 54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 55 }, 56 expectedFrames: []can.Frame{}, 57 }, 58 { 59 msg: "whole single frame", 60 input: []byte{ 61 // id---------------> | dlc | padding-------> | data----------------------------------------> | 62 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 63 }, 64 expectedFrames: []can.Frame{ 65 {ID: 0x01, Length: 2, Data: can.Data{0x12, 0x34}}, 66 }, 67 }, 68 { 69 msg: "one whole one incomplete", 70 input: []byte{ 71 // id---------------> | dlc | padding-------> | data----------------------------------------> | 72 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 73 0x00, 74 }, 75 expectedFrames: []can.Frame{ 76 {ID: 0x01, Length: 2, Data: can.Data{0x12, 0x34}}, 77 }, 78 }, 79 { 80 msg: "two whole frames", 81 input: []byte{ 82 // id---------------> | dlc | padding-------> | data----------------------------------------> | 83 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 84 // id---------------> | dlc | padding-------> | data----------------------------------------> | 85 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 86 }, 87 expectedFrames: []can.Frame{ 88 {ID: 0x01, Length: 2, Data: can.Data{0x12, 0x34}}, 89 {ID: 0x02, Length: 2, Data: can.Data{0x56, 0x78}}, 90 }, 91 }, 92 } { 93 tt := tt 94 t.Run(tt.msg, func(t *testing.T) { 95 receiver := NewReceiver(io.NopCloser(bytes.NewReader(tt.input))) 96 for i, expected := range tt.expectedFrames { 97 assert.Assert(t, receiver.Receive(), "expecting %d CAN frames", i+1) 98 assert.NilError(t, receiver.Err()) 99 assert.Assert(t, !receiver.HasErrorFrame()) 100 assert.DeepEqual(t, expected, receiver.Frame()) 101 } 102 assert.Assert(t, !receiver.Receive(), "expecting exactly %d CAN frames", len(tt.expectedFrames)) 103 assert.NilError(t, receiver.Err()) 104 }) 105 } 106 } 107 108 func TestReceiver_ReceiveErrorFrame(t *testing.T) { 109 input := []byte{ 110 // frame 111 // id---------------> | dlc | padding-------> | data----------------------------------------> | 112 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 113 // error frame 114 // id---------------> | dlc | padding-------> | data----------------------------------------> | 115 0x01, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 116 // frame 117 // id---------------> | dlc | padding-------> | data----------------------------------------> | 118 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 119 } 120 receiver := NewReceiver(io.NopCloser(bytes.NewReader(input))) 121 // expect frame 122 assert.Assert(t, receiver.Receive()) 123 assert.Assert(t, !receiver.HasErrorFrame()) 124 assert.Equal(t, can.Frame{ID: 0x01, Length: 2, Data: can.Data{0x12, 0x34}}, receiver.Frame()) 125 // expect error frame 126 assert.Assert(t, receiver.Receive()) 127 assert.Assert(t, receiver.HasErrorFrame()) 128 assert.Equal(t, ErrorFrame{ErrorClass: ErrorClassTxTimeout}, receiver.ErrorFrame()) 129 // expect frame 130 assert.Assert(t, receiver.Receive()) 131 assert.Assert(t, !receiver.HasErrorFrame()) 132 assert.Equal(t, can.Frame{ID: 0x02, Length: 2, Data: can.Data{0x12, 0x34}}, receiver.Frame()) 133 // expect end of stream 134 assert.Assert(t, !receiver.Receive()) 135 assert.NilError(t, receiver.Err()) 136 }