github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/socketcan/frame_test.go (about)

     1  package socketcan
     2  
     3  import (
     4  	"testing"
     5  	"testing/quick"
     6  
     7  	"github.com/blueinnovationsgroup/can-go"
     8  	"gotest.tools/v3/assert"
     9  )
    10  
    11  func TestFrame_MarshalUnmarshalBinary_Property_Idempotent(t *testing.T) {
    12  	f := func(data [lengthOfFrame]byte) [lengthOfFrame]byte {
    13  		data[5], data[6], data[7] = 0, 0, 0 // padding+reserved fields
    14  		return data
    15  	}
    16  	g := func(data [lengthOfFrame]byte) [lengthOfFrame]byte {
    17  		var f frame
    18  		f.unmarshalBinary(data[:])
    19  		var newData [lengthOfFrame]byte
    20  		f.marshalBinary(newData[:])
    21  		return newData
    22  	}
    23  	assert.NilError(t, quick.CheckEqual(f, g, nil))
    24  }
    25  
    26  func TestFrame_EncodeDecode(t *testing.T) {
    27  	for _, tt := range []struct {
    28  		msg            string
    29  		frame          can.Frame
    30  		socketCANFrame frame
    31  	}{
    32  		{
    33  			msg: "data",
    34  			frame: can.Frame{
    35  				ID:     0x00000001,
    36  				Length: 8,
    37  				Data:   can.Data{1, 2, 3, 4, 5, 6, 7, 8},
    38  			},
    39  			socketCANFrame: frame{
    40  				idAndFlags:     0x00000001,
    41  				dataLengthCode: 8,
    42  				data:           [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
    43  			},
    44  		},
    45  		{
    46  			msg: "extended",
    47  			frame: can.Frame{
    48  				ID:         0x00000001,
    49  				IsExtended: true,
    50  			},
    51  			socketCANFrame: frame{
    52  				idAndFlags: 0x80000001,
    53  			},
    54  		},
    55  		{
    56  			msg: "remote",
    57  			frame: can.Frame{
    58  				ID:       0x00000001,
    59  				IsRemote: true,
    60  			},
    61  			socketCANFrame: frame{
    62  				idAndFlags: 0x40000001,
    63  			},
    64  		},
    65  		{
    66  			msg: "extended and remote",
    67  			frame: can.Frame{
    68  				ID:         0x00000001,
    69  				IsExtended: true,
    70  				IsRemote:   true,
    71  			},
    72  			socketCANFrame: frame{
    73  				idAndFlags: 0xc0000001,
    74  			},
    75  		},
    76  	} {
    77  		tt := tt
    78  		t.Run(tt.msg, func(t *testing.T) {
    79  			t.Run("encode", func(t *testing.T) {
    80  				var actual frame
    81  				actual.encodeFrame(tt.frame)
    82  				assert.Equal(t, tt.socketCANFrame, actual)
    83  			})
    84  			t.Run("decode", func(t *testing.T) {
    85  				assert.Equal(t, tt.frame, tt.socketCANFrame.decodeFrame())
    86  			})
    87  		})
    88  	}
    89  }
    90  
    91  func TestFrame_IsError(t *testing.T) {
    92  	assert.Assert(t, (&frame{idAndFlags: 0x20000001}).isError())
    93  	assert.Assert(t, !(&frame{idAndFlags: 0x00000001}).isError())
    94  }
    95  
    96  func TestFrame_DecodeErrorFrame(t *testing.T) {
    97  	for _, tt := range []struct {
    98  		msg      string
    99  		f        frame
   100  		expected ErrorFrame
   101  	}{
   102  		{
   103  			msg: "lost arbitration",
   104  			f: frame{
   105  				idAndFlags:     0x20000002,
   106  				dataLengthCode: 8,
   107  				data: [8]byte{
   108  					42,
   109  				},
   110  			},
   111  			expected: ErrorFrame{
   112  				ErrorClass:         ErrorClassLostArbitration,
   113  				LostArbitrationBit: 42,
   114  			},
   115  		},
   116  		{
   117  			msg: "controller",
   118  			f: frame{
   119  				idAndFlags:     0x20000004,
   120  				dataLengthCode: 8,
   121  				data: [8]byte{
   122  					0,
   123  					0x04,
   124  				},
   125  			},
   126  			expected: ErrorFrame{
   127  				ErrorClass:      ErrorClassController,
   128  				ControllerError: ControllerErrorRxWarning,
   129  			},
   130  		},
   131  		{
   132  			msg: "protocol violation",
   133  			f: frame{
   134  				idAndFlags:     0x20000008,
   135  				dataLengthCode: 8,
   136  				data: [8]byte{
   137  					0,
   138  					0,
   139  					0x10,
   140  					0x02,
   141  				},
   142  			},
   143  			expected: ErrorFrame{
   144  				ErrorClass:                     ErrorClassProtocolViolation,
   145  				ProtocolError:                  ProtocolViolationErrorBit1,
   146  				ProtocolViolationErrorLocation: ProtocolViolationErrorLocationID28To21,
   147  			},
   148  		},
   149  		{
   150  			msg: "transceiver",
   151  			f: frame{
   152  				idAndFlags:     0x20000010,
   153  				dataLengthCode: 8,
   154  				data: [8]byte{
   155  					0,
   156  					0,
   157  					0,
   158  					0,
   159  					0x07,
   160  				},
   161  			},
   162  			expected: ErrorFrame{
   163  				ErrorClass:       ErrorClassTransceiver,
   164  				TransceiverError: TransceiverErrorCANHShortToGND,
   165  			},
   166  		},
   167  		{
   168  			msg: "controller-specific information",
   169  			f: frame{
   170  				idAndFlags:     0x20000001,
   171  				dataLengthCode: 8,
   172  				data: [8]byte{
   173  					0,
   174  					0,
   175  					0,
   176  					0,
   177  					0,
   178  					1,
   179  					2,
   180  					3,
   181  				},
   182  			},
   183  			expected: ErrorFrame{
   184  				ErrorClass:                    ErrorClassTxTimeout,
   185  				ControllerSpecificInformation: [3]byte{1, 2, 3},
   186  			},
   187  		},
   188  	} {
   189  		tt := tt
   190  		t.Run(tt.msg, func(t *testing.T) {
   191  			assert.Equal(t, tt.expected, tt.f.decodeErrorFrame())
   192  		})
   193  	}
   194  }