tactics_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2020, 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. "context"
  22. "encoding/json"
  23. "io/ioutil"
  24. "net"
  25. "os"
  26. "sync/atomic"
  27. "testing"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  31. )
  32. func TestStandAloneGetTactics(t *testing.T) {
  33. testDataDirName, err := ioutil.TempDir("", "psiphon-tactics-test")
  34. if err != nil {
  35. t.Fatalf("TempDir failed: %s\n", err)
  36. }
  37. defer os.RemoveAll(testDataDirName)
  38. configJSON, err := ioutil.ReadFile("controller_test.config")
  39. if err != nil {
  40. // Skip, don't fail, if config file is not present
  41. t.Skipf("error loading configuration file: %s", err)
  42. }
  43. var modifyConfig map[string]interface{}
  44. json.Unmarshal(configJSON, &modifyConfig)
  45. modifyConfig["DataRootDirectory"] = testDataDirName
  46. configJSON, _ = json.Marshal(modifyConfig)
  47. config, err := LoadConfig(configJSON)
  48. if err != nil {
  49. t.Fatalf("error processing configuration file: %s", err)
  50. }
  51. if config.ClientPlatform == "" {
  52. config.ClientPlatform = testClientPlatform
  53. }
  54. // Use the legacy encoding to both exercise that case, and facilitate a
  55. // gradual network upgrade to new encoding support.
  56. config.TargetAPIEncoding = protocol.PSIPHON_API_ENCODING_JSON
  57. err = config.Commit(false)
  58. if err != nil {
  59. t.Fatalf("error committing configuration file: %s", err)
  60. }
  61. resolver := NewResolver(config, true)
  62. defer resolver.Stop()
  63. config.SetResolver(resolver)
  64. gotTactics := int32(0)
  65. SetNoticeWriter(NewNoticeReceiver(
  66. func(notice []byte) {
  67. noticeType, _, err := GetNotice(notice)
  68. if err != nil {
  69. return
  70. }
  71. switch noticeType {
  72. case "RequestedTactics":
  73. atomic.StoreInt32(&gotTactics, 1)
  74. }
  75. }))
  76. ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  77. defer cancelFunc()
  78. err = OpenDataStore(config)
  79. if err != nil {
  80. t.Fatalf("error committing initializing datastore: %s", err)
  81. }
  82. untunneledDialConfig := &DialConfig{
  83. ResolveIP: func(ctx context.Context, hostname string) ([]net.IP, error) {
  84. IPs, err := UntunneledResolveIP(
  85. ctx, config, resolver, hostname, "")
  86. if err != nil {
  87. return nil, errors.Trace(err)
  88. }
  89. return IPs, nil
  90. },
  91. UpstreamProxyURL: config.UpstreamProxyURL,
  92. }
  93. err = FetchCommonRemoteServerList(ctx, config, 0, nil, untunneledDialConfig)
  94. if err != nil {
  95. t.Fatalf("error fetching remote server list: %s", err)
  96. }
  97. // Close the datastore to exercise the OpenDatastore/CloseDatastore
  98. // operations in GetTactics.
  99. CloseDataStore()
  100. GetTactics(ctx, config)
  101. if atomic.LoadInt32(&gotTactics) != 1 {
  102. t.Fatalf("failed to get tactics")
  103. }
  104. }