tactics_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "os"
  25. "sync/atomic"
  26. "testing"
  27. "time"
  28. )
  29. func TestStandAloneGetTactics(t *testing.T) {
  30. testDataDirName, err := ioutil.TempDir("", "psiphon-tactics-test")
  31. if err != nil {
  32. t.Fatalf("TempDir failed: %s\n", err)
  33. }
  34. defer os.RemoveAll(testDataDirName)
  35. configJSON, err := ioutil.ReadFile("controller_test.config")
  36. if err != nil {
  37. // Skip, don't fail, if config file is not present
  38. t.Skipf("error loading configuration file: %s", err)
  39. }
  40. var modifyConfig map[string]interface{}
  41. json.Unmarshal(configJSON, &modifyConfig)
  42. modifyConfig["DataRootDirectory"] = testDataDirName
  43. configJSON, _ = json.Marshal(modifyConfig)
  44. config, err := LoadConfig(configJSON)
  45. if err != nil {
  46. t.Fatalf("error processing configuration file: %s", err)
  47. }
  48. err = config.Commit(false)
  49. if err != nil {
  50. t.Fatalf("error committing configuration file: %s", err)
  51. }
  52. gotTactics := int32(0)
  53. SetNoticeWriter(NewNoticeReceiver(
  54. func(notice []byte) {
  55. noticeType, _, err := GetNotice(notice)
  56. if err != nil {
  57. return
  58. }
  59. switch noticeType {
  60. case "RequestedTactics":
  61. atomic.StoreInt32(&gotTactics, 1)
  62. }
  63. }))
  64. ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
  65. defer cancelFunc()
  66. err = OpenDataStore(config)
  67. if err != nil {
  68. t.Fatalf("error committing initializing datastore: %s", err)
  69. }
  70. untunneledDialConfig := &DialConfig{
  71. UpstreamProxyURL: config.UpstreamProxyURL,
  72. }
  73. err = FetchCommonRemoteServerList(ctx, config, 0, nil, untunneledDialConfig)
  74. if err != nil {
  75. t.Fatalf("error cfetching remote server list: %s", err)
  76. }
  77. // Close the datastore to exercise the OpenDatastore/CloseDatastore
  78. // operations in GetTactics.
  79. CloseDataStore()
  80. GetTactics(ctx, config)
  81. if atomic.LoadInt32(&gotTactics) != 1 {
  82. t.Fatalf("failed to get tactics")
  83. }
  84. }