Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
13KB

  1. package testing
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/model"
  5. "cwtch.im/cwtch/peer"
  6. "cwtch.im/cwtch/protocol/connections"
  7. cwtchserver "cwtch.im/cwtch/server"
  8. "fmt"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  10. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  11. "golang.org/x/net/proxy"
  12. "os"
  13. "runtime"
  14. "runtime/pprof"
  15. "testing"
  16. "time"
  17. )
  18. const (
  19. serverKeyfile = "./../server/app/private_key"
  20. localKeyfile = "./private_key"
  21. )
  22. var (
  23. aliceLines = []string{"Hello, I'm Alice", "bye"}
  24. bobLines = []string{"Hi, my name is Bob.", "toodles", "welcome"}
  25. carolLines = []string{"Howdy, thanks!"}
  26. )
  27. func printAndCountVerifedTimeline(t *testing.T, timeline []model.Message) int {
  28. numVerified := 0
  29. for _, message := range timeline {
  30. fmt.Printf("%v %v> %s\n", message.Timestamp, message.PeerID, message.Message)
  31. numVerified++
  32. }
  33. return numVerified
  34. }
  35. func serverCheck(t *testing.T, serverAddr string) bool {
  36. torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
  37. if err != nil {
  38. t.Logf("Could not get SOCKS5 proxy: %v", err)
  39. return false
  40. }
  41. // Doesn't seem to be a way to turn the default timeout of 2 minutes down
  42. conn, err := torDialer.Dial("tcp", serverAddr+".onion:9878")
  43. if err != nil {
  44. t.Logf("Could not dial %v: %v", serverAddr, err)
  45. return false
  46. }
  47. conn.Close()
  48. return true
  49. }
  50. func waitForPeerServerConnection(t *testing.T, peer peer.CwtchPeer, server string) {
  51. for {
  52. servers := peer.GetServers()
  53. state, ok := servers[server]
  54. if ok {
  55. if state == connections.FAILED {
  56. t.Fatalf("%v could not connect to %v", peer.GetProfile().Onion, server)
  57. }
  58. if state != connections.AUTHENTICATED {
  59. fmt.Printf("peer %v waiting connect to server %v, currently: %v\n", peer.GetProfile().Onion, server, connections.ConnectionStateName[state])
  60. time.Sleep(time.Second * 5)
  61. continue
  62. } else {
  63. break
  64. }
  65. } // It might take a second for the server to show up as it is now going through the event bus
  66. time.Sleep(time.Second)
  67. }
  68. return
  69. }
  70. func waitForPeerPeerConnection(t *testing.T, peera peer.CwtchPeer, peerb peer.CwtchPeer) {
  71. for {
  72. peers := peera.GetPeers()
  73. state, ok := peers[peerb.GetProfile().Onion]
  74. if ok {
  75. if state == connections.FAILED {
  76. t.Fatalf("%v could not connect to %v", peera.GetProfile().Onion, peerb.GetProfile().Onion)
  77. }
  78. if state != connections.AUTHENTICATED {
  79. fmt.Printf("peer% v waiting connect to peer %v, currently: %v\n", peera.GetProfile().Onion, peerb.GetProfile().Onion, connections.ConnectionStateName[state])
  80. time.Sleep(time.Second * 5)
  81. continue
  82. } else {
  83. break
  84. }
  85. } // It might take a second for the peer to show up as it is now going through the event bus
  86. time.Sleep(time.Second)
  87. }
  88. return
  89. }
  90. func TestCwtchPeerIntegration(t *testing.T) {
  91. // Hide logging "noise"
  92. numGoRoutinesStart := runtime.NumGoroutine()
  93. log.AddEverythingFromPattern("connectivity")
  94. acn, err := connectivity.StartTor(".", "")
  95. if err != nil {
  96. t.Fatalf("Could not start Tor: %v", err)
  97. }
  98. // ***** Cwtch Server management *****
  99. var server *cwtchserver.Server
  100. serverOnline := false
  101. var serverAddr string
  102. if !serverOnline {
  103. // launch app with new key
  104. fmt.Println("No server found!")
  105. server = new(cwtchserver.Server)
  106. fmt.Println("Starting cwtch server...")
  107. os.Remove("server-test.json")
  108. config := cwtchserver.LoadConfig(".", "server-test.json")
  109. identity := config.Identity()
  110. serverAddr = identity.Hostname()
  111. go server.Run(acn, config)
  112. // let tor get established
  113. fmt.Printf("Establishing Tor hidden service: %v...\n", serverAddr)
  114. } else {
  115. fmt.Printf("Found existing cwtch server %v, using for tests...\n", serverAddr)
  116. }
  117. numGoRoutinesPostServer := runtime.NumGoroutine()
  118. // ***** cwtchPeer setup *****
  119. // It's important that each Peer have their own EventBus
  120. aliceEventBus := new(event.Manager)
  121. aliceEventBus.Initialize()
  122. bobEventBus := new(event.Manager)
  123. bobEventBus.Initialize()
  124. carolEventBus := new(event.Manager)
  125. carolEventBus.Initialize()
  126. fmt.Println("Creating Alice...")
  127. alice := peer.NewCwtchPeer("Alice")
  128. alice.Init(acn, aliceEventBus)
  129. alice.Listen()
  130. fmt.Println("Alice created:", alice.GetProfile().Onion)
  131. fmt.Println("Creating Bob...")
  132. bob := peer.NewCwtchPeer("Bob")
  133. bob.Init(acn, bobEventBus)
  134. bob.Listen()
  135. fmt.Println("Bob created:", bob.GetProfile().Onion)
  136. fmt.Println("Creating Carol...")
  137. carol := peer.NewCwtchPeer("Carol")
  138. carol.Init(acn, carolEventBus)
  139. carol.Listen()
  140. fmt.Println("Carol created:", carol.GetProfile().Onion)
  141. fmt.Println("Waiting for Alice, Bob, and Carol to connection with onion network...")
  142. time.Sleep(time.Second * 90)
  143. numGoRoutinesPostPeerStart := runtime.NumGoroutine()
  144. // ***** Peering, server joining, group creation / invite *****
  145. fmt.Println("Creating group on ", serverAddr, "...")
  146. groupID, _, err := alice.StartGroup(serverAddr)
  147. fmt.Printf("Created group: %v!\n", groupID)
  148. if err != nil {
  149. t.Errorf("Failed to init group: %v", err)
  150. return
  151. }
  152. fmt.Println("Alice peering with Bob...")
  153. alice.PeerWithOnion(bob.GetProfile().Onion)
  154. fmt.Println("Alice peering with Carol...")
  155. alice.PeerWithOnion(carol.GetProfile().Onion)
  156. fmt.Println("Alice joining server...")
  157. alice.JoinServer(serverAddr)
  158. fmt.Println("Bob joining server...")
  159. bob.JoinServer(serverAddr)
  160. fmt.Println("Waiting for alice to join server...")
  161. waitForPeerServerConnection(t, alice, serverAddr)
  162. fmt.Println("Waiting for bob to join server...")
  163. waitForPeerServerConnection(t, bob, serverAddr)
  164. fmt.Println("Waiting for alice and Bob to peer...")
  165. waitForPeerPeerConnection(t, alice, bob)
  166. /*fmt.Println("Waiting for peerings and server joins...")
  167. time.Sleep(time.Second * 240)*/
  168. fmt.Println("Alice inviting Bob to group...")
  169. err = alice.InviteOnionToGroup(bob.GetProfile().Onion, groupID)
  170. if err != nil {
  171. t.Fatalf("Error for Alice inviting Bob to group: %v", err)
  172. }
  173. time.Sleep(time.Second * 10)
  174. fmt.Println("Bob examining groups and accepting invites...")
  175. for _, groupID := range bob.GetGroups() {
  176. group := bob.GetGroup(groupID)
  177. fmt.Printf("Bob group: %v (Accepted: %v)\n", group.GroupID, group.Accepted)
  178. if group.Accepted == false {
  179. fmt.Printf("Bob received and accepting group invite: %v\n", group.GroupID)
  180. bob.AcceptInvite(group.GroupID)
  181. }
  182. }
  183. time.Sleep(time.Second * 5)
  184. numGoRoutinesPostServerConnect := runtime.NumGoroutine()
  185. // ***** Fill up message history of server ******
  186. /*
  187. // filler group will be used to fill up the servers message history a bit to stress test fetch later for carol
  188. fillerGroupId, _, err := alice.Profile.StartGroup(serverAddr)
  189. if err != nil {
  190. t.Errorf("Failed to init filler group: %v", err)
  191. return
  192. }
  193. fmt.Println("Alice filling message history of server...")
  194. for i := 0; i < 100; i++ {
  195. go func (x int) {
  196. time.Sleep(time.Second * time.Duration(x))
  197. err := alice.SendMessageToGroup(fillerGroupId, aliceLines[0])
  198. if err != nil {
  199. fmt.Println("SEND", x, "ERROR:", err)
  200. } else {
  201. fmt.Println("SEND", x, " SUCCESS!")
  202. }
  203. }(i)
  204. }
  205. time.Sleep(time.Second * 110)
  206. */
  207. // Wait for them to join the server
  208. waitForPeerServerConnection(t, alice, serverAddr)
  209. waitForPeerServerConnection(t, bob, serverAddr)
  210. //numGouRoutinesPostServerConnect := runtime.NumGoroutine()
  211. // ***** Conversation *****
  212. fmt.Println("Starting conversation in group...")
  213. // Conversation
  214. fmt.Println("Alice> ", aliceLines[0])
  215. err = alice.SendMessageToGroup(groupID, aliceLines[0])
  216. if err != nil {
  217. t.Fatalf("Alice failed to send a message to the group: %v", err)
  218. }
  219. time.Sleep(time.Second * 10)
  220. fmt.Println("Bob> ", bobLines[0])
  221. err = bob.SendMessageToGroup(groupID, bobLines[0])
  222. if err != nil {
  223. t.Fatalf("Bob failed to send a message to the group: %v", err)
  224. }
  225. time.Sleep(time.Second * 10)
  226. fmt.Println("Alice> ", aliceLines[1])
  227. alice.SendMessageToGroup(groupID, aliceLines[1])
  228. time.Sleep(time.Second * 10)
  229. fmt.Println("Bob> ", bobLines[1])
  230. bob.SendMessageToGroup(groupID, bobLines[1])
  231. time.Sleep(time.Second * 10)
  232. fmt.Println("Alice inviting Carol to group...")
  233. err = alice.InviteOnionToGroup(carol.GetProfile().Onion, groupID)
  234. if err != nil {
  235. t.Fatalf("Error for Alice inviting Carol to group: %v", err)
  236. }
  237. time.Sleep(time.Second * 10)
  238. fmt.Println("Carol examining groups and accepting invites...")
  239. for _, groupID := range carol.GetGroups() {
  240. group := carol.GetGroup(groupID)
  241. fmt.Printf("Carol group: %v (Accepted: %v)\n", group.GroupID, group.Accepted)
  242. if group.Accepted == false {
  243. fmt.Printf("Carol received and accepting group invite: %v\n", group.GroupID)
  244. carol.AcceptInvite(group.GroupID)
  245. }
  246. }
  247. fmt.Println("Shutting down Alice...")
  248. alice.Shutdown()
  249. aliceEventBus.Shutdown()
  250. time.Sleep(time.Second * 5)
  251. numGoRoutinesPostAlice := runtime.NumGoroutine()
  252. fmt.Println("Carol joining server...")
  253. carol.JoinServer(serverAddr)
  254. waitForPeerServerConnection(t, carol, serverAddr)
  255. numGoRotinesPostCarolConnect := runtime.NumGoroutine()
  256. fmt.Println("Bob> ", bobLines[2])
  257. bob.SendMessageToGroup(groupID, bobLines[2])
  258. time.Sleep(time.Second * 10)
  259. fmt.Println("Carol> ", carolLines[0])
  260. carol.SendMessageToGroup(groupID, carolLines[0])
  261. time.Sleep(time.Second * 10)
  262. // ***** Verify Test *****
  263. fmt.Println("Final syncing time...")
  264. time.Sleep(time.Second * 30)
  265. alicesGroup := alice.GetGroup(groupID)
  266. if alicesGroup == nil {
  267. t.Error("aliceGroup == nil")
  268. return
  269. }
  270. fmt.Printf("Alice's TimeLine:\n")
  271. aliceVerified := printAndCountVerifedTimeline(t, alicesGroup.GetTimeline())
  272. if aliceVerified != 4 {
  273. t.Errorf("Alice did not have 4 verified messages")
  274. }
  275. bobsGroup := bob.GetGroup(groupID)
  276. if bobsGroup == nil {
  277. t.Error("bobGroup == nil")
  278. return
  279. }
  280. fmt.Printf("Bob's TimeLine:\n")
  281. bobVerified := printAndCountVerifedTimeline(t, bobsGroup.GetTimeline())
  282. if bobVerified != 6 {
  283. t.Errorf("Bob did not have 5 verified messages")
  284. }
  285. carolsGroup := carol.GetGroup(groupID)
  286. fmt.Printf("Carol's TimeLine:\n")
  287. carolVerified := printAndCountVerifedTimeline(t, carolsGroup.GetTimeline())
  288. if carolVerified != 6 {
  289. t.Errorf("Carol did not have 3 verified messages")
  290. }
  291. if len(alicesGroup.GetTimeline()) != 4 {
  292. t.Errorf("Alice's timeline does not have all messages")
  293. } else {
  294. // check message 0,1,2,3
  295. aliceGroupTimeline := alicesGroup.GetTimeline()
  296. if aliceGroupTimeline[0].Message != aliceLines[0] || aliceGroupTimeline[1].Message != bobLines[0] ||
  297. aliceGroupTimeline[2].Message != aliceLines[1] || aliceGroupTimeline[3].Message != bobLines[1] {
  298. t.Errorf("Some of Alice's timeline messages did not have the expected content!")
  299. }
  300. }
  301. if len(bobsGroup.GetTimeline()) != 6 {
  302. t.Errorf("Bob's timeline does not have all messages")
  303. } else {
  304. // check message 0,1,2,3,4,5
  305. bobGroupTimeline := bobsGroup.GetTimeline()
  306. if bobGroupTimeline[0].Message != aliceLines[0] || bobGroupTimeline[1].Message != bobLines[0] ||
  307. bobGroupTimeline[2].Message != aliceLines[1] || bobGroupTimeline[3].Message != bobLines[1] ||
  308. bobGroupTimeline[4].Message != bobLines[2] || bobGroupTimeline[5].Message != carolLines[0] {
  309. t.Errorf("Some of Bob's timeline messages did not have the expected content!")
  310. }
  311. }
  312. if len(carolsGroup.GetTimeline()) != 6 {
  313. t.Errorf("Carol's timeline does not have all messages")
  314. } else {
  315. // check message 0,1,2,3,4,5
  316. carolGroupTimeline := carolsGroup.GetTimeline()
  317. if carolGroupTimeline[0].Message != aliceLines[0] || carolGroupTimeline[1].Message != bobLines[0] ||
  318. carolGroupTimeline[2].Message != aliceLines[1] || carolGroupTimeline[3].Message != bobLines[1] ||
  319. carolGroupTimeline[4].Message != bobLines[2] || carolGroupTimeline[5].Message != carolLines[0] {
  320. t.Errorf("Some of Carol's timeline messages did not have the expected content!")
  321. }
  322. }
  323. fmt.Println("Shutting down Bob...")
  324. bob.Shutdown()
  325. bobEventBus.Shutdown()
  326. time.Sleep(time.Second * 3)
  327. numGoRoutinesPostBob := runtime.NumGoroutine()
  328. if server != nil {
  329. fmt.Println("Shutting down server...")
  330. server.Shutdown()
  331. time.Sleep(time.Second * 3)
  332. }
  333. numGoRoutinesPostServerShutdown := runtime.NumGoroutine()
  334. fmt.Println("Shutting down Carol...")
  335. carol.Shutdown()
  336. carolEventBus.Shutdown()
  337. time.Sleep(time.Second * 3)
  338. numGoRoutinesPostCarol := runtime.NumGoroutine()
  339. fmt.Println("Shutting down ACN...")
  340. acn.Close()
  341. time.Sleep(time.Second * 2) // Server ^^ has a 5 second loop attempting reconnect before exiting
  342. numGoRoutinesPostACN := runtime.NumGoroutine()
  343. // Printing out the current goroutines
  344. // Very useful if we are leaking any.
  345. pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
  346. fmt.Printf("numGoRoutinesStart: %v\nnumGoRoutinesPostServer: %v\nnumGoRoutinesPostPeerStart: %v\nnumGoRoutinesPostPeerAndServerConnect: %v\n"+
  347. "numGoRoutinesPostAlice: %v\nnumGoRotinesPostCarolConnect: %v\nnumGoRoutinesPostBob: %v\nnumGoRoutinesPostServerShutdown: %v\nnumGoRoutinesPostCarol: %v\nnumGoRoutinesPostACN: %v\n",
  348. numGoRoutinesStart, numGoRoutinesPostServer, numGoRoutinesPostPeerStart, numGoRoutinesPostServerConnect,
  349. numGoRoutinesPostAlice, numGoRotinesPostCarolConnect, numGoRoutinesPostBob, numGoRoutinesPostServerShutdown, numGoRoutinesPostCarol, numGoRoutinesPostACN)
  350. if numGoRoutinesStart != numGoRoutinesPostACN {
  351. t.Errorf("Number of GoRoutines at start (%v) does not match number of goRoutines after cleanup of peers and servers (%v), clean up failed, leak detected!", numGoRoutinesStart, numGoRoutinesPostACN)
  352. }
  353. }