From 051af604e2f076e131875d558b052a4e4d1bf713 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 29 Oct 2014 18:35:33 +0100 Subject: [PATCH 1/5] ethcrypto: fix the tests The Sha3 test contained the wrong expected result. Note that the implementation hasn't changed, the test simply contained an outdated expected value that was valid for the previous implementation. --- ethcrypto/crypto_test.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/ethcrypto/crypto_test.go b/ethcrypto/crypto_test.go index 689bcecb44..e8db6362f8 100644 --- a/ethcrypto/crypto_test.go +++ b/ethcrypto/crypto_test.go @@ -2,16 +2,35 @@ package ethcrypto import ( "bytes" + "encoding/hex" "testing" - - "github.com/ethereum/go-ethereum/ethutil" ) -// FIPS 202 test (reverted back to FIPS 180) +// These tests are sanity checks. +// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256 +// and that the sha3 library uses keccak-f permutation. + func TestSha3(t *testing.T) { - const exp = "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532" - sha3_256 := Sha3Bin([]byte("abc")) - if bytes.Compare(sha3_256, ethutil.Hex2Bytes(exp)) != 0 { - t.Errorf("Sha3_256 failed. Incorrect result %x", sha3_256) + msg := []byte("abc") + exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") + checkhash(t, "Sha3-256", Sha3, msg, exp) +} + +func TestSha256(t *testing.T) { + msg := []byte("abc") + exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") + checkhash(t, "Sha256", Sha256, msg, exp) +} + +func TestRipemd160(t *testing.T) { + msg := []byte("abc") + exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc") + checkhash(t, "Ripemd160", Ripemd160, msg, exp) +} + +func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) { + sum := f(msg) + if bytes.Compare(exp, sum) != 0 { + t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp) } } From d9bccdde234f2493722d8fadfffecc5ef2e27b9b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 29 Oct 2014 18:38:05 +0100 Subject: [PATCH 2/5] ethchain: fix tests Adapted to recent BlockChain -> ChainManager rename. --- ethchain/helper_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethchain/helper_test.go b/ethchain/helper_test.go index e4aef67d03..a863c75416 100644 --- a/ethchain/helper_test.go +++ b/ethchain/helper_test.go @@ -18,7 +18,7 @@ type TestManager struct { db ethutil.Database txPool *TxPool - blockChain *BlockChain + blockChain *ChainManager Blocks []*Block } @@ -38,7 +38,7 @@ func (s *TestManager) Peers() *list.List { return list.New() } -func (s *TestManager) BlockChain() *BlockChain { +func (s *TestManager) ChainManager() *ChainManager { return s.blockChain } @@ -82,7 +82,7 @@ func NewTestManager() *TestManager { testManager.eventMux = new(event.TypeMux) testManager.db = db testManager.txPool = NewTxPool(testManager) - testManager.blockChain = NewBlockChain(testManager) + testManager.blockChain = NewChainManager(testManager) testManager.stateManager = NewStateManager(testManager) // Start the tx pool From 56014844a960ea59f18e3d6b4662a7674ac9c78d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 29 Oct 2014 18:38:52 +0100 Subject: [PATCH 3/5] ethpipe: delete useless test It didn't compile and wouldn't have checked any results if it ever ran. --- ethpipe/pipe_test.go | 58 -------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 ethpipe/pipe_test.go diff --git a/ethpipe/pipe_test.go b/ethpipe/pipe_test.go deleted file mode 100644 index 3b5dc5e4c4..0000000000 --- a/ethpipe/pipe_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package ethpipe - -import ( - "testing" - - "github.com/ethereum/go-ethereum/ethcrypto" - "github.com/ethereum/go-ethereum/ethstate" - "github.com/ethereum/go-ethereum/ethutil" -) - -func Val(v interface{}) *ethutil.Value { - return ethutil.NewValue(v) -} - -func TestNew(t *testing.T) { - pipe := New(nil) - - var addr, privy, recp, data []byte - var object *ethstate.StateObject - var key *ethcrypto.KeyPair - - world := pipe.World() - world.Get(addr) - world.Coinbase() - world.IsMining() - world.IsListening() - world.State() - peers := world.Peers() - peers.Len() - - // Shortcut functions - pipe.Balance(addr) - pipe.Nonce(addr) - pipe.Block(addr) - pipe.Storage(addr, addr) - pipe.ToAddress(privy) - pipe.Exists(addr) - // Doesn't change state - pipe.Execute(addr, nil, Val(0), Val(1000000), Val(10)) - // Doesn't change state - pipe.ExecuteObject(object, nil, Val(0), Val(1000000), Val(10)) - - conf := world.Config() - namereg := conf.Get("NameReg") - namereg.Storage(addr) - - var err error - // Transact - err = pipe.Transact(key, recp, ethutil.NewValue(0), ethutil.NewValue(0), ethutil.NewValue(0), nil) - if err != nil { - t.Error(err) - } - // Create - err = pipe.Transact(key, nil, ethutil.NewValue(0), ethutil.NewValue(0), ethutil.NewValue(0), data) - if err != nil { - t.Error(err) - } -} From 38034c306693cd0acbc4edb0d0afde1f2e525bf6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 29 Oct 2014 18:39:51 +0100 Subject: [PATCH 4/5] ethwire: fix ClientIdentity test --- ethwire/client_identity_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ethwire/client_identity_test.go b/ethwire/client_identity_test.go index f3c8bfd50a..1724fe57be 100644 --- a/ethwire/client_identity_test.go +++ b/ethwire/client_identity_test.go @@ -9,22 +9,22 @@ import ( func TestClientIdentity(t *testing.T) { clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test") clientString := clientIdentity.String() - expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/Go", runtime.GOOS) + expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/%s", runtime.GOOS, runtime.Version()) if clientString != expected { - t.Error("Expected clientIdentity to be %v, got %v", expected, clientString) + t.Errorf("Expected clientIdentity to be %q, got %q", expected, clientString) } customIdentifier := clientIdentity.GetCustomIdentifier() if customIdentifier != "test" { - t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %v", customIdentifier) + t.Errorf("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %q", customIdentifier) } clientIdentity.SetCustomIdentifier("test2") customIdentifier = clientIdentity.GetCustomIdentifier() if customIdentifier != "test2" { - t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %v", customIdentifier) + t.Errorf("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %q", customIdentifier) } clientString = clientIdentity.String() - expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/Go", runtime.GOOS) + expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/%s", runtime.GOOS, runtime.Version()) if clientString != expected { - t.Error("Expected clientIdentity to be %v, got %v", expected, clientString) + t.Errorf("Expected clientIdentity to be %q, got %q", expected, clientString) } } From 4cf69d7cd34394fa7408bb5a6e152610a9daa81c Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 29 Oct 2014 18:40:18 +0100 Subject: [PATCH 5/5] vm: fix basic VM tests The tests now compile and won't panic for unexpected return values. We need a recent-enough version of the mutan compiler because of the new JUMPDEST requirements. Skip some tests if the installed mutan version is too old. The debug VM test still fails, probably because of an implementation bug. --- vm/vm_test.go | 54 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/vm/vm_test.go b/vm/vm_test.go index 84cca3a9d1..8818cc8ec4 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -14,22 +14,30 @@ import ( "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethtrie" "github.com/ethereum/go-ethereum/ethutil" + "github.com/obscuren/mutan" ) -type TestEnv struct { +type TestEnv struct{} + +func (TestEnv) Origin() []byte { return nil } +func (TestEnv) BlockNumber() *big.Int { return nil } +func (TestEnv) BlockHash() []byte { return nil } +func (TestEnv) PrevHash() []byte { return nil } +func (TestEnv) Coinbase() []byte { return nil } +func (TestEnv) Time() int64 { return 0 } +func (TestEnv) GasLimit() *big.Int { return nil } +func (TestEnv) Difficulty() *big.Int { return nil } +func (TestEnv) Value() *big.Int { return nil } +func (TestEnv) AddLog(Log) {} + +func (TestEnv) Transfer(from, to Account, amount *big.Int) error { + return nil } -func (self TestEnv) Origin() []byte { return nil } -func (self TestEnv) BlockNumber() *big.Int { return nil } -func (self TestEnv) BlockHash() []byte { return nil } -func (self TestEnv) PrevHash() []byte { return nil } -func (self TestEnv) Coinbase() []byte { return nil } -func (self TestEnv) Time() int64 { return 0 } -func (self TestEnv) Difficulty() *big.Int { return nil } -func (self TestEnv) Value() *big.Int { return nil } - // This is likely to fail if anything ever gets looked up in the state trie :-) -func (self TestEnv) State() *ethstate.State { return ethstate.New(ethtrie.New(nil, "")) } +func (TestEnv) State() *ethstate.State { + return ethstate.New(ethtrie.New(nil, "")) +} const mutcode = ` var x = 0; @@ -56,27 +64,35 @@ func setup(level ethlog.LogLevel, typ Type) (*Closure, VirtualMachine) { return callerClosure, New(TestEnv{}, typ) } +var big9 = ethutil.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000009") + func TestDebugVm(t *testing.T) { + if mutan.Version < "0.6" { + t.Skip("skipping for mutan version", mutan.Version, " < 0.6") + } + closure, vm := setup(ethlog.DebugLevel, DebugVmTy) ret, _, e := closure.Call(vm, nil) if e != nil { - fmt.Println("error", e) + t.Fatalf("Call returned error: %v", e) } - - if ret[len(ret)-1] != 9 { - t.Errorf("Expected VM to return 9, got", ret, "instead.") + if !bytes.Equal(ret, big9) { + t.Errorf("Wrong return value '%x', want '%x'", ret, big9) } } func TestVm(t *testing.T) { + if mutan.Version < "0.6" { + t.Skip("skipping for mutan version", mutan.Version, " < 0.6") + } + closure, vm := setup(ethlog.DebugLevel, StandardVmTy) ret, _, e := closure.Call(vm, nil) if e != nil { - fmt.Println("error", e) + t.Fatalf("Call returned error: %v", e) } - - if ret[len(ret)-1] != 9 { - t.Errorf("Expected VM to return 9, got", ret, "instead.") + if !bytes.Equal(ret, big9) { + t.Errorf("Wrong return value '%x', want '%x'", ret, big9) } }