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"
|
|
|
|
)
|
|
|
|
|
2019-01-07 09:43:50 +03:00
|
|
|
// Some common manifestations of PSI
|
|
|
|
var (
|
2019-01-10 10:04:54 +03:00
|
|
|
// standardPat is a minimal PAT.
|
2019-01-10 10:02:42 +03:00
|
|
|
standardPat = PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x00,
|
|
|
|
Ssi: true,
|
|
|
|
Pb: false,
|
|
|
|
Sl: 0x0d,
|
|
|
|
Tss: &TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PAT{
|
|
|
|
Pn: 0x01,
|
|
|
|
Pmpid: 0x1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:04:54 +03:00
|
|
|
// standardPmt is a minimal PMT, without time and location descriptors.
|
2019-01-10 10:02:42 +03:00
|
|
|
standardPmt = PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: 0x12,
|
|
|
|
Tss: &TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100, // wrong
|
|
|
|
Pil: 0,
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:59:48 +03:00
|
|
|
// standardPmtTimeLocation is a standard PMT with time and location
|
|
|
|
// descriptors, but time and location fields zeroed out.
|
2019-01-20 13:13:00 +03:00
|
|
|
standardPmtWithMeta = PSI{
|
2019-01-07 09:43:50 +03:00
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
|
|
|
Sl: 0x3e,
|
|
|
|
Tss: &TSS{
|
|
|
|
Tide: 0x01,
|
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100,
|
|
|
|
Pil: PmtTimeLocationPil,
|
|
|
|
Pd: []Desc{
|
|
|
|
{
|
|
|
|
Dt: TimeDescTag,
|
|
|
|
Dl: TimeDataSize,
|
|
|
|
Dd: make([]byte, TimeDataSize),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Dt: LocationDescTag,
|
|
|
|
Dl: LocationDataSize,
|
|
|
|
Dd: make([]byte, LocationDataSize),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// Times as ints for testing
|
2018-12-11 08:32:57 +03:00
|
|
|
const (
|
2019-01-07 09:02:18 +03:00
|
|
|
tstTime1 = 1235367435 // 0x49a2360b
|
|
|
|
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-14 09:16:36 +03:00
|
|
|
// TODO: make these realistic
|
2019-01-20 13:42:26 +03:00
|
|
|
const (
|
2019-01-20 13:41:09 +03:00
|
|
|
locationTstStr1 = "$GPGGA,123519,4807.038,N,01131.0"
|
|
|
|
locationTstStr2 = "$GPGGA,183710,4902.048,N,02171.0"
|
2018-12-11 08:32:57 +03:00
|
|
|
)
|
|
|
|
|
2018-12-12 08:56:59 +03:00
|
|
|
// err message
|
|
|
|
const (
|
2019-01-03 10:26:08 +03:00
|
|
|
errCmp = "Incorrect output, for: %v \nwant: %v, \ngot: %v"
|
2018-12-12 08:56:59 +03:00
|
|
|
)
|
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 (
|
2019-01-20 13:13:00 +03:00
|
|
|
pmtWithMetaHead = []byte{
|
2018-12-12 08:56:59 +03:00
|
|
|
0x00, 0x02, 0xb0, 0x12, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe1, 0x00, 0xf0, 0x0a,
|
2019-01-07 09:43:50 +03:00
|
|
|
TimeDescTag, // Descriptor tag for timestamp
|
|
|
|
TimeDataSize, // Length of bytes to follow
|
2019-01-11 03:24:25 +03:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x67, 0x6f, 0x74, 0x5f, // Timestamp data
|
2019-01-07 09:43:50 +03:00
|
|
|
LocationDescTag, // Descriptor tag for location
|
|
|
|
LocationDataSize, // Length of bytes to follow
|
2018-12-10 10:12:45 +03:00
|
|
|
}
|
2019-01-20 13:13:00 +03:00
|
|
|
pmtWithMetaTail = []byte{
|
2018-12-12 08:56:59 +03:00
|
|
|
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,
|
2019-01-07 09:43:50 +03:00
|
|
|
TimeDescTag, // Descriptor tag
|
|
|
|
TimeDataSize, // Length of bytes to follow
|
2019-01-11 03:24:25 +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,
|
2019-01-07 09:43:50 +03:00
|
|
|
TimeDescTag, // Descriptor tag
|
|
|
|
TimeDataSize, // Length of bytes to follow
|
2019-01-11 03:24:25 +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
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// Bytes representing pmt with time1 and location1
|
2019-01-20 13:34:27 +03:00
|
|
|
pmtWithMetaTst1 = buildPmtWithMeta(locationTstStr1)
|
2018-12-12 08:56:59 +03:00
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// bytes representing pmt with with time1 and location 2
|
2019-01-20 13:34:27 +03:00
|
|
|
pmtWithMetaTst2 = buildPmtWithMeta(locationTstStr2)
|
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()",
|
2019-01-10 10:04:54 +03:00
|
|
|
input: standardPat,
|
2019-01-07 09:32:57 +03:00
|
|
|
want: StandardPatBytes,
|
2018-12-12 08:56:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Pmt test data no descriptor
|
|
|
|
{
|
|
|
|
name: "pmt to Bytes() without descriptors",
|
2019-01-10 10:04:54 +03:00
|
|
|
input: standardPmt,
|
2019-01-07 09:32:57 +03:00
|
|
|
want: StandardPmtBytes,
|
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,
|
2019-01-07 09:28:12 +03:00
|
|
|
Sl: 0x12,
|
2018-12-12 08:56:59 +03:00
|
|
|
Tss: &TSS{
|
2019-01-07 09:28:12 +03:00
|
|
|
Tide: 0x01,
|
2018-12-12 08:56:59 +03:00
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100, // wrong
|
|
|
|
Pil: 10,
|
|
|
|
Pd: []Desc{
|
2019-01-07 09:34:57 +03:00
|
|
|
{
|
2019-01-07 09:43:50 +03:00
|
|
|
Dt: TimeDescTag,
|
|
|
|
Dl: TimeDataSize,
|
2018-12-12 08:56:59 +03:00
|
|
|
Dd: TimeBytes(tstTime1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: pmtTimeBytes1,
|
|
|
|
},
|
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// Pmt with time and location
|
2018-12-12 08:56:59 +03:00
|
|
|
{
|
2018-12-14 08:32:47 +03:00
|
|
|
name: "pmt Bytes() with time and location",
|
2018-12-12 08:56:59 +03:00
|
|
|
input: PSI{
|
|
|
|
Pf: 0x00,
|
|
|
|
Tid: 0x02,
|
|
|
|
Ssi: true,
|
2019-01-07 09:28:12 +03:00
|
|
|
Sl: 0x12,
|
2018-12-12 08:56:59 +03:00
|
|
|
Tss: &TSS{
|
2019-01-07 09:28:12 +03:00
|
|
|
Tide: 0x01,
|
2018-12-12 08:56:59 +03:00
|
|
|
V: 0,
|
|
|
|
Cni: true,
|
|
|
|
Sn: 0,
|
|
|
|
Lsn: 0,
|
|
|
|
Sd: &PMT{
|
|
|
|
Pcrpid: 0x0100, // wrong
|
|
|
|
Pil: 10,
|
|
|
|
Pd: []Desc{
|
2019-01-07 09:34:57 +03:00
|
|
|
{
|
2019-01-07 09:43:50 +03:00
|
|
|
Dt: TimeDescTag,
|
|
|
|
Dl: TimeDataSize,
|
2018-12-12 08:56:59 +03:00
|
|
|
Dd: TimeBytes(tstTime2),
|
|
|
|
},
|
2019-01-07 09:34:57 +03:00
|
|
|
{
|
2019-01-07 09:43:50 +03:00
|
|
|
Dt: LocationDescTag,
|
|
|
|
Dl: LocationDataSize,
|
2019-01-20 13:41:09 +03:00
|
|
|
Dd: []byte(locationTstStr1),
|
2018-12-12 08:56:59 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Essd: &ESSD{
|
|
|
|
St: 0x1b,
|
|
|
|
Epid: 0x0100,
|
|
|
|
Esil: 0x00,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-20 13:34:27 +03:00
|
|
|
want: buildPmtWithMeta(locationTstStr1),
|
2018-12-12 08:56:59 +03:00
|
|
|
},
|
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()
|
2018-12-27 06:41:23 +03:00
|
|
|
if !bytes.Equal(got, addPadding(addCrc(test.want))) {
|
2018-12-12 08:56:59 +03:00
|
|
|
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-13 07:39:23 +03:00
|
|
|
cpy = addCrc(cpy)
|
2018-12-11 09:35:40 +03:00
|
|
|
err := UpdateTime(cpy, tstTime2)
|
2018-12-13 07:39:23 +03:00
|
|
|
cpy = cpy[:len(cpy)-4]
|
2018-12-11 09:35:40 +03:00
|
|
|
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-14 08:36:01 +03:00
|
|
|
s, err := TimeFrom(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-14 09:16:36 +03:00
|
|
|
// TestLocationGet checks that we can correctly get location data from a pmt table
|
2018-12-14 08:32:47 +03:00
|
|
|
func TestLocationGet(t *testing.T) {
|
2019-01-20 13:13:00 +03:00
|
|
|
pb := standardPmtWithMeta.Bytes()
|
2019-01-20 13:41:09 +03:00
|
|
|
err := UpdateLocation(pb, locationTstStr1)
|
2018-12-14 03:45:49 +03:00
|
|
|
if err != nil {
|
2019-01-20 13:34:27 +03:00
|
|
|
t.Errorf("Error for TestLocationGet UpdateLocation(pb, locationTstStr1): %v", err)
|
2018-12-14 03:45:49 +03:00
|
|
|
}
|
2018-12-14 08:36:01 +03:00
|
|
|
g, err := LocationFrom(pb)
|
2018-12-14 03:45:49 +03:00
|
|
|
if err != nil {
|
2018-12-14 08:32:47 +03:00
|
|
|
t.Errorf("Error for TestLocationGet LocationOf(pb): %v", err)
|
2018-12-14 03:45:49 +03:00
|
|
|
}
|
2019-01-20 13:41:09 +03:00
|
|
|
if g != locationTstStr1 {
|
2019-01-20 13:34:27 +03:00
|
|
|
t.Errorf(errCmp, "TestLocationGet", locationTstStr1, g)
|
2018-12-14 03:45:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// TestLocationUpdate checks to see if we can update the location string in a pmt correctly
|
|
|
|
func TestLocationUpdate(t *testing.T) {
|
2019-01-20 13:13:00 +03:00
|
|
|
cpy := make([]byte, len(pmtWithMetaTst1))
|
|
|
|
copy(cpy, pmtWithMetaTst1)
|
2018-12-13 07:39:23 +03:00
|
|
|
cpy = addCrc(cpy)
|
2019-01-20 13:41:09 +03:00
|
|
|
err := UpdateLocation(cpy, locationTstStr2)
|
2018-12-13 07:39:23 +03:00
|
|
|
cpy = cpy[:len(cpy)-4]
|
2018-12-12 08:56:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Update time returned err: %v", err)
|
|
|
|
}
|
2019-01-20 13:13:00 +03:00
|
|
|
if !bytes.Equal(pmtWithMetaTst2, cpy) {
|
|
|
|
t.Errorf(errCmp, "TestLocationUpdate", pmtWithMetaTst2, cpy)
|
2018-12-12 08:56:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 06:54:54 +03:00
|
|
|
func TestTrim(t *testing.T) {
|
|
|
|
test := []byte{0xa3, 0x01, 0x03, 0x00, 0xde}
|
|
|
|
want := []byte{0xa3, 0x01, 0x03}
|
2019-01-10 10:01:14 +03:00
|
|
|
got := trimTo(test, 0x00)
|
2019-01-07 06:54:54 +03:00
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
t.Errorf(errCmp, "TestTrim", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:55:06 +03:00
|
|
|
// buildPmtTimeLocationBytes returns a PMT with time and location from s.
|
2019-01-20 13:41:09 +03:00
|
|
|
func buildPmtWithMeta(tstStr string) []byte {
|
2019-01-20 14:28:23 +03:00
|
|
|
dst := make([]byte, len(pmtWithMetaHead)+32+len(pmtWithMetaTail))
|
|
|
|
copy(dst, pmtWithMetaHead)
|
|
|
|
copy(dst[len(pmtWithMetaHead):], tstStr)
|
|
|
|
copy(dst[len(pmtWithMetaHead)+32:], pmtWithMetaTail)
|
|
|
|
return dst
|
2018-12-12 08:56:59 +03:00
|
|
|
}
|