tactics_test.go 3.1 KB

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