whisper: API tests added + bugfix

This commit is contained in:
Vlad 2016-11-03 23:03:31 +01:00
parent 2010c20a15
commit 568b1dc50e
2 changed files with 512 additions and 25 deletions

View file

@ -155,7 +155,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber,
} }
filter := whisperv5.Filter{ filter := whisperv5.Filter{
Src: crypto.ToECDSAPub(args.From), Src: crypto.ToECDSAPub(common.FromHex(args.From)),
KeySym: api.whisper.GetSymKey(args.KeyName), KeySym: api.whisper.GetSymKey(args.KeyName),
PoW: args.PoW, PoW: args.PoW,
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage), Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
@ -195,7 +195,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber,
} }
if len(args.To) > 0 { if len(args.To) > 0 {
dst := crypto.ToECDSAPub(args.To) dst := crypto.ToECDSAPub(common.FromHex(args.To))
if !whisperv5.ValidatePublicKey(dst) { if !whisperv5.ValidatePublicKey(dst) {
info := "NewFilter: Invalid 'To' address" info := "NewFilter: Invalid 'To' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
@ -259,7 +259,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
params := whisperv5.MessageParams{ params := whisperv5.MessageParams{
TTL: args.TTL, TTL: args.TTL,
Dst: crypto.ToECDSAPub(args.To), Dst: crypto.ToECDSAPub(common.FromHex(args.To)),
KeySym: api.whisper.GetSymKey(args.KeyName), KeySym: api.whisper.GetSymKey(args.KeyName),
Topic: args.Topic, Topic: args.Topic,
Payload: args.Payload, Payload: args.Payload,
@ -269,7 +269,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
} }
if len(args.From) > 0 { if len(args.From) > 0 {
pub := crypto.ToECDSAPub(args.From) pub := crypto.ToECDSAPub(common.FromHex(args.From))
if !whisperv5.ValidatePublicKey(pub) { if !whisperv5.ValidatePublicKey(pub) {
info := "Post: Invalid 'From' address" info := "Post: Invalid 'From' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
@ -321,13 +321,13 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
return errors.New(info) return errors.New(info)
} }
if len(args.To) == 0 && len(args.KeyName) == 0 { if len(args.To) == 0 && len(params.KeySym) == 0 {
info := "Post: message must be encrypted either symmetrically or asymmetrically" info := "Post: message must be encrypted either symmetrically or asymmetrically"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
} }
if len(args.To) != 0 && len(args.KeyName) != 0 { if len(args.To) != 0 && len(params.KeySym) != 0 {
info := "Post: ambigous encryption method requested" info := "Post: ambigous encryption method requested"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
@ -367,32 +367,32 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
} }
type PostArgs struct { type PostArgs struct {
TTL uint32 `json:"ttl"` TTL uint32
From rpc.HexBytes `json:"from"` From string
To rpc.HexBytes `json:"to"` To string
KeyName string `json:"keyname"` KeyName string
Topic whisperv5.TopicType `json:"topic"` Topic whisperv5.TopicType
Padding rpc.HexBytes `json:"padding"` Padding rpc.HexBytes
Payload rpc.HexBytes `json:"payload"` Payload rpc.HexBytes
WorkTime uint32 `json:"worktime"` WorkTime uint32
PoW float64 `json:"pow"` PoW float64
FilterID int `json:"filter"` FilterID int
PeerID rpc.HexBytes `json:"directP2P"` PeerID rpc.HexBytes
} }
func (args *PostArgs) UnmarshalJSON(data []byte) (err error) { func (args *PostArgs) UnmarshalJSON(data []byte) (err error) {
var obj struct { var obj struct {
TTL uint32 `json:"ttl"` TTL uint32 `json:"ttl"`
From rpc.HexBytes `json:"from"` From string `json:"from"`
To rpc.HexBytes `json:"to"` To string `json:"to"`
KeyName string `json:"keyname"` KeyName string `json:"keyname"`
Topic whisperv5.TopicType `json:"topic"` Topic whisperv5.TopicType `json:"topic"`
Payload rpc.HexBytes `json:"payload"` Payload rpc.HexBytes `json:"payload"`
Padding rpc.HexBytes `json:"padding"` Padding rpc.HexBytes `json:"padding"`
WorkTime uint32 `json:"worktime"` WorkTime uint32 `json:"worktime"`
PoW float64 `json:"pow"` PoW float64 `json:"pow"`
FilterID rpc.HexBytes `json:"filter"` FilterID rpc.HexBytes `json:"filterID"`
PeerID rpc.HexBytes `json:"directP2P"` PeerID rpc.HexBytes `json:"peerID"`
} }
if err := json.Unmarshal(data, &obj); err != nil { if err := json.Unmarshal(data, &obj); err != nil {
@ -420,8 +420,8 @@ func (args *PostArgs) UnmarshalJSON(data []byte) (err error) {
} }
type WhisperFilterArgs struct { type WhisperFilterArgs struct {
To []byte To string
From []byte From string
KeyName string KeyName string
PoW float64 PoW float64
Topics []whisperv5.TopicType Topics []whisperv5.TopicType
@ -433,8 +433,8 @@ type WhisperFilterArgs struct {
func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
// Unmarshal the JSON message and sanity check // Unmarshal the JSON message and sanity check
var obj struct { var obj struct {
To rpc.HexBytes `json:"to"` To string `json:"to"`
From rpc.HexBytes `json:"from"` From string `json:"from"`
KeyName string `json:"keyname"` KeyName string `json:"keyname"`
PoW float64 `json:"pow"` PoW float64 `json:"pow"`
Topics []interface{} `json:"topics"` Topics []interface{} `json:"topics"`

View file

@ -17,8 +17,11 @@
package shhapi package shhapi
import ( import (
"bytes"
"testing" "testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper/whisperv5" "github.com/ethereum/go-ethereum/whisper/whisperv5"
) )
@ -168,3 +171,487 @@ func TestBasic(x *testing.T) {
return return
} }
} }
func TestUnmarshalFilterArgs(x *testing.T) {
s := []byte(`{
"to":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
"from":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
"keyname":"testname",
"pow":2.34,
"topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"],
"acceptP2P":true
}`)
var f WhisperFilterArgs
err := f.UnmarshalJSON(s)
if err != nil {
x.Errorf("failed UnmarshalJSON: %s.", err)
return
}
if f.To != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
x.Errorf("wrong To: %x.", f.To)
return
}
if f.From != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
x.Errorf("wrong From: %x.", f.To)
return
}
if f.KeyName != "testname" {
x.Errorf("wrong KeyName: %s.", f.KeyName)
return
}
if f.PoW != 2.34 {
x.Errorf("wrong pow: %f.", f.PoW)
return
}
if !f.AcceptP2P {
x.Errorf("wrong AcceptP2P: %v.", f.AcceptP2P)
return
}
if len(f.Topics) != 4 {
x.Errorf("wrong topics number: %d.", len(f.Topics))
return
}
i := 0
if f.Topics[i] != (whisperv5.TopicType{0x00, 0x00, 0x00, 0x00}) {
x.Errorf("wrong topic[%d]: %x.", i, f.Topics[i])
return
}
i++
if f.Topics[i] != (whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}) {
x.Errorf("wrong topic[%d]: %x.", i, f.Topics[i])
return
}
i++
if f.Topics[i] != (whisperv5.TopicType{0xff, 0x80, 0x7f, 0x00}) {
x.Errorf("wrong topic[%d]: %x.", i, f.Topics[i])
return
}
i++
if f.Topics[i] != (whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}) {
x.Errorf("wrong topic[%d]: %x.", i, f.Topics[i])
return
}
}
func TestUnmarshalPostArgs(x *testing.T) {
s := []byte(`{
"ttl":12345,
"from":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
"to":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
"keyname":"shh_test",
"topic":"0xf26e7779",
"padding":"0x74686973206973206D79207465737420737472696E67",
"payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D",
"worktime":777,
"pow":3.1416,
"filterID":"0x40",
"peerID":"0xf26e7779"
}`)
var a PostArgs
err := a.UnmarshalJSON(s)
if err != nil {
x.Errorf("failed UnmarshalJSON: %s.", err)
return
}
if a.TTL != 12345 {
x.Errorf("wrong ttl: %d.", a.TTL)
return
}
if a.From != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
x.Errorf("wrong From: %x.", a.To)
return
}
if a.To != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
x.Errorf("wrong To: %x.", a.To)
return
}
if a.KeyName != "shh_test" {
x.Errorf("wrong KeyName: %s.", a.KeyName)
return
}
if a.Topic != (whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}) {
x.Errorf("wrong topic: %x.", a.Topic)
return
}
if string(a.Padding) != "this is my test string" {
x.Errorf("wrong Padding: %s.", string(a.Padding))
return
}
if string(a.Payload) != "payload should be pseudorandom" {
x.Errorf("wrong Payload: %s.", string(a.Payload))
return
}
if a.WorkTime != 777 {
x.Errorf("wrong WorkTime: %d.", a.WorkTime)
return
}
if a.PoW != 3.1416 {
x.Errorf("wrong pow: %f.", a.PoW)
return
}
if a.FilterID != 64 {
x.Errorf("wrong FilterID: %d.", a.FilterID)
return
}
if bytes.Compare(a.PeerID[:], a.Topic[:]) != 0 {
x.Errorf("wrong PeerID: %x.", a.PeerID)
return
}
}
func waitForMessage(api *PublicWhisperAPI, id *rpc.HexNumber, target int) bool {
for i := 0; i < 64; i++ {
all := api.GetMessages(*id)
if len(all) >= target {
return true
}
time.Sleep(time.Millisecond * 16)
}
// timeout 1024 milliseconds
return false
}
func TestIntegrationAsym(x *testing.T) {
api := NewPublicWhisperAPI()
if api == nil {
x.Errorf("failed 1 to create API.")
return
}
sig, err := api.NewIdentity()
if err != nil {
x.Errorf("failed 22 NewIdentity: %s.", err)
return
}
if len(sig) == 0 {
x.Errorf("NewIdentity 23: empty")
return
}
exist, err := api.HasIdentity(sig)
if err != nil {
x.Errorf("failed 24 HasIdentity: %s.", err)
return
}
if !exist {
x.Errorf("failed 25 HasIdentity: false negative.")
return
}
key, err := api.NewIdentity()
if err != nil {
x.Errorf("failed 26 NewIdentity: %s.", err)
return
}
if len(key) == 0 {
x.Errorf("failed 27 to generate new identity")
return
}
var topics [2]whisperv5.TopicType
topics[0] = whisperv5.TopicType{0x00, 0x64, 0x00, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs
f.To = key
f.From = sig
f.Topics = topics[:]
f.PoW = whisperv5.MinimumPoW / 2
f.AcceptP2P = true
id, err := api.NewFilter(f)
if err != nil {
x.Errorf("failed 31 to create new filter: %s.", err)
return
}
var p PostArgs
p.TTL = 2
p.From = f.From
p.To = f.To
p.Padding = []byte("test string")
p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2
err = api.Post(p)
if err != nil {
x.Errorf("failed 32 to post message: %s.", err)
return
}
ok := waitForMessage(api, id, 1)
if !ok {
x.Errorf("failed 33 receive first message: timeout.")
return
}
mail := api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 34 to GetFilterChanges: got %d messages.", len(mail))
return
}
text := string(common.FromHex(mail[0].Payload))
if text != string("extended test string") {
x.Errorf("failed 35 to decrypt first message: %s.", text)
return
}
p.Padding = []byte("new value")
p.Payload = []byte("extended new value")
err = api.Post(p)
if err != nil {
x.Errorf("failed 42 to post message: %s.", err)
return
}
ok = waitForMessage(api, id, 2)
if !ok {
x.Errorf("failed 43 receive second message: timeout.")
return
}
mail = api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 44 to GetFilterChanges: got %d messages.", len(mail))
return
}
text = string(common.FromHex(mail[0].Payload))
if text != string("extended new value") {
x.Errorf("failed 45 to decrypt second message: %s.", text)
return
}
}
func TestIntegrationSym(x *testing.T) {
api := NewPublicWhisperAPI()
if api == nil {
x.Errorf("failed 1 to create API.")
return
}
keyname := "schluessel"
err := api.GenerateSymKey(keyname)
if err != nil {
x.Errorf("failed 2 to GenerateSymKey: %s.", err)
return
}
sig, err := api.NewIdentity()
if err != nil {
x.Errorf("failed 22 NewIdentity: %s.", err)
return
}
if len(sig) == 0 {
x.Errorf("NewIdentity 23: empty")
return
}
exist, err := api.HasIdentity(sig)
if err != nil {
x.Errorf("failed 24 HasIdentity: %s.", err)
return
}
if !exist {
x.Errorf("failed 25 HasIdentity: false negative.")
return
}
var topics [2]whisperv5.TopicType
topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs
f.KeyName = keyname
f.Topics = topics[:]
f.PoW = 0.324
f.From = sig
f.AcceptP2P = false
id, err := api.NewFilter(f)
if err != nil {
x.Errorf("failed 31 to create new filter: %s.", err)
return
}
var p PostArgs
p.TTL = 1
p.KeyName = keyname
p.From = f.From
p.Padding = []byte("test string")
p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2
err = api.Post(p)
if err != nil {
x.Errorf("failed 32 to post message: %s.", err)
return
}
ok := waitForMessage(api, id, 1)
if !ok {
x.Errorf("failed 33 to receive first message: timeout.")
return
}
mail := api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 34 to GetFilterChanges: got %d messages.", len(mail))
return
}
text := string(common.FromHex(mail[0].Payload))
if text != string("extended test string") {
x.Errorf("failed 35 to decrypt first message: %s.", text)
return
}
p.Padding = []byte("new value")
p.Payload = []byte("extended new value")
err = api.Post(p)
if err != nil {
x.Errorf("failed 42 to post message: %s.", err)
return
}
ok = waitForMessage(api, id, 2)
if !ok {
x.Errorf("failed 43 receive second message: timeout.")
return
}
mail = api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 44 to GetFilterChanges: got %d messages.", len(mail))
return
}
text = string(common.FromHex(mail[0].Payload))
if text != string("extended new value") {
x.Errorf("failed 45 to decrypt second message: %s.", text)
return
}
}
func TestIntegrationSymWithFilter(x *testing.T) {
api := NewPublicWhisperAPI()
if api == nil {
x.Errorf("failed 1 to create API.")
return
}
keyname := "schluessel"
err := api.GenerateSymKey(keyname)
if err != nil {
x.Errorf("failed 2 to GenerateSymKey: %s.", err)
return
}
sig, err := api.NewIdentity()
if err != nil {
x.Errorf("failed 22 NewIdentity: %s.", err)
return
}
if len(sig) == 0 {
x.Errorf("NewIdentity 23: empty")
return
}
exist, err := api.HasIdentity(sig)
if err != nil {
x.Errorf("failed 24 HasIdentity: %s.", err)
return
}
if !exist {
x.Errorf("failed 25 HasIdentity: false negative.")
return
}
var topics [2]whisperv5.TopicType
topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs
f.KeyName = keyname
f.Topics = topics[:]
f.PoW = 0.324
f.From = sig
f.AcceptP2P = false
id, err := api.NewFilter(f)
if err != nil {
x.Errorf("failed 31 to create new filter: %s.", err)
return
}
var p PostArgs
p.TTL = 1
p.FilterID = id.Int()
p.From = sig
p.Padding = []byte("test string")
p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2
err = api.Post(p)
if err != nil {
x.Errorf("failed 32 to post message: %s.", err)
return
}
ok := waitForMessage(api, id, 1)
if !ok {
x.Errorf("failed 33 to receive first message: timeout.")
return
}
mail := api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 34 to GetFilterChanges: got %d messages.", len(mail))
return
}
text := string(common.FromHex(mail[0].Payload))
if text != string("extended test string") {
x.Errorf("failed 35 to decrypt first message: %s.", text)
return
}
p.Padding = []byte("new value")
p.Payload = []byte("extended new value")
err = api.Post(p)
if err != nil {
x.Errorf("failed 42 to post message: %s.", err)
return
}
ok = waitForMessage(api, id, 2)
if !ok {
x.Errorf("failed 43 receive second message: timeout.")
return
}
mail = api.GetFilterChanges(*id)
if len(mail) != 1 {
x.Errorf("failed 44 to GetFilterChanges: got %d messages.", len(mail))
return
}
text = string(common.FromHex(mail[0].Payload))
if text != string("extended new value") {
x.Errorf("failed 45 to decrypt second message: %s.", text)
return
}
}