2017-12-02 18:51:55 +00:00
|
|
|
package debug_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2017-12-02 18:51:55 +00:00
|
|
|
"github.com/status-im/status-go/cmd/statusd/debug"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-06-12 16:50:25 +00:00
|
|
|
testifyAssert "github.com/stretchr/testify/assert"
|
2017-12-02 18:51:55 +00:00
|
|
|
)
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
const stopNodeCommandLine = "StopNode()"
|
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
// TestInvalidExpressions tests invalid expressions.
|
|
|
|
func TestInvalidExpressions(t *testing.T) {
|
2018-06-12 16:50:25 +00:00
|
|
|
assert := testifyAssert.New(t)
|
2017-12-02 18:51:55 +00:00
|
|
|
|
|
|
|
startDebugging(assert)
|
|
|
|
|
|
|
|
conn := connectDebug(assert)
|
|
|
|
tests := []struct {
|
|
|
|
commandLine string
|
|
|
|
replies []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
commandLine: "",
|
|
|
|
replies: []string{"[0] cannot read command: 1:2: expected operand, found 'EOF'"},
|
|
|
|
}, {
|
|
|
|
commandLine: "1 + 1",
|
|
|
|
replies: []string{"[0] cannot read command: invalid command line: \"1 + 1\\n\""},
|
|
|
|
}, {
|
|
|
|
commandLine: "func() { panic(42) }",
|
|
|
|
replies: []string{"[0] cannot read command: invalid command line: \"func() { panic(42) }\\n\""},
|
|
|
|
}, {
|
|
|
|
commandLine: "DoesNotExist()",
|
|
|
|
replies: []string{"[0] cannot execute command: command \"DoesNotExist\" not found"},
|
|
|
|
}, {
|
|
|
|
commandLine: "node.Start()",
|
|
|
|
replies: []string{"[0] cannot read command: invalid expression: \"node.Start()\\n\""},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
replies := sendCommandLine(assert, conn, test.commandLine)
|
|
|
|
assert.Equal(test.replies, replies)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStartStopNode tests starting and stopping a node remotely.
|
|
|
|
func TestStartStopNode(t *testing.T) {
|
2018-06-12 16:50:25 +00:00
|
|
|
assert := testifyAssert.New(t)
|
2017-12-02 18:51:55 +00:00
|
|
|
configJSON, cleanup, err := mkConfigJSON("start-stop-node")
|
|
|
|
assert.NoError(err)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
startDebugging(assert)
|
|
|
|
|
|
|
|
conn := connectDebug(assert)
|
|
|
|
|
|
|
|
commandLine := fmt.Sprintf("StartNode(%q)", configJSON)
|
|
|
|
replies := sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
replies = sendCommandLine(assert, conn, stopNodeCommandLine)
|
2017-12-02 18:51:55 +00:00
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCreateAccount tests creating an account on the server.
|
|
|
|
func TestCreateAccount(t *testing.T) {
|
2018-06-12 16:50:25 +00:00
|
|
|
assert := testifyAssert.New(t)
|
2017-12-02 18:51:55 +00:00
|
|
|
configJSON, cleanup, err := mkConfigJSON("create-account")
|
|
|
|
assert.NoError(err)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
startDebugging(assert)
|
|
|
|
|
|
|
|
conn := connectDebug(assert)
|
|
|
|
|
|
|
|
commandLine := fmt.Sprintf("StartNode(%q)", configJSON)
|
|
|
|
replies := sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
|
|
|
|
commandLine = fmt.Sprintf("CreateAccount(%q)", "password")
|
|
|
|
replies = sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 4)
|
|
|
|
assert.NotEqual("[0] <nil>", replies[0])
|
|
|
|
assert.NotEqual("[1] <nil>", replies[1])
|
|
|
|
assert.NotEqual("[2] <nil>", replies[2])
|
|
|
|
assert.Equal("[3] <nil>", replies[3])
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
replies = sendCommandLine(assert, conn, stopNodeCommandLine)
|
2017-12-02 18:51:55 +00:00
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSelectAccountLogout tests selecting an account on the server
|
|
|
|
// and logging out afterwards.
|
|
|
|
func TestSelectAccountLogout(t *testing.T) {
|
2018-06-12 16:50:25 +00:00
|
|
|
assert := testifyAssert.New(t)
|
2017-12-02 18:51:55 +00:00
|
|
|
configJSON, cleanup, err := mkConfigJSON("select-account")
|
|
|
|
assert.NoError(err)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
startDebugging(assert)
|
|
|
|
|
|
|
|
conn := connectDebug(assert)
|
|
|
|
|
|
|
|
commandLine := fmt.Sprintf("StartNode(%q)", configJSON)
|
|
|
|
replies := sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
|
|
|
|
commandLine = fmt.Sprintf("CreateAccount(%q)", "password")
|
|
|
|
replies = sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 4)
|
|
|
|
assert.NotEqual("[0] <nil>", replies[0])
|
|
|
|
assert.NotEqual("[1] <nil>", replies[1])
|
|
|
|
assert.NotEqual("[2] <nil>", replies[2])
|
|
|
|
assert.Equal("[3] <nil>", replies[3])
|
|
|
|
|
|
|
|
address := replies[0][4:]
|
|
|
|
|
|
|
|
commandLine = fmt.Sprintf("SelectAccount(%q, %q)", address, "password")
|
|
|
|
replies = sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
|
|
|
|
commandLine = "Logout()"
|
|
|
|
replies = sendCommandLine(assert, conn, commandLine)
|
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
replies = sendCommandLine(assert, conn, stopNodeCommandLine)
|
2017-12-02 18:51:55 +00:00
|
|
|
assert.Len(replies, 1)
|
|
|
|
assert.Equal("[0] <nil>", replies[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----
|
|
|
|
// HELPERS
|
|
|
|
//-----
|
|
|
|
|
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
|
|
|
d *debug.Server
|
|
|
|
)
|
|
|
|
|
|
|
|
// startDebugging lazily creates or reuses a debug instance.
|
2018-06-12 16:50:25 +00:00
|
|
|
func startDebugging(assert *testifyAssert.Assertions) {
|
2017-12-02 18:51:55 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if d == nil {
|
|
|
|
var err error
|
2018-06-19 07:49:24 +00:00
|
|
|
backend := api.NewStatusBackend()
|
|
|
|
d, err = debug.New(backend, debug.CLIPort)
|
2017-12-02 18:51:55 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mkConfigJSON creates a configuration matching to
|
|
|
|
// a temporary directory and a cleanup for that directory.
|
|
|
|
func mkConfigJSON(name string) (string, func(), error) {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), name)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
cleanup := func() {
|
|
|
|
os.RemoveAll(tmpDir) //nolint: errcheck
|
|
|
|
}
|
|
|
|
configJSON := `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(params.RopstenNetworkID) + `,
|
|
|
|
"DataDir": "` + tmpDir + `",
|
|
|
|
"LogLevel": "INFO",
|
|
|
|
"RPCEnabled": true
|
|
|
|
}`
|
|
|
|
return configJSON, cleanup, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectDebug connects to the debug instance.
|
2018-06-12 16:50:25 +00:00
|
|
|
func connectDebug(assert *testifyAssert.Assertions) net.Conn {
|
2017-12-02 18:51:55 +00:00
|
|
|
conn, err := net.Dial("tcp", ":51515")
|
|
|
|
assert.NoError(err)
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendCommandLine sends a command line via the passed connection.
|
2018-06-12 16:50:25 +00:00
|
|
|
// nolint: interfacer
|
|
|
|
func sendCommandLine(assert *testifyAssert.Assertions, conn net.Conn, commandLine string) []string {
|
2017-12-02 18:51:55 +00:00
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
writer := bufio.NewWriter(conn)
|
|
|
|
_, err := writer.WriteString(commandLine + "\n")
|
|
|
|
assert.NoError(err)
|
|
|
|
err = writer.Flush()
|
|
|
|
assert.NoError(err)
|
|
|
|
countStr, err := reader.ReadString('\n')
|
|
|
|
assert.NoError(err)
|
|
|
|
count, err := strconv.Atoi(strings.TrimSuffix(countStr, "\n"))
|
|
|
|
assert.NoError(err)
|
|
|
|
replies := make([]string, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
reply, err := reader.ReadString('\n')
|
|
|
|
assert.NoError(err)
|
|
|
|
replies[i] = strings.TrimSuffix(reply, "\n")
|
|
|
|
}
|
|
|
|
return replies
|
|
|
|
}
|