feedback_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Unmarshal config, configure data root directory and re-marshal
  46. var config map[string]interface{}
  47. err = json.Unmarshal(configFileContents, &config)
  48. if err != nil {
  49. t.Fatalf("Unmarshal failed: %s", err)
  50. }
  51. testDataDirName, err := ioutil.TempDir("", "psiphon-feedback-test")
  52. if err != nil {
  53. t.Fatalf("TempDir failed: %s", err)
  54. }
  55. config["DataRootDirectory"] = testDataDirName
  56. configFileContents, err = json.Marshal(config)
  57. if err != nil {
  58. t.Fatalf("Marshal failed: %s", err)
  59. }
  60. // git_rev is a file which contains the shortened hash of the latest commit
  61. // pointed to by HEAD, i.e. git rev-parse --short HEAD.
  62. shortRevHash, err := ioutil.ReadFile("git_rev")
  63. if err != nil {
  64. // Skip, don't fail, if git rev file is not present
  65. t.Skipf("error loading git revision file: %s", err)
  66. }
  67. // Construct feedback data which can be verified later
  68. diagnostics := Diagnostics{}
  69. diagnostics.Feedback.Message.Text = "Test feedback from feedback_test.go, revision: " + string(shortRevHash)
  70. diagnostics.Metadata.Id = "0000000000000000"
  71. diagnostics.Metadata.Platform = "android"
  72. diagnostics.Metadata.Version = 4
  73. diagnosticData, err := json.Marshal(diagnostics)
  74. if err != nil {
  75. t.Fatalf("Marshal failed: %s", err)
  76. }
  77. err = SendFeedback(context.Background(), string(configFileContents), string(diagnosticData), "")
  78. if err != nil {
  79. t.Fatalf("SendFeedback failed: %s", err)
  80. }
  81. }