api_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. package whisperv5
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. func TestBasic(t *testing.T) {
  25. var id string = "test"
  26. w := New()
  27. api := NewPublicWhisperAPI(w)
  28. if api == nil {
  29. t.Fatalf("failed to create API.")
  30. }
  31. ver, err := api.Version()
  32. if err != nil {
  33. t.Fatalf("failed generateFilter: %s.", err)
  34. }
  35. if uint64(ver) != ProtocolVersion {
  36. t.Fatalf("wrong version: %d.", ver)
  37. }
  38. mail := api.GetFilterChanges("non-existent-id")
  39. if len(mail) != 0 {
  40. t.Fatalf("failed GetFilterChanges: premature result")
  41. }
  42. exist, err := api.HasIdentity(id)
  43. if err != nil {
  44. t.Fatalf("failed initial HasIdentity: %s.", err)
  45. }
  46. if exist {
  47. t.Fatalf("failed initial HasIdentity: false positive.")
  48. }
  49. err = api.DeleteIdentity(id)
  50. if err != nil {
  51. t.Fatalf("failed DeleteIdentity: %s.", err)
  52. }
  53. pub, err := api.NewIdentity()
  54. if err != nil {
  55. t.Fatalf("failed NewIdentity: %s.", err)
  56. }
  57. if len(pub) == 0 {
  58. t.Fatalf("failed NewIdentity: empty")
  59. }
  60. exist, err = api.HasIdentity(pub)
  61. if err != nil {
  62. t.Fatalf("failed HasIdentity: %s.", err)
  63. }
  64. if !exist {
  65. t.Fatalf("failed HasIdentity: false negative.")
  66. }
  67. err = api.DeleteIdentity(pub)
  68. if err != nil {
  69. t.Fatalf("failed to delete second identity: %s.", err)
  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 positive.")
  77. }
  78. id = "arbitrary text"
  79. id2 := "another arbitrary string"
  80. exist, err = api.HasSymKey(id)
  81. if err != nil {
  82. t.Fatalf("failed HasSymKey: %s.", err)
  83. }
  84. if exist {
  85. t.Fatalf("failed HasSymKey: false positive.")
  86. }
  87. err = api.GenerateSymKey(id)
  88. if err != nil {
  89. t.Fatalf("failed GenerateSymKey: %s.", err)
  90. }
  91. exist, err = api.HasSymKey(id)
  92. if err != nil {
  93. t.Fatalf("failed HasSymKey(): %s.", err)
  94. }
  95. if !exist {
  96. t.Fatalf("failed HasSymKey(): false negative.")
  97. }
  98. err = api.AddSymKey(id, []byte("some stuff here"))
  99. if err == nil {
  100. t.Fatalf("failed AddSymKey: %s.", err)
  101. }
  102. err = api.AddSymKey(id2, []byte("some stuff here"))
  103. if err != nil {
  104. t.Fatalf("failed AddSymKey: %s.", err)
  105. }
  106. exist, err = api.HasSymKey(id2)
  107. if err != nil {
  108. t.Fatalf("failed HasSymKey(id2): %s.", err)
  109. }
  110. if !exist {
  111. t.Fatalf("failed HasSymKey(id2): false negative.")
  112. }
  113. err = api.DeleteSymKey(id)
  114. if err != nil {
  115. t.Fatalf("failed DeleteSymKey(id): %s.", err)
  116. }
  117. exist, err = api.HasSymKey(id)
  118. if err != nil {
  119. t.Fatalf("failed HasSymKey(id): %s.", err)
  120. }
  121. if exist {
  122. t.Fatalf("failed HasSymKey(id): false positive.")
  123. }
  124. }
  125. func TestUnmarshalFilterArgs(t *testing.T) {
  126. s := []byte(`{
  127. "to":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
  128. "from":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
  129. "keyname":"testname",
  130. "pow":2.34,
  131. "topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"],
  132. "p2p":true
  133. }`)
  134. var f WhisperFilterArgs
  135. err := f.UnmarshalJSON(s)
  136. if err != nil {
  137. t.Fatalf("failed UnmarshalJSON: %s.", err)
  138. }
  139. if f.To != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
  140. t.Fatalf("wrong To: %x.", f.To)
  141. }
  142. if f.From != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
  143. t.Fatalf("wrong From: %x.", f.To)
  144. }
  145. if f.KeyName != "testname" {
  146. t.Fatalf("wrong KeyName: %s.", f.KeyName)
  147. }
  148. if f.PoW != 2.34 {
  149. t.Fatalf("wrong pow: %f.", f.PoW)
  150. }
  151. if !f.AcceptP2P {
  152. t.Fatalf("wrong AcceptP2P: %v.", f.AcceptP2P)
  153. }
  154. if len(f.Topics) != 4 {
  155. t.Fatalf("wrong topics number: %d.", len(f.Topics))
  156. }
  157. i := 0
  158. if f.Topics[i] != (TopicType{0x00, 0x00, 0x00, 0x00}) {
  159. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  160. }
  161. i++
  162. if f.Topics[i] != (TopicType{0x00, 0x7f, 0x80, 0xff}) {
  163. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  164. }
  165. i++
  166. if f.Topics[i] != (TopicType{0xff, 0x80, 0x7f, 0x00}) {
  167. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  168. }
  169. i++
  170. if f.Topics[i] != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
  171. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  172. }
  173. }
  174. func TestUnmarshalPostArgs(t *testing.T) {
  175. s := []byte(`{
  176. "ttl":12345,
  177. "from":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
  178. "to":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
  179. "keyname":"shh_test",
  180. "topic":"0xf26e7779",
  181. "padding":"0x74686973206973206D79207465737420737472696E67",
  182. "payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D",
  183. "worktime":777,
  184. "pow":3.1416,
  185. "filterid":"test-filter-id",
  186. "peerid":"0xf26e7779"
  187. }`)
  188. var a PostArgs
  189. err := json.Unmarshal(s, &a)
  190. if err != nil {
  191. t.Fatalf("failed UnmarshalJSON: %s.", err)
  192. }
  193. if a.TTL != 12345 {
  194. t.Fatalf("wrong ttl: %d.", a.TTL)
  195. }
  196. if a.From != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
  197. t.Fatalf("wrong From: %x.", a.To)
  198. }
  199. if a.To != "0x9b2055d370f73ec7d8a03e965129118dc8f5bf83" {
  200. t.Fatalf("wrong To: %x.", a.To)
  201. }
  202. if a.KeyName != "shh_test" {
  203. t.Fatalf("wrong KeyName: %s.", a.KeyName)
  204. }
  205. if a.Topic != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
  206. t.Fatalf("wrong topic: %x.", a.Topic)
  207. }
  208. if string(a.Padding) != "this is my test string" {
  209. t.Fatalf("wrong Padding: %s.", string(a.Padding))
  210. }
  211. if string(a.Payload) != "payload should be pseudorandom" {
  212. t.Fatalf("wrong Payload: %s.", string(a.Payload))
  213. }
  214. if a.WorkTime != 777 {
  215. t.Fatalf("wrong WorkTime: %d.", a.WorkTime)
  216. }
  217. if a.PoW != 3.1416 {
  218. t.Fatalf("wrong pow: %f.", a.PoW)
  219. }
  220. if a.FilterID != "test-filter-id" {
  221. t.Fatalf("wrong FilterID: %s.", a.FilterID)
  222. }
  223. if !bytes.Equal(a.PeerID[:], a.Topic[:]) {
  224. t.Fatalf("wrong PeerID: %x.", a.PeerID)
  225. }
  226. }
  227. func waitForMessage(api *PublicWhisperAPI, id string, target int) bool {
  228. for i := 0; i < 64; i++ {
  229. all := api.GetMessages(id)
  230. if len(all) >= target {
  231. return true
  232. }
  233. time.Sleep(time.Millisecond * 16)
  234. }
  235. // timeout 1024 milliseconds
  236. return false
  237. }
  238. func TestIntegrationAsym(t *testing.T) {
  239. w := New()
  240. api := NewPublicWhisperAPI(w)
  241. if api == nil {
  242. t.Fatalf("failed to create API.")
  243. }
  244. api.Start()
  245. defer api.Stop()
  246. sig, err := api.NewIdentity()
  247. if err != nil {
  248. t.Fatalf("failed NewIdentity: %s.", err)
  249. }
  250. if len(sig) == 0 {
  251. t.Fatalf("wrong signature")
  252. }
  253. exist, err := api.HasIdentity(sig)
  254. if err != nil {
  255. t.Fatalf("failed HasIdentity: %s.", err)
  256. }
  257. if !exist {
  258. t.Fatalf("failed HasIdentity: false negative.")
  259. }
  260. key, err := api.NewIdentity()
  261. if err != nil {
  262. t.Fatalf("failed NewIdentity(): %s.", err)
  263. }
  264. if len(key) == 0 {
  265. t.Fatalf("wrong key")
  266. }
  267. var topics [2]TopicType
  268. topics[0] = TopicType{0x00, 0x64, 0x00, 0xff}
  269. topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
  270. var f WhisperFilterArgs
  271. f.To = key
  272. f.From = sig
  273. f.Topics = topics[:]
  274. f.PoW = MinimumPoW / 2
  275. f.AcceptP2P = true
  276. id, err := api.NewFilter(f)
  277. if err != nil {
  278. t.Fatalf("failed to create new filter: %s.", err)
  279. }
  280. var p PostArgs
  281. p.TTL = 2
  282. p.From = f.From
  283. p.To = f.To
  284. p.Padding = []byte("test string")
  285. p.Payload = []byte("extended test string")
  286. p.PoW = MinimumPoW
  287. p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
  288. p.WorkTime = 2
  289. err = api.Post(p)
  290. if err != nil {
  291. t.Errorf("failed to post message: %s.", err)
  292. }
  293. ok := waitForMessage(api, id, 1)
  294. if !ok {
  295. t.Fatalf("failed to receive first message: timeout.")
  296. }
  297. mail := api.GetFilterChanges(id)
  298. if len(mail) != 1 {
  299. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  300. }
  301. text := string(common.FromHex(mail[0].Payload))
  302. if text != string("extended test string") {
  303. t.Fatalf("failed to decrypt first message: %s.", text)
  304. }
  305. p.Padding = []byte("new value")
  306. p.Payload = []byte("extended new value")
  307. err = api.Post(p)
  308. if err != nil {
  309. t.Fatalf("failed to post next message: %s.", err)
  310. }
  311. ok = waitForMessage(api, id, 2)
  312. if !ok {
  313. t.Fatalf("failed to receive second message: timeout.")
  314. }
  315. mail = api.GetFilterChanges(id)
  316. if len(mail) != 1 {
  317. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  318. }
  319. text = string(common.FromHex(mail[0].Payload))
  320. if text != string("extended new value") {
  321. t.Fatalf("failed to decrypt second message: %s.", text)
  322. }
  323. }
  324. func TestIntegrationSym(t *testing.T) {
  325. w := New()
  326. api := NewPublicWhisperAPI(w)
  327. if api == nil {
  328. t.Fatalf("failed to create API.")
  329. }
  330. api.Start()
  331. defer api.Stop()
  332. keyname := "schluessel"
  333. err := api.GenerateSymKey(keyname)
  334. if err != nil {
  335. t.Fatalf("failed GenerateSymKey: %s.", err)
  336. }
  337. sig, err := api.NewIdentity()
  338. if err != nil {
  339. t.Fatalf("failed NewIdentity: %s.", err)
  340. }
  341. if len(sig) == 0 {
  342. t.Fatalf("wrong signature")
  343. }
  344. exist, err := api.HasIdentity(sig)
  345. if err != nil {
  346. t.Fatalf("failed HasIdentity: %s.", err)
  347. }
  348. if !exist {
  349. t.Fatalf("failed HasIdentity: false negative.")
  350. }
  351. var topics [2]TopicType
  352. topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
  353. topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
  354. var f WhisperFilterArgs
  355. f.KeyName = keyname
  356. f.Topics = topics[:]
  357. f.PoW = 0.324
  358. f.From = sig
  359. f.AcceptP2P = false
  360. id, err := api.NewFilter(f)
  361. if err != nil {
  362. t.Fatalf("failed to create new filter: %s.", err)
  363. }
  364. var p PostArgs
  365. p.TTL = 1
  366. p.KeyName = keyname
  367. p.From = f.From
  368. p.Padding = []byte("test string")
  369. p.Payload = []byte("extended test string")
  370. p.PoW = MinimumPoW
  371. p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
  372. p.WorkTime = 2
  373. err = api.Post(p)
  374. if err != nil {
  375. t.Fatalf("failed to post first message: %s.", err)
  376. }
  377. ok := waitForMessage(api, id, 1)
  378. if !ok {
  379. t.Fatalf("failed to receive first message: timeout.")
  380. }
  381. mail := api.GetFilterChanges(id)
  382. if len(mail) != 1 {
  383. t.Fatalf("failed GetFilterChanges: got %d messages.", len(mail))
  384. }
  385. text := string(common.FromHex(mail[0].Payload))
  386. if text != string("extended test string") {
  387. t.Fatalf("failed to decrypt first message: %s.", text)
  388. }
  389. p.Padding = []byte("new value")
  390. p.Payload = []byte("extended new value")
  391. err = api.Post(p)
  392. if err != nil {
  393. t.Fatalf("failed to post second message: %s.", err)
  394. }
  395. ok = waitForMessage(api, id, 2)
  396. if !ok {
  397. t.Fatalf("failed to receive second message: timeout.")
  398. }
  399. mail = api.GetFilterChanges(id)
  400. if len(mail) != 1 {
  401. t.Fatalf("failed second GetFilterChanges: got %d messages.", len(mail))
  402. }
  403. text = string(common.FromHex(mail[0].Payload))
  404. if text != string("extended new value") {
  405. t.Fatalf("failed to decrypt second message: %s.", text)
  406. }
  407. }
  408. func TestIntegrationSymWithFilter(t *testing.T) {
  409. w := New()
  410. api := NewPublicWhisperAPI(w)
  411. if api == nil {
  412. t.Fatalf("failed to create API.")
  413. }
  414. api.Start()
  415. defer api.Stop()
  416. keyname := "schluessel"
  417. err := api.GenerateSymKey(keyname)
  418. if err != nil {
  419. t.Fatalf("failed to GenerateSymKey: %s.", err)
  420. }
  421. sig, err := api.NewIdentity()
  422. if err != nil {
  423. t.Fatalf("failed NewIdentity: %s.", err)
  424. }
  425. if len(sig) == 0 {
  426. t.Fatalf("wrong signature.")
  427. }
  428. exist, err := api.HasIdentity(sig)
  429. if err != nil {
  430. t.Fatalf("failed HasIdentity: %s.", err)
  431. }
  432. if !exist {
  433. t.Fatalf("failed HasIdentity: does not exist.")
  434. }
  435. var topics [2]TopicType
  436. topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
  437. topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
  438. var f WhisperFilterArgs
  439. f.KeyName = keyname
  440. f.Topics = topics[:]
  441. f.PoW = 0.324
  442. f.From = sig
  443. f.AcceptP2P = false
  444. id, err := api.NewFilter(f)
  445. if err != nil {
  446. t.Fatalf("failed to create new filter: %s.", err)
  447. }
  448. var p PostArgs
  449. p.TTL = 1
  450. p.FilterID = id
  451. p.From = sig
  452. p.Padding = []byte("test string")
  453. p.Payload = []byte("extended test string")
  454. p.PoW = MinimumPoW
  455. p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
  456. p.WorkTime = 2
  457. err = api.Post(p)
  458. if err != nil {
  459. t.Fatalf("failed to post message: %s.", err)
  460. }
  461. ok := waitForMessage(api, id, 1)
  462. if !ok {
  463. t.Fatalf("failed to receive first message: timeout.")
  464. }
  465. mail := api.GetFilterChanges(id)
  466. if len(mail) != 1 {
  467. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  468. }
  469. text := string(common.FromHex(mail[0].Payload))
  470. if text != string("extended test string") {
  471. t.Fatalf("failed to decrypt first message: %s.", text)
  472. }
  473. p.Padding = []byte("new value")
  474. p.Payload = []byte("extended new value")
  475. err = api.Post(p)
  476. if err != nil {
  477. t.Fatalf("failed to post next message: %s.", err)
  478. }
  479. ok = waitForMessage(api, id, 2)
  480. if !ok {
  481. t.Fatalf("failed to receive second message: timeout.")
  482. }
  483. mail = api.GetFilterChanges(id)
  484. if len(mail) != 1 {
  485. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  486. }
  487. text = string(common.FromHex(mail[0].Payload))
  488. if text != string("extended new value") {
  489. t.Fatalf("failed to decrypt second message: %s.", text)
  490. }
  491. }