1
2
3
4
5 package auth
6
7 import (
8 "strings"
9 "testing"
10 )
11
12 func TestParseGitAuth(t *testing.T) {
13 testCases := []struct {
14 gitauth string
15 wantPrefix string
16 wantUsername string
17 wantPassword string
18 }{
19 {
20 gitauth: `
21 protocol=https
22 host=example.com
23 username=bob
24 password=secr3t
25 `,
26 wantPrefix: "https://example.com",
27 wantUsername: "bob",
28 wantPassword: "secr3t",
29 },
30 {
31 gitauth: `
32 protocol=https
33 host=example.com
34 username=bob
35 password=secr3t
36 url=invalid
37 `,
38 wantPrefix: "https://example.com",
39 wantUsername: "bob",
40 wantPassword: "secr3t",
41 },
42 {
43 gitauth: `
44 protocol=https
45 host=example.com
46 username=bob
47 password=secr3t
48 url=https://go.dev
49 `,
50 wantPrefix: "https://go.dev",
51 wantUsername: "bob",
52 wantPassword: "secr3t",
53 },
54 {
55 gitauth: `
56 `,
57 wantPrefix: "",
58 wantUsername: "",
59 wantPassword: "",
60 },
61 {
62 gitauth: `
63 protocol:https
64 host:example.com
65 username:bob
66 password:secr3t
67 `,
68 wantPrefix: "",
69 wantUsername: "",
70 wantPassword: "",
71 },
72 }
73 for _, tc := range testCases {
74 parsedPrefix, username, password := parseGitAuth([]byte(tc.gitauth))
75 if parsedPrefix != tc.wantPrefix {
76 t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, parsedPrefix, tc.wantPrefix)
77 }
78 if username != tc.wantUsername {
79 t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, username, tc.wantUsername)
80 }
81 if password != tc.wantPassword {
82 t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, password, tc.wantPassword)
83 }
84 }
85 }
86
87 func BenchmarkParseGitAuth(b *testing.B) {
88
89 testCases := []struct {
90 name string
91 data []byte
92 }{{
93
94 name: "standard",
95 data: []byte(`
96 protocol=https
97 host=example.com
98 username=bob
99 password=secr3t
100 `),
101 }, {
102
103 name: "with_url",
104 data: []byte(`
105 protocol=https
106 host=example.com
107 username=bob
108 password=secr3t
109 url=https://example.com/repo
110 `),
111 }, {
112
113 name: "minimal",
114 data: []byte(`
115 protocol=https
116 host=example.com
117 `),
118 }, {
119
120 name: "complex",
121 data: func() []byte {
122 var builder strings.Builder
123 builder.WriteString("protocol=https\n")
124 builder.WriteString("host=example.com\n")
125 builder.WriteString("username=longusernamenamename\n")
126 builder.WriteString("password=longpasswordwithmanycharacters123456789\n")
127 builder.WriteString("url=https://example.com/very/long/path/to/repository\n")
128 builder.WriteString("extra1=value1\n")
129 builder.WriteString("extra2=value2\n")
130 return []byte(builder.String())
131 }(),
132 }, {
133
134 name: "empty",
135 data: []byte(``),
136 }, {
137
138 name: "malformed",
139 data: []byte(`
140 protocol:https
141 host:example.com
142 username:bob
143 password:secr3t
144 `),
145 }}
146
147 for _, tc := range testCases {
148 b.Run(tc.name, func(b *testing.B) {
149 b.ResetTimer()
150 for b.Loop() {
151 prefix, username, password := parseGitAuth(tc.data)
152
153 _ = prefix
154 _ = username
155 _ = password
156 }
157 })
158 }
159 }
160
View as plain text