|
| 1 | +package nmea |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestABM(t *testing.T) { |
| 9 | + var tests = []struct { |
| 10 | + name string |
| 11 | + raw string |
| 12 | + err string |
| 13 | + msg ABM |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "Good single fragment message", |
| 17 | + raw: "!AIABM,26,2,1,3381581370,3,8,177KQJ5000G?tO`K>RA1wUbN0TKH,0*02", |
| 18 | + msg: ABM{ |
| 19 | + NumFragments: 26, |
| 20 | + FragmentNumber: 2, |
| 21 | + MessageID: 1, |
| 22 | + MMSI: "3381581370", |
| 23 | + Channel: "3", |
| 24 | + VDLMessageNumber: 8, |
| 25 | + Payload: []byte{ |
| 26 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, // 10 |
| 27 | + 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x0, 0x1, // 20 |
| 28 | + 0x1, 0x0, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, // 30 |
| 29 | + 0x0, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, // 40 |
| 30 | + 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 50 |
| 31 | + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 60 |
| 32 | + 0x0, 0x1, 0x0, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, // 70 |
| 33 | + 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x1, // 80 |
| 34 | + 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, // 90 |
| 35 | + 0x0, 0x1, 0x1, 0x0, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, // 100 |
| 36 | + 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, // 110 |
| 37 | + 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // 120 |
| 38 | + 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, // 130 |
| 39 | + 0x0, 0x1, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x1, // 140 |
| 40 | + 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // 150 |
| 41 | + 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, // 160 |
| 42 | + 0x1, 0x1, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, // 168 |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Good single fragment message with padding", |
| 48 | + raw: "!AIABM,26,2,1,3381581370,3,8,H77nSfPh4U=<E`H4U8G;:222220,2*42", |
| 49 | + msg: ABM{ |
| 50 | + NumFragments: 26, |
| 51 | + FragmentNumber: 2, |
| 52 | + MessageID: 1, |
| 53 | + MMSI: "3381581370", |
| 54 | + Channel: "3", |
| 55 | + VDLMessageNumber: 8, |
| 56 | + Payload: []byte{ |
| 57 | + 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, |
| 58 | + 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, |
| 59 | + 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, |
| 60 | + 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, |
| 61 | + 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, |
| 62 | + 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, |
| 63 | + 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, |
| 64 | + 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, |
| 65 | + 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, |
| 66 | + 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, |
| 67 | + 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, |
| 68 | + 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, |
| 69 | + 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, |
| 70 | + 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
| 71 | + 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, |
| 72 | + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Empty payload", |
| 78 | + raw: "!AIABM,26,2,1,3381581370,3,8,,0*7b", |
| 79 | + msg: ABM{ |
| 80 | + NumFragments: 26, |
| 81 | + FragmentNumber: 2, |
| 82 | + MessageID: 1, |
| 83 | + MMSI: "3381581370", |
| 84 | + Channel: "3", |
| 85 | + VDLMessageNumber: 8, |
| 86 | + Payload: []byte{}, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "Invalid number of fragments", |
| 91 | + raw: "!AIABM,x,2,1,3381581370,3,8,177KQJ5000G?tO`K>RA1wUbN0TKH,0*7e", |
| 92 | + err: "nmea: AIABM invalid number of fragments: x", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "Invalid VDLMessageNumber", |
| 96 | + raw: "!AIABM,26,2,1,3381581370,3,x,177KQJ5000G?tO`K>RA1wUbN0TKH,0*42", |
| 97 | + err: "nmea: AIABM invalid VDL message number: x", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "Invalid symbol in payload", |
| 101 | + raw: "!AIABM,26,2,1,3381581370,3,8,1 1,0*5b", |
| 102 | + err: "nmea: AIABM invalid payload: data byte", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "Negative number of fill bits", |
| 106 | + raw: "!AIABM,26,2,1,3381581370,3,8,177KQJ5000G?tO`K>RA1wUbN0TKH,-1*2e", |
| 107 | + err: "nmea: AIABM invalid payload: fill bits", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "Too high number of fill bits", |
| 111 | + raw: "!AIABM,26,2,1,3381581370,3,8,177KQJ5000G?tO`K>RA1wUbN0TKH,20*30", |
| 112 | + err: "nmea: AIABM invalid payload: fill bits", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Negative number of bits", |
| 116 | + raw: "!AIABM,26,2,1,3381581370,3,8,,2*79", |
| 117 | + err: "nmea: AIABM invalid payload: num bits", |
| 118 | + }, |
| 119 | + } |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.name, func(t *testing.T) { |
| 122 | + m, err := Parse(tt.raw) |
| 123 | + if tt.err != "" { |
| 124 | + assert.Error(t, err) |
| 125 | + assert.EqualError(t, err, tt.err) |
| 126 | + } else { |
| 127 | + assert.NoError(t, err) |
| 128 | + abm := m.(ABM) |
| 129 | + abm.BaseSentence = BaseSentence{} |
| 130 | + assert.Equal(t, tt.msg, abm) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments