2018-12-12 02:48:05 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
psi_test.go
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
psi_test.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2018-12-07 08:23:38 +03:00
|
|
|
package psi
|
|
|
|
|
|
|
|
import (
|
2018-12-11 07:22:18 +03:00
|
|
|
"bytes"
|
2018-12-07 08:23:38 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// Times as ints for testing
|
2018-12-11 08:32:57 +03:00
|
|
|
const (
|
2018-12-12 08:56:59 +03:00
|
|
|
tstTime1 = 1235367435 // 0x49A2360B
|
2018-12-11 09:50:01 +03:00
|
|
|
tstTime2 = 1735357535 // 0x676F745F
|
2018-12-11 09:35:40 +03:00
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// GPS string for testing
|
2018-12-11 09:35:40 +03:00
|
|
|
const (
|
2018-12-12 08:56:59 +03:00
|
|
|
gpsTstStr1 = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47"
|
|
|
|
gpsTstStr2 = "$GPGGA,183710,4902.048,N,02171.020,E,1,09,0.5,547.2,M,43.4,M,,*42"
|
2018-12-11 08:32:57 +03:00
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// err message
|
|
|
|
const (
|
|
|
|
errCmp = "Incorrect output, for: %v wanted: %v, got: %v"
|
|
|
|
)
|
2018-12-11 09:50:01 +03:00
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// Test time to bytes test Data
|
2018-12-10 10:12:45 +03:00
|
|
|
var (
|
2018-12-11 08:32:57 +03:00
|
|
|
timeSlice = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B,
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parts to construct bytes of pmt with time and bytes
|
|
|
|
var (
|
|
|
|
pmtTimeGpsBytesPart1 = []byte{
|
|
|
|
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
|
|
|
|
byte(timeDescTag), // Descriptor tag for timestamp
|
2018-12-12 09:32:03 +03:00
|
|
|
byte(timeDataSize), // Length of bytes to follow
|
2018-12-12 08:56:59 +03:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // Timestamp data
|
2018-12-12 09:32:03 +03:00
|
|
|
byte(gpsDescTag), // Descriptor tag for gps
|
|
|
|
byte(gpsDataSize), // Length of bytes to follow
|
2018-12-10 10:12:45 +03:00
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
pmtTimeGpsBytesPart2 = []byte{
|
|
|
|
0x1b, 0xe1, 0x00, 0xf0, 0x00,
|
2018-12-10 10:12:45 +03:00
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Bytes representing pmt with tstTime1
|
|
|
|
pmtTimeBytes1 = []byte{
|
|
|
|
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
|
2018-12-11 09:35:40 +03:00
|
|
|
byte(timeDescTag), // Descriptor tag
|
2018-12-12 09:32:03 +03:00
|
|
|
byte(timeDataSize), // Length of bytes to follow
|
2018-12-11 08:32:57 +03:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x49, 0xA2, 0x36, 0x0B, // timestamp
|
2018-12-12 08:56:59 +03:00
|
|
|
0x1b, 0xe1, 0x00, 0xf0, 0x00,
|
2018-12-11 08:32:57 +03:00
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
|
|
|
|
// Bytes representing pmt with tstTime 2
|
|
|
|
pmtTimeBytes2 = []byte{
|
|
|
|
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
|
2018-12-11 09:35:40 +03:00
|
|
|
byte(timeDescTag), // Descriptor tag
|
2018-12-12 09:32:03 +03:00
|
|
|
byte(timeDataSize), // Length of bytes to follow
|
2018-12-11 09:35:40 +03:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x67, 0x6F, 0x74, 0x5F, // timestamp
|
2018-12-12 08:56:59 +03:00
|
|
|
0x1b, 0xe1, 0x00, 0xf0, 0x00,
|
2018-12-11 09:35:40 +03:00
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
|
|
|
|
// Bytes representing pmt with time1 and gps1
|
|
|
|
pmtTimeGpsBytes1 = buildPmtTimeGpsBytes(gpsTstStr1)
|
|
|
|
|
|
|
|
// bytes representing pmt with with time1 and gps 2
|
|
|
|
pmtTimeGpsBytes2 = buildPmtTimeGpsBytes(gpsTstStr2)
|
2018-12-10 10:12:45 +03:00
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// bytesTests contains data for testing the Bytes() funcs for the PSI data struct
|
|
|
|
var bytesTests = []struct {
|
|
|
|
name string
|
|
|
|
input PSI
|
|
|
|
want []byte
|
|
|
|
}{
|
|
|
|
// Pat test
|
|
|
|
{
|
|
|
|
name: "pat Bytes()",
|
2018-12-13 04:52:06 +03:00
|
|
|
input: StdPat,
|
|
|
|
want: StdPatBytes,
|
2018-12-12 08:56:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Pmt test data no descriptor
|
|
|
|
{
|
|
|
|
name: "pmt to Bytes() without descriptors",
|
2018-12-13 04:52:06 +03:00
|
|
|
input: StdPmt,
|
|
|
|
want: StdPmtBytes,
|
2018-12-12 08:56:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Pmt with time descriptor
|
|
|
|
{
|
|
|
|
name: "pmt to Bytes() with time descriptor",
|
|
|
|
input: PSI{
|
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: uint16(0x12),
|
|
|
|
Tss: &TSS{
|
|
|
|
Tide: uint16(0x01),
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100, // wrong
|
|
|
|
Pil: 10,
|
|
|
|
Pd: []Desc{
|
|
|
|
Desc{
|
|
|
|
Dt: byte(timeDescTag),
|
2018-12-12 09:32:03 +03:00
|
|
|
Dl: byte(timeDataSize),
|
2018-12-12 08:56:59 +03:00
|
|
|
Dd: TimeBytes(tstTime1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: pmtTimeBytes1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Pmt with time and gps
|
|
|
|
{
|
|
|
|
name: "pmt Bytes() with time and gps",
|
|
|
|
input: PSI{
|
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: uint16(0x12),
|
|
|
|
Tss: &TSS{
|
|
|
|
Tide: uint16(0x01),
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100, // wrong
|
|
|
|
Pil: 10,
|
|
|
|
Pd: []Desc{
|
|
|
|
Desc{
|
|
|
|
Dt: byte(timeDescTag),
|
2018-12-12 09:32:03 +03:00
|
|
|
Dl: byte(timeDataSize),
|
2018-12-12 08:56:59 +03:00
|
|
|
Dd: TimeBytes(tstTime2),
|
|
|
|
},
|
|
|
|
Desc{
|
|
|
|
Dt: byte(gpsDescTag),
|
2018-12-12 09:32:03 +03:00
|
|
|
Dl: byte(gpsDataSize),
|
2018-12-12 08:56:59 +03:00
|
|
|
Dd: GpsStrBytes(gpsTstStr1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: buildPmtTimeGpsBytes(gpsTstStr1),
|
|
|
|
},
|
2018-12-11 07:22:18 +03:00
|
|
|
}
|
2018-12-07 08:23:38 +03:00
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// TestBytes ensures that the Bytes() funcs are working correctly to take PSI
|
2018-12-12 09:36:01 +03:00
|
|
|
// structs and convert them to byte slices
|
2018-12-12 08:56:59 +03:00
|
|
|
func TestBytes(t *testing.T) {
|
|
|
|
for _, test := range bytesTests {
|
|
|
|
got := test.input.Bytes()
|
|
|
|
// Remove crc32s
|
|
|
|
got = got[:len(got)-4]
|
|
|
|
if !bytes.Equal(got, test.want) {
|
|
|
|
t.Errorf("unexpected error for test %v: got:%v want:%v", test.name, got,
|
|
|
|
test.want)
|
|
|
|
}
|
2018-12-11 07:22:18 +03:00
|
|
|
}
|
2018-12-07 08:23:38 +03:00
|
|
|
}
|
2018-12-11 08:32:57 +03:00
|
|
|
|
2018-12-12 09:36:01 +03:00
|
|
|
// TestTimestampToBytes is a quick sanity check of the int64 time to []byte func
|
2018-12-11 08:32:57 +03:00
|
|
|
func TestTimestampToBytes(t *testing.T) {
|
2018-12-12 08:56:59 +03:00
|
|
|
tb := TimeBytes(tstTime1)
|
|
|
|
if !bytes.Equal(timeSlice, tb) {
|
|
|
|
t.Errorf(errCmp, "testTimeStampToBytes", timeSlice, tb)
|
2018-12-11 09:35:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:36:01 +03:00
|
|
|
// TestTimeUpdate checks to see if we can correctly update the timstamp in pmt
|
2018-12-11 09:35:40 +03:00
|
|
|
func TestTimeUpdate(t *testing.T) {
|
2018-12-12 08:56:59 +03:00
|
|
|
cpy := make([]byte, len(pmtTimeBytes1))
|
|
|
|
copy(cpy, pmtTimeBytes1)
|
2018-12-11 09:35:40 +03:00
|
|
|
err := UpdateTime(cpy, tstTime2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Update time returned err: %v", err)
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
if !bytes.Equal(pmtTimeBytes2, cpy) {
|
|
|
|
t.Errorf(errCmp, "TestTimeUpdate", pmtTimeBytes2, cpy)
|
2018-12-11 09:35:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:36:01 +03:00
|
|
|
// TestTimeGet tsts to see if we can correctly get the timestamp from a pmt
|
2018-12-11 09:35:40 +03:00
|
|
|
func TestTimeGet(t *testing.T) {
|
2018-12-12 08:56:59 +03:00
|
|
|
s, err := TimeOf(pmtTimeBytes1)
|
2018-12-11 09:35:40 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Getting timestamp failed with err: %v", err)
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
if s != tstTime1 {
|
|
|
|
t.Errorf(errCmp, "TestTimeGet", tstTime1, s)
|
2018-12-11 08:32:57 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
|
2018-12-12 09:36:01 +03:00
|
|
|
// TestGpsUpdate checks to see if we can update the gps string in a pmt correctly
|
2018-12-12 08:56:59 +03:00
|
|
|
func TestGpsUpdate(t *testing.T) {
|
|
|
|
cpy := make([]byte, len(pmtTimeGpsBytes1))
|
|
|
|
copy(cpy, pmtTimeGpsBytes1)
|
2018-12-12 09:32:03 +03:00
|
|
|
err := UpdateGps(cpy, gpsTstStr2)
|
2018-12-12 08:56:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Update time returned err: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pmtTimeGpsBytes2, cpy) {
|
|
|
|
t.Errorf(errCmp, "TestGpsUpdate", pmtTimeGpsBytes2, cpy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:36:01 +03:00
|
|
|
// buildPmtTimeGpsBytes is a helper function to help construct the byte slices
|
|
|
|
// for pmts with time and gps, as the gps data field is 32 bytes, i.e. quite large
|
|
|
|
// to type out
|
2018-12-12 08:56:59 +03:00
|
|
|
func buildPmtTimeGpsBytes(tstStr string) []byte {
|
|
|
|
return append(append(append(make([]byte, 0), pmtTimeGpsBytesPart1...),
|
|
|
|
GpsStrBytes(tstStr)...), pmtTimeGpsBytesPart2...)
|
|
|
|
}
|