github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/feedback_test.go (about)

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