| 1 |
package launcher |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"reflect" |
| 8 |
|
| 9 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"net/http" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/google/go-cmp/cmp" |
| 18 |
"github.com/google/go-tpm-tools/client" |
| 19 |
"github.com/google/go-tpm-tools/internal/test" |
| 20 |
"google.golang.org/api/option" |
| 21 |
) |
| 22 |
|
| 23 |
var expectedEmail = "[email protected]" |
| 24 |
var expectedToken = []byte("test_token") |
| 25 |
var expectedURL = fmt.Sprintf(idTokenEndpoint, expectedEmail) |
| 26 |
|
| 27 |
var testClient = &http.Client{ |
| 28 |
Transport: &testRoundTripper{ |
| 29 |
roundTripFunc: func(req *http.Request) *http.Response { |
| 30 |
if req.URL.String() != expectedURL { |
| 31 |
return &http.Response{ |
| 32 |
StatusCode: http.StatusNotFound, |
| 33 |
} |
| 34 |
} |
| 35 |
resp := idTokenResp{ |
| 36 |
Token: string(expectedToken), |
| 37 |
} |
| 38 |
respBody, err := json.Marshal(resp) |
| 39 |
if err != nil { |
| 40 |
return &http.Response{ |
| 41 |
StatusCode: http.StatusInternalServerError, |
| 42 |
} |
| 43 |
} |
| 44 |
return &http.Response{ |
| 45 |
StatusCode: http.StatusOK, |
| 46 |
Header: make(http.Header), |
| 47 |
Body: io.NopCloser(bytes.NewBuffer(respBody)), |
| 48 |
} |
| 49 |
}, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
func TestTPMDAOps(t *testing.T) { |
| 54 |
rwc := test.GetTPM(t) |
| 55 |
defer client.CheckedClose(t, rwc) |
| 56 |
|
| 57 |
daInfo, err := GetTPMDAInfo(rwc) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
|
| 62 |
// default simualator TPM params |
| 63 |
expectedDaInfo := TPMDAParams{0, 3, 1000, 1000, true} |
| 64 |
if !cmp.Equal(*daInfo, expectedDaInfo) { |
| 65 |
t.Errorf("expected default DA parameters, got %+v, want %+v", daInfo, expectedDaInfo) |
| 66 |
} |
| 67 |
|
| 68 |
err = SetTPMDAParams(rwc, TPMDAParams{MaxTries: 123, RecoveryTime: 456, LockoutRecovery: 789}) |
| 69 |
if err != nil { |
| 70 |
t.Fatal(err) |
| 71 |
} |
| 72 |
|
| 73 |
daInfo, err = GetTPMDAInfo(rwc) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
expectedDaInfo = TPMDAParams{0 /*LockoutCounter*/, 123 /*MaxTries*/, 456 /*RecoveryTime*/, 789 /*LockoutRecovery*/, true} |
| 78 |
if !cmp.Equal(*daInfo, expectedDaInfo) { |
| 79 |
t.Errorf("expected default DA parameters, got %+v, want %+v", daInfo, expectedDaInfo) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestFetchImpersonatedToken(t *testing.T) { |
| 84 |
token, err := FetchImpersonatedToken(context.Background(), expectedEmail, "test_aud", option.WithHTTPClient(testClient)) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("fetchImpersonatedToken returned error: %v", err) |
| 87 |
} |
| 88 |
|
| 89 |
if !bytes.Equal(token, expectedToken) { |
| 90 |
t.Errorf("fetchImpersonatedToken did not return expected token: got %v, want %v", token, expectedToken) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestFetchImpersonatedTokenNilAud(t *testing.T) { |
| 95 |
_, err := FetchImpersonatedToken(context.Background(), expectedEmail, "", option.WithHTTPClient(testClient)) |
| 96 |
if err == nil || !strings.Contains(err.Error(), "audience") { |
| 97 |
t.Fatalf("got %v error, want audience error", err) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestFetchImpersonatedTokenBadEmail(t *testing.T) { |
| 102 |
_, err := FetchImpersonatedToken(context.Background(), "", "test_aud", option.WithHTTPClient(testClient)) |
| 103 |
if err == nil || strings.Contains(err.Error(), "audience") { |
| 104 |
t.Fatalf("got %v error, want creating token source error", err) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
type testRoundTripper struct { |
| 109 |
roundTripFunc func(*http.Request) *http.Response |
| 110 |
} |
| 111 |
|
| 112 |
func (t *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 113 |
return t.roundTripFunc(req), nil |
| 114 |
} |
| 115 |
|
| 116 |
type idTokenResp struct { |
| 117 |
Token string `json:"token"` |
| 118 |
} |
| 119 |
|
| 120 |
func TestListFilesWithPrefix(t *testing.T) { |
| 121 |
|
| 122 |
tmpDir := t.TempDir() |
| 123 |
file1 := filepath.Join(tmpDir, "file1.txt") |
| 124 |
file2 := filepath.Join(tmpDir, "file2.txt") |
| 125 |
file3 := filepath.Join(tmpDir, "dir", "file3.txt") |
| 126 |
|
| 127 |
var testCases = []struct { |
| 128 |
dir string |
| 129 |
pattern string |
| 130 |
want []string |
| 131 |
wantErr bool |
| 132 |
}{ |
| 133 |
{ |
| 134 |
dir: tmpDir, |
| 135 |
pattern: "file", |
| 136 |
want: []string{file1, file2}, |
| 137 |
wantErr: false, |
| 138 |
}, |
| 139 |
{ |
| 140 |
dir: filepath.Join(tmpDir, "dir"), |
| 141 |
pattern: "file", |
| 142 |
want: []string{file3}, |
| 143 |
wantErr: false, |
| 144 |
}, |
| 145 |
{ |
| 146 |
dir: tmpDir, |
| 147 |
pattern: "newfile", |
| 148 |
wantErr: false, |
| 149 |
}, |
| 150 |
{ |
| 151 |
dir: "otherdir", |
| 152 |
pattern: "file", |
| 153 |
want: nil, |
| 154 |
wantErr: true, |
| 155 |
}, |
| 156 |
{ |
| 157 |
dir: "otherdir", |
| 158 |
pattern: "tmpfile", |
| 159 |
want: nil, |
| 160 |
wantErr: true, |
| 161 |
}, |
| 162 |
} |
| 163 |
|
| 164 |
// Create test files |
| 165 |
os.WriteFile(file1, []byte("File 1 content"), 0644) |
| 166 |
os.WriteFile(file2, []byte("File 2 content"), 0644) |
| 167 |
os.MkdirAll(filepath.Dir(file3), 0755) |
| 168 |
os.WriteFile(file3, []byte("File 3 content"), 0644) |
| 169 |
|
| 170 |
for _, tc := range testCases { |
| 171 |
t.Run(fmt.Sprintf("Dir: %s, Pattern: %s", tc.dir, tc.pattern), func(t *testing.T) { |
| 172 |
got, err := listFilesWithPrefix(tc.dir, tc.pattern) |
| 173 |
if (err != nil) != tc.wantErr { |
| 174 |
t.Errorf("listFilesWithPrefix(%s, %s): got error: %v, want error: %v", tc.dir, tc.pattern, err, tc.wantErr) |
| 175 |
return |
| 176 |
} |
| 177 |
if !tc.wantErr && !reflect.DeepEqual(got, tc.want) { |
| 178 |
t.Errorf("listFilesWithPrefix(%s, %s): got: %v, want: %v", tc.dir, tc.pattern, got, tc.want) |
| 179 |
} |
| 180 |
}) |
| 181 |
} |
| 182 |
} |