all: replace t.Log(); t.FailNow() with t.Fatal() (#19849)

This commit is contained in:
Felix Lange 2019-07-18 14:21:24 +02:00 committed by Péter Szilágyi
parent 9466b9eec5
commit f088c650a5
4 changed files with 56 additions and 116 deletions

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log"
"math/big" "math/big"
"reflect" "reflect"
"strings" "strings"
@ -102,8 +101,7 @@ func TestReader(t *testing.T) {
func TestTestNumbers(t *testing.T) { func TestTestNumbers(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata2)) abi, err := JSON(strings.NewReader(jsondata2))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if _, err := abi.Pack("balance"); err != nil { if _, err := abi.Pack("balance"); err != nil {
@ -140,8 +138,7 @@ func TestTestNumbers(t *testing.T) {
func TestTestString(t *testing.T) { func TestTestString(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata2)) abi, err := JSON(strings.NewReader(jsondata2))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if _, err := abi.Pack("string", "hello world"); err != nil { if _, err := abi.Pack("string", "hello world"); err != nil {
@ -152,8 +149,7 @@ func TestTestString(t *testing.T) {
func TestTestBool(t *testing.T) { func TestTestBool(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata2)) abi, err := JSON(strings.NewReader(jsondata2))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if _, err := abi.Pack("bool", true); err != nil { if _, err := abi.Pack("bool", true); err != nil {
@ -164,8 +160,7 @@ func TestTestBool(t *testing.T) {
func TestTestSlice(t *testing.T) { func TestTestSlice(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata2)) abi, err := JSON(strings.NewReader(jsondata2))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
slice := make([]uint64, 2) slice := make([]uint64, 2)
@ -221,8 +216,7 @@ func TestMethodSignature(t *testing.T) {
func TestMultiPack(t *testing.T) { func TestMultiPack(t *testing.T) {
abi, err := JSON(strings.NewReader(jsondata2)) abi, err := JSON(strings.NewReader(jsondata2))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4] sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
@ -232,10 +226,8 @@ func TestMultiPack(t *testing.T) {
packed, err := abi.Pack("bar", uint32(10), uint16(11)) packed, err := abi.Pack("bar", uint32(10), uint16(11))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if !bytes.Equal(packed, sig) { if !bytes.Equal(packed, sig) {
t.Errorf("expected %x got %x", sig, packed) t.Errorf("expected %x got %x", sig, packed)
} }
@ -246,11 +238,11 @@ func ExampleJSON() {
abi, err := JSON(strings.NewReader(definition)) abi, err := JSON(strings.NewReader(definition))
if err != nil { if err != nil {
log.Fatalln(err) panic(err)
} }
out, err := abi.Pack("isBar", common.HexToAddress("01")) out, err := abi.Pack("isBar", common.HexToAddress("01"))
if err != nil { if err != nil {
log.Fatalln(err) panic(err)
} }
fmt.Printf("%x\n", out) fmt.Printf("%x\n", out)

@ -89,17 +89,15 @@ func TestRecipientEmpty(t *testing.T) {
_, addr := defaultTestKey() _, addr := defaultTestKey()
tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d")) tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d"))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
from, err := Sender(HomesteadSigner{}, tx) from, err := Sender(HomesteadSigner{}, tx)
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if addr != from { if addr != from {
t.Error("derived address doesn't match") t.Fatal("derived address doesn't match")
} }
} }
@ -108,18 +106,15 @@ func TestRecipientNormal(t *testing.T) {
tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6")) tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6"))
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
from, err := Sender(HomesteadSigner{}, tx) from, err := Sender(HomesteadSigner{}, tx)
if err != nil { if err != nil {
t.Error(err) t.Fatal(err)
t.FailNow()
} }
if addr != from { if addr != from {
t.Error("derived address doesn't match") t.Fatal("derived address doesn't match")
} }
} }

@ -58,12 +58,10 @@ func TestKDF(t *testing.T) {
k, err := concatKDF(h, msg, nil, 64) k, err := concatKDF(h, msg, nil, 64)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
if len(k) != 64 { if len(k) != 64 {
t.Logf("KDF: generated key is the wrong size (%d instead of 64\n", len(k)) t.Fatalf("KDF: generated key is the wrong size (%d instead of 64\n", len(k))
t.FailNow()
} }
} }
@ -97,32 +95,27 @@ func cmpPublic(pub1, pub2 PublicKey) bool {
func TestSharedKey(t *testing.T) { func TestSharedKey(t *testing.T) {
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
skLen := MaxSharedKeyLength(&prv1.PublicKey) / 2 skLen := MaxSharedKeyLength(&prv1.PublicKey) / 2
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
if !bytes.Equal(sk1, sk2) { if !bytes.Equal(sk1, sk2) {
t.Log(ErrBadSharedKeys.Error()) t.Fatal(ErrBadSharedKeys)
t.FailNow()
} }
} }
@ -169,26 +162,22 @@ func TestSharedKeyPadding(t *testing.T) {
func TestTooBigSharedKey(t *testing.T) { func TestTooBigSharedKey(t *testing.T) {
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
_, err = prv1.GenerateShared(&prv2.PublicKey, 32, 32) _, err = prv1.GenerateShared(&prv2.PublicKey, 32, 32)
if err != ErrSharedKeyTooBig { if err != ErrSharedKeyTooBig {
t.Log("ecdh: shared key should be too large for curve") t.Fatal("ecdh: shared key should be too large for curve")
t.FailNow()
} }
_, err = prv2.GenerateShared(&prv1.PublicKey, 32, 32) _, err = prv2.GenerateShared(&prv1.PublicKey, 32, 32)
if err != ErrSharedKeyTooBig { if err != ErrSharedKeyTooBig {
t.Log("ecdh: shared key should be too large for curve") t.Fatal("ecdh: shared key should be too large for curve")
t.FailNow()
} }
} }
@ -196,8 +185,7 @@ func TestTooBigSharedKey(t *testing.T) {
func BenchmarkGenerateKeyP256(b *testing.B) { func BenchmarkGenerateKeyP256(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil { if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil {
b.Log(err.Error()) b.Fatal(err)
b.FailNow()
} }
} }
} }
@ -206,15 +194,13 @@ func BenchmarkGenerateKeyP256(b *testing.B) {
func BenchmarkGenSharedKeyP256(b *testing.B) { func BenchmarkGenSharedKeyP256(b *testing.B) {
prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil) prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil)
if err != nil { if err != nil {
b.Log(err.Error()) b.Fatal(err)
b.FailNow()
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) _, err := prv.GenerateShared(&prv.PublicKey, 16, 16)
if err != nil { if err != nil {
b.Log(err.Error()) b.Fatal(err)
b.FailNow()
} }
} }
} }
@ -223,15 +209,13 @@ func BenchmarkGenSharedKeyP256(b *testing.B) {
func BenchmarkGenSharedKeyS256(b *testing.B) { func BenchmarkGenSharedKeyS256(b *testing.B) {
prv, err := GenerateKey(rand.Reader, crypto.S256(), nil) prv, err := GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil { if err != nil {
b.Log(err.Error()) b.Fatal(err)
b.FailNow()
} }
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := prv.GenerateShared(&prv.PublicKey, 16, 16) _, err := prv.GenerateShared(&prv.PublicKey, 16, 16)
if err != nil { if err != nil {
b.Log(err.Error()) b.Fatal(err)
b.FailNow()
} }
} }
} }
@ -240,38 +224,32 @@ func BenchmarkGenSharedKeyS256(b *testing.B) {
func TestEncryptDecrypt(t *testing.T) { func TestEncryptDecrypt(t *testing.T) {
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
message := []byte("Hello, world.") message := []byte("Hello, world.")
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
pt, err := prv2.Decrypt(ct, nil, nil) pt, err := prv2.Decrypt(ct, nil, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
if !bytes.Equal(pt, message) { if !bytes.Equal(pt, message) {
t.Log("ecies: plaintext doesn't match message") t.Fatal("ecies: plaintext doesn't match message")
t.FailNow()
} }
_, err = prv1.Decrypt(ct, nil, nil) _, err = prv1.Decrypt(ct, nil, nil)
if err == nil { if err == nil {
t.Log("ecies: encryption should not have succeeded") t.Fatal("ecies: encryption should not have succeeded")
t.FailNow()
} }
} }
@ -341,50 +319,39 @@ func TestParamSelection(t *testing.T) {
func testParamSelection(t *testing.T, c testCase) { func testParamSelection(t *testing.T, c testCase) {
params := ParamsFromCurve(c.Curve) params := ParamsFromCurve(c.Curve)
if params == nil && c.Expected != nil { if params == nil && c.Expected != nil {
t.Logf("%s (%s)\n", ErrInvalidParams.Error(), c.Name) t.Fatalf("%s (%s)\n", ErrInvalidParams.Error(), c.Name)
t.FailNow()
} else if params != nil && !cmpParams(params, c.Expected) { } else if params != nil && !cmpParams(params, c.Expected) {
t.Logf("ecies: parameters should be invalid (%s)\n", t.Fatalf("ecies: parameters should be invalid (%s)\n", c.Name)
c.Name)
t.FailNow()
} }
prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Logf("%s (%s)\n", err.Error(), c.Name) t.Fatalf("%s (%s)\n", err.Error(), c.Name)
t.FailNow()
} }
prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Logf("%s (%s)\n", err.Error(), c.Name) t.Fatalf("%s (%s)\n", err.Error(), c.Name)
t.FailNow()
} }
message := []byte("Hello, world.") message := []byte("Hello, world.")
ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil) ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
if err != nil { if err != nil {
t.Logf("%s (%s)\n", err.Error(), c.Name) t.Fatalf("%s (%s)\n", err.Error(), c.Name)
t.FailNow()
} }
pt, err := prv2.Decrypt(ct, nil, nil) pt, err := prv2.Decrypt(ct, nil, nil)
if err != nil { if err != nil {
t.Logf("%s (%s)\n", err.Error(), c.Name) t.Fatalf("%s (%s)\n", err.Error(), c.Name)
t.FailNow()
} }
if !bytes.Equal(pt, message) { if !bytes.Equal(pt, message) {
t.Logf("ecies: plaintext doesn't match message (%s)\n", t.Fatalf("ecies: plaintext doesn't match message (%s)\n", c.Name)
c.Name)
t.FailNow()
} }
_, err = prv1.Decrypt(ct, nil, nil) _, err = prv1.Decrypt(ct, nil, nil)
if err == nil { if err == nil {
t.Logf("ecies: encryption should not have succeeded (%s)\n", t.Fatalf("ecies: encryption should not have succeeded (%s)\n", c.Name)
c.Name)
t.FailNow()
} }
} }
@ -396,23 +363,20 @@ func TestBasicKeyValidation(t *testing.T) {
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil) prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
message := []byte("Hello, world.") message := []byte("Hello, world.")
ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil) ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
for _, b := range badBytes { for _, b := range badBytes {
ct[0] = b ct[0] = b
_, err := prv.Decrypt(ct, nil, nil) _, err := prv.Decrypt(ct, nil, nil)
if err != ErrInvalidPublicKey { if err != ErrInvalidPublicKey {
t.Log("ecies: validated an invalid key") t.Fatal("ecies: validated an invalid key")
t.FailNow()
} }
} }
} }
@ -450,19 +414,16 @@ func TestSharedKeyStatic(t *testing.T) {
sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen) sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen) sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen)
if err != nil { if err != nil {
t.Log(err.Error()) t.Fatal(err)
t.FailNow()
} }
if !bytes.Equal(sk1, sk2) { if !bytes.Equal(sk1, sk2) {
t.Log(ErrBadSharedKeys.Error()) t.Fatal(ErrBadSharedKeys)
t.FailNow()
} }
sk, _ := hex.DecodeString("167ccc13ac5e8a26b131c3446030c60fbfac6aa8e31149d0869f93626a4cdf62") sk, _ := hex.DecodeString("167ccc13ac5e8a26b131c3446030c60fbfac6aa8e31149d0869f93626a4cdf62")

@ -21,34 +21,26 @@ import "testing"
func TestWSGetConfigNoAuth(t *testing.T) { func TestWSGetConfigNoAuth(t *testing.T) {
config, err := wsGetConfig("ws://example.com:1234", "") config, err := wsGetConfig("ws://example.com:1234", "")
if err != nil { if err != nil {
t.Logf("wsGetConfig failed: %s", err) t.Fatalf("wsGetConfig failed: %s", err)
t.Fail()
return
} }
if config.Location.User != nil { if config.Location.User != nil {
t.Log("User should have been stripped from the URL") t.Fatalf("User should have been stripped from the URL")
t.Fail()
} }
if config.Location.Hostname() != "example.com" || if config.Location.Hostname() != "example.com" ||
config.Location.Port() != "1234" || config.Location.Scheme != "ws" { config.Location.Port() != "1234" || config.Location.Scheme != "ws" {
t.Logf("Unexpected URL: %s", config.Location) t.Fatalf("Unexpected URL: %s", config.Location)
t.Fail()
} }
} }
func TestWSGetConfigWithBasicAuth(t *testing.T) { func TestWSGetConfigWithBasicAuth(t *testing.T) {
config, err := wsGetConfig("wss://testuser:test-PASS_01@example.com:1234", "") config, err := wsGetConfig("wss://testuser:test-PASS_01@example.com:1234", "")
if err != nil { if err != nil {
t.Logf("wsGetConfig failed: %s", err) t.Fatalf("wsGetConfig failed: %s", err)
t.Fail()
return
} }
if config.Location.User != nil { if config.Location.User != nil {
t.Log("User should have been stripped from the URL") t.Fatal("User should have been stripped from the URL")
t.Fail()
} }
if config.Header.Get("Authorization") != "Basic dGVzdHVzZXI6dGVzdC1QQVNTXzAx" { if config.Header.Get("Authorization") != "Basic dGVzdHVzZXI6dGVzdC1QQVNTXzAx" {
t.Log("Basic auth header is incorrect") t.Fatal("Basic auth header is incorrect")
t.Fail()
} }
} }