Files
gocryptotrader/common/crypto/crypto_test.go
Adrian Gallagher d5ba674fc4 codebase: Cleanup various things (#1935)
* codebase: Rid base64/hex to string common funcs

* codebase: Rid local scope variable usage and other improvements

* codebase: Refactor currency pair usage across multiple exchanges

- Updated HitBTC tests to use the new currency pair format.
- Modified Kraken futures types to use currency.Pair instead of string for Symbol.
- Adjusted Kraken wrapper methods to handle currency pairs correctly.
- Refined OKX tests and types to utilize currency.Pair for instrument IDs.
- Enhanced Poloniex tests to consistently use predefined currency pairs.
- Streamlined order and orderbook tests to replace string pairs with currency.NewBTCUSD().
- Improved Yobit tests to utilize a standardized currency pair format.
- Updated validator wrapper to use currency pairs directly instead of string conversions.

* codebase: Use types.Number where possible

* refactor: update PayoutFee type to types.Number for consistency

* Refactor: Remove crypto functions to use standard library and other minor changes

- Removed custom crypto functions for SHA256, SHA512, and MD5 from the common/crypto package.
- Replaced usages of removed functions with standard library implementations in various files including:
  - cmd/websocket_client/main.go
  - engine/apiserver.go
  - exchanges/kraken/kraken.go
  - exchanges/lbank/lbank.go
  - exchanges/okx/okx_business_websocket.go
  - exchanges/kucoin/kucoin_websocket.go
  - gctscript/vm/vm.go
- Updated tests to reflect changes in the crypto functions.
- Renamed several functions for clarity, particularly in the context of order book updates across multiple exchanges.

* refactor: replace assert with require for consistency in test assertions

* refactor: Improve Binance futures candlestick test, standardise orderbook update function names and improve test parallelism

* refactor: Replace require.Len with require.Equal for better output in TestGetFuturesKlineData
2025-06-12 14:12:36 +10:00

97 lines
2.9 KiB
Go

package crypto
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetRandomSalt(t *testing.T) {
t.Parallel()
_, err := GetRandomSalt(nil, -1)
assert.ErrorContains(t, err, "salt length is too small", "Expected error on negative salt length")
salt, err := GetRandomSalt(nil, 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)
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 TestGetHMAC(t *testing.T) {
t.Parallel()
expectedSha1 := []byte{
74, 253, 245, 154, 87, 168, 110, 182, 172, 101, 177, 49, 142, 2, 253, 165,
100, 66, 86, 246,
}
expectedsha256 := []byte{
54, 68, 6, 12, 32, 158, 80, 22, 142, 8, 131, 111, 248, 145, 17, 202, 224,
59, 135, 206, 11, 170, 154, 197, 183, 28, 150, 79, 168, 105, 62, 102,
}
expectedsha512 := []byte{
249, 212, 31, 38, 23, 3, 93, 220, 81, 209, 214, 112, 92, 75, 126, 40, 109,
95, 247, 182, 210, 54, 217, 224, 199, 252, 129, 226, 97, 201, 245, 220, 37,
201, 240, 15, 137, 236, 75, 6, 97, 12, 190, 31, 53, 153, 223, 17, 214, 11,
153, 203, 49, 29, 158, 217, 204, 93, 179, 109, 140, 216, 202, 71,
}
expectedsha512384 := []byte{
121, 203, 109, 105, 178, 68, 179, 57, 21, 217, 76, 82, 94, 100, 210, 1, 55,
201, 8, 232, 194, 168, 165, 58, 192, 26, 193, 167, 254, 183, 172, 4, 189,
158, 158, 150, 173, 33, 119, 125, 94, 13, 125, 89, 241, 184, 166, 128,
}
expectedmd5 := []byte{
113, 64, 132, 129, 213, 68, 231, 99, 252, 15, 175, 109, 198, 132, 139, 39,
}
sha1, err := GetHMAC(HashSHA1, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sha1, expectedSha1) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
sha256, err := GetHMAC(HashSHA256, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sha256, expectedsha256) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha256, sha256,
)
}
sha512, err := GetHMAC(HashSHA512, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sha512, expectedsha512) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha512, sha512,
)
}
sha512384, err := GetHMAC(HashSHA512_384, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(sha512384, expectedsha512384) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha512384, sha512384,
)
}
md5, err := GetHMAC(HashMD5, []byte("Hello World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(md5, expectedmd5) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedmd5, md5,
)
}
}