Skip to content

Commit

Permalink
linter: Enable error checking linter (thrasher-corp#766)
Browse files Browse the repository at this point in the history
* golangci: Enable err checking linter to expose unchecked errors.

* gct: handle errors across the board

* gct: handle errors NOTE: Found bug in FTX (WIP)

* linter: fix issues

* ftx/exchanges: fix bug where error was being returned when setting pair management variables to an already enabled state

* bitmex: fix bug where a dangly supported asset in config danglied up the place.

* linter: fix more linter issues

* linter: fix my terrible spelling.

* currency: fix test

* exchanges: fix tests

* logger: fix test

* exchanges: fix tests

* glorious: nits

* vm: revert rm variable and instigate test
  • Loading branch information
shazbert authored Aug 30, 2021
1 parent c9ab0b1 commit 8020e1e
Show file tree
Hide file tree
Showing 99 changed files with 814 additions and 293 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ linters:
enable:
# defaults
# - deadcode
# - errcheck
- errcheck
- gosimple
- govet
- ineffassign
Expand Down
5 changes: 4 additions & 1 deletion cmd/apichecker/apicheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,10 @@ func htmlScrapeLocalBitcoins(htmlData *HTMLScrapingData) ([]string, error) {
return nil, err
}
str := r.FindString(string(a))
sha := crypto.GetSHA256([]byte(str))
sha, err := crypto.GetSHA256([]byte(str))
if err != nil {
return nil, err
}
var resp []string
resp = append(resp, crypto.HexEncodeToString(sha))
return resp, nil
Expand Down
30 changes: 10 additions & 20 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2953,8 +2953,7 @@ var withdrawalRequestCommand = &cli.Command{

func withdrawlRequestByID(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

var ID string
Expand Down Expand Up @@ -2990,8 +2989,7 @@ func withdrawlRequestByID(c *cli.Context) error {

func withdrawlRequestByExchangeID(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

var exchange, currency string
Expand Down Expand Up @@ -3058,8 +3056,7 @@ func withdrawlRequestByExchangeID(c *cli.Context) error {

func withdrawlRequestByDate(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

var exchange string
Expand Down Expand Up @@ -3842,8 +3839,7 @@ var gctScriptCommand = &cli.Command{

func gctScriptAutoload(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

var command, script string
Expand All @@ -3866,8 +3862,7 @@ func gctScriptAutoload(c *cli.Context) error {
case "remove":
status = true
default:
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

conn, err := setupClient()
Expand All @@ -3893,8 +3888,7 @@ func gctScriptAutoload(c *cli.Context) error {

func gctScriptExecute(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

if !c.IsSet("filename") {
Expand Down Expand Up @@ -3973,8 +3967,7 @@ func gctScriptList(c *cli.Context) error {

func gctScriptStop(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

if !c.IsSet("uuid") {
Expand Down Expand Up @@ -4024,8 +4017,7 @@ func gctScriptStopAll(c *cli.Context) error {

func gctScriptRead(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

if !c.IsSet("name") {
Expand Down Expand Up @@ -4058,8 +4050,7 @@ func gctScriptRead(c *cli.Context) error {

func gctScriptQuery(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

if !c.IsSet("uuid") {
Expand Down Expand Up @@ -4093,8 +4084,7 @@ func gctScriptQuery(c *cli.Context) error {

func gctScriptUpload(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
_ = cli.ShowSubcommandHelp(c)
return nil
return cli.ShowSubcommandHelp(c)
}

var overwrite bool
Expand Down
7 changes: 6 additions & 1 deletion cmd/portfolio/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func main() {
Subtotal float64
}

cfg.RetrieveConfigCurrencyPairs(true, asset.Spot)
err = cfg.RetrieveConfigCurrencyPairs(true, asset.Spot)
if err != nil {
log.Printf("Failed to retrieve config currency pairs %v\n", err)
os.Exit(1)
}

portfolioMap := make(map[currency.Code]PortfolioTemp)
total := float64(0)

Expand Down
8 changes: 7 additions & 1 deletion cmd/websocket_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ func main() {
resp.Body.Close()
log.Println("Connected to websocket!")

hash, err := crypto.GetSHA256([]byte(cfg.RemoteControl.Password))
if err != nil {
log.Println("Unable to generate SHA256 hash from remote control password:", err)
return
}

log.Println("Authenticating..")
var wsResp WebsocketEventResponse
reqData := WebsocketAuth{
Username: cfg.RemoteControl.Username,
Password: crypto.HexEncodeToString(crypto.GetSHA256([]byte(cfg.RemoteControl.Password))),
Password: crypto.HexEncodeToString(hash),
}
err = SendWebsocketEvent("auth", reqData, &wsResp)
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions common/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {
}

// GetMD5 returns a MD5 hash of a byte array
func GetMD5(input []byte) []byte {
func GetMD5(input []byte) ([]byte, error) {
m := md5.New() // nolint:gosec // hash function used by some exchanges
m.Write(input)
return m.Sum(nil)
_, err := m.Write(input)
return m.Sum(nil), err
}

// GetSHA512 returns a SHA512 hash of a byte array
func GetSHA512(input []byte) []byte {
func GetSHA512(input []byte) ([]byte, error) {
sha := sha512.New()
sha.Write(input)
return sha.Sum(nil)
_, err := sha.Write(input)
return sha.Sum(nil), err
}

// GetSHA256 returns a SHA256 hash of a byte array
func GetSHA256(input []byte) []byte {
func GetSHA256(input []byte) ([]byte, error) {
sha := sha256.New()
sha.Write(input)
return sha.Sum(nil)
_, err := sha.Write(input)
return sha.Sum(nil), err
}

// GetHMAC returns a keyed-hash message authentication code using the desired
// hashtype
func GetHMAC(hashType int, input, key []byte) []byte {
func GetHMAC(hashType int, input, key []byte) ([]byte, error) {
var hasher func() hash.Hash

switch hashType {
Expand All @@ -101,14 +101,14 @@ func GetHMAC(hashType int, input, key []byte) []byte {
}

h := hmac.New(hasher, key)
h.Write(input)
return h.Sum(nil)
_, 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 {
func Sha1ToHex(data string) (string, error) {
h := sha1.New() // nolint:gosec // hash function used by some exchanges
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
_, err := h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil)), err
}
46 changes: 37 additions & 9 deletions common/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func TestGetMD5(t *testing.T) {
t.Parallel()
var originalString = []byte("I am testing the MD5 function in common!")
var expectedOutput = []byte("18fddf4a41ba90a7352765e62e7a8744")
actualOutput := GetMD5(originalString)
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'",
Expand All @@ -88,7 +91,10 @@ func TestGetSHA512(t *testing.T) {
var expectedOutput = []byte(
`a2273f492ea73fddc4f25c267b34b3b74998bd8a6301149e1e1c835678e3c0b90859fce22e4e7af33bde1711cbb924809aedf5d759d648d61774b7185c5dc02b`,
)
actualOutput := GetSHA512(originalString)
actualOutput, err := GetSHA512(originalString)
if err != nil {
t.Fatal(err)
}
actualStr := HexEncodeToString(actualOutput)
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
t.Errorf("Expected '%x'. Actual '%x'",
Expand All @@ -102,7 +108,11 @@ func TestGetSHA256(t *testing.T) {
var expectedOutput = []byte(
"0962813d7a9f739cdcb7f0c0be0c2a13bd630167e6e54468266e4af6b1ad9303",
)
actualOutput := GetSHA256(originalString)
actualOutput, err := GetSHA256(originalString)
if err != nil {
t.Fatal(err)
}

actualStr := HexEncodeToString(actualOutput)
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
t.Errorf("Expected '%x'. Actual '%x'", expectedOutput,
Expand Down Expand Up @@ -135,31 +145,46 @@ func TestGetHMAC(t *testing.T) {
113, 64, 132, 129, 213, 68, 231, 99, 252, 15, 175, 109, 198, 132, 139, 39,
}

sha1 := GetHMAC(HashSHA1, []byte("Hello,World"), []byte("1234"))
sha1, err := GetHMAC(HashSHA1, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if string(sha1) != string(expectedSha1) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
sha256 := GetHMAC(HashSHA256, []byte("Hello,World"), []byte("1234"))
sha256, err := GetHMAC(HashSHA256, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if string(sha256) != string(expectedsha256) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha256, sha256,
)
}
sha512 := GetHMAC(HashSHA512, []byte("Hello,World"), []byte("1234"))
sha512, err := GetHMAC(HashSHA512, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if string(sha512) != string(expectedsha512) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha512, sha512,
)
}
sha512384 := GetHMAC(HashSHA512_384, []byte("Hello,World"), []byte("1234"))
sha512384, err := GetHMAC(HashSHA512_384, []byte("Hello,World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if string(sha512384) != string(expectedsha512384) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedsha512384, sha512384,
)
}
md5 := GetHMAC(HashMD5, []byte("Hello World"), []byte("1234"))
md5, err := GetHMAC(HashMD5, []byte("Hello World"), []byte("1234"))
if err != nil {
t.Fatal(err)
}
if string(md5) != string(expectedmd5) {
t.Errorf("Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedmd5, md5,
Expand All @@ -170,7 +195,10 @@ func TestGetHMAC(t *testing.T) {
func TestSha1Tohex(t *testing.T) {
t.Parallel()
expectedResult := "fcfbfcd7d31d994ef660f6972399ab5d7a890149"
actualResult := Sha1ToHex("Testing Sha1ToHex")
actualResult, err := Sha1ToHex("Testing Sha1ToHex")
if err != nil {
t.Fatal(err)
}
if actualResult != expectedResult {
t.Errorf("Expected '%s'. Actual '%s'",
expectedResult, actualResult)
Expand Down
5 changes: 4 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func TestGetNonExistentDefaultFilePathDoesNotCreateDefaultDir(t *testing.T) {
if file.Exists(dir) {
t.Skip("The default directory already exists before running the test")
}
GetFilePath("")
_, _, err := GetFilePath("")
if err != nil {
t.Fatal(err)
}
if file.Exists(dir) {
t.Fatalf("The target directory was created in %s", dir)
}
Expand Down
5 changes: 4 additions & 1 deletion currency/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,15 @@ func TestBaseCode(t *testing.T) {
true)
}

main.LoadItem(&Item{
err = main.LoadItem(&Item{
ID: 0,
FullName: "Cardano",
Role: Cryptocurrency,
Symbol: "ADA",
})
if err != nil {
t.Fatal(err)
}

full, err := main.GetFullCurrencyData()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exchangeratehost

import (
"log"
"os"
"testing"
"time"
Expand All @@ -14,9 +15,12 @@ var (
)

func TestMain(t *testing.M) {
e.Setup(base.Settings{
err := e.Setup(base.Settings{
Name: "ExchangeRateHost",
})
if err != nil {
log.Fatal(err)
}
os.Exit(t.Run())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
"github.com/thrasher-corp/gocryptotrader/log"
)

var errAPIKeyNotSet = errors.New("API key must be set")

// Setup sets appropriate values for CurrencyLayer
func (e *ExchangeRates) Setup(config base.Settings) error {
if config.APIKey == "" {
return errors.New("API key must be set")
return errAPIKeyNotSet
}
e.Name = config.Name
e.Enabled = config.Enabled
Expand Down
Loading

0 comments on commit 8020e1e

Please sign in to comment.