2016-09-11 11:44:14 +00:00
|
|
|
package jail_test
|
|
|
|
|
|
|
|
import (
|
2016-10-07 14:48:36 +00:00
|
|
|
"encoding/json"
|
2017-03-14 13:17:32 +00:00
|
|
|
"os"
|
2016-09-11 11:44:14 +00:00
|
|
|
"reflect"
|
2017-02-05 18:41:26 +00:00
|
|
|
"strconv"
|
2016-10-07 14:48:36 +00:00
|
|
|
"strings"
|
2016-09-11 11:44:14 +00:00
|
|
|
"testing"
|
2016-10-07 14:48:36 +00:00
|
|
|
"time"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-10-07 14:48:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-03-21 16:27:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/geth"
|
2017-03-14 13:17:32 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail"
|
2017-03-18 18:23:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-21 16:27:59 +00:00
|
|
|
whisperMessage1 = `test message 1 (K1 -> K2, signed+encrypted, from us)`
|
|
|
|
whisperMessage2 = `test message 2 (K1 -> K1, signed+encrypted to ourselves)`
|
|
|
|
whisperMessage3 = `test message 3 (K1 -> "", signed broadcast)`
|
|
|
|
whisperMessage4 = `test message 4 ("" -> "", anon broadcast)`
|
|
|
|
whisperMessage5 = `test message 5 ("" -> K1, encrypted anon broadcast)`
|
|
|
|
whisperMessage6 = `test message 6 (K2 -> K1, signed+encrypted, to us)`
|
2017-05-03 14:24:48 +00:00
|
|
|
testChatID = "testChat"
|
2017-03-21 16:27:59 +00:00
|
|
|
statusJSFilePath = "testdata/status.js"
|
|
|
|
txSendFolder = "testdata/tx-send/"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2017-03-14 13:17:32 +00:00
|
|
|
var testConfig *geth.TestConfig
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// load shared test configuration
|
|
|
|
var err error
|
|
|
|
testConfig, err = geth.LoadTestConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run tests
|
|
|
|
retCode := m.Run()
|
|
|
|
|
|
|
|
//time.Sleep(25 * time.Second) // to give some time to propagate txs to the rest of the network
|
|
|
|
os.Exit(retCode)
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
func TestJailUnInited(t *testing.T) {
|
|
|
|
errorWrapper := func(err error) string {
|
|
|
|
return `{"error":"` + err.Error() + `"}`
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedError := errorWrapper(jail.ErrInvalidJail)
|
|
|
|
|
|
|
|
var jailInstance *jail.Jail
|
2017-05-03 14:24:48 +00:00
|
|
|
response := jailInstance.Parse(testChatID, ``)
|
2016-09-11 11:44:14 +00:00
|
|
|
if response != expectedError {
|
|
|
|
t.Errorf("error expected, but got: %v", response)
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
response = jailInstance.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2016-09-11 11:44:14 +00:00
|
|
|
if response != expectedError {
|
|
|
|
t.Errorf("error expected, but got: %v", response)
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
_, err := jailInstance.GetVM(testChatID)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != jail.ErrInvalidJail {
|
|
|
|
t.Errorf("error expected, but got: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-21 09:02:38 +00:00
|
|
|
_, err = jailInstance.RPCClient()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != jail.ErrInvalidJail {
|
|
|
|
t.Errorf("error expected, but got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now make sure that if Init is called, then Parse doesn't produce any error
|
|
|
|
jailInstance = jail.Init(``)
|
|
|
|
if jailInstance == nil {
|
|
|
|
t.Error("jail instance shouldn't be nil at this point")
|
|
|
|
return
|
|
|
|
}
|
2017-03-21 16:27:59 +00:00
|
|
|
statusJS := geth.LoadFromFile(statusJSFilePath) + `;
|
2016-09-11 11:44:14 +00:00
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};`
|
2017-05-03 14:24:48 +00:00
|
|
|
response = jailInstance.Parse(testChatID, statusJS)
|
2016-09-11 11:44:14 +00:00
|
|
|
expectedResponse := `{"result": {"commands":{},"responses":{}}}`
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("unexpected response received: %v", response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// however, we still expect issue voiced if somebody tries to execute code with Call
|
2017-05-03 14:24:48 +00:00
|
|
|
response = jailInstance.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2016-09-11 11:44:14 +00:00
|
|
|
if response != errorWrapper(geth.ErrInvalidGethNode) {
|
|
|
|
t.Errorf("error expected, but got: %v", response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that Call() succeeds when node is started
|
|
|
|
err = geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
response = jailInstance.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2016-09-11 11:44:14 +00:00
|
|
|
expectedResponse = `{"result": 144}`
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJailInit(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
initCode := `
|
|
|
|
var _status_catalog = {
|
|
|
|
foo: 'bar'
|
|
|
|
};
|
|
|
|
`
|
|
|
|
jailInstance := jail.Init(initCode)
|
|
|
|
|
|
|
|
extraCode := `
|
|
|
|
var extraFunc = function (x) {
|
|
|
|
return x * x;
|
|
|
|
};
|
|
|
|
`
|
2017-03-21 16:27:59 +00:00
|
|
|
response := jailInstance.Parse("newChat", extraCode)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
expectedResponse := `{"result": {"foo":"bar"}}`
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedResponse, response) {
|
|
|
|
t.Error("Expected output not returned from jail.Parse()")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJailFunctionCall(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
|
|
|
|
|
|
|
// load Status JS and add test command to it
|
2017-03-21 16:27:59 +00:00
|
|
|
statusJS := geth.LoadFromFile(statusJSFilePath) + `;
|
2016-09-11 11:44:14 +00:00
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};`
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, statusJS)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
// call with wrong chat id
|
2017-05-03 14:24:48 +00:00
|
|
|
response := jailInstance.Call("chatIDNonExistent", "", "")
|
|
|
|
expectedError := `{"error":"Cell[chatIDNonExistent] doesn't exist."}`
|
2016-09-11 11:44:14 +00:00
|
|
|
if response != expectedError {
|
|
|
|
t.Errorf("expected error is not returned: expected %s, got %s", expectedError, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// call extraFunc()
|
2017-05-03 14:24:48 +00:00
|
|
|
response = jailInstance.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2016-09-11 11:44:14 +00:00
|
|
|
expectedResponse := `{"result": 144}`
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJailRPCSend(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
|
|
|
|
|
|
|
// load Status JS and add test command to it
|
2017-03-21 16:27:59 +00:00
|
|
|
statusJS := geth.LoadFromFile(statusJSFilePath)
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, statusJS)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-05-03 14:24:48 +00:00
|
|
|
vm, err := jailInstance.GetVM(testChatID)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:48:36 +00:00
|
|
|
// internally (since we replaced `web3.send` with `jail.Send`)
|
|
|
|
// all requests to web3 are forwarded to `jail.Send`
|
2016-09-11 11:44:14 +00:00
|
|
|
_, err = vm.Run(`
|
2017-03-14 22:57:07 +00:00
|
|
|
var balance = web3.eth.getBalance("` + testConfig.Account1.Address + `");
|
2016-10-07 14:48:36 +00:00
|
|
|
var sendResult = web3.fromWei(balance, "ether")
|
2016-09-11 11:44:14 +00:00
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot run custom code on VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := vm.Get("sendResult")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot obtain result of balance check operation: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
balance, err := value.ToFloat()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot obtain result of balance check operation: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:19:20 +00:00
|
|
|
if balance < 100 {
|
2016-09-11 11:44:14 +00:00
|
|
|
t.Error("wrong balance (there should be lots of test Ether on that account)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
t.Logf("Balance of %.2f ETH found on '%s' account", balance, testConfig.Account1.Address)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 14:48:36 +00:00
|
|
|
func TestJailSendQueuedTransaction(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-15 09:49:05 +00:00
|
|
|
// log into account from which transactions will be sent
|
2017-03-14 22:57:07 +00:00
|
|
|
if err := geth.SelectAccount(testConfig.Account1.Address, testConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot select account: %v", testConfig.Account1.Address)
|
2016-12-15 09:49:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:48:36 +00:00
|
|
|
txParams := `{
|
2017-03-14 22:57:07 +00:00
|
|
|
"from": "` + testConfig.Account1.Address + `",
|
2016-10-07 14:48:36 +00:00
|
|
|
"to": "0xf82da7547534045b4e00442bc89e16186cf8c272",
|
|
|
|
"value": "0.000001"
|
|
|
|
}`
|
|
|
|
|
2016-10-13 18:02:48 +00:00
|
|
|
txCompletedSuccessfully := make(chan struct{})
|
|
|
|
txCompletedCounter := make(chan struct{})
|
|
|
|
txHashes := make(chan common.Hash)
|
2016-10-07 14:48:36 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
requireMessageId := false
|
|
|
|
geth.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
2016-12-18 20:36:17 +00:00
|
|
|
var envelope geth.SignalEnvelope
|
2016-10-07 14:48:36 +00:00
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if envelope.Type == geth.EventTransactionQueued {
|
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
messageId, ok := event["message_id"].(string)
|
|
|
|
if !ok {
|
|
|
|
t.Error("Message id is required, but not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if requireMessageId {
|
|
|
|
if len(messageId) == 0 {
|
|
|
|
t.Error("Message id is required, but not provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(messageId) != 0 {
|
|
|
|
t.Error("Message id is not required, but provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 13:17:32 +00:00
|
|
|
t.Logf("Transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
|
2016-10-07 14:48:36 +00:00
|
|
|
|
2016-10-13 18:02:48 +00:00
|
|
|
var txHash common.Hash
|
2017-03-14 22:57:07 +00:00
|
|
|
if txHash, err = geth.CompleteTransaction(event["id"].(string), testConfig.Account1.Password); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
t.Errorf("cannot complete queued transaction[%v]: %v", event["id"], err)
|
2016-10-13 18:02:48 +00:00
|
|
|
} else {
|
|
|
|
t.Logf("Transaction complete: https://testnet.etherscan.io/tx/%s", txHash.Hex())
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:02:48 +00:00
|
|
|
txCompletedSuccessfully <- struct{}{} // so that timeout is aborted
|
|
|
|
txHashes <- txHash
|
|
|
|
txCompletedCounter <- struct{}{}
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-13 18:02:48 +00:00
|
|
|
type testCommand struct {
|
2016-10-07 14:48:36 +00:00
|
|
|
command string
|
|
|
|
params string
|
|
|
|
expectedResponse string
|
|
|
|
}
|
2016-10-13 18:02:48 +00:00
|
|
|
type testCase struct {
|
2016-10-07 14:48:36 +00:00
|
|
|
name string
|
|
|
|
file string
|
|
|
|
requireMessageId bool
|
2016-10-13 18:02:48 +00:00
|
|
|
commands []testCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testCase{
|
2016-10-07 14:48:36 +00:00
|
|
|
{
|
|
|
|
// no context or message id
|
|
|
|
name: "Case 1: no message id or context in inited JS",
|
|
|
|
file: "no-message-id-or-context.js",
|
|
|
|
requireMessageId: false,
|
2016-10-13 18:02:48 +00:00
|
|
|
commands: []testCommand{
|
2016-10-07 14:48:36 +00:00
|
|
|
{
|
|
|
|
`["commands", "send"]`,
|
|
|
|
txParams,
|
|
|
|
`{"result": {"transaction-hash":"TX_HASH"}}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["commands", "getBalance"]`,
|
2017-03-14 22:57:07 +00:00
|
|
|
`{"address": "` + testConfig.Account1.Address + `"}`,
|
2016-10-07 14:48:36 +00:00
|
|
|
`{"result": {"balance":42}}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// context is present in inited JS (but no message id is there)
|
|
|
|
name: "Case 2: context is present in inited JS (but no message id is there)",
|
|
|
|
file: "context-no-message-id.js",
|
|
|
|
requireMessageId: false,
|
2016-10-13 18:02:48 +00:00
|
|
|
commands: []testCommand{
|
2016-10-07 14:48:36 +00:00
|
|
|
{
|
|
|
|
`["commands", "send"]`,
|
|
|
|
txParams,
|
|
|
|
`{"result": {"context":{"` + geth.SendTransactionRequest + `":true},"result":{"transaction-hash":"TX_HASH"}}}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["commands", "getBalance"]`,
|
2017-03-14 22:57:07 +00:00
|
|
|
`{"address": "` + testConfig.Account1.Address + `"}`,
|
2017-05-03 14:24:48 +00:00
|
|
|
`{"result": {"context":{},"result":{"balance":42}}}`, // note empty (but present) context!
|
2016-10-07 14:48:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// message id is present in inited JS, but no context is there
|
|
|
|
name: "Case 3: message id is present, context is not present",
|
|
|
|
file: "message-id-no-context.js",
|
|
|
|
requireMessageId: true,
|
2016-10-13 18:02:48 +00:00
|
|
|
commands: []testCommand{
|
2016-10-07 14:48:36 +00:00
|
|
|
{
|
|
|
|
`["commands", "send"]`,
|
|
|
|
txParams,
|
|
|
|
`{"result": {"transaction-hash":"TX_HASH"}}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["commands", "getBalance"]`,
|
2017-03-14 22:57:07 +00:00
|
|
|
`{"address": "` + testConfig.Account1.Address + `"}`,
|
2017-05-03 14:24:48 +00:00
|
|
|
`{"result": {"balance":42}}`, // note empty context!
|
2016-10-07 14:48:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// both message id and context are present in inited JS (this UC is what we normally expect to see)
|
|
|
|
name: "Case 4: both message id and context are present",
|
|
|
|
file: "tx-send.js",
|
|
|
|
requireMessageId: true,
|
2016-10-13 18:02:48 +00:00
|
|
|
commands: []testCommand{
|
2016-10-07 14:48:36 +00:00
|
|
|
{
|
|
|
|
`["commands", "send"]`,
|
|
|
|
txParams,
|
|
|
|
`{"result": {"context":{"eth_sendTransaction":true,"message_id":"foobar"},"result":{"transaction-hash":"TX_HASH"}}}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["commands", "getBalance"]`,
|
2017-03-14 22:57:07 +00:00
|
|
|
`{"address": "` + testConfig.Account1.Address + `"}`,
|
2016-10-13 18:02:48 +00:00
|
|
|
`{"result": {"context":{"message_id":"42"},"result":{"balance":42}}}`, // message id in context, but default one is used!
|
2016-10-07 14:48:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-03-21 16:27:59 +00:00
|
|
|
jailInstance := jail.Init(geth.LoadFromFile(txSendFolder + test.file))
|
2016-10-13 18:02:48 +00:00
|
|
|
geth.PanicAfter(60*time.Second, txCompletedSuccessfully, test.name)
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, ``)
|
2016-10-07 14:48:36 +00:00
|
|
|
|
|
|
|
requireMessageId = test.requireMessageId
|
|
|
|
|
2016-10-13 18:02:48 +00:00
|
|
|
for _, command := range test.commands {
|
|
|
|
go func(jail *jail.Jail, test testCase, command testCommand) {
|
|
|
|
t.Logf("->%s: %s", test.name, command.command)
|
2017-05-03 14:24:48 +00:00
|
|
|
response := jail.Call(testChatID, command.command, command.params)
|
2016-10-13 18:02:48 +00:00
|
|
|
var txHash common.Hash
|
|
|
|
if command.command == `["commands", "send"]` {
|
|
|
|
txHash = <-txHashes
|
|
|
|
}
|
|
|
|
expectedResponse := strings.Replace(command.expectedResponse, "TX_HASH", txHash.Hex(), 1)
|
|
|
|
if response != expectedResponse {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(jailInstance, test, command)
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
2016-10-13 18:02:48 +00:00
|
|
|
<-txCompletedCounter
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
func TestJailMultipleInitSingletonJail(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance1 := jail.Init("")
|
|
|
|
jailInstance2 := jail.Init("")
|
|
|
|
jailInstance3 := jail.New()
|
|
|
|
jailInstance4 := jail.GetInstance()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(jailInstance1, jailInstance2) {
|
|
|
|
t.Error("singleton property of jail instance is violated")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(jailInstance2, jailInstance3) {
|
|
|
|
t.Error("singleton property of jail instance is violated")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(jailInstance3, jailInstance4) {
|
|
|
|
t.Error("singleton property of jail instance is violated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJailGetVM(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
expectedError := `cell[nonExistentChat] doesn't exist`
|
2017-03-21 16:27:59 +00:00
|
|
|
_, err = jailInstance.GetVM("nonExistentChat")
|
2016-09-11 11:44:14 +00:00
|
|
|
if err == nil || err.Error() != expectedError {
|
|
|
|
t.Error("expected error, but call succeeded")
|
|
|
|
}
|
|
|
|
|
|
|
|
// now let's create VM..
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, ``)
|
2016-09-11 11:44:14 +00:00
|
|
|
// ..and see if VM becomes available
|
2017-05-03 14:24:48 +00:00
|
|
|
_, err = jailInstance.GetVM(testChatID)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 16:52:37 +00:00
|
|
|
|
|
|
|
func TestIsConnected(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, "")
|
2016-12-01 16:52:37 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-05-03 14:24:48 +00:00
|
|
|
vm, err := jailInstance.GetVM(testChatID)
|
2016-12-01 16:52:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = vm.Run(`
|
|
|
|
var responseValue = web3.isConnected();
|
|
|
|
responseValue = JSON.stringify(responseValue);
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot run custom code on VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
responseValue, err := vm.Get("responseValue")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot obtain result of isConnected(): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := responseValue.ToString()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot parse result: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
expectedResponse := `{"jsonrpc":"2.0","result":true}`
|
|
|
|
if !reflect.DeepEqual(response, expectedResponse) {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStorageSet(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, "")
|
2016-12-18 20:36:17 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-05-03 14:24:48 +00:00
|
|
|
vm, err := jailInstance.GetVM(testChatID)
|
2016-12-18 20:36:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testData := "foobar"
|
|
|
|
|
|
|
|
opCompletedSuccessfully := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
geth.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope geth.SignalEnvelope
|
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if envelope.Type == jail.EventLocalStorageSet {
|
|
|
|
event := envelope.Event.(map[string]interface{})
|
2017-05-03 14:24:48 +00:00
|
|
|
chatID, ok := event["chat_id"].(string)
|
2016-12-18 20:36:17 +00:00
|
|
|
if !ok {
|
|
|
|
t.Error("Chat id is required, but not found")
|
|
|
|
return
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if chatID != testChatID {
|
|
|
|
t.Errorf("incorrect chat id: expected %q, got: %q", testChatID, chatID)
|
2016-12-18 20:36:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actualData, ok := event["data"].(string)
|
|
|
|
if !ok {
|
|
|
|
t.Error("Data field is required, but not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualData != testData {
|
|
|
|
t.Errorf("incorrect data: expected %q, got: %q", testData, actualData)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("event processed: %s", jsonEvent)
|
|
|
|
opCompletedSuccessfully <- struct{}{} // so that timeout is aborted
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = vm.Run(`
|
|
|
|
var responseValue = localStorage.set("` + testData + `");
|
|
|
|
responseValue = JSON.stringify(responseValue);
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot run custom code on VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that signal is sent (and its parameters are correct)
|
|
|
|
select {
|
|
|
|
case <-opCompletedSuccessfully:
|
|
|
|
// pass
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Error("operation timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
responseValue, err := vm.Get("responseValue")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot obtain result of localStorage.set(): %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := responseValue.ToString()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot parse result: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResponse := `{"jsonrpc":"2.0","result":true}`
|
2016-12-01 16:52:37 +00:00
|
|
|
if !reflect.DeepEqual(response, expectedResponse) {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 18:41:26 +00:00
|
|
|
|
|
|
|
func TestContractDeployment(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, "")
|
2017-02-05 18:41:26 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-05-03 14:24:48 +00:00
|
|
|
vm, err := jailInstance.GetVM(testChatID)
|
2017-02-05 18:41:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
completeQueuedTransaction := make(chan struct{}, 1)
|
|
|
|
geth.PanicAfter(30*time.Second, completeQueuedTransaction, "TestContractDeployment")
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
var txHash common.Hash
|
2017-05-03 14:24:48 +00:00
|
|
|
handler, err := geth.MakeTestCompleteTxHandler(t, &txHash, completeQueuedTransaction)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
geth.SetDefaultNodeNotificationHandler(handler)
|
2017-02-05 18:41:26 +00:00
|
|
|
|
|
|
|
_, err = vm.Run(`
|
|
|
|
var responseValue = null;
|
|
|
|
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]);
|
|
|
|
var test = testContract.new(
|
|
|
|
{
|
2017-03-14 22:57:07 +00:00
|
|
|
from: '` + testConfig.Account1.Address + `',
|
2017-02-05 18:41:26 +00:00
|
|
|
data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029',
|
2017-03-15 21:03:01 +00:00
|
|
|
gas: '` + strconv.Itoa(params.DefaultGas) + `'
|
2017-02-05 18:41:26 +00:00
|
|
|
}, function (e, contract){
|
|
|
|
if (!e) {
|
|
|
|
responseValue = contract.transactionHash
|
|
|
|
}
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot run custom code on VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-completeQueuedTransaction
|
|
|
|
|
|
|
|
responseValue, err := vm.Get("responseValue")
|
|
|
|
if err != nil {
|
2017-02-23 00:57:09 +00:00
|
|
|
t.Errorf("vm.Get() failed: %v", err)
|
2017-02-05 18:41:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := responseValue.ToString()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot parse result: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResponse := txHash.Hex()
|
|
|
|
if !reflect.DeepEqual(response, expectedResponse) {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 00:57:09 +00:00
|
|
|
|
|
|
|
func TestGasEstimation(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
2017-05-03 14:24:48 +00:00
|
|
|
jailInstance.Parse(testChatID, "")
|
2017-02-23 00:57:09 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-05-03 14:24:48 +00:00
|
|
|
vm, err := jailInstance.GetVM(testChatID)
|
2017-02-23 00:57:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure you panic if transaction complete doesn't return
|
|
|
|
completeQueuedTransaction := make(chan struct{}, 1)
|
|
|
|
geth.PanicAfter(30*time.Second, completeQueuedTransaction, "TestContractDeployment")
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
var txHash common.Hash
|
2017-05-03 14:24:48 +00:00
|
|
|
handler, err := geth.MakeTestCompleteTxHandler(t, &txHash, completeQueuedTransaction)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
geth.SetDefaultNodeNotificationHandler(handler)
|
2017-02-23 00:57:09 +00:00
|
|
|
|
|
|
|
_, err = vm.Run(`
|
|
|
|
var responseValue = null;
|
|
|
|
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]);
|
|
|
|
var test = testContract.new(
|
|
|
|
{
|
2017-03-14 22:57:07 +00:00
|
|
|
from: '` + testConfig.Account1.Address + `',
|
2017-02-23 00:57:09 +00:00
|
|
|
data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029',
|
|
|
|
}, function (e, contract){
|
|
|
|
if (!e) {
|
|
|
|
responseValue = contract.transactionHash
|
|
|
|
}
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot run custom code on VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-completeQueuedTransaction
|
|
|
|
|
|
|
|
responseValue, err := vm.Get("responseValue")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("vm.Get() failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := responseValue.ToString()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot parse result: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResponse := txHash.Hex()
|
|
|
|
if !reflect.DeepEqual(response, expectedResponse) {
|
|
|
|
t.Errorf("expected response is not returned: expected %s, got %s", expectedResponse, response)
|
|
|
|
return
|
2017-03-14 13:17:32 +00:00
|
|
|
}
|
2017-02-23 00:57:09 +00:00
|
|
|
}
|
2017-03-21 16:27:59 +00:00
|
|
|
|
|
|
|
func TestJailWhisper(t *testing.T) {
|
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
whisperAPI := whisper.NewPublicWhisperAPI(whisperService)
|
|
|
|
|
|
|
|
// account1
|
|
|
|
_, accountKey1, err := geth.AddressToDecryptedAccount(testConfig.Account1.Address, testConfig.Account1.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
accountKey1Hex := common.ToHex(crypto.FromECDSAPub(&accountKey1.PrivateKey.PublicKey))
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if _, err := whisperService.AddKeyPair(accountKey1.PrivateKey); err != nil {
|
|
|
|
t.Fatalf("identity not injected: %v", accountKey1Hex)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if ok, err := whisperAPI.HasKeyPair(accountKey1Hex); err != nil || !ok {
|
2017-03-21 16:27:59 +00:00
|
|
|
t.Fatalf("identity not injected: %v", accountKey1Hex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// account2
|
|
|
|
_, accountKey2, err := geth.AddressToDecryptedAccount(testConfig.Account2.Address, testConfig.Account2.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
accountKey2Hex := common.ToHex(crypto.FromECDSAPub(&accountKey2.PrivateKey.PublicKey))
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if _, err := whisperService.AddKeyPair(accountKey2.PrivateKey); err != nil {
|
|
|
|
t.Fatalf("identity not injected: %v", accountKey2Hex)
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
if ok, err := whisperAPI.HasKeyPair(accountKey2Hex); err != nil || !ok {
|
2017-03-21 16:27:59 +00:00
|
|
|
t.Fatalf("identity not injected: %v", accountKey2Hex)
|
|
|
|
}
|
|
|
|
|
|
|
|
passedTests := map[string]bool{
|
|
|
|
whisperMessage1: false,
|
|
|
|
whisperMessage2: false,
|
|
|
|
whisperMessage3: false,
|
|
|
|
whisperMessage4: false,
|
|
|
|
whisperMessage5: false,
|
|
|
|
whisperMessage6: false,
|
|
|
|
}
|
|
|
|
installedFilters := map[string]string{
|
|
|
|
whisperMessage1: "",
|
|
|
|
whisperMessage2: "",
|
|
|
|
whisperMessage3: "",
|
|
|
|
whisperMessage4: "",
|
|
|
|
whisperMessage5: "",
|
|
|
|
whisperMessage6: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
jailInstance := jail.Init("")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
testCode string
|
|
|
|
useFilter bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"test 0: ensure correct version of Whisper is used",
|
|
|
|
`
|
|
|
|
var expectedVersion = '0x5';
|
|
|
|
if (web3.version.whisper != expectedVersion) {
|
|
|
|
throw 'unexpected shh version, expected: ' + expectedVersion + ', got: ' + web3.version.whisper;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 1: encrypted signed message from us (From != nil && To != nil)",
|
|
|
|
`
|
|
|
|
var identity1 = '` + accountKey1Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity1)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
|
|
|
var identity2 = '` + accountKey2Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity2)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey2Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage1 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity1,
|
|
|
|
key: identity2,
|
|
|
|
topics: [topic]
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
2017-05-02 14:35:37 +00:00
|
|
|
console.log(JSON.stringify(filter));
|
2017-03-21 16:27:59 +00:00
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity1,
|
|
|
|
key: identity2,
|
|
|
|
topic: topic,
|
|
|
|
payload: payload,
|
|
|
|
ttl: 20,
|
2017-03-21 16:27:59 +00:00
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage1 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 2: encrypted signed message to yourself (From != nil && To != nil)",
|
|
|
|
`
|
|
|
|
var identity = '` + accountKey1Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage2 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity,
|
|
|
|
key: identity,
|
|
|
|
topics: [topic],
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity,
|
|
|
|
key: identity,
|
|
|
|
topic: topic,
|
|
|
|
payload: payload,
|
|
|
|
ttl: 20,
|
2017-03-21 16:27:59 +00:00
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage2 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 3: signed (known sender) broadcast (From != nil && To == nil)",
|
|
|
|
`
|
|
|
|
var identity = '` + accountKey1Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage3 + `';
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
// generate symmetric key
|
|
|
|
var keyid = shh.generateSymmetricKey();
|
|
|
|
if (!shh.hasSymmetricKey(keyid)) {
|
|
|
|
throw new Error('key not found');
|
2017-03-21 16:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "sym",
|
|
|
|
sig: identity,
|
|
|
|
topics: [topic],
|
|
|
|
key: keyid
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "sym",
|
|
|
|
sig: identity,
|
|
|
|
topic: topic,
|
2017-03-21 16:27:59 +00:00
|
|
|
payload: payload,
|
|
|
|
ttl: 20,
|
2017-05-02 14:35:37 +00:00
|
|
|
key: keyid
|
2017-03-21 16:27:59 +00:00
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage3 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 4: anonymous broadcast (From == nil && To == nil)",
|
|
|
|
`
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage4 + `';
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
// generate symmetric key
|
|
|
|
var keyid = shh.generateSymmetricKey();
|
|
|
|
if (!shh.hasSymmetricKey(keyid)) {
|
|
|
|
throw new Error('key not found');
|
2017-03-21 16:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "sym",
|
|
|
|
topics: [topic],
|
|
|
|
key: keyid
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "sym",
|
|
|
|
topic: topic,
|
2017-03-21 16:27:59 +00:00
|
|
|
payload: payload,
|
|
|
|
ttl: 20,
|
2017-05-02 14:35:37 +00:00
|
|
|
key: keyid
|
2017-03-21 16:27:59 +00:00
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage4 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 5: encrypted anonymous message (From == nil && To != nil)",
|
|
|
|
`
|
|
|
|
var identity = '` + accountKey2Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey2Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage5 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
key: identity,
|
|
|
|
topics: [topic],
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
key: identity,
|
|
|
|
topic: topic,
|
2017-03-21 16:27:59 +00:00
|
|
|
payload: payload,
|
|
|
|
ttl: 20
|
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage5 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test 6: encrypted signed response to us (From != nil && To != nil)",
|
|
|
|
`
|
|
|
|
var identity1 = '` + accountKey1Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity1)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey1Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
|
|
|
var identity2 = '` + accountKey2Hex + `';
|
2017-05-02 14:35:37 +00:00
|
|
|
if (!web3.shh.hasKeyPair(identity2)) {
|
2017-03-21 16:27:59 +00:00
|
|
|
throw 'idenitity "` + accountKey2Hex + `" not found in whisper';
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var topic = makeTopic();
|
2017-03-21 16:27:59 +00:00
|
|
|
var payload = '` + whisperMessage6 + `';
|
|
|
|
|
|
|
|
// start watching for messages
|
|
|
|
var filter = shh.filter({
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity2,
|
|
|
|
key: identity1,
|
|
|
|
topics: [topic]
|
2017-03-21 16:27:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// post message
|
|
|
|
var message = {
|
2017-05-02 14:35:37 +00:00
|
|
|
type: "asym",
|
|
|
|
sig: identity2,
|
|
|
|
key: identity1,
|
|
|
|
topic: topic,
|
|
|
|
payload: payload,
|
|
|
|
ttl: 20
|
2017-03-21 16:27:59 +00:00
|
|
|
};
|
|
|
|
var err = shh.post(message)
|
|
|
|
if (err !== null) {
|
|
|
|
throw 'message not sent: ' + message;
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterName = '` + whisperMessage6 + `';
|
|
|
|
var filterId = filter.filterId;
|
|
|
|
if (!filterId) {
|
|
|
|
throw 'filter not installed properly';
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Log(testCase.name)
|
|
|
|
testCaseKey := crypto.Keccak256Hash([]byte(testCase.name)).Hex()
|
2017-05-02 14:35:37 +00:00
|
|
|
jailInstance.Parse(testCaseKey, `
|
|
|
|
var shh = web3.shh;
|
|
|
|
var makeTopic = function () {
|
|
|
|
var min = 1;
|
|
|
|
var max = Math.pow(16, 8);
|
|
|
|
var randInt = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
return web3.toHex(randInt);
|
|
|
|
};
|
|
|
|
`)
|
2017-03-21 16:27:59 +00:00
|
|
|
vm, err := jailInstance.GetVM(testCaseKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get VM: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// post messages
|
|
|
|
if _, err := vm.Run(testCase.testCode); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.useFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// update installed filters
|
|
|
|
filterId, err := vm.Get("filterId")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get filterId: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filterName, err := vm.Get("filterName")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot get filterName: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := installedFilters[filterName.String()]; !ok {
|
|
|
|
t.Fatal("unrecognized filter")
|
|
|
|
}
|
|
|
|
|
|
|
|
installedFilters[filterName.String()] = filterId.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second) // allow whisper to poll
|
|
|
|
|
|
|
|
for testKey, filter := range installedFilters {
|
|
|
|
if filter != "" {
|
|
|
|
t.Logf("filter found: %v", filter)
|
2017-05-16 07:42:30 +00:00
|
|
|
for _, message := range whisperAPI.GetNewSubscriptionMessages(filter) {
|
2017-03-21 16:27:59 +00:00
|
|
|
t.Logf("message found: %s", common.FromHex(message.Payload))
|
|
|
|
passedTests[testKey] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for testName, passedTest := range passedTests {
|
|
|
|
if !passedTest {
|
|
|
|
t.Fatalf("test not passed: %v", testName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|