github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/whisper/whisperv5/api_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package whisperv5
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  func TestBasic(t *testing.T) {
    29  	var id string = "test"
    30  	w := New()
    31  	api := NewPublicWhisperAPI(w)
    32  	if api == nil {
    33  		t.Fatalf("failed to create API.")
    34  	}
    35  
    36  	ver, err := api.Version()
    37  	if err != nil {
    38  		t.Fatalf("failed generateFilter: %s.", err)
    39  	}
    40  
    41  	if uint64(ver) != ProtocolVersion {
    42  		t.Fatalf("wrong version: %d.", ver)
    43  	}
    44  
    45  	mail := api.GetFilterChanges("non-existent-id")
    46  	if len(mail) != 0 {
    47  		t.Fatalf("failed GetFilterChanges: premature result")
    48  	}
    49  
    50  	exist, err := api.HasIdentity(id)
    51  	if err != nil {
    52  		t.Fatalf("failed initial HasIdentity: %s.", err)
    53  	}
    54  	if exist {
    55  		t.Fatalf("failed initial HasIdentity: false positive.")
    56  	}
    57  
    58  	err = api.DeleteIdentity(id)
    59  	if err != nil {
    60  		t.Fatalf("failed DeleteIdentity: %s.", err)
    61  	}
    62  
    63  	pub, err := api.NewIdentity()
    64  	if err != nil {
    65  		t.Fatalf("failed NewIdentity: %s.", err)
    66  	}
    67  	if len(pub) == 0 {
    68  		t.Fatalf("failed NewIdentity: empty")
    69  	}
    70  
    71  	exist, err = api.HasIdentity(pub)
    72  	if err != nil {
    73  		t.Fatalf("failed HasIdentity: %s.", err)
    74  	}
    75  	if !exist {
    76  		t.Fatalf("failed HasIdentity: false negative.")
    77  	}
    78  
    79  	err = api.DeleteIdentity(pub)
    80  	if err != nil {
    81  		t.Fatalf("failed to delete second identity: %s.", err)
    82  	}
    83  
    84  	exist, err = api.HasIdentity(pub)
    85  	if err != nil {
    86  		t.Fatalf("failed HasIdentity(): %s.", err)
    87  	}
    88  	if exist {
    89  		t.Fatalf("failed HasIdentity(): false positive.")
    90  	}
    91  
    92  	id = "arbitrary text"
    93  	id2 := "another arbitrary string"
    94  
    95  	exist, err = api.HasSymKey(id)
    96  	if err != nil {
    97  		t.Fatalf("failed HasSymKey: %s.", err)
    98  	}
    99  	if exist {
   100  		t.Fatalf("failed HasSymKey: false positive.")
   101  	}
   102  
   103  	err = api.GenerateSymKey(id)
   104  	if err != nil {
   105  		t.Fatalf("failed GenerateSymKey: %s.", err)
   106  	}
   107  
   108  	exist, err = api.HasSymKey(id)
   109  	if err != nil {
   110  		t.Fatalf("failed HasSymKey(): %s.", err)
   111  	}
   112  	if !exist {
   113  		t.Fatalf("failed HasSymKey(): false negative.")
   114  	}
   115  
   116  	err = api.AddSymKey(id, []byte("some stuff here"))
   117  	if err == nil {
   118  		t.Fatalf("failed AddSymKey: %s.", err)
   119  	}
   120  
   121  	err = api.AddSymKey(id2, []byte("some stuff here"))
   122  	if err != nil {
   123  		t.Fatalf("failed AddSymKey: %s.", err)
   124  	}
   125  
   126  	exist, err = api.HasSymKey(id2)
   127  	if err != nil {
   128  		t.Fatalf("failed HasSymKey(id2): %s.", err)
   129  	}
   130  	if !exist {
   131  		t.Fatalf("failed HasSymKey(id2): false negative.")
   132  	}
   133  
   134  	err = api.DeleteSymKey(id)
   135  	if err != nil {
   136  		t.Fatalf("failed DeleteSymKey(id): %s.", err)
   137  	}
   138  
   139  	exist, err = api.HasSymKey(id)
   140  	if err != nil {
   141  		t.Fatalf("failed HasSymKey(id): %s.", err)
   142  	}
   143  	if exist {
   144  		t.Fatalf("failed HasSymKey(id): false positive.")
   145  	}
   146  }
   147  
   148  func TestUnmarshalFilterArgs(t *testing.T) {
   149  	s := []byte(`{
   150  	"to":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
   151  	"from":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
   152  	"keyname":"testname",
   153  	"pow":2.34,
   154  	"topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"],
   155  	"p2p":true
   156  	}`)
   157  
   158  	var f WhisperFilterArgs
   159  	err := f.UnmarshalJSON(s)
   160  	if err != nil {
   161  		t.Fatalf("failed UnmarshalJSON: %s.", err)
   162  	}
   163  
   164  	if f.To != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
   165  		t.Fatalf("wrong To: %x.", f.To)
   166  	}
   167  	if f.From != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
   168  		t.Fatalf("wrong From: %x.", f.To)
   169  	}
   170  	if f.KeyName != "testname" {
   171  		t.Fatalf("wrong KeyName: %s.", f.KeyName)
   172  	}
   173  	if f.PoW != 2.34 {
   174  		t.Fatalf("wrong pow: %f.", f.PoW)
   175  	}
   176  	if !f.AcceptP2P {
   177  		t.Fatalf("wrong AcceptP2P: %v.", f.AcceptP2P)
   178  	}
   179  	if len(f.Topics) != 4 {
   180  		t.Fatalf("wrong topics number: %d.", len(f.Topics))
   181  	}
   182  
   183  	i := 0
   184  	if f.Topics[i] != (TopicType{0x00, 0x00, 0x00, 0x00}) {
   185  		t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
   186  	}
   187  
   188  	i++
   189  	if f.Topics[i] != (TopicType{0x00, 0x7f, 0x80, 0xff}) {
   190  		t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
   191  	}
   192  
   193  	i++
   194  	if f.Topics[i] != (TopicType{0xff, 0x80, 0x7f, 0x00}) {
   195  		t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
   196  	}
   197  
   198  	i++
   199  	if f.Topics[i] != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
   200  		t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
   201  	}
   202  }
   203  
   204  func TestUnmarshalPostArgs(t *testing.T) {
   205  	s := []byte(`{
   206  	"ttl":12345,
   207  	"from":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
   208  	"to":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
   209  	"keyname":"shh_test",
   210  	"topic":"0xf26e7779",
   211  	"padding":"0x74686973206973206D79207465737420737472696E67",
   212  	"payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D",
   213  	"worktime":777,
   214  	"pow":3.1416,
   215  	"filterid":"test-filter-id",
   216  	"peerid":"0xf26e7779"
   217  	}`)
   218  
   219  	var a PostArgs
   220  	err := json.Unmarshal(s, &a)
   221  	if err != nil {
   222  		t.Fatalf("failed UnmarshalJSON: %s.", err)
   223  	}
   224  
   225  	if a.TTL != 12345 {
   226  		t.Fatalf("wrong ttl: %d.", a.TTL)
   227  	}
   228  	if a.From != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
   229  		t.Fatalf("wrong From: %x.", a.To)
   230  	}
   231  	if a.To != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
   232  		t.Fatalf("wrong To: %x.", a.To)
   233  	}
   234  	if a.KeyName != "shh_test" {
   235  		t.Fatalf("wrong KeyName: %s.", a.KeyName)
   236  	}
   237  	if a.Topic != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
   238  		t.Fatalf("wrong topic: %x.", a.Topic)
   239  	}
   240  	if string(a.Padding) != "this is my test string" {
   241  		t.Fatalf("wrong Padding: %s.", string(a.Padding))
   242  	}
   243  	if string(a.Payload) != "payload should be pseudorandom" {
   244  		t.Fatalf("wrong Payload: %s.", string(a.Payload))
   245  	}
   246  	if a.WorkTime != 777 {
   247  		t.Fatalf("wrong WorkTime: %d.", a.WorkTime)
   248  	}
   249  	if a.PoW != 3.1416 {
   250  		t.Fatalf("wrong pow: %f.", a.PoW)
   251  	}
   252  	if a.FilterID != "test-filter-id" {
   253  		t.Fatalf("wrong FilterID: %s.", a.FilterID)
   254  	}
   255  	if !bytes.Equal(a.PeerID[:], a.Topic[:]) {
   256  		t.Fatalf("wrong PeerID: %x.", a.PeerID)
   257  	}
   258  }
   259  
   260  func waitForMessage(api *PublicWhisperAPI, id string, target int) bool {
   261  	for i := 0; i < 64; i++ {
   262  		all := api.GetMessages(id)
   263  		if len(all) >= target {
   264  			return true
   265  		}
   266  		time.Sleep(time.Millisecond * 16)
   267  	}
   268  
   269  	// timeout 1024 milliseconds
   270  	return false
   271  }
   272  
   273  func TestIntegrationAsym(t *testing.T) {
   274  	w := New()
   275  	api := NewPublicWhisperAPI(w)
   276  	if api == nil {
   277  		t.Fatalf("failed to create API.")
   278  	}
   279  
   280  	api.Start()
   281  	defer api.Stop()
   282  
   283  	sig, err := api.NewIdentity()
   284  	if err != nil {
   285  		t.Fatalf("failed NewIdentity: %s.", err)
   286  	}
   287  	if len(sig) == 0 {
   288  		t.Fatalf("wrong signature")
   289  	}
   290  
   291  	exist, err := api.HasIdentity(sig)
   292  	if err != nil {
   293  		t.Fatalf("failed HasIdentity: %s.", err)
   294  	}
   295  	if !exist {
   296  		t.Fatalf("failed HasIdentity: false negative.")
   297  	}
   298  
   299  	key, err := api.NewIdentity()
   300  	if err != nil {
   301  		t.Fatalf("failed NewIdentity(): %s.", err)
   302  	}
   303  	if len(key) == 0 {
   304  		t.Fatalf("wrong key")
   305  	}
   306  
   307  	var topics [2]TopicType
   308  	topics[0] = TopicType{0x00, 0x64, 0x00, 0xff}
   309  	topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
   310  	var f WhisperFilterArgs
   311  	f.To = key
   312  	f.From = sig
   313  	f.Topics = topics[:]
   314  	f.PoW = MinimumPoW / 2
   315  	f.AcceptP2P = true
   316  
   317  	id, err := api.NewFilter(f)
   318  	if err != nil {
   319  		t.Fatalf("failed to create new filter: %s.", err)
   320  	}
   321  
   322  	var p PostArgs
   323  	p.TTL = 2
   324  	p.From = f.From
   325  	p.To = f.To
   326  	p.Padding = []byte("test string")
   327  	p.Payload = []byte("extended test string")
   328  	p.PoW = MinimumPoW
   329  	p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
   330  	p.WorkTime = 2
   331  
   332  	err = api.Post(p)
   333  	if err != nil {
   334  		t.Errorf("failed to post message: %s.", err)
   335  	}
   336  
   337  	ok := waitForMessage(api, id, 1)
   338  	if !ok {
   339  		t.Fatalf("failed to receive first message: timeout.")
   340  	}
   341  
   342  	mail := api.GetFilterChanges(id)
   343  	if len(mail) != 1 {
   344  		t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
   345  	}
   346  
   347  	text := string(common.FromHex(mail[0].Payload))
   348  	if text != string("extended test string") {
   349  		t.Fatalf("failed to decrypt first message: %s.", text)
   350  	}
   351  
   352  	p.Padding = []byte("new value")
   353  	p.Payload = []byte("extended new value")
   354  	err = api.Post(p)
   355  	if err != nil {
   356  		t.Fatalf("failed to post next message: %s.", err)
   357  	}
   358  
   359  	ok = waitForMessage(api, id, 2)
   360  	if !ok {
   361  		t.Fatalf("failed to receive second message: timeout.")
   362  	}
   363  
   364  	mail = api.GetFilterChanges(id)
   365  	if len(mail) != 1 {
   366  		t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
   367  	}
   368  
   369  	text = string(common.FromHex(mail[0].Payload))
   370  	if text != string("extended new value") {
   371  		t.Fatalf("failed to decrypt second message: %s.", text)
   372  	}
   373  }
   374  
   375  func TestIntegrationSym(t *testing.T) {
   376  	w := New()
   377  	api := NewPublicWhisperAPI(w)
   378  	if api == nil {
   379  		t.Fatalf("failed to create API.")
   380  	}
   381  
   382  	api.Start()
   383  	defer api.Stop()
   384  
   385  	keyname := "schluessel"
   386  	err := api.GenerateSymKey(keyname)
   387  	if err != nil {
   388  		t.Fatalf("failed GenerateSymKey: %s.", err)
   389  	}
   390  
   391  	sig, err := api.NewIdentity()
   392  	if err != nil {
   393  		t.Fatalf("failed NewIdentity: %s.", err)
   394  	}
   395  	if len(sig) == 0 {
   396  		t.Fatalf("wrong signature")
   397  	}
   398  
   399  	exist, err := api.HasIdentity(sig)
   400  	if err != nil {
   401  		t.Fatalf("failed HasIdentity: %s.", err)
   402  	}
   403  	if !exist {
   404  		t.Fatalf("failed HasIdentity: false negative.")
   405  	}
   406  
   407  	var topics [2]TopicType
   408  	topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
   409  	topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
   410  	var f WhisperFilterArgs
   411  	f.KeyName = keyname
   412  	f.Topics = topics[:]
   413  	f.PoW = 0.324
   414  	f.From = sig
   415  	f.AcceptP2P = false
   416  
   417  	id, err := api.NewFilter(f)
   418  	if err != nil {
   419  		t.Fatalf("failed to create new filter: %s.", err)
   420  	}
   421  
   422  	var p PostArgs
   423  	p.TTL = 1
   424  	p.KeyName = keyname
   425  	p.From = f.From
   426  	p.Padding = []byte("test string")
   427  	p.Payload = []byte("extended test string")
   428  	p.PoW = MinimumPoW
   429  	p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
   430  	p.WorkTime = 2
   431  
   432  	err = api.Post(p)
   433  	if err != nil {
   434  		t.Fatalf("failed to post first message: %s.", err)
   435  	}
   436  
   437  	ok := waitForMessage(api, id, 1)
   438  	if !ok {
   439  		t.Fatalf("failed to receive first message: timeout.")
   440  	}
   441  
   442  	mail := api.GetFilterChanges(id)
   443  	if len(mail) != 1 {
   444  		t.Fatalf("failed GetFilterChanges: got %d messages.", len(mail))
   445  	}
   446  
   447  	text := string(common.FromHex(mail[0].Payload))
   448  	if text != string("extended test string") {
   449  		t.Fatalf("failed to decrypt first message: %s.", text)
   450  	}
   451  
   452  	p.Padding = []byte("new value")
   453  	p.Payload = []byte("extended new value")
   454  	err = api.Post(p)
   455  	if err != nil {
   456  		t.Fatalf("failed to post second message: %s.", err)
   457  	}
   458  
   459  	ok = waitForMessage(api, id, 2)
   460  	if !ok {
   461  		t.Fatalf("failed to receive second message: timeout.")
   462  	}
   463  
   464  	mail = api.GetFilterChanges(id)
   465  	if len(mail) != 1 {
   466  		t.Fatalf("failed second GetFilterChanges: got %d messages.", len(mail))
   467  	}
   468  
   469  	text = string(common.FromHex(mail[0].Payload))
   470  	if text != string("extended new value") {
   471  		t.Fatalf("failed to decrypt second message: %s.", text)
   472  	}
   473  }
   474  
   475  func TestIntegrationSymWithFilter(t *testing.T) {
   476  	w := New()
   477  	api := NewPublicWhisperAPI(w)
   478  	if api == nil {
   479  		t.Fatalf("failed to create API.")
   480  	}
   481  
   482  	api.Start()
   483  	defer api.Stop()
   484  
   485  	keyname := "schluessel"
   486  	err := api.GenerateSymKey(keyname)
   487  	if err != nil {
   488  		t.Fatalf("failed to GenerateSymKey: %s.", err)
   489  	}
   490  
   491  	sig, err := api.NewIdentity()
   492  	if err != nil {
   493  		t.Fatalf("failed NewIdentity: %s.", err)
   494  	}
   495  	if len(sig) == 0 {
   496  		t.Fatalf("wrong signature.")
   497  	}
   498  
   499  	exist, err := api.HasIdentity(sig)
   500  	if err != nil {
   501  		t.Fatalf("failed HasIdentity: %s.", err)
   502  	}
   503  	if !exist {
   504  		t.Fatalf("failed HasIdentity: does not exist.")
   505  	}
   506  
   507  	var topics [2]TopicType
   508  	topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
   509  	topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
   510  	var f WhisperFilterArgs
   511  	f.KeyName = keyname
   512  	f.Topics = topics[:]
   513  	f.PoW = 0.324
   514  	f.From = sig
   515  	f.AcceptP2P = false
   516  
   517  	id, err := api.NewFilter(f)
   518  	if err != nil {
   519  		t.Fatalf("failed to create new filter: %s.", err)
   520  	}
   521  
   522  	var p PostArgs
   523  	p.TTL = 1
   524  	p.FilterID = id
   525  	p.From = sig
   526  	p.Padding = []byte("test string")
   527  	p.Payload = []byte("extended test string")
   528  	p.PoW = MinimumPoW
   529  	p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
   530  	p.WorkTime = 2
   531  
   532  	err = api.Post(p)
   533  	if err != nil {
   534  		t.Fatalf("failed to post message: %s.", err)
   535  	}
   536  
   537  	ok := waitForMessage(api, id, 1)
   538  	if !ok {
   539  		t.Fatalf("failed to receive first message: timeout.")
   540  	}
   541  
   542  	mail := api.GetFilterChanges(id)
   543  	if len(mail) != 1 {
   544  		t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
   545  	}
   546  
   547  	text := string(common.FromHex(mail[0].Payload))
   548  	if text != string("extended test string") {
   549  		t.Fatalf("failed to decrypt first message: %s.", text)
   550  	}
   551  
   552  	p.Padding = []byte("new value")
   553  	p.Payload = []byte("extended new value")
   554  	err = api.Post(p)
   555  	if err != nil {
   556  		t.Fatalf("failed to post next message: %s.", err)
   557  	}
   558  
   559  	ok = waitForMessage(api, id, 2)
   560  	if !ok {
   561  		t.Fatalf("failed to receive second message: timeout.")
   562  	}
   563  
   564  	mail = api.GetFilterChanges(id)
   565  	if len(mail) != 1 {
   566  		t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
   567  	}
   568  
   569  	text = string(common.FromHex(mail[0].Payload))
   570  	if text != string("extended new value") {
   571  		t.Fatalf("failed to decrypt second message: %s.", text)
   572  	}
   573  }