Skip to content

Commit

Permalink
Merge pull request #26 from vwhitteron/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
vwhitteron authored Nov 13, 2024
2 parents 9d510b9 + 31656a0 commit 5ede4af
Show file tree
Hide file tree
Showing 12 changed files with 1,533 additions and 11 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test:
.PHONY: test/cover
test/cover:
go test -v -race -buildvcs -coverprofile=coverage.out ./...
@sed -i '/gran_turismo_telemetry.go/d' coverage.out

## test/cover/show: run all tests and display coverage in a browser
.PHONY: test/cover/show
Expand Down
5 changes: 4 additions & 1 deletion cmd/capture_replay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func main() {
gt.Telemetry.VehicleManufacturer(),
gt.Telemetry.VehicleModel(),
)

// add extra data to the gzip header
if b, ok := buffer.(*gzip.Writer); ok {
b.Extra = []byte(extraData)
}
Expand All @@ -109,7 +111,7 @@ func main() {
time.Sleep(4 * time.Millisecond)
}

// Write the frame to the gzip buffer
// write the frame to the file buffer
if framesCaptured >= 0 {
if diff > 1 {
fmt.Printf("Dropped %d frames\n", diff-1)
Expand All @@ -132,6 +134,7 @@ func main() {
}
}

// flush and close the gzip file fuffer
if b, ok := buffer.(*gzip.Writer); ok {
b.Flush()
b.Close()
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ go 1.22.5
require (
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.29.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0 h1:bxazq0XLMSVMm/DIVFLl9BqIWehrqcLsyVWSacEjIKE=
github.com/kaitai-io/kaitai_struct_go_runtime v0.10.0/go.mod h1:fBebEoDoc0xNbZsIcRQWqDp4jViaTKv6uxAUjmCFGgM=
Expand All @@ -9,9 +11,13 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -23,3 +29,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 4 additions & 1 deletion internal/telemetrysrc/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (r *UDPReader) Read() (int, []byte, error) {
return 0, buffer, fmt.Errorf("no data received")
}

decipheredPacket := utils.Salsa20Decode(buffer[:bufLen])
decipheredPacket, err := utils.Salsa20Decode(buffer[:bufLen])
if err != nil {
return 0, buffer, fmt.Errorf("failed to decipher telemetry: %s", err.Error())
}

return bufLen, decipheredPacket, nil
}
Expand Down
13 changes: 10 additions & 3 deletions internal/utils/salsa20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package utils

import (
"encoding/binary"
"fmt"

"golang.org/x/crypto/salsa20"
)

const cipherKey string = "Simulator Interface Packet GT7 ver 0.0"

func Salsa20Decode(dat []byte) []byte {
func Salsa20Decode(dat []byte) ([]byte, error) {
datLen := len(dat)
if datLen < 32 {
return nil, fmt.Errorf("salsa20 data is too short: %d < 32", datLen)
}

key := [32]byte{}
copy(key[:], cipherKey)

Expand All @@ -21,7 +27,8 @@ func Salsa20Decode(dat []byte) []byte {
salsa20.XORKeyStream(ddata, dat, nonce, &key)
magic := binary.LittleEndian.Uint32(ddata[:4])
if magic != 0x47375330 {
return nil
return nil, fmt.Errorf("invalid magic value: %x", magic)
}
return ddata

return ddata, nil
}
113 changes: 113 additions & 0 deletions internal/utils/salsa20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package utils

import (
"bytes"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

var validSalsa20Content = []byte{
0xf6, 0x37, 0x4a, 0x12, 0x43, 0x32, 0x23, 0xcf,
0x4e, 0x53, 0x1f, 0xeb, 0x64, 0x6d, 0xff, 0xae,
0xc8, 0xf0, 0xc8, 0xb9, 0x6e, 0xdc, 0xaf, 0x52,
0x91, 0xb4, 0x40, 0xf2, 0xab, 0x48, 0x01, 0xf1,
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, 0x2d, 0xf9, 0x6a, 0x13, 0x00,
0x4f, 0xd1, 0xc2, 0xc8, 0x8d, 0x58, 0xda, 0xcb,
0xea, 0x28, 0x04, 0x57, 0x1b, 0x48, 0xeb, 0xf9,
0x57, 0x95, 0x39, 0xbf, 0xb8, 0x9d, 0x49, 0xf0,
0x27, 0x1c, 0x11, 0xeb, 0x6f, 0x27, 0x3b, 0x0e,
0x6a, 0x81, 0x86, 0x9c, 0x88, 0x44, 0x48, 0x31,
0x16, 0x88, 0x52, 0x6e, 0x31, 0xf0, 0xf6, 0x4a,
0xa8, 0x20, 0xa9, 0xf8, 0xa2, 0x55, 0xd6, 0x49,
0x36, 0x98, 0x9a, 0x9e, 0xb4, 0x5e, 0x8e, 0x1d,
0x2b, 0xd7, 0x46, 0x02, 0xfd, 0x30, 0x41, 0x8a,
}

var magicPacketHeader = []byte{0x30, 0x53, 0x37, 0x47}

const standardPacketSize = 296

type Salsa20TestSuite struct {
suite.Suite
}

func TestSalsa20TestSuite(t *testing.T) {
suite.Run(t, new(Salsa20TestSuite))
}

func (suite *Salsa20TestSuite) TestEmptySalsa20ContentReturnsNilWithError() {
// Arrange
encodedValue := []byte{}

// Act
gotValue, err := Salsa20Decode(encodedValue)

// Assert
suite.Nil(gotValue)
suite.ErrorContains(err, "salsa20 data is too short: 0 < 32")
}

func (suite *Salsa20TestSuite) TestTruncatedSalsa20ContentReturnsNilWithError() {
// Arrange
wantLen := 31
encodedValue := bytes.Repeat([]byte{0x00}, wantLen)

// Act
gotValue, err := Salsa20Decode(encodedValue)

// Assert
suite.Nil(gotValue)
suite.ErrorContains(err, "salsa20 data is too short: "+strconv.Itoa(wantLen)+" < 32")
}

func (suite *Salsa20TestSuite) TestInvalidSalsa20MagicValueReturnsNilWithError() {
// Arrange
encodedValue := bytes.Repeat([]byte{0x00}, standardPacketSize)
copy(encodedValue[:4], validSalsa20Content[4:])

// Act
gotValue, err := Salsa20Decode(encodedValue)

// Assert
suite.Nil(gotValue)
suite.ErrorContains(err, "invalid magic value: 90f8359c")
}

func TestValidSalsa20ContentReturnsDecodedData(t *testing.T) {
// Arrange
wantValue := magicPacketHeader

// Act
gotValue, err := Salsa20Decode(validSalsa20Content)
require.NoError(t, err)

// Assert
assert.Equal(t, wantValue, gotValue[0:4])
}
53 changes: 53 additions & 0 deletions internal/utils/unit_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package utils

import (
"reflect"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/suite"
)

type UnitConversionTestSuite struct {
suite.Suite
}

func TestUnitConversionTestSuite(t *testing.T) {
suite.Run(t, new(UnitConversionTestSuite))
}

func (suite *UnitConversionTestSuite) TestUnitConversionFunctionsReturnCorrectValues() {
type testCase struct {
function func(float32) float32
withValue float32
wantValue float32
}

// Arrange
testCases := []testCase{
{BarToPSI, 1, 14.50377},
{BarToInHg, 1, 29.52998},
{BarToKPA, 1, 100},
{CelsiusToFahrenheit, 1, 33.8},
{MetersToFeet, 1, 3.28084},
{MetersToInches, 1, 39.3701},
{MetersToMillimeters, 1, 1000},
{MetersPerSecondToKilometersPerHour, 1, 3.6},
{MetersPerSecondToMilesPerHour, 1, 2.2369363},
{RadiansPerSecondToRevolutionsPerMinute, 1, 9.549296},
}

for _, tc := range testCases {
fnNameSegments := strings.Split(runtime.FuncForPC(reflect.ValueOf(tc.function).Pointer()).Name(), ".")
fnName := fnNameSegments[len(fnNameSegments)-1]

suite.Run(fnName, func() {
// Act
gotValue := tc.function(tc.withValue)

// Assert
suite.Equal(tc.wantValue, gotValue)
})
}
}
13 changes: 7 additions & 6 deletions transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/vwhitteron/gt-telemetry/internal/gttelemetry"
"github.com/vwhitteron/gt-telemetry/internal/utils"
"github.com/vwhitteron/gt-telemetry/internal/vehicles"
)

Expand Down Expand Up @@ -386,8 +387,8 @@ func (t *transformer) TyreRadiusMeters() CornerSet {
}

func (t *transformer) TyreSlipRatio() CornerSet {
groundSpeed := t.GroundSpeedKPH()
wheelSpeed := t.WheelSpeedKPH()
groundSpeed := utils.MetersPerSecondToKilometersPerHour(t.GroundSpeedMetersPerSecond())
wheelSpeed := t.WheelSpeedMetersPerSecond()
if groundSpeed == 0 {
return CornerSet{
FrontLeft: 1,
Expand All @@ -398,10 +399,10 @@ func (t *transformer) TyreSlipRatio() CornerSet {
}

return CornerSet{
FrontLeft: wheelSpeed.FrontLeft / groundSpeed,
FrontRight: wheelSpeed.FrontRight / groundSpeed,
RearLeft: wheelSpeed.RearLeft / groundSpeed,
RearRight: wheelSpeed.RearRight / groundSpeed,
FrontLeft: utils.MetersPerSecondToKilometersPerHour(wheelSpeed.FrontLeft) / groundSpeed,
FrontRight: utils.MetersPerSecondToKilometersPerHour(wheelSpeed.FrontRight) / groundSpeed,
RearLeft: utils.MetersPerSecondToKilometersPerHour(wheelSpeed.RearLeft) / groundSpeed,
RearRight: utils.MetersPerSecondToKilometersPerHour(wheelSpeed.RearRight) / groundSpeed,
}
}

Expand Down
Loading

0 comments on commit 5ede4af

Please sign in to comment.