controller_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "io/ioutil"
  22. "sync"
  23. "testing"
  24. "time"
  25. )
  26. func TestControllerRunSSH(t *testing.T) {
  27. controllerRun(t, TUNNEL_PROTOCOL_SSH)
  28. }
  29. func TestControllerRunObfuscatedSSH(t *testing.T) {
  30. controllerRun(t, TUNNEL_PROTOCOL_OBFUSCATED_SSH)
  31. }
  32. func TestControllerRunUnfrontedMeek(t *testing.T) {
  33. controllerRun(t, TUNNEL_PROTOCOL_UNFRONTED_MEEK)
  34. }
  35. func TestControllerRunFrontedMeek(t *testing.T) {
  36. controllerRun(t, TUNNEL_PROTOCOL_FRONTED_MEEK)
  37. }
  38. func controllerRun(t *testing.T, protocol string) {
  39. configFileContents, err := ioutil.ReadFile("controller_test.config")
  40. if err != nil {
  41. // Skip, don't fail, if config file is not present
  42. t.Skipf("error loading configuration file: %s", err)
  43. }
  44. config, err := LoadConfig(configFileContents)
  45. if err != nil {
  46. t.Errorf("error processing configuration file: %s", err)
  47. t.FailNow()
  48. }
  49. config.TunnelProtocol = protocol
  50. err = InitDataStore(config)
  51. if err != nil {
  52. t.Errorf("error initializing datastore: %s", err)
  53. t.FailNow()
  54. }
  55. controller, err := NewController(config)
  56. if err != nil {
  57. t.Errorf("error creating controller: %s", err)
  58. t.FailNow()
  59. }
  60. // Monitor notices for "Tunnels" with count > 1, the
  61. // indication of tunnel establishment success
  62. tunnelEstablished := make(chan struct{}, 1)
  63. SetNoticeOutput(NewNoticeReceiver(
  64. func(notice []byte) {
  65. // TODO: log notices without logging server IPs:
  66. // fmt.Fprintf(os.Stderr, "%s\n", string(notice))
  67. count, ok := GetNoticeTunnels(notice)
  68. if ok && count > 0 {
  69. select {
  70. case tunnelEstablished <- *new(struct{}):
  71. default:
  72. }
  73. }
  74. }))
  75. // Run controller, which establishes tunnels
  76. shutdownBroadcast := make(chan struct{})
  77. controllerWaitGroup := new(sync.WaitGroup)
  78. controllerWaitGroup.Add(1)
  79. go func() {
  80. defer controllerWaitGroup.Done()
  81. controller.Run(shutdownBroadcast)
  82. }()
  83. // Test: tunnel must be established within 60 seconds
  84. establishTimeout := time.NewTimer(60 * time.Second)
  85. select {
  86. case <-tunnelEstablished:
  87. case <-establishTimeout.C:
  88. t.Errorf("tunnel establish timeout exceeded")
  89. }
  90. close(shutdownBroadcast)
  91. // Test: shutdown must complete within 10 seconds
  92. shutdownTimeout := time.NewTimer(10 * time.Second)
  93. shutdownOk := make(chan struct{}, 1)
  94. go func() {
  95. controllerWaitGroup.Wait()
  96. shutdownOk <- *new(struct{})
  97. }()
  98. select {
  99. case <-shutdownOk:
  100. case <-shutdownTimeout.C:
  101. t.Errorf("controller shutdown timeout exceeded")
  102. }
  103. }