clientlib_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2018, 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 clientlib
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "io/ioutil"
  25. "os"
  26. "testing"
  27. "time"
  28. )
  29. var testDataDirName string
  30. func TestMain(m *testing.M) {
  31. flag.Parse()
  32. var err error
  33. testDataDirName, err = ioutil.TempDir("", "psiphon-clientlib-test")
  34. if err != nil {
  35. fmt.Printf("TempDir failed: %s\n", err)
  36. os.Exit(1)
  37. }
  38. defer os.RemoveAll(testDataDirName)
  39. os.Exit(m.Run())
  40. }
  41. func getConfigJSON(t *testing.T) []byte {
  42. configJSON, err := ioutil.ReadFile("../../psiphon/controller_test.config")
  43. if err != nil {
  44. // Skip, don't fail, if config file is not present
  45. t.Skipf("error loading configuration file: %s", err)
  46. }
  47. return configJSON
  48. }
  49. func TestStartTunnel(t *testing.T) {
  50. // TODO: More comprehensive tests. This is only a smoke test.
  51. configJSON := getConfigJSON(t)
  52. clientPlatform := "clientlib_test.go"
  53. networkID := "UNKNOWN"
  54. timeout := 60
  55. // Cancels the context after a duration. Pass 0 for no cancel.
  56. // (Note that cancelling causes an error, not a timeout.)
  57. contextGetter := func(cancelAfter time.Duration) func() context.Context {
  58. return func() context.Context {
  59. if cancelAfter == 0 {
  60. return context.Background()
  61. }
  62. ctx, ctxCancel := context.WithCancel(context.Background())
  63. go func() {
  64. time.Sleep(cancelAfter)
  65. ctxCancel()
  66. }()
  67. return ctx
  68. }
  69. }
  70. type args struct {
  71. ctxGetter func() context.Context
  72. configJSON []byte
  73. embeddedServerEntryList string
  74. params Parameters
  75. paramsDelta ParametersDelta
  76. noticeReceiver func(NoticeEvent)
  77. }
  78. tests := []struct {
  79. name string
  80. args args
  81. wantTunnel bool
  82. wantErr bool
  83. }{
  84. {
  85. name: "Success: simple",
  86. args: args{
  87. ctxGetter: contextGetter(0),
  88. configJSON: configJSON,
  89. embeddedServerEntryList: "",
  90. params: Parameters{
  91. DataRootDirectory: &testDataDirName,
  92. ClientPlatform: &clientPlatform,
  93. NetworkID: &networkID,
  94. EstablishTunnelTimeoutSeconds: &timeout,
  95. },
  96. paramsDelta: nil,
  97. noticeReceiver: nil,
  98. },
  99. wantTunnel: true,
  100. wantErr: false,
  101. },
  102. {
  103. name: "Failure: timeout",
  104. args: args{
  105. ctxGetter: contextGetter(10 * time.Millisecond),
  106. configJSON: configJSON,
  107. embeddedServerEntryList: "",
  108. params: Parameters{
  109. DataRootDirectory: &testDataDirName,
  110. ClientPlatform: &clientPlatform,
  111. NetworkID: &networkID,
  112. EstablishTunnelTimeoutSeconds: &timeout,
  113. },
  114. paramsDelta: nil,
  115. noticeReceiver: nil,
  116. },
  117. wantTunnel: false,
  118. wantErr: true,
  119. },
  120. }
  121. for _, tt := range tests {
  122. t.Run(tt.name, func(t *testing.T) {
  123. gotTunnel, err := StartTunnel(tt.args.ctxGetter(),
  124. tt.args.configJSON, tt.args.embeddedServerEntryList,
  125. tt.args.params, tt.args.paramsDelta, tt.args.noticeReceiver)
  126. if (err != nil) != tt.wantErr {
  127. t.Fatalf("StartTunnel() error = %v, wantErr %v", err, tt.wantErr)
  128. return
  129. }
  130. if (gotTunnel != nil) != tt.wantTunnel {
  131. t.Errorf("StartTunnel() gotTunnel = %v, wantTunnel %v", err, tt.wantTunnel)
  132. }
  133. if gotTunnel != nil {
  134. gotTunnel.Stop()
  135. }
  136. })
  137. }
  138. }