feedback_test.go 2.6 KB

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