api_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 shhapi
  17. import (
  18. "bytes"
  19. "testing"
  20. "time"
  21. "encoding/json"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/whisper/whisperv5"
  24. )
  25. func TestBasic(t *testing.T) {
  26. var id string = "test"
  27. api := NewPublicWhisperAPI()
  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) != whisperv5.ProtocolVersion {
  36. t.Fatalf("wrong version: %d.", ver)
  37. }
  38. mail := api.GetFilterChanges(1)
  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. "acceptP2P":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] != (whisperv5.TopicType{0x00, 0x00, 0x00, 0x00}) {
  159. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  160. }
  161. i++
  162. if f.Topics[i] != (whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}) {
  163. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  164. }
  165. i++
  166. if f.Topics[i] != (whisperv5.TopicType{0xff, 0x80, 0x7f, 0x00}) {
  167. t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
  168. }
  169. i++
  170. if f.Topics[i] != (whisperv5.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":64,
  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 != (whisperv5.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 != 64 {
  221. t.Fatalf("wrong FilterID: %d.", a.FilterID)
  222. }
  223. if bytes.Compare(a.PeerID[:], a.Topic[:]) != 0 {
  224. t.Fatalf("wrong PeerID: %x.", a.PeerID)
  225. }
  226. }
  227. func waitForMessage(api *PublicWhisperAPI, id uint32, 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. api := NewPublicWhisperAPI()
  240. if api == nil {
  241. t.Fatalf("failed to create API.")
  242. }
  243. api.Start()
  244. defer api.Stop()
  245. sig, err := api.NewIdentity()
  246. if err != nil {
  247. t.Fatalf("failed NewIdentity: %s.", err)
  248. }
  249. if len(sig) == 0 {
  250. t.Fatalf("wrong signature")
  251. }
  252. exist, err := api.HasIdentity(sig)
  253. if err != nil {
  254. t.Fatalf("failed HasIdentity: %s.", err)
  255. }
  256. if !exist {
  257. t.Fatalf("failed HasIdentity: false negative.")
  258. }
  259. key, err := api.NewIdentity()
  260. if err != nil {
  261. t.Fatalf("failed NewIdentity(): %s.", err)
  262. }
  263. if len(key) == 0 {
  264. t.Fatalf("wrong key")
  265. }
  266. var topics [2]whisperv5.TopicType
  267. topics[0] = whisperv5.TopicType{0x00, 0x64, 0x00, 0xff}
  268. topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  269. var f WhisperFilterArgs
  270. f.To = key
  271. f.From = sig
  272. f.Topics = topics[:]
  273. f.PoW = whisperv5.MinimumPoW / 2
  274. f.AcceptP2P = true
  275. id, err := api.NewFilter(f)
  276. if err != nil {
  277. t.Fatalf("failed to create new filter: %s.", err)
  278. }
  279. var p PostArgs
  280. p.TTL = 2
  281. p.From = f.From
  282. p.To = f.To
  283. p.Padding = []byte("test string")
  284. p.Payload = []byte("extended test string")
  285. p.PoW = whisperv5.MinimumPoW
  286. p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  287. p.WorkTime = 2
  288. err = api.Post(p)
  289. if err != nil {
  290. t.Errorf("failed to post message: %s.", err)
  291. }
  292. ok := waitForMessage(api, id, 1)
  293. if !ok {
  294. t.Fatalf("failed to receive first message: timeout.")
  295. }
  296. mail := api.GetFilterChanges(id)
  297. if len(mail) != 1 {
  298. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  299. }
  300. text := string(common.FromHex(mail[0].Payload))
  301. if text != string("extended test string") {
  302. t.Fatalf("failed to decrypt first message: %s.", text)
  303. }
  304. p.Padding = []byte("new value")
  305. p.Payload = []byte("extended new value")
  306. err = api.Post(p)
  307. if err != nil {
  308. t.Fatalf("failed to post next message: %s.", err)
  309. }
  310. ok = waitForMessage(api, id, 2)
  311. if !ok {
  312. t.Fatalf("failed to receive second message: timeout.")
  313. }
  314. mail = api.GetFilterChanges(id)
  315. if len(mail) != 1 {
  316. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  317. }
  318. text = string(common.FromHex(mail[0].Payload))
  319. if text != string("extended new value") {
  320. t.Fatalf("failed to decrypt second message: %s.", text)
  321. }
  322. }
  323. func TestIntegrationSym(t *testing.T) {
  324. api := NewPublicWhisperAPI()
  325. if api == nil {
  326. t.Fatalf("failed to create API.")
  327. }
  328. api.Start()
  329. defer api.Stop()
  330. keyname := "schluessel"
  331. err := api.GenerateSymKey(keyname)
  332. if err != nil {
  333. t.Fatalf("failed GenerateSymKey: %s.", err)
  334. }
  335. sig, err := api.NewIdentity()
  336. if err != nil {
  337. t.Fatalf("failed NewIdentity: %s.", err)
  338. }
  339. if len(sig) == 0 {
  340. t.Fatalf("wrong signature")
  341. }
  342. exist, err := api.HasIdentity(sig)
  343. if err != nil {
  344. t.Fatalf("failed HasIdentity: %s.", err)
  345. }
  346. if !exist {
  347. t.Fatalf("failed HasIdentity: false negative.")
  348. }
  349. var topics [2]whisperv5.TopicType
  350. topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}
  351. topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  352. var f WhisperFilterArgs
  353. f.KeyName = keyname
  354. f.Topics = topics[:]
  355. f.PoW = 0.324
  356. f.From = sig
  357. f.AcceptP2P = false
  358. id, err := api.NewFilter(f)
  359. if err != nil {
  360. t.Fatalf("failed to create new filter: %s.", err)
  361. }
  362. var p PostArgs
  363. p.TTL = 1
  364. p.KeyName = keyname
  365. p.From = f.From
  366. p.Padding = []byte("test string")
  367. p.Payload = []byte("extended test string")
  368. p.PoW = whisperv5.MinimumPoW
  369. p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  370. p.WorkTime = 2
  371. err = api.Post(p)
  372. if err != nil {
  373. t.Fatalf("failed to post first message: %s.", err)
  374. }
  375. ok := waitForMessage(api, id, 1)
  376. if !ok {
  377. t.Fatalf("failed to receive first message: timeout.")
  378. }
  379. mail := api.GetFilterChanges(id)
  380. if len(mail) != 1 {
  381. t.Fatalf("failed GetFilterChanges: got %d messages.", len(mail))
  382. }
  383. text := string(common.FromHex(mail[0].Payload))
  384. if text != string("extended test string") {
  385. t.Fatalf("failed to decrypt first message: %s.", text)
  386. }
  387. p.Padding = []byte("new value")
  388. p.Payload = []byte("extended new value")
  389. err = api.Post(p)
  390. if err != nil {
  391. t.Fatalf("failed to post second message: %s.", err)
  392. }
  393. ok = waitForMessage(api, id, 2)
  394. if !ok {
  395. t.Fatalf("failed to receive second message: timeout.")
  396. }
  397. mail = api.GetFilterChanges(id)
  398. if len(mail) != 1 {
  399. t.Fatalf("failed second GetFilterChanges: got %d messages.", len(mail))
  400. }
  401. text = string(common.FromHex(mail[0].Payload))
  402. if text != string("extended new value") {
  403. t.Fatalf("failed to decrypt second message: %s.", text)
  404. }
  405. }
  406. func TestIntegrationSymWithFilter(t *testing.T) {
  407. api := NewPublicWhisperAPI()
  408. if api == nil {
  409. t.Fatalf("failed to create API.")
  410. }
  411. api.Start()
  412. defer api.Stop()
  413. keyname := "schluessel"
  414. err := api.GenerateSymKey(keyname)
  415. if err != nil {
  416. t.Fatalf("failed to GenerateSymKey: %s.", err)
  417. }
  418. sig, err := api.NewIdentity()
  419. if err != nil {
  420. t.Fatalf("failed NewIdentity: %s.", err)
  421. }
  422. if len(sig) == 0 {
  423. t.Fatalf("wrong signature.")
  424. }
  425. exist, err := api.HasIdentity(sig)
  426. if err != nil {
  427. t.Fatalf("failed HasIdentity: %s.", err)
  428. }
  429. if !exist {
  430. t.Fatalf("failed HasIdentity: does not exist.")
  431. }
  432. var topics [2]whisperv5.TopicType
  433. topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}
  434. topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  435. var f WhisperFilterArgs
  436. f.KeyName = keyname
  437. f.Topics = topics[:]
  438. f.PoW = 0.324
  439. f.From = sig
  440. f.AcceptP2P = false
  441. id, err := api.NewFilter(f)
  442. if err != nil {
  443. t.Fatalf("failed to create new filter: %s.", err)
  444. }
  445. var p PostArgs
  446. p.TTL = 1
  447. p.FilterID = id
  448. p.From = sig
  449. p.Padding = []byte("test string")
  450. p.Payload = []byte("extended test string")
  451. p.PoW = whisperv5.MinimumPoW
  452. p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}
  453. p.WorkTime = 2
  454. err = api.Post(p)
  455. if err != nil {
  456. t.Fatalf("failed to post message: %s.", err)
  457. }
  458. ok := waitForMessage(api, id, 1)
  459. if !ok {
  460. t.Fatalf("failed to receive first message: timeout.")
  461. }
  462. mail := api.GetFilterChanges(id)
  463. if len(mail) != 1 {
  464. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  465. }
  466. text := string(common.FromHex(mail[0].Payload))
  467. if text != string("extended test string") {
  468. t.Fatalf("failed to decrypt first message: %s.", text)
  469. }
  470. p.Padding = []byte("new value")
  471. p.Payload = []byte("extended new value")
  472. err = api.Post(p)
  473. if err != nil {
  474. t.Fatalf("failed to post next message: %s.", err)
  475. }
  476. ok = waitForMessage(api, id, 2)
  477. if !ok {
  478. t.Fatalf("failed to receive second message: timeout.")
  479. }
  480. mail = api.GetFilterChanges(id)
  481. if len(mail) != 1 {
  482. t.Fatalf("failed to GetFilterChanges: got %d messages.", len(mail))
  483. }
  484. text = string(common.FromHex(mail[0].Payload))
  485. if text != string("extended new value") {
  486. t.Fatalf("failed to decrypt second message: %s.", text)
  487. }
  488. }