tactics_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. utls "github.com/Psiphon-Labs/utls"
  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. err = json.Unmarshal(configJSON, &modifyConfig)
  45. if err != nil {
  46. t.Fatalf("json.Unmarshal failed: %v", err)
  47. }
  48. modifyConfig["DataRootDirectory"] = testDataDirName
  49. configJSON, _ = json.Marshal(modifyConfig)
  50. config, err := LoadConfig(configJSON)
  51. if err != nil {
  52. t.Fatalf("error processing configuration file: %s", err)
  53. }
  54. if config.ClientPlatform == "" {
  55. config.ClientPlatform = testClientPlatform
  56. }
  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. err = 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. if err != nil {
  77. t.Fatalf("error setting notice writer: %s", err)
  78. }
  79. defer ResetNoticeWriter()
  80. ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  81. defer cancelFunc()
  82. err = OpenDataStore(config)
  83. if err != nil {
  84. t.Fatalf("error committing initializing datastore: %s", err)
  85. }
  86. untunneledDialConfig := &DialConfig{
  87. ResolveIP: func(ctx context.Context, hostname string) ([]net.IP, error) {
  88. IPs, err := UntunneledResolveIP(
  89. ctx, config, resolver, hostname, "")
  90. if err != nil {
  91. return nil, errors.Trace(err)
  92. }
  93. return IPs, nil
  94. },
  95. UpstreamProxyURL: config.UpstreamProxyURL,
  96. }
  97. tlsCache := utls.NewLRUClientSessionCache(0)
  98. err = FetchCommonRemoteServerList(ctx, config, 0, nil, untunneledDialConfig, tlsCache)
  99. if err != nil {
  100. t.Fatalf("error fetching remote server list: %s", err)
  101. }
  102. // Close the datastore to exercise the OpenDatastore/CloseDatastore
  103. // operations in GetTactics.
  104. CloseDataStore()
  105. GetTactics(ctx, config, true)
  106. if atomic.LoadInt32(&gotTactics) != 1 {
  107. t.Fatalf("failed to get tactics")
  108. }
  109. }