Tapir provides a framework for building Anonymous / metadata resistant Services
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.

85 lines
2.6 KiB

  1. package testing
  2. import (
  3. "cwtch.im/tapir"
  4. "cwtch.im/tapir/applications"
  5. "cwtch.im/tapir/networks/tor"
  6. "cwtch.im/tapir/primitives"
  7. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  8. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/utils"
  10. "golang.org/x/crypto/ed25519"
  11. "runtime"
  12. "sync"
  13. "testing"
  14. "time"
  15. )
  16. func TestTapirMaliciousRemote(t *testing.T) {
  17. numRoutinesStart := runtime.NumGoroutine()
  18. log.SetLevel(log.LevelDebug)
  19. log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
  20. // Connect to Tor
  21. var acn connectivity.ACN
  22. acn, _ = connectivity.StartTor("./", "")
  23. acn.WaitTillBootstrapped()
  24. // Generate Server Keys, not we generate two sets
  25. id, _ := primitives.InitializeEphemeralIdentity()
  26. id2, sk2 := primitives.InitializeEphemeralIdentity()
  27. // Init the Server running the Simple App.
  28. var service tapir.Service
  29. service = new(tor.BaseOnionService)
  30. // Initialize an onion service with one identity, but the auth app with another, this should
  31. // trigger a failure in authentication protocol
  32. service.Init(acn, sk2, &id)
  33. // Goroutine Management
  34. sg := new(sync.WaitGroup)
  35. sg.Add(1)
  36. go func() {
  37. service.Listen(new(applications.AuthApp))
  38. sg.Done()
  39. }()
  40. // Wait for server to come online
  41. time.Sleep(time.Second * 30)
  42. wg := new(sync.WaitGroup)
  43. wg.Add(1)
  44. // Init a Client to Connect to the Server
  45. log.Infof("initializing the client....")
  46. client, _ := genclient(acn)
  47. go connectclientandfail(client, id2.PublicKey(), wg, t)
  48. wg.Wait()
  49. // Wait for Server to Sync
  50. time.Sleep(time.Second * 2)
  51. log.Infof("closing ACN...")
  52. acn.Close()
  53. sg.Wait()
  54. time.Sleep(time.Second * 2)
  55. log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
  56. if numRoutinesStart != runtime.NumGoroutine() {
  57. t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
  58. }
  59. }
  60. // Client will Connect and launch it's own Echo App goroutine.
  61. func connectclientandfail(client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup, t *testing.T) {
  62. client.Connect(utils.GetTorV3Hostname(key), new(applications.AuthApp))
  63. // Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
  64. // we will wait a little while then exit.
  65. time.Sleep(time.Second * 5)
  66. log.Infof("Checking connection status...")
  67. conn, err := client.GetConnection(utils.GetTorV3Hostname(key))
  68. if err == nil {
  69. group.Done()
  70. t.Fatalf("Connection should have failed! %v %v", conn, err)
  71. }
  72. log.Infof("Successfully failed to authenticate...")
  73. group.Done()
  74. }