mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues These checks include: - Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`). - Replacing `%s` with %q - Enforcing consistent usage of should/must wording for testify assert/require messages * Add support for checking backticked string format specifiers and fix issues * tests: Fix error comparisons * tests: Replace errors.Is(err, nil) usage with testify and automate check * refactor: Rename ExtractPort to ExtractPortOrDefault * tests: Replace assert with require for error handling in multiple test files * tests: Replace assert with require for error handling and improve assertions in data tests * tests: Fix typo in assertion message for StreamVol test * OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions * OKX: Revert intentional error check * Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
This commit is contained in:
@@ -318,8 +318,9 @@ func EncodeURLValues(urlPath string, values url.Values) string {
|
||||
return u
|
||||
}
|
||||
|
||||
// ExtractHost returns the hostname out of a string
|
||||
func ExtractHost(address string) string {
|
||||
// ExtractHostOrDefault extracts the hostname from an address string.
|
||||
// If the host is empty, it defaults to "localhost".
|
||||
func ExtractHostOrDefault(address string) string {
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
if host == "" {
|
||||
return "localhost"
|
||||
@@ -327,8 +328,9 @@ func ExtractHost(address string) string {
|
||||
return host
|
||||
}
|
||||
|
||||
// ExtractPort returns the port name out of a string
|
||||
func ExtractPort(host string) int {
|
||||
// ExtractPortOrDefault returns the port from an address string.
|
||||
// If the port is empty, it defaults to 80.
|
||||
func ExtractPortOrDefault(host string) int {
|
||||
_, port, _ := net.SplitHostPort(host)
|
||||
if port == "" {
|
||||
return 80
|
||||
|
||||
@@ -53,9 +53,7 @@ func TestSendHTTPRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
err = SetHTTPUserAgent("GCTbot/1337.69 (+http://www.lol.com/)")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: %v but expected: %v", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = SendHTTPRequest(t.Context(),
|
||||
methodDelete, "https://www.google.com", headers,
|
||||
@@ -88,9 +86,7 @@ func TestSetHTTPClientWithTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
err = SetHTTPClientWithTimeout(time.Second * 15)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: %v but expected: %v", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSetHTTPUserAgent(t *testing.T) {
|
||||
@@ -101,9 +97,7 @@ func TestSetHTTPUserAgent(t *testing.T) {
|
||||
}
|
||||
|
||||
err = SetHTTPUserAgent("testy test")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: %v but expected: %v", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSetHTTPClient(t *testing.T) {
|
||||
@@ -114,9 +108,7 @@ func TestSetHTTPClient(t *testing.T) {
|
||||
}
|
||||
|
||||
err = SetHTTPClient(new(http.Client))
|
||||
if !errors.Is(err, nil) {
|
||||
t.Fatalf("received: %v but expected: %v", err, nil)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestIsEnabled(t *testing.T) {
|
||||
@@ -227,48 +219,19 @@ func TestEncodeURLValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractHost(t *testing.T) {
|
||||
func TestExtractHostOrDefault(t *testing.T) {
|
||||
t.Parallel()
|
||||
address := "localhost:1337"
|
||||
addresstwo := ":1337"
|
||||
expectedOutput := "localhost"
|
||||
actualResult := ExtractHost(address)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
actualResultTwo := ExtractHost(addresstwo)
|
||||
if expectedOutput != actualResultTwo {
|
||||
t.Errorf(
|
||||
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
|
||||
address = "192.168.1.100:1337"
|
||||
expectedOutput = "192.168.1.100"
|
||||
actualResult = ExtractHost(address)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
assert.Equal(t, "localhost", ExtractHostOrDefault("localhost:1337"))
|
||||
assert.Equal(t, "localhost", ExtractHostOrDefault(":1337"))
|
||||
assert.Equal(t, "192.168.1.100", ExtractHostOrDefault("192.168.1.100:1337"))
|
||||
}
|
||||
|
||||
func TestExtractPort(t *testing.T) {
|
||||
func TestExtractPortOrDefault(t *testing.T) {
|
||||
t.Parallel()
|
||||
address := "localhost:1337"
|
||||
expectedOutput := 1337
|
||||
actualResult := ExtractPort(address)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Expected '%d'. Actual '%d'.", expectedOutput, actualResult)
|
||||
}
|
||||
|
||||
address = "localhost"
|
||||
expectedOutput = 80
|
||||
actualResult = ExtractPort(address)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
"Expected '%d'. Actual '%d'.", expectedOutput, actualResult)
|
||||
}
|
||||
assert.Equal(t, 1337, ExtractPortOrDefault("localhost:1337"))
|
||||
assert.Equal(t, 80, ExtractPortOrDefault("localhost"))
|
||||
}
|
||||
|
||||
func TestGetURIPath(t *testing.T) {
|
||||
@@ -280,11 +243,7 @@ func TestGetURIPath(t *testing.T) {
|
||||
"http://www.google.com/accounts?!@#$%;^^": "",
|
||||
}
|
||||
for testInput, expectedOutput := range testTable {
|
||||
actualOutput := GetURIPath(testInput)
|
||||
if actualOutput != expectedOutput {
|
||||
t.Errorf("Expected '%s'. Actual '%s'.",
|
||||
expectedOutput, actualOutput)
|
||||
}
|
||||
assert.Equal(t, expectedOutput, GetURIPath(testInput))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,7 +482,7 @@ func TestErrors(t *testing.T) {
|
||||
assert.NotErrorIs(t, ExcludeError(err, e5), e5, "e4 should be excluded")
|
||||
|
||||
// Formatting retention
|
||||
err = AppendError(e1, fmt.Errorf("%w: Run out of `%s`: %w", e3, "sausages", e5))
|
||||
err = AppendError(e1, fmt.Errorf("%w: Run out of %q: %w", e3, "sausages", e5))
|
||||
assert.ErrorIs(t, err, e1, "Should be an e1")
|
||||
assert.ErrorIs(t, err, e3, "Should be an e3")
|
||||
assert.ErrorIs(t, err, e5, "Should be an e5")
|
||||
@@ -573,9 +532,7 @@ func TestParseStartEndDate(t *testing.T) {
|
||||
}
|
||||
|
||||
err = StartEndTimeCheck(pt, et)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received %v, expected %v", err, nil)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestGetAssertError(t *testing.T) {
|
||||
@@ -683,7 +640,7 @@ func TestBatch(t *testing.T) {
|
||||
assert.Len(t, b[3], 1)
|
||||
|
||||
b[0][0] = 42
|
||||
assert.Equal(t, 1, s[0], "Changing the batches must not change the source")
|
||||
assert.Equal(t, 1, s[0], "Changing the batches should not change the source")
|
||||
|
||||
require.NotPanics(t, func() { Batch(s, -1) }, "Must not panic on negative batch size")
|
||||
done := make(chan any, 1)
|
||||
@@ -692,7 +649,7 @@ func TestBatch(t *testing.T) {
|
||||
|
||||
for _, i := range []int{-1, 0, 50} {
|
||||
b = Batch(s, i)
|
||||
require.Lenf(t, b, 1, "A batch size of %v should produce a single batch", i)
|
||||
require.Lenf(t, b, 1, "A batch size of %v must produce a single batch", i)
|
||||
assert.Lenf(t, b[0], len(s), "A batch size of %v should produce a single batch", i)
|
||||
}
|
||||
}
|
||||
@@ -742,5 +699,5 @@ func TestNilGuard(t *testing.T) {
|
||||
assert.ErrorIs(t, NilGuard(nil), ErrNilPointer, "Unusual input of an untyped nil should still error correctly")
|
||||
|
||||
err = NilGuard()
|
||||
require.NoError(t, err, "NilGuard with no arguments should not panic")
|
||||
require.NoError(t, err, "NilGuard with no arguments must not error")
|
||||
}
|
||||
|
||||
@@ -64,20 +64,6 @@ func TimeFromUnixTimestampDecimal(input float64) time.Time {
|
||||
return time.Unix(int64(i), int64(f*(1e9))).UTC()
|
||||
}
|
||||
|
||||
// UnixTimestampToTime returns time.time
|
||||
func UnixTimestampToTime(timeint64 int64) time.Time {
|
||||
return time.Unix(timeint64, 0)
|
||||
}
|
||||
|
||||
// UnixTimestampStrToTime returns a time.time and an error
|
||||
func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
|
||||
i, err := strconv.ParseInt(timeStr, 10, 64)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
return time.Unix(i, 0), nil
|
||||
}
|
||||
|
||||
// BoolPtr takes in boolean condition and returns pointer version of it
|
||||
func BoolPtr(condition bool) *bool {
|
||||
b := condition
|
||||
|
||||
@@ -111,37 +111,6 @@ func TestTimeFromUnixTimestampDecimal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixTimestampToTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := int64(1489439831)
|
||||
tm := time.Unix(testTime, 0)
|
||||
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
|
||||
actualResult := UnixTimestampToTime(testTime)
|
||||
if tm.String() != actualResult.String() {
|
||||
t.Errorf(
|
||||
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixTimestampStrToTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := "1489439831"
|
||||
incorrectTime := "DINGDONG"
|
||||
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
|
||||
actualResult, err := UnixTimestampStrToTime(testTime)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if actualResult.UTC().String() != expectedOutput {
|
||||
t.Errorf(
|
||||
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
_, err = UnixTimestampStrToTime(incorrectTime)
|
||||
if err == nil {
|
||||
t.Error("should throw an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolPtr(t *testing.T) {
|
||||
y := BoolPtr(true)
|
||||
if !*y {
|
||||
|
||||
@@ -103,11 +103,3 @@ func GetHMAC(hashType int, input, key []byte) ([]byte, error) {
|
||||
_, err := h.Write(input)
|
||||
return h.Sum(nil), err
|
||||
}
|
||||
|
||||
// Sha1ToHex takes a string, sha1 hashes it and return a hex string of the
|
||||
// result
|
||||
func Sha1ToHex(data string) (string, error) {
|
||||
h := sha1.New() //nolint:gosec // hash function used by some exchanges
|
||||
_, err := h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil)), err
|
||||
}
|
||||
|
||||
@@ -2,86 +2,56 @@ package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHexEncodeToString(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := []byte("string")
|
||||
expectedOutput := "737472696e67"
|
||||
actualResult := HexEncodeToString(originalInput)
|
||||
if actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
assert.Equal(t, "737472696e67", HexEncodeToString([]byte("string")))
|
||||
}
|
||||
|
||||
func TestBase64Decode(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := "aGVsbG8="
|
||||
expectedOutput := []byte("hello")
|
||||
actualResult, err := Base64Decode(originalInput)
|
||||
if !bytes.Equal(actualResult, expectedOutput) {
|
||||
t.Errorf("Expected '%s'. Actual '%s'. Error: %s",
|
||||
expectedOutput, actualResult, err)
|
||||
}
|
||||
|
||||
r, err := Base64Decode("aGVsbG8=")
|
||||
require.NoError(t, err, "Base64Decode must not error")
|
||||
assert.Equal(t, []byte("hello"), r, "Base64Decode should return the correct byte slice")
|
||||
|
||||
_, err = Base64Decode("-")
|
||||
if err == nil {
|
||||
t.Error("Bad base64 string failed returned nil error")
|
||||
}
|
||||
assert.Error(t, err, "Base64Decode should error on invalid input")
|
||||
}
|
||||
|
||||
func TestBase64Encode(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := []byte("hello")
|
||||
actualResult := Base64Encode(originalInput)
|
||||
if expectedOutput := "aGVsbG8="; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
|
||||
assert.Equal(t, "aGVsbG8=", Base64Encode([]byte("hello")),
|
||||
"Base64Encode should return the correct base64 string")
|
||||
}
|
||||
|
||||
func TestGetRandomSalt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := GetRandomSalt(nil, -1)
|
||||
if err == nil {
|
||||
t.Fatal("Expected err on negative salt length")
|
||||
}
|
||||
assert.ErrorContains(t, err, "salt length is too small", "Expected error on negative salt length")
|
||||
|
||||
salt, err := GetRandomSalt(nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(salt) != 10 {
|
||||
t.Fatal("Expected salt of len=10")
|
||||
}
|
||||
require.NoError(t, err, "GetRandomSalt must not error")
|
||||
assert.Len(t, salt, 10, "GetRandomSalt should return a salt of the specified length")
|
||||
|
||||
salt, err = GetRandomSalt([]byte("RAWR"), 12)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(salt) != 16 {
|
||||
t.Fatal("Expected salt of len=16")
|
||||
}
|
||||
require.NoError(t, err, "GetRandomSalt must not error")
|
||||
assert.Len(t, salt, 16, "GetRandomSalt should return a salt of the specified length plus input length")
|
||||
}
|
||||
|
||||
func TestGetMD5(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalString := []byte("I am testing the MD5 function in common!")
|
||||
expectedOutput := []byte("18fddf4a41ba90a7352765e62e7a8744")
|
||||
actualOutput, err := GetMD5(originalString)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actualStr := HexEncodeToString(actualOutput)
|
||||
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
|
||||
t.Errorf("Expected '%s'. Actual '%s'",
|
||||
expectedOutput, []byte(actualStr))
|
||||
}
|
||||
r, err := GetMD5([]byte("I am testing the MD5 function in common!"))
|
||||
require.NoError(t, err, "GetMD5 must not error")
|
||||
assert.Equal(t, "18fddf4a41ba90a7352765e62e7a8744", hex.EncodeToString(r), "GetMD5 result should match the expected output")
|
||||
}
|
||||
|
||||
func TestGetSHA512(t *testing.T) {
|
||||
@@ -190,16 +160,3 @@ func TestGetHMAC(t *testing.T) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSha1Tohex(t *testing.T) {
|
||||
t.Parallel()
|
||||
expectedResult := "fcfbfcd7d31d994ef660f6972399ab5d7a890149"
|
||||
actualResult, err := Sha1ToHex("Testing Sha1ToHex")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if actualResult != expectedResult {
|
||||
t.Errorf("Expected '%s'. Actual '%s'",
|
||||
expectedResult, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,17 +39,17 @@ func TestZip(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
outFile := filepath.Join(tempDir, "out.zip")
|
||||
err := Zip(filepath.Join("..", "..", "..", "testdata", "configtest.json"), outFile)
|
||||
require.NoError(t, err, "Zip should not error")
|
||||
require.NoError(t, err, "Zip must not error")
|
||||
o, err := UnZip(outFile, tempDir)
|
||||
require.NoError(t, err, "UnZip should not error")
|
||||
require.NoError(t, err, "UnZip must not error")
|
||||
assert.Len(t, o, 1, "Should extract 1 file")
|
||||
|
||||
folder := filepath.Join("..", "..", "..", "testdata", "gctscript")
|
||||
outFolderZip := filepath.Join(tempDir, "out_folder.zip")
|
||||
err = Zip(folder, outFolderZip)
|
||||
require.NoError(t, err, "Zip should not error")
|
||||
require.NoError(t, err, "Zip must not error")
|
||||
o, err = UnZip(outFolderZip, tempDir)
|
||||
require.NoError(t, err, "UnZip should not error")
|
||||
require.NoError(t, err, "UnZip must not error")
|
||||
var found bool
|
||||
for i := range o {
|
||||
if filepath.Base(o[i]) == "timer.gct" {
|
||||
|
||||
@@ -265,7 +265,7 @@ func TestWriterNoPermissionFails(t *testing.T) {
|
||||
|
||||
tempDir := t.TempDir()
|
||||
err := os.Chmod(tempDir, 0o555)
|
||||
require.NoError(t, err, "Chmod should not fail to set read-only permissions")
|
||||
require.NoError(t, err, "Chmod must not fail to set read-only permissions")
|
||||
_, err = Writer(filepath.Join(tempDir, "path", "to", "somefile"))
|
||||
assert.Error(t, err, "Writer should fail with no write permissions")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user