github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/whisper/shhapi/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 shhapi 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "testing" 23 "time" 24 25 "github.com/atheioschain/go-atheios/common" 26 "github.com/atheioschain/go-atheios/whisper/whisperv5" 27 ) 28 29 func TestBasic(t *testing.T) { 30 var id string = "test" 31 api := NewPublicWhisperAPI() 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) != whisperv5.ProtocolVersion { 42 t.Fatalf("wrong version: %d.", ver) 43 } 44 45 mail := api.GetFilterChanges(1) 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 "acceptP2P":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] != (whisperv5.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] != (whisperv5.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] != (whisperv5.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] != (whisperv5.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":64, 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 != (whisperv5.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 != 64 { 253 t.Fatalf("wrong FilterID: %d.", 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 uint32, 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 api := NewPublicWhisperAPI() 275 if api == nil { 276 t.Fatalf("failed to create API.") 277 } 278 279 api.Start() 280 defer api.Stop() 281 282 sig, err := api.NewIdentity() 283 if err != nil { 284 t.Fatalf("failed NewIdentity: %s.", err) 285 } 286 if len(sig) == 0 { 287 t.Fatalf("wrong signature") 288 } 289 290 exist, err := api.HasIdentity(sig) 291 if err != nil { 292 t.Fatalf("failed HasIdentity: %s.", err) 293 } 294 if !exist { 295 t.Fatalf("failed HasIdentity: false negative.") 296 } 297 298 key, err := api.NewIdentity() 299 if err != nil { 300 t.Fatalf("failed NewIdentity(): %s.", err) 301 } 302 if len(key) == 0 { 303 t.Fatalf("wrong key") 304 } 305 306 var topics [2]whisperv5.TopicType 307 topics[0] = whisperv5.TopicType{0x00, 0x64, 0x00, 0xff} 308 topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 309 var f WhisperFilterArgs 310 f.To = key 311 f.From = sig 312 f.Topics = topics[:] 313 f.PoW = whisperv5.MinimumPoW / 2 314 f.AcceptP2P = true 315 316 id, err := api.NewFilter(f) 317 if err != nil { 318 t.Fatalf("failed to create new filter: %s.", err) 319 } 320 321 var p PostArgs 322 p.TTL = 2 323 p.From = f.From 324 p.To = f.To 325 p.Padding = []byte("test string") 326 p.Payload = []byte("extended test string") 327 p.PoW = whisperv5.MinimumPoW 328 p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 329 p.WorkTime = 2 330 331 err = api.Post(p) 332 if err != nil { 333 t.Errorf("failed to post message: %s.", err) 334 } 335 336 ok := waitForMessage(api, id, 1) 337 if !ok { 338 t.Fatalf("failed to receive first message: timeout.") 339 } 340 341 mail := api.GetFilterChanges(id) 342 if len(mail) != 1 { 343 t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) 344 } 345 346 text := string(common.FromHex(mail[0].Payload)) 347 if text != string("extended test string") { 348 t.Fatalf("failed to decrypt first message: %s.", text) 349 } 350 351 p.Padding = []byte("new value") 352 p.Payload = []byte("extended new value") 353 err = api.Post(p) 354 if err != nil { 355 t.Fatalf("failed to post next message: %s.", err) 356 } 357 358 ok = waitForMessage(api, id, 2) 359 if !ok { 360 t.Fatalf("failed to receive second message: timeout.") 361 } 362 363 mail = api.GetFilterChanges(id) 364 if len(mail) != 1 { 365 t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) 366 } 367 368 text = string(common.FromHex(mail[0].Payload)) 369 if text != string("extended new value") { 370 t.Fatalf("failed to decrypt second message: %s.", text) 371 } 372 } 373 374 func TestIntegrationSym(t *testing.T) { 375 api := NewPublicWhisperAPI() 376 if api == nil { 377 t.Fatalf("failed to create API.") 378 } 379 380 api.Start() 381 defer api.Stop() 382 383 keyname := "schluessel" 384 err := api.GenerateSymKey(keyname) 385 if err != nil { 386 t.Fatalf("failed GenerateSymKey: %s.", err) 387 } 388 389 sig, err := api.NewIdentity() 390 if err != nil { 391 t.Fatalf("failed NewIdentity: %s.", err) 392 } 393 if len(sig) == 0 { 394 t.Fatalf("wrong signature") 395 } 396 397 exist, err := api.HasIdentity(sig) 398 if err != nil { 399 t.Fatalf("failed HasIdentity: %s.", err) 400 } 401 if !exist { 402 t.Fatalf("failed HasIdentity: false negative.") 403 } 404 405 var topics [2]whisperv5.TopicType 406 topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} 407 topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 408 var f WhisperFilterArgs 409 f.KeyName = keyname 410 f.Topics = topics[:] 411 f.PoW = 0.324 412 f.From = sig 413 f.AcceptP2P = false 414 415 id, err := api.NewFilter(f) 416 if err != nil { 417 t.Fatalf("failed to create new filter: %s.", err) 418 } 419 420 var p PostArgs 421 p.TTL = 1 422 p.KeyName = keyname 423 p.From = f.From 424 p.Padding = []byte("test string") 425 p.Payload = []byte("extended test string") 426 p.PoW = whisperv5.MinimumPoW 427 p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 428 p.WorkTime = 2 429 430 err = api.Post(p) 431 if err != nil { 432 t.Fatalf("failed to post first message: %s.", err) 433 } 434 435 ok := waitForMessage(api, id, 1) 436 if !ok { 437 t.Fatalf("failed to receive first message: timeout.") 438 } 439 440 mail := api.GetFilterChanges(id) 441 if len(mail) != 1 { 442 t.Fatalf("failed GetFilterChanges: got %d messages.", len(mail)) 443 } 444 445 text := string(common.FromHex(mail[0].Payload)) 446 if text != string("extended test string") { 447 t.Fatalf("failed to decrypt first message: %s.", text) 448 } 449 450 p.Padding = []byte("new value") 451 p.Payload = []byte("extended new value") 452 err = api.Post(p) 453 if err != nil { 454 t.Fatalf("failed to post second message: %s.", err) 455 } 456 457 ok = waitForMessage(api, id, 2) 458 if !ok { 459 t.Fatalf("failed to receive second message: timeout.") 460 } 461 462 mail = api.GetFilterChanges(id) 463 if len(mail) != 1 { 464 t.Fatalf("failed second GetFilterChanges: got %d messages.", len(mail)) 465 } 466 467 text = string(common.FromHex(mail[0].Payload)) 468 if text != string("extended new value") { 469 t.Fatalf("failed to decrypt second message: %s.", text) 470 } 471 } 472 473 func TestIntegrationSymWithFilter(t *testing.T) { 474 api := NewPublicWhisperAPI() 475 if api == nil { 476 t.Fatalf("failed to create API.") 477 } 478 479 api.Start() 480 defer api.Stop() 481 482 keyname := "schluessel" 483 err := api.GenerateSymKey(keyname) 484 if err != nil { 485 t.Fatalf("failed to GenerateSymKey: %s.", err) 486 } 487 488 sig, err := api.NewIdentity() 489 if err != nil { 490 t.Fatalf("failed NewIdentity: %s.", err) 491 } 492 if len(sig) == 0 { 493 t.Fatalf("wrong signature.") 494 } 495 496 exist, err := api.HasIdentity(sig) 497 if err != nil { 498 t.Fatalf("failed HasIdentity: %s.", err) 499 } 500 if !exist { 501 t.Fatalf("failed HasIdentity: does not exist.") 502 } 503 504 var topics [2]whisperv5.TopicType 505 topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} 506 topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 507 var f WhisperFilterArgs 508 f.KeyName = keyname 509 f.Topics = topics[:] 510 f.PoW = 0.324 511 f.From = sig 512 f.AcceptP2P = false 513 514 id, err := api.NewFilter(f) 515 if err != nil { 516 t.Fatalf("failed to create new filter: %s.", err) 517 } 518 519 var p PostArgs 520 p.TTL = 1 521 p.FilterID = id 522 p.From = sig 523 p.Padding = []byte("test string") 524 p.Payload = []byte("extended test string") 525 p.PoW = whisperv5.MinimumPoW 526 p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} 527 p.WorkTime = 2 528 529 err = api.Post(p) 530 if err != nil { 531 t.Fatalf("failed to post message: %s.", err) 532 } 533 534 ok := waitForMessage(api, id, 1) 535 if !ok { 536 t.Fatalf("failed to receive first message: timeout.") 537 } 538 539 mail := api.GetFilterChanges(id) 540 if len(mail) != 1 { 541 t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) 542 } 543 544 text := string(common.FromHex(mail[0].Payload)) 545 if text != string("extended test string") { 546 t.Fatalf("failed to decrypt first message: %s.", text) 547 } 548 549 p.Padding = []byte("new value") 550 p.Payload = []byte("extended new value") 551 err = api.Post(p) 552 if err != nil { 553 t.Fatalf("failed to post next message: %s.", err) 554 } 555 556 ok = waitForMessage(api, id, 2) 557 if !ok { 558 t.Fatalf("failed to receive second message: timeout.") 559 } 560 561 mail = api.GetFilterChanges(id) 562 if len(mail) != 1 { 563 t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail)) 564 } 565 566 text = string(common.FromHex(mail[0].Payload)) 567 if text != string("extended new value") { 568 t.Fatalf("failed to decrypt second message: %s.", text) 569 } 570 }