tactics_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. json.Unmarshal(configJSON, &modifyConfig)
  44. modifyConfig["DataRootDirectory"] = testDataDirName
  45. configJSON, _ = json.Marshal(modifyConfig)
  46. config, err := LoadConfig(configJSON)
  47. if err != nil {
  48. t.Fatalf("error processing configuration file: %s", err)
  49. }
  50. if config.ClientPlatform == "" {
  51. config.ClientPlatform = testClientPlatform
  52. }
  53. err = config.Commit(false)
  54. if err != nil {
  55. t.Fatalf("error committing configuration file: %s", err)
  56. }
  57. resolver := NewResolver(config, true)
  58. defer resolver.Stop()
  59. config.SetResolver(resolver)
  60. gotTactics := int32(0)
  61. SetNoticeWriter(NewNoticeReceiver(
  62. func(notice []byte) {
  63. noticeType, _, err := GetNotice(notice)
  64. if err != nil {
  65. return
  66. }
  67. switch noticeType {
  68. case "RequestedTactics":
  69. atomic.StoreInt32(&gotTactics, 1)
  70. }
  71. }))
  72. ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  73. defer cancelFunc()
  74. err = OpenDataStore(config)
  75. if err != nil {
  76. t.Fatalf("error committing initializing datastore: %s", err)
  77. }
  78. untunneledDialConfig := &DialConfig{
  79. ResolveIP: func(ctx context.Context, hostname string) ([]net.IP, error) {
  80. IPs, err := UntunneledResolveIP(
  81. ctx, config, resolver, hostname)
  82. if err != nil {
  83. return nil, errors.Trace(err)
  84. }
  85. return IPs, nil
  86. },
  87. UpstreamProxyURL: config.UpstreamProxyURL,
  88. }
  89. err = FetchCommonRemoteServerList(ctx, config, 0, nil, untunneledDialConfig)
  90. if err != nil {
  91. t.Fatalf("error fetching remote server list: %s", err)
  92. }
  93. // Close the datastore to exercise the OpenDatastore/CloseDatastore
  94. // operations in GetTactics.
  95. CloseDataStore()
  96. GetTactics(ctx, config)
  97. if atomic.LoadInt32(&gotTactics) != 1 {
  98. t.Fatalf("failed to get tactics")
  99. }
  100. }