1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package auth
import (
"crypto/sha256"
"encoding/hex"
"testing"
)
func hashHex(token string) string {
h := sha256.Sum256([]byte(token))
return hex.EncodeToString(h[:])
}
func TestPATVerify(t *testing.T) {
const user = "u@d.com"
file := "# my tokens\n" +
hashHex("goodtoken") + " laptop\n" +
hashHex("second") + "\n" +
"garbage line that is not hex\n"
st := writeTree(t, map[string]string{
user + "/gitweb/tokens": file,
})
v := NewPATVerifier(st)
cases := []struct {
token string
want bool
}{
{"goodtoken", true},
{"second", true},
{"wrong", false},
{"", false},
}
for _, c := range cases {
got, err := v.Verify(user, c.token)
if err != nil {
t.Fatalf("Verify(%q): %v", c.token, err)
}
if got != c.want {
t.Errorf("Verify(%q) = %v, want %v", c.token, got, c.want)
}
}
}
func TestPATVerifyNoFile(t *testing.T) {
st := writeTree(t, map[string]string{
"u@d.com/other": "x", // no tokens file
})
v := NewPATVerifier(st)
got, err := v.Verify("u@d.com", "anything")
if err != nil {
t.Fatalf("Verify: %v", err)
}
if got {
t.Error("Verify with no tokens file = true, want false")
}
}
func TestParseTokenFileSkipsMalformed(t *testing.T) {
data := []byte("#c\n\n \nnothex\n" + hashHex("x") + " label\n" +
"abcd\n") // too short
got := parseTokenFile(data)
if len(got) != 1 {
t.Fatalf("parsed %d hashes, want 1", len(got))
}
}
|