feedback_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2016, 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/exec"
  25. "testing"
  26. )
  27. type Diagnostics struct {
  28. Feedback struct {
  29. Message struct {
  30. Text string `json:"text"`
  31. }
  32. Email string `json:"email"`
  33. }
  34. Metadata struct {
  35. Id string `json:"id"`
  36. Platform string `json:"platform"`
  37. Version int `json:"version"`
  38. }
  39. }
  40. func TestFeedbackUpload(t *testing.T) {
  41. configFileContents, err := ioutil.ReadFile("controller_test.config")
  42. if err != nil {
  43. // Skip, don't fail, if config file is not present
  44. t.Skipf("error loading configuration file: %s", err)
  45. }
  46. // Load config, configure data root directory and commit it
  47. config, err := LoadConfig(configFileContents)
  48. if err != nil {
  49. t.Fatalf("error loading configuration file: %s", err)
  50. }
  51. if !config.EnableFeedbackUpload {
  52. config.EnableFeedbackUpload = true
  53. }
  54. if config.ClientPlatform == "" {
  55. config.ClientPlatform = testClientPlatform
  56. }
  57. testDataDirName, err := ioutil.TempDir("", "psiphon-feedback-test")
  58. if err != nil {
  59. t.Fatalf("TempDir failed: %s", err)
  60. }
  61. config.DataRootDirectory = testDataDirName
  62. err = config.Commit(true)
  63. if err != nil {
  64. t.Fatalf("error committing configuration file: %s", err)
  65. }
  66. shortRevHash, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
  67. if err != nil {
  68. // Log, don't fail, if git rev is not available
  69. t.Logf("error loading git revision file: %s", err)
  70. shortRevHash = []byte("unknown")
  71. }
  72. // Construct feedback data which can be verified later
  73. diagnostics := Diagnostics{}
  74. diagnostics.Feedback.Message.Text = "Test feedback from feedback_test.go, revision: " + string(shortRevHash)
  75. diagnostics.Metadata.Id = "0000000000000000"
  76. diagnostics.Metadata.Platform = "android"
  77. diagnostics.Metadata.Version = 4
  78. diagnosticData, err := json.Marshal(diagnostics)
  79. if err != nil {
  80. t.Fatalf("Marshal failed: %s", err)
  81. }
  82. err = SendFeedback(context.Background(), config, string(diagnosticData), "")
  83. if err != nil {
  84. t.Fatalf("SendFeedback failed: %s", err)
  85. }
  86. }