Skip to content

Commit 81ec564

Browse files
committed
Merge pull request bnb-chain#168 from fjl/feature/working-tests
Fix the tests
2 parents 9e2f071 + 4cf69d7 commit 81ec564

5 files changed

Lines changed: 70 additions & 93 deletions

File tree

‎ethchain/helper_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type TestManager struct {
1818

1919
db ethutil.Database
2020
txPool *TxPool
21-
blockChain *BlockChain
21+
blockChain *ChainManager
2222
Blocks []*Block
2323
}
2424

@@ -38,7 +38,7 @@ func (s *TestManager) Peers() *list.List {
3838
return list.New()
3939
}
4040

41-
func (s *TestManager) BlockChain() *BlockChain {
41+
func (s *TestManager) ChainManager() *ChainManager {
4242
return s.blockChain
4343
}
4444

@@ -82,7 +82,7 @@ func NewTestManager() *TestManager {
8282
testManager.eventMux = new(event.TypeMux)
8383
testManager.db = db
8484
testManager.txPool = NewTxPool(testManager)
85-
testManager.blockChain = NewBlockChain(testManager)
85+
testManager.blockChain = NewChainManager(testManager)
8686
testManager.stateManager = NewStateManager(testManager)
8787

8888
// Start the tx pool

‎ethcrypto/crypto_test.go‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ package ethcrypto
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"testing"
6-
7-
"github.com/ethereum/go-ethereum/ethutil"
87
)
98

10-
// FIPS 202 test (reverted back to FIPS 180)
9+
// These tests are sanity checks.
10+
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
11+
// and that the sha3 library uses keccak-f permutation.
12+
1113
func TestSha3(t *testing.T) {
12-
const exp = "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"
13-
sha3_256 := Sha3Bin([]byte("abc"))
14-
if bytes.Compare(sha3_256, ethutil.Hex2Bytes(exp)) != 0 {
15-
t.Errorf("Sha3_256 failed. Incorrect result %x", sha3_256)
14+
msg := []byte("abc")
15+
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
16+
checkhash(t, "Sha3-256", Sha3, msg, exp)
17+
}
18+
19+
func TestSha256(t *testing.T) {
20+
msg := []byte("abc")
21+
exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
22+
checkhash(t, "Sha256", Sha256, msg, exp)
23+
}
24+
25+
func TestRipemd160(t *testing.T) {
26+
msg := []byte("abc")
27+
exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
28+
checkhash(t, "Ripemd160", Ripemd160, msg, exp)
29+
}
30+
31+
func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
32+
sum := f(msg)
33+
if bytes.Compare(exp, sum) != 0 {
34+
t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp)
1635
}
1736
}

‎ethpipe/pipe_test.go‎

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎ethwire/client_identity_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
func TestClientIdentity(t *testing.T) {
1010
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test")
1111
clientString := clientIdentity.String()
12-
expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/Go", runtime.GOOS)
12+
expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/%s", runtime.GOOS, runtime.Version())
1313
if clientString != expected {
14-
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
14+
t.Errorf("Expected clientIdentity to be %q, got %q", expected, clientString)
1515
}
1616
customIdentifier := clientIdentity.GetCustomIdentifier()
1717
if customIdentifier != "test" {
18-
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %v", customIdentifier)
18+
t.Errorf("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %q", customIdentifier)
1919
}
2020
clientIdentity.SetCustomIdentifier("test2")
2121
customIdentifier = clientIdentity.GetCustomIdentifier()
2222
if customIdentifier != "test2" {
23-
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %v", customIdentifier)
23+
t.Errorf("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %q", customIdentifier)
2424
}
2525
clientString = clientIdentity.String()
26-
expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/Go", runtime.GOOS)
26+
expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/%s", runtime.GOOS, runtime.Version())
2727
if clientString != expected {
28-
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
28+
t.Errorf("Expected clientIdentity to be %q, got %q", expected, clientString)
2929
}
3030
}

‎vm/vm_test.go‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ import (
1414
"github.com/ethereum/go-ethereum/ethstate"
1515
"github.com/ethereum/go-ethereum/ethtrie"
1616
"github.com/ethereum/go-ethereum/ethutil"
17+
"github.com/obscuren/mutan"
1718
)
1819

19-
type TestEnv struct {
20+
type TestEnv struct{}
21+
22+
func (TestEnv) Origin() []byte { return nil }
23+
func (TestEnv) BlockNumber() *big.Int { return nil }
24+
func (TestEnv) BlockHash() []byte { return nil }
25+
func (TestEnv) PrevHash() []byte { return nil }
26+
func (TestEnv) Coinbase() []byte { return nil }
27+
func (TestEnv) Time() int64 { return 0 }
28+
func (TestEnv) GasLimit() *big.Int { return nil }
29+
func (TestEnv) Difficulty() *big.Int { return nil }
30+
func (TestEnv) Value() *big.Int { return nil }
31+
func (TestEnv) AddLog(Log) {}
32+
33+
func (TestEnv) Transfer(from, to Account, amount *big.Int) error {
34+
return nil
2035
}
2136

22-
func (self TestEnv) Origin() []byte { return nil }
23-
func (self TestEnv) BlockNumber() *big.Int { return nil }
24-
func (self TestEnv) BlockHash() []byte { return nil }
25-
func (self TestEnv) PrevHash() []byte { return nil }
26-
func (self TestEnv) Coinbase() []byte { return nil }
27-
func (self TestEnv) Time() int64 { return 0 }
28-
func (self TestEnv) Difficulty() *big.Int { return nil }
29-
func (self TestEnv) Value() *big.Int { return nil }
30-
3137
// This is likely to fail if anything ever gets looked up in the state trie :-)
32-
func (self TestEnv) State() *ethstate.State { return ethstate.New(ethtrie.New(nil, "")) }
38+
func (TestEnv) State() *ethstate.State {
39+
return ethstate.New(ethtrie.New(nil, ""))
40+
}
3341

3442
const mutcode = `
3543
var x = 0;
@@ -56,27 +64,35 @@ func setup(level ethlog.LogLevel, typ Type) (*Closure, VirtualMachine) {
5664
return callerClosure, New(TestEnv{}, typ)
5765
}
5866

67+
var big9 = ethutil.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000009")
68+
5969
func TestDebugVm(t *testing.T) {
70+
if mutan.Version < "0.6" {
71+
t.Skip("skipping for mutan version", mutan.Version, " < 0.6")
72+
}
73+
6074
closure, vm := setup(ethlog.DebugLevel, DebugVmTy)
6175
ret, _, e := closure.Call(vm, nil)
6276
if e != nil {
63-
fmt.Println("error", e)
77+
t.Fatalf("Call returned error: %v", e)
6478
}
65-
66-
if ret[len(ret)-1] != 9 {
67-
t.Errorf("Expected VM to return 9, got", ret, "instead.")
79+
if !bytes.Equal(ret, big9) {
80+
t.Errorf("Wrong return value '%x', want '%x'", ret, big9)
6881
}
6982
}
7083

7184
func TestVm(t *testing.T) {
85+
if mutan.Version < "0.6" {
86+
t.Skip("skipping for mutan version", mutan.Version, " < 0.6")
87+
}
88+
7289
closure, vm := setup(ethlog.DebugLevel, StandardVmTy)
7390
ret, _, e := closure.Call(vm, nil)
7491
if e != nil {
75-
fmt.Println("error", e)
92+
t.Fatalf("Call returned error: %v", e)
7693
}
77-
78-
if ret[len(ret)-1] != 9 {
79-
t.Errorf("Expected VM to return 9, got", ret, "instead.")
94+
if !bytes.Equal(ret, big9) {
95+
t.Errorf("Wrong return value '%x', want '%x'", ret, big9)
8096
}
8197
}
8298

0 commit comments

Comments
 (0)