api_test.go 13 KB

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