diff --git a/erisdb/methods.go b/erisdb/methods.go index 261a57e14201f134d7fa4ba116d25d0cc37b2137..77c19bee9232abe6e08b93b68c9a169f519ba381 100644 --- a/erisdb/methods.go +++ b/erisdb/methods.go @@ -7,6 +7,7 @@ import ( ep "github.com/eris-ltd/erisdb/erisdb/pipe" rpc "github.com/eris-ltd/erisdb/rpc" "github.com/tendermint/tendermint/types" + "strings" ) const ( @@ -421,6 +422,7 @@ func generateSubId() (string, error) { if err != nil { return "", err } - return hex.EncodeToString(b), nil + rStr := hex.EncodeToString(b) + return strings.ToUpper(rStr), nil } diff --git a/erisdb/pipe/accounts.go b/erisdb/pipe/accounts.go index 9861e18dfc0d61a27b11266e63d0e9ffe6b23a00..17e80e89432d3315290912c5b7406cc432bdff08 100644 --- a/erisdb/pipe/accounts.go +++ b/erisdb/pipe/accounts.go @@ -182,11 +182,11 @@ type AccountBalanceFilter struct { } func (this *AccountBalanceFilter) Configure(fd *FilterData) error { - val, err := parseNumberValue(fd.Value) + val, err := ParseNumberValue(fd.Value) if err != nil { return err } - match, err2 := rangeFilter(fd.Op, "balance") + match, err2 := GetRangeFilter(fd.Op, "balance") if err2 != nil { return err2 } diff --git a/erisdb/pipe/filters.go b/erisdb/pipe/filters.go index e7545cd4bcdb2069f5d8497a6e4aa66ce09cff8b..ba05335541202d087c7b2740bf13c800dfda1c7b 100644 --- a/erisdb/pipe/filters.go +++ b/erisdb/pipe/filters.go @@ -127,7 +127,7 @@ func (this *FilterFactory) newSingleFilter(fd *FilterData) (ConfigurableFilter, // Some standard value parsing functions. -func parseNumberValue(value string) (int64, error) { +func ParseNumberValue(value string) (int64, error) { var val int64 // Check for wildcards. if value == "min" { @@ -147,7 +147,7 @@ func parseNumberValue(value string) (int64, error) { // Some standard filtering functions. -func rangeFilter(op, fName string) (func(a, b int64) bool, error) { +func GetRangeFilter(op, fName string) (func(a, b int64) bool, error) { if op == "==" { return func(a, b int64) bool { return a == b @@ -176,3 +176,17 @@ func rangeFilter(op, fName string) (func(a, b int64) bool, error) { return nil, fmt.Errorf("Op: " + op + " is not supported for '" + fName + "' filtering") } } + +func GetStringFilter(op, fName string) (func(s0, s1 string) bool, error){ + if op == "==" { + return func(s0, s1 string) bool { + return strings.EqualFold(s0,s1) + }, nil + } else if op == "!=" { + return func(s0, s1 string) bool { + return !strings.EqualFold(s0,s1) + }, nil + } else { + return nil, fmt.Errorf("Op: " + op + " is not supported for '" + fName + "' filtering.") + } +} diff --git a/erisdb/wsService.go b/erisdb/wsService.go index 0f88da101fb6bce121e7ae157e7d8880b7ceee97..e7f5af3a297fbcca8df9398518c0fbd193b9ec3a 100644 --- a/erisdb/wsService.go +++ b/erisdb/wsService.go @@ -102,7 +102,7 @@ func (this *ErisDbWsService) EventSubscribe(request *rpc.RPCRequest, requester i return nil, rpc.INTERNAL_ERROR, errSID } callback := func(ret interface{}) { - this.writeResponse(request.Id, ret, session) + this.writeResponse(subId, ret, session) } _, errC := this.pipe.Events().Subscribe(subId, eventId, callback) if errC != nil { diff --git a/rpc/rpc.go b/rpc/codec.go similarity index 100% rename from rpc/rpc.go rename to rpc/codec.go diff --git a/rpc/rpc_test.go b/rpc/rpc_test.go new file mode 100644 index 0000000000000000000000000000000000000000..eb2cfd8abccb5ea9f59bbcd6fc665a695d4105bc --- /dev/null +++ b/rpc/rpc_test.go @@ -0,0 +1,35 @@ +package rpc + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +// ... +func TestNewJsonRpcResponse(t *testing.T) { + id := "testId" + data := "a string" + resp := &RPCResponse{ + Result: data, + Error: nil, + Id: id, + JSONRPC: "2.0", + } + respGen := NewRPCResponse(id, data) + assert.Equal(t, respGen, resp) +} + +// ... +func TestNewJsonRpcErrorResponse(t *testing.T) { + id := "testId" + code := 100 + message := "the error" + resp := &RPCResponse{ + Result: nil, + Error: &RPCError{code, message}, + Id: id, + JSONRPC: "2.0", + } + respGen := NewRPCErrorResponse(id, code, message) + assert.Equal(t, respGen, resp) +} \ No newline at end of file diff --git a/test/filters/filter_test.go b/test/filters/filter_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0163301748195b4df2c212f44598ed655ad0e52a --- /dev/null +++ b/test/filters/filter_test.go @@ -0,0 +1,246 @@ +package filters + +import ( + "fmt" + "github.com/stretchr/testify/suite" + "testing" + "sync" + . "github.com/eris-ltd/erisdb/erisdb/pipe" +) + +const OBJECTS = 100 + +type FilterableObject struct { + Integer int + String string +} + +// Filter for integer value. +// Ops: All +type IntegerFilter struct { + op string + value int64 + match func(int64, int64) bool +} + +func (this *IntegerFilter) Configure(fd *FilterData) error { + val, err := ParseNumberValue(fd.Value) + if err != nil { + return err + } + match, err2 := GetRangeFilter(fd.Op, "integer") + if err2 != nil { + return err2 + } + this.match = match + this.op = fd.Op + this.value = val + return nil +} + +func (this *IntegerFilter) Match(v interface{}) bool { + fo, ok := v.(FilterableObject) + if !ok { + return false + } + return this.match(int64(fo.Integer), this.value) +} + +// Filter for integer value. +// Ops: All +type StringFilter struct { + op string + value string + match func(string, string) bool +} + +func (this *StringFilter) Configure(fd *FilterData) error { + match, err := GetStringFilter(fd.Op, "string") + if err != nil { + return err + } + this.match = match + this.op = fd.Op + this.value = fd.Value + return nil +} + +func (this *StringFilter) Match(v interface{}) bool { + fo, ok := v.(FilterableObject) + if !ok { + return false + } + return this.match(fo.String, this.value) +} + +// Test suite +type FilterSuite struct { + suite.Suite + objects []FilterableObject + filterFactory *FilterFactory +} + +func (this *FilterSuite) SetupSuite() { + objects := make([]FilterableObject, OBJECTS, OBJECTS) + + for i := 0; i < 100; i++ { + objects[i] = FilterableObject{i, fmt.Sprintf("string%d",i)} + } + + ff := NewFilterFactory() + + ff.RegisterFilterPool("integer", &sync.Pool{ + New: func() interface{} { + return &IntegerFilter{} + }, + }) + + ff.RegisterFilterPool("string", &sync.Pool{ + New: func() interface{} { + return &StringFilter{} + }, + }) + + this.objects = objects + this.filterFactory = ff +} + +func (this *FilterSuite) TearDownSuite() { + +} + +// ********************************************* Tests ********************************************* + +func (this *FilterSuite) Test_FilterIntegersEquals() { + fd := &FilterData{"integer", "==", "5"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + break + } + } + this.Equal(arr, this.objects[5:6]) +} + +func (this *FilterSuite) Test_FilterIntegersLT() { + fd := &FilterData{"integer", "<", "5"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[:5]) +} + +func (this *FilterSuite) Test_FilterIntegersLTEQ() { + fd := &FilterData{"integer", "<=", "10"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[:11]) +} + +func (this *FilterSuite) Test_FilterIntegersGT() { + fd := &FilterData{"integer", ">", "50"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[51:]) +} + +func (this *FilterSuite) Test_FilterIntegersRange() { + fd0 := &FilterData{"integer", ">", "5"} + fd1 := &FilterData{"integer", "<", "38"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd0, fd1}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[6:38]) +} + +func (this *FilterSuite) Test_FilterIntegersGTEQ() { + fd := &FilterData{"integer", ">=", "77"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[77:]) +} + + +func (this *FilterSuite) Test_FilterIntegersNEQ() { + fd := &FilterData{"integer", "!=", "50"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + res := make([]FilterableObject, OBJECTS) + copy(res, this.objects) + res = append(res[:50], res[51:]...) + this.Equal(arr, res) +} + +func (this *FilterSuite) Test_FilterStringEquals() { + fd := &FilterData{"string", "==", "string7"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + this.Equal(arr, this.objects[7:8]) +} + + +func (this *FilterSuite) Test_FilterStringNEQ() { + fd := &FilterData{"string", "!=", "string50"} + filter, err := this.filterFactory.NewFilter([]*FilterData{fd}) + this.NoError(err) + arr := []FilterableObject{} + + for _, o := range this.objects { + if filter.Match(o) { + arr = append(arr, o) + } + } + res := make([]FilterableObject, OBJECTS) + copy(res, this.objects) + res = append(res[:50], res[51:]...) + this.Equal(arr, res) +} + +// ********************************************* Entrypoint ********************************************* + +func TestFilterSuite(t *testing.T) { + suite.Run(t, &FilterSuite{}) +} \ No newline at end of file diff --git a/test/mock/mock_web_api_test.go b/test/mock/mock_web_api_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a9dd13c3f6a61de95ef8ced841e5431ef0c758f2 --- /dev/null +++ b/test/mock/mock_web_api_test.go @@ -0,0 +1,262 @@ +package mock + +// Basic imports +import ( + "bytes" + "fmt" + // edb "github.com/eris-ltd/erisdb/erisdb" + edb "github.com/eris-ltd/erisdb/erisdb" + ep "github.com/eris-ltd/erisdb/erisdb/pipe" + "github.com/eris-ltd/erisdb/rpc" + "github.com/eris-ltd/erisdb/server" + td "github.com/eris-ltd/erisdb/test/testdata/testdata" + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/account" + "github.com/tendermint/log15" + "net/http" + "testing" + "os" + "runtime" +) + +func init() { + runtime.GOMAXPROCS(runtime.NumCPU()) + log15.Root().SetHandler(log15.LvlFilterHandler( + log15.LvlWarn, + log15.StreamHandler(os.Stdout, log15.TerminalFormat()), + )) + gin.SetMode(gin.ReleaseMode) +} + +type WebApiSuite struct { + suite.Suite + baseDir string + serveProcess *server.ServeProcess + codec rpc.Codec + sUrl string + testData *td.TestData +} + +func (this *WebApiSuite) SetupSuite() { + gin.SetMode(gin.ReleaseMode) + // Load the supporting objects. + testData := td.LoadTestData() + pipe := NewMockPipe(testData) + codec := &edb.TCodec{} + evtSubs := edb.NewEventSubscriptions(pipe.Events()) + // The server + restServer := edb.NewRestServer(codec, pipe, evtSubs) + sConf := server.DefaultServerConfig() + sConf.Bind.Port = 31400 + // Create a server process. + proc := server.NewServeProcess(sConf, restServer) + _ = proc.Start() + this.serveProcess = proc + this.codec = edb.NewTCodec() + this.testData = testData + this.sUrl = "http://localhost:31400" +} + +func (this *WebApiSuite) TearDownSuite() { + sec := this.serveProcess.StopEventChannel() + this.serveProcess.Stop(0) + <-sec +} + +// ********************************************* Consensus ********************************************* + +func (this *WebApiSuite) Test_A0_ConsensusState() { + resp := this.get("/consensus") + ret := &ep.ConsensusState{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + ret.StartTime = "" + this.Equal(ret, this.testData.Output.ConsensusState) +} + +func (this *WebApiSuite) Test_A1_Validators() { + resp := this.get("/consensus/validators") + ret := &ep.ValidatorList{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Validators) +} + +// ********************************************* Network ********************************************* + +func (this *WebApiSuite) Test_B0_NetworkInfo() { + resp := this.get("/network") + ret := &ep.NetworkInfo{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.NetworkInfo) +} + +func (this *WebApiSuite) Test_B1_Moniker() { + resp := this.get("/network/moniker") + ret := &ep.Moniker{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Moniker) +} + +func (this *WebApiSuite) Test_B2_Listening() { + resp := this.get("/network/listening") + ret := &ep.Listening{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Listening) +} + +func (this *WebApiSuite) Test_B3_Listeners() { + resp := this.get("/network/listeners") + ret := &ep.Listeners{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Listeners) +} + +func (this *WebApiSuite) Test_B4_Peers() { + resp := this.get("/network/peers") + ret := []*ep.Peer{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Peers) +} + +// ********************************************* Transactions ********************************************* + +func (this *WebApiSuite) Test_C0_TxCreate() { + resp := this.postJson("/unsafe/txpool", this.testData.Input.TxCreate) + ret := &ep.Receipt{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.TxCreateReceipt) +} + +func (this *WebApiSuite) Test_C1_Tx() { + resp := this.postJson("/unsafe/txpool", this.testData.Input.Tx) + ret := &ep.Receipt{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.TxReceipt) +} + +func (this *WebApiSuite) Test_C2_UnconfirmedTxs() { + resp := this.get("/txpool") + ret := &ep.UnconfirmedTxs{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.UnconfirmedTxs) +} + +func (this *WebApiSuite) Test_C3_CallCode() { + resp := this.postJson("/calls", this.testData.Input.CallCode) + ret := &ep.Call{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.CallCode) +} + +// ********************************************* Accounts ********************************************* + +func (this *WebApiSuite) Test_D0_Accounts() { + resp := this.get("/accounts") + ret := &ep.AccountList{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Accounts) +} + +func (this *WebApiSuite) Test_D1_Account() { + resp := this.get("/accounts/" + this.testData.Input.AccountAddress) + ret := &account.Account{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Account) +} + +func (this *WebApiSuite) Test_D2_Storage() { + resp := this.get("/accounts/" + this.testData.Input.AccountAddress + "/storage") + ret := &ep.Storage{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Storage) +} + +func (this *WebApiSuite) Test_D3_StorageAt() { + addr := this.testData.Input.AccountAddress + key := this.testData.Input.StorageAddress + resp := this.get("/accounts/" + addr + "/storage/" + key) + ret := &ep.StorageItem{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.StorageAt) +} + +// ********************************************* Blockchain ********************************************* + +func (this *WebApiSuite) Test_E0_BlockchainInfo() { + resp := this.get("/blockchain") + ret := &ep.BlockchainInfo{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.BlockchainInfo) +} + +func (this *WebApiSuite) Test_E1_ChainId() { + resp := this.get("/blockchain/chain_id") + ret := &ep.ChainId{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.ChainId) +} + +func (this *WebApiSuite) Test_E2_GenesisHash() { + resp := this.get("/blockchain/genesis_hash") + ret := &ep.GenesisHash{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.GenesisHash) +} + +func (this *WebApiSuite) Test_E3_LatestBlockHeight() { + resp := this.get("/blockchain/latest_block_height") + ret := &ep.LatestBlockHeight{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.LatestBlockHeight) +} + +func (this *WebApiSuite) Test_E4_Blocks() { + br := this.testData.Input.BlockRange + resp := this.get(fmt.Sprintf("/blockchain/blocks?q=height:%d..%d", br.Min, br.Max)) + ret := &ep.Blocks{} + errD := this.codec.Decode(ret, resp.Body) + this.NoError(errD) + this.Equal(ret, this.testData.Output.Blocks) +} + +// ********************************************* Utilities ********************************************* + +func (this *WebApiSuite) get(endpoint string) *http.Response { + resp, errG := http.Get(this.sUrl + endpoint) + this.NoError(errG) + return resp +} + +func (this *WebApiSuite) postJson(endpoint string, v interface{}) *http.Response { + bts, errE := this.codec.EncodeBytes(v) + this.NoError(errE) + resp, errP := http.Post(this.sUrl+endpoint, "application/json", bytes.NewBuffer(bts)) + this.NoError(errP) + this.Equal(200, resp.StatusCode) + return resp +} + +// ********************************************* Entrypoint ********************************************* + +func TestWebApiSuite(t *testing.T) { + suite.Run(t, &WebApiSuite{}) +} diff --git a/test/mock/pipe.go b/test/mock/pipe.go index e85cad89f8c3fb90955762be2de53ea4c8d81681..00134082afb0466f098c799dc12b5210fe08f547 100644 --- a/test/mock/pipe.go +++ b/test/mock/pipe.go @@ -2,13 +2,14 @@ package mock import ( ep "github.com/eris-ltd/erisdb/erisdb/pipe" + td "github.com/eris-ltd/erisdb/test/testdata/testdata" "github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/types" ) // Base struct. type MockPipe struct { - mockData *MockData + testOutput *td.Output accounts ep.Accounts blockchain ep.Blockchain consensus ep.Consensus @@ -18,15 +19,16 @@ type MockPipe struct { } // Create a new mock tendermint pipe. -func NewMockPipe(mockData *MockData) ep.Pipe { - accounts := &accounts{mockData} - blockchain := &blockchain{mockData} - consensus := &consensus{mockData} - events := &events{mockData} - net := &net{mockData} - transactor := &transactor{mockData} +func NewMockPipe(td *td.TestData) ep.Pipe { + testOutput := td.Output + accounts := &accounts{testOutput} + blockchain := &blockchain{testOutput} + consensus := &consensus{testOutput} + events := &events{testOutput} + net := &net{testOutput} + transactor := &transactor{testOutput} return &MockPipe{ - mockData, + testOutput, accounts, blockchain, consensus, @@ -38,7 +40,7 @@ func NewMockPipe(mockData *MockData) ep.Pipe { // Create a mock pipe with default mock data. func NewDefaultMockPipe() ep.Pipe { - return NewMockPipe(NewDefaultMockData()) + return NewMockPipe(td.LoadTestData()) } func (this *MockPipe) Accounts() ep.Accounts { @@ -69,82 +71,82 @@ func (this *MockPipe) Transactor() ep.Transactor { // Accounts type accounts struct { - mockData *MockData + testOutput *td.Output } func (this *accounts) GenPrivAccount() (*account.PrivAccount, error) { - return this.mockData.PrivAccount, nil + return this.testOutput.GenPrivAccount, nil } func (this *accounts) GenPrivAccountFromKey(key []byte) (*account.PrivAccount, error) { - return this.mockData.PrivAccount, nil + return this.testOutput.GenPrivAccount, nil } func (this *accounts) Accounts([]*ep.FilterData) (*ep.AccountList, error) { - return this.mockData.Accounts, nil + return this.testOutput.Accounts, nil } func (this *accounts) Account(address []byte) (*account.Account, error) { - return this.mockData.Account, nil + return this.testOutput.Account, nil } func (this *accounts) Storage(address []byte) (*ep.Storage, error) { - return this.mockData.Storage, nil + return this.testOutput.Storage, nil } func (this *accounts) StorageAt(address, key []byte) (*ep.StorageItem, error) { - return this.mockData.StorageAt, nil + return this.testOutput.StorageAt, nil } // Blockchain type blockchain struct { - mockData *MockData + testOutput *td.Output } func (this *blockchain) Info() (*ep.BlockchainInfo, error) { - return this.mockData.BlockchainInfo, nil + return this.testOutput.BlockchainInfo, nil } func (this *blockchain) ChainId() (string, error) { - return this.mockData.ChainId.ChainId, nil + return this.testOutput.ChainId.ChainId, nil } func (this *blockchain) GenesisHash() ([]byte, error) { - return this.mockData.GenesisHash.Hash, nil + return this.testOutput.GenesisHash.Hash, nil } func (this *blockchain) LatestBlockHeight() (uint, error) { - return this.mockData.LatestBlockHeight.Height, nil + return this.testOutput.LatestBlockHeight.Height, nil } func (this *blockchain) LatestBlock() (*types.Block, error) { - return this.mockData.LatestBlock, nil + return nil, nil } func (this *blockchain) Blocks([]*ep.FilterData) (*ep.Blocks, error) { - return this.mockData.Blocks, nil + return this.testOutput.Blocks, nil } func (this *blockchain) Block(height uint) (*types.Block, error) { - return this.mockData.Block, nil + return this.testOutput.Block, nil } // Consensus type consensus struct { - mockData *MockData + testOutput *td.Output } func (this *consensus) State() (*ep.ConsensusState, error) { - return this.mockData.ConsensusState, nil + return this.testOutput.ConsensusState, nil } func (this *consensus) Validators() (*ep.ValidatorList, error) { - return this.mockData.Validators, nil + return this.testOutput.Validators, nil } // Events type events struct { - mockData *MockData + testOutput *td.Output } func (this *events) Subscribe(subId, event string, callback func(interface{})) (bool, error) { @@ -152,57 +154,57 @@ func (this *events) Subscribe(subId, event string, callback func(interface{})) ( } func (this *events) Unsubscribe(subId string) (bool, error) { - return this.mockData.EventUnSub.Result, nil + return true, nil } // Net type net struct { - mockData *MockData + testOutput *td.Output } func (this *net) Info() (*ep.NetworkInfo, error) { - return this.mockData.NetworkInfo, nil + return this.testOutput.NetworkInfo, nil } func (this *net) Moniker() (string, error) { - return this.mockData.Moniker.Moniker, nil + return this.testOutput.Moniker.Moniker, nil } func (this *net) Listening() (bool, error) { - return this.mockData.Listening.Listening, nil + return this.testOutput.Listening.Listening, nil } func (this *net) Listeners() ([]string, error) { - return this.mockData.Listeners.Listeners, nil + return this.testOutput.Listeners.Listeners, nil } func (this *net) Peers() ([]*ep.Peer, error) { - return this.mockData.Peers, nil + return this.testOutput.Peers, nil } func (this *net) Peer(address string) (*ep.Peer, error) { - return this.mockData.Peer, nil + return nil, nil } // Txs type transactor struct { - mockData *MockData + testOutput *td.Output } func (this *transactor) Call(address, data []byte) (*ep.Call, error) { - return this.mockData.Call, nil + return nil, nil } func (this *transactor) CallCode(code, data []byte) (*ep.Call, error) { - return this.mockData.CallCode, nil + return this.testOutput.CallCode, nil } func (this *transactor) BroadcastTx(tx types.Tx) (*ep.Receipt, error) { - return this.mockData.BroadcastTx, nil + return nil, nil } func (this *transactor) UnconfirmedTxs() (*ep.UnconfirmedTxs, error) { - return this.mockData.UnconfirmedTxs, nil + return this.testOutput.UnconfirmedTxs, nil } func (this *transactor) TransactAsync(privKey, address, data []byte, gasLimit, fee uint64) (*ep.TransactionResult, error) { @@ -210,9 +212,12 @@ func (this *transactor) TransactAsync(privKey, address, data []byte, gasLimit, f } func (this *transactor) Transact(privKey, address, data []byte, gasLimit, fee uint64) (*ep.Receipt, error) { - return this.mockData.BroadcastTx, nil + if address == nil || len(address) == 0 { + return this.testOutput.TxCreateReceipt, nil + } + return this.testOutput.TxReceipt, nil } func (this *transactor) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (types.Tx, error) { - return this.mockData.SignTx, nil + return nil, nil } diff --git a/test/mock/pipedata.go b/test/mock/pipedata.go deleted file mode 100644 index 383594965c7fcbb6c29a21e1c0baf003d4d3e297..0000000000000000000000000000000000000000 --- a/test/mock/pipedata.go +++ /dev/null @@ -1,116 +0,0 @@ -package mock - -import ( - ep "github.com/eris-ltd/erisdb/erisdb/pipe" - "github.com/tendermint/tendermint/account" - "github.com/tendermint/tendermint/types" -) - -// TODO add from js as soon as this serialization stuff is sorted out. -var mockDataJson = ` - -` - -// PipeData is passed into the mock pipe implementation. It provides a -// return value for each of the rpc functions. -type MockData struct { - PrivAccount *account.PrivAccount `json:"priv_account"` - Account *account.Account `json:"account"` - Accounts *ep.AccountList `json:"accounts"` - Storage *ep.Storage `json:"storage"` - StorageAt *ep.StorageItem `json:"storage_at"` - BlockchainInfo *ep.BlockchainInfo `json:"blockchain_status"` - GenesisHash *ep.GenesisHash `json:"genesis_hash"` - LatestBlockHeight *ep.LatestBlockHeight `json:"latest_block_height"` - LatestBlock *types.Block `json:"latest_block"` - Blocks *ep.Blocks `json:"blocks"` - Block *types.Block `json:"block"` - ConsensusState *ep.ConsensusState `json:"consensus_state"` - Validators *ep.ValidatorList `json:"validators"` - EventSub *ep.EventSub `json:"event_sub"` - EventUnSub *ep.EventUnsub `json:"event_unSub"` - NetworkInfo *ep.NetworkInfo `json:"network_info"` - Moniker *ep.Moniker `json:"moniker"` - ChainId *ep.ChainId `json:"chain_id"` - Listening *ep.Listening `json:"listening"` - Listeners *ep.Listeners `json:"listening"` - Peers []*ep.Peer `json:"peers"` - Peer *ep.Peer `json:"peer"` - Call *ep.Call `json:"call"` - CallCode *ep.Call `json:"call_code"` - BroadcastTx *ep.Receipt `json:"broadcast_tx"` - UnconfirmedTxs *ep.UnconfirmedTxs `json:"unconfirmed_txs"` - SignTx *types.CallTx `json:"sign_tx"` -} - -func NewDefaultMockData() *MockData { - - /* - acc := &account.Account{Address: []byte("0000000000000000000000000000000000000000")} - acc.Code = []byte{} - acc.StorageRoot = []byte{} - accs := make([]*account.Account, 1) - accs[0] = acc - accounts := &ep.AccountList{accs} - storage := &ep.Storage{} - storageAt := &ep.StorageItem{} - - genesisHash := []byte{0} - latestBlockHeight := uint(1) - latestBlock := &ep.Block{} - chainId := "mock_chain" - blockchainInfo := &ep.BlockchainInfo{} - blocks := &ep.Blocks{} - block := &ep.Block{} - - consensusState := &ep.ConsensusState{} - validators := &ep.ValidatorList{} - - eventSub := true - eventUnSub := true - moniker := "mock_moniker" - listening := true - listeners := []string{} - peer := &ep.Peer{} - peers := []*ep.Peer{peer} - networkInfo := &ep.NetworkInfo{moniker, listening, listeners, peers} - - call := &ep.Call{} - callCode := &ep.Call{} - broadcastTx := &ep.Receipt{} - unconfirmedTxs := &ep.UnconfirmedTxs{} - signTx := &types.CallTx{} - privAccount := &account.PrivAccount{} - - return &MockData{ - Account: acc, - Accounts: accounts, - Storage: storage, - StorageAt: storageAt, - BlockchainInfo: blockchainInfo, - GenesisHash: genesisHash, - LatestBlockHeight: latestBlockHeight, - LatestBlock: latestBlock, - Blocks: blocks, - Block: block, - ConsensusState: consensusState, - Validators: validators, - EventSub: eventSub, - EventUnSub: eventUnSub, - NetworkInfo: networkInfo, - Moniker: moniker, - ChainId: chainId, - Listening: listening, - Listeners: listeners, - Peers: peers, - Peer: peer, - Call: call, - CallCode: callCode, - BroadcastTx: broadcastTx, - UnconfirmedTxs: unconfirmedTxs, - SignTx: signTx, - PrivAccount: privAccount, - } - */ - return &MockData{} -} diff --git a/test/server/http_flood_test.go b/test/server/http_flood_test.go index 128f73c089f30bb5cba276490dfe5135b958a518..e3c02bf447c1362215010f8e9c69ab1d23eb1ca9 100644 --- a/test/server/http_flood_test.go +++ b/test/server/http_flood_test.go @@ -28,7 +28,6 @@ func TestHttpFlooding(t *testing.T) { errStop := serveProcess.Stop(0) <-stopC assert.NoError(t, errStop, "Scumbag-ed!") - } func runHttp() error { @@ -36,7 +35,7 @@ func runHttp() error { duration := DURATION * time.Second targeter := vegeta.NewStaticTargeter(&vegeta.Target{ Method: "GET", - URL: "http://localhost:1337/scumbag", + URL: "http://localhost:31333/scumbag", }) attacker := vegeta.NewAttacker() var results vegeta.Results diff --git a/test/server/scumbag.go b/test/server/scumbag.go index 72d02c4d5b41bcffaa2d02ec228e72ff5611f05e..d895b6a4fb06793dc6bfd50a2f4d1f9f28ec075a 100644 --- a/test/server/scumbag.go +++ b/test/server/scumbag.go @@ -56,7 +56,9 @@ func NewScumsocketServer(maxConnections uint) *server.WebSocketServer { } func NewServeScumbag() *server.ServeProcess { - return server.NewServeProcess(nil, NewScumbagServer()) + cfg := server.DefaultServerConfig() + cfg.Bind.Port = uint16(31333) + return server.NewServeProcess(cfg, NewScumbagServer()) } func NewServeScumSocket(wsServer *server.WebSocketServer) *server.ServeProcess { diff --git a/test/web_api/web_api_test.go b/test/web_api/web_api_test.go index 84f64835e7281e7b1bc4f03bcee8814d60302f5a..ebfc6c75046a29c6a7aa21e32a90390944c03b45 100644 --- a/test/web_api/web_api_test.go +++ b/test/web_api/web_api_test.go @@ -20,7 +20,6 @@ import ( "testing" ) -// type WebApiSuite struct { suite.Suite baseDir string diff --git a/util/util.go b/util/util.go index 08b64b0272a4b575cedf2558600f4767d79223a1..b3f7b1b3a3de89ee55cf40f9c3910b1d0e3a0d51 100644 --- a/util/util.go +++ b/util/util.go @@ -24,12 +24,12 @@ func IsAddress(str string) bool { return addrRe.MatchString(str) } -// Is the candidate a public key string (32 bytes, hex). -func IsPub(str string) bool { +// Is the candidate a public key string (32 bytes). This is not a good name. +func IsPubKey(str string) bool { return pubRe.MatchString(str) } -// Is the candidate a private key string (64 bytes, hex). -func IsPriv(str string) bool { +// Is the candidate a private key string (64 bytes, hex). This is not a good name. +func IsPrivKey(str string) bool { return privRe.MatchString(str) -} +} \ No newline at end of file diff --git a/util/util_test.go b/util/util_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5353d6714c7106761155f761a7279bf6e9ee3553 --- /dev/null +++ b/util/util_test.go @@ -0,0 +1,76 @@ +package util + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +// This should succeed +func TestIsHexSuccess(t *testing.T) { + assert.True(t, IsHex("5B33600060006101000A81548173FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF021916908302179055505B6102828061003B6000396000F3006000357C01000000000000000000000000000000000000000000000000000000009004806337F428411461004557806340C10F191461005A578063D0679D341461006E57005B610050600435610244565B8060005260206000F35B610068600435602435610082565B60006000F35B61007C600435602435610123565B60006000F35B600060009054906101000A900473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1673FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF163373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1614156100DD576100E2565B61011F565B80600160005060008473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020019081526020016000206000828282505401925050819055505B5050565B80600160005060003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF168152602001908152602001600020600050541061015E57610163565B610240565B80600160005060003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF16815260200190815260200160002060008282825054039250508190555080600160005060008473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020019081526020016000206000828282505401925050819055507F93EB3C629EB575EDAF0252E4F9FC0C5CCADA50496F8C1D32F0F93A65A8257EB560003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020018373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020018281526020016000A15B5050565B6000600160005060008373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF16815260200190815260200160002060005054905061027D565B91905056")) +} + +// This should fail because there is a non matching character. +func TestIsHexFailChar(t *testing.T) { + assert.False(t,IsHex("562Q")) +} + +// This should succeed +func TestIsHashSuccess(t *testing.T) { + assert.True(t, IsHash("DA4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5FC")) +} + +// This should fail because there is a non matching character. +func TestIsHashFailChar(t *testing.T) { + assert.False(t,IsHash("RM4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5FC")) +} + +// This should fail because the length is not right. +func TestIsHashFailLength(t *testing.T) { + assert.False(t,IsHash("DA4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5F")) +} + +// This should succeed +func TestIsAddressSuccess(t *testing.T) { + assert.True(t, IsAddress("37236DF251AB70022B1DA351F08A20FB52443E37")) +} + +// This should fail because there is a non matching character. +func TestIsAddressFailChar(t *testing.T) { + assert.False(t,IsAddress("37236DF251AB70022B1DA351F08A20FB52443E3Q")) +} + +// This should fail because the length is not right. +func TestIsAddressFailLength(t *testing.T) { + assert.False(t,IsAddress("37236DF251AB70022B1DA351F08A20FB52443E")) +} + +// This should succeed +func TestIsPubKeySuccess(t *testing.T) { + assert.True(t, IsPubKey("CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) +} + +// This should fail because there is a non matching character. +func TestIsPubKeyFailChar(t *testing.T) { + assert.False(t,IsPubKey("CB3688B7I6TD488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) +} + +// This should fail because the length is not right. +func TestIsPubKeyFailLength(t *testing.T) { + assert.False(t,IsPubKey("CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11")) +} + +// This should succeed +func TestIsPrivKeySuccess(t *testing.T) { + assert.True(t, IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774E9C9C334687A65DCBAD2C4151CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) +} + +// This should fail because there is a non matching character. +func TestIsPrivKeyFailChar(t *testing.T) { + assert.False(t,IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774E9C9C334687A65DCBAD2C4151CB3688B7561D488A2A4834ESAEE9398BEF94844D8BDBBCA980C11E3654A45906")) +} + +// This should fail because the length is not right. +func TestIsPrivKeyFailLength(t *testing.T) { + assert.False(t,IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774ED2C4151CB3688B7561D488A2A48EF94844D8BDBBCA980C11E3654A45906")) +} \ No newline at end of file