github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/git-codereview/api_test.go (about)

     1  // Copyright 2014 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  var authTests = []struct {
    13  	netrc       string
    14  	cookiefile  string
    15  	user        string
    16  	password    string
    17  	cookieName  string
    18  	cookieValue string
    19  	died        bool
    20  }{
    21  	{
    22  		died: true,
    23  	},
    24  	{
    25  		netrc:    "machine go.googlesource.com login u1 password pw\n",
    26  		user:     "u1",
    27  		password: "pw",
    28  	},
    29  	{
    30  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o2	git-u2=pw\n",
    31  		cookieName:  "o2",
    32  		cookieValue: "git-u2=pw",
    33  	},
    34  	{
    35  		cookiefile: ".googlesource.com	TRUE	/	TRUE	2147483647	o3	git-u3=pw\n",
    36  		cookieName:  "o3",
    37  		cookieValue: "git-u3=pw",
    38  	},
    39  	{
    40  		cookiefile: ".googlesource.com	TRUE	/	TRUE	2147483647	o4	WRONG\n" +
    41  			"go.googlesource.com	TRUE	/	TRUE	2147483647	o4	git-u4=pw\n",
    42  		cookieName:  "o4",
    43  		cookieValue: "git-u4=pw",
    44  	},
    45  	{
    46  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o5	git-u5=pw\n" +
    47  			".googlesource.com	TRUE	/	TRUE	2147483647	o5	WRONG\n",
    48  		cookieName:  "o5",
    49  		cookieValue: "git-u5=pw",
    50  	},
    51  	{
    52  		netrc:      "machine go.googlesource.com login u6 password pw\n",
    53  		cookiefile: "BOGUS",
    54  		user:       "u6",
    55  		password:   "pw",
    56  	},
    57  	{
    58  		netrc: "BOGUS",
    59  		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o7	git-u7=pw\n",
    60  		cookieName:  "o7",
    61  		cookieValue: "git-u7=pw",
    62  	},
    63  	{
    64  		netrc:      "machine go.googlesource.com login u8 password pw\n",
    65  		cookiefile: "MISSING",
    66  		user:       "u8",
    67  		password:   "pw",
    68  	},
    69  }
    70  
    71  func TestLoadAuth(t *testing.T) {
    72  	gt := newGitTest(t)
    73  	defer gt.done()
    74  	gt.work(t)
    75  
    76  	defer os.Setenv("HOME", os.Getenv("HOME"))
    77  	os.Setenv("HOME", gt.client)
    78  	trun(t, gt.client, "git", "config", "remote.origin.url", "https://go.googlesource.com/go")
    79  
    80  	for i, tt := range authTests {
    81  		t.Logf("#%d", i)
    82  		auth.user = ""
    83  		auth.password = ""
    84  		auth.cookieName = ""
    85  		auth.cookieValue = ""
    86  		trun(t, gt.client, "git", "config", "http.cookiefile", "XXX")
    87  		trun(t, gt.client, "git", "config", "--unset", "http.cookiefile")
    88  
    89  		remove(t, gt.client+"/.netrc")
    90  		remove(t, gt.client+"/.cookies")
    91  		if tt.netrc != "" {
    92  			write(t, gt.client+"/.netrc", tt.netrc)
    93  		}
    94  		if tt.cookiefile != "" {
    95  			if tt.cookiefile != "MISSING" {
    96  				write(t, gt.client+"/.cookies", tt.cookiefile)
    97  			}
    98  			trun(t, gt.client, "git", "config", "http.cookiefile", gt.client+"/.cookies")
    99  		}
   100  
   101  		// Run command via testMain to trap stdout, stderr, death.
   102  		// mail -n will load auth info for us.
   103  		if tt.died {
   104  			testMainDied(t, "test-loadAuth")
   105  		} else {
   106  			testMain(t, "test-loadAuth")
   107  		}
   108  
   109  		if auth.user != tt.user || auth.password != tt.password {
   110  			t.Errorf("#%d: have user, password = %q, %q, want %q, %q", i, auth.user, auth.password, tt.user, tt.password)
   111  		}
   112  		if auth.cookieName != tt.cookieName || auth.cookieValue != tt.cookieValue {
   113  			t.Errorf("#%d: have cookie name, value = %q, %q, want %q, %q", i, auth.cookieName, auth.cookieValue, tt.cookieName, tt.cookieValue)
   114  		}
   115  	}
   116  }