-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from vwhitteron/add-unit-tests
Add unit tests
- Loading branch information
Showing
12 changed files
with
1,533 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
0xfc, 0x5a, 0x34, 0x2c, 0x25, 0x75, 0x74, 0x09, | ||
0xf4, 0x97, 0xe1, 0x38, 0x75, 0x46, 0x58, 0x49, | ||
0x5e, 0xc3, 0xbc, 0xdd, 0x6b, 0xc5, 0x48, 0xcd, | ||
0xc3, 0x1a, 0x8f, 0xbc, 0x41, 0x63, 0x1f, 0x1b, | ||
0xfb, 0x59, 0x9f, 0x0c, 0x0b, 0x8d, 0xd6, 0x58, | ||
0x70, 0x06, 0x55, 0xdd, 0xc6, 0xec, 0xa7, 0x4c, | ||
0xc9, 0x91, 0xba, 0x7a, 0x1c, 0x14, 0xab, 0x1a, | ||
0x39, 0x90, 0x75, 0xad, 0xce, 0x55, 0x5a, 0x9d, | ||
0x20, 0x30, 0x41, 0x2e, 0x8a, 0xe3, 0x04, 0x33, | ||
0xa1, 0x20, 0xbc, 0x77, 0x61, 0x34, 0xee, 0xb3, | ||
0x18, 0x8a, 0xdc, 0x80, 0x39, 0x4b, 0xb8, 0xbe, | ||
0x75, 0xaa, 0xef, 0x6f, 0x67, 0x03, 0x95, 0x01, | ||
0x74, 0x5f, 0x85, 0x13, 0x35, 0xa2, 0xbc, 0x9c, | ||
0xe1, 0xc6, 0x55, 0xb5, 0x07, 0x09, 0x23, 0x67, | ||
0x27, 0xf5, 0x6f, 0xd8, 0x89, 0x4d, 0xff, 0x66, | ||
0x10, 0x65, 0x5c, 0x1b, 0x12, 0x32, 0xcd, 0x5c, | ||
0xad, 0x28, 0xef, 0xad, 0x03, 0x73, 0x03, 0xde, | ||
0x47, 0xd7, 0x20, 0xf2, 0x41, 0x1a, 0xe2, 0x0d, | ||
0x54, 0xd6, 0x42, 0x04, 0xfd, 0xaf, 0x19, 0x81, | ||
0x23, 0x21, 0x5b, 0x60, 0x89, 0x62, 0x3e, 0xc2, | ||
0x3d, 0xcb, 0xac, 0x96, 0x5b, 0xf6, 0xd2, 0x2a, | ||
0x29, 0x32, 0x59, 0xb2, 0x7e, 0xdd, 0xfe, 0x4c, | ||
0xa6, 0x06, 0x8c, 0xbe, 0xc6, 0x2e, 0xeb, 0x6a, | ||
0x58, 0xef, 0x98, 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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.