GitLabgitlab-org
cli
main
1//go:build !integration
3package login
5import (
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13)
15func gradlePropertiesPath(home string) string {
16 return filepath.Join(home, ".gradle", "gradle.properties")
17}
19func TestLoginGradle_CreatesFileWhenAbsent(t *testing.T) {
20 home := t.TempDir()
21 setHome(t, home)
23 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
25 want := "myAliasUrl=https://ar.example.com\n" +
26 "myAliasUsername=__token__\n" +
27 "myAliasPassword=tok1\n"
29 got, err := os.ReadFile(gradlePropertiesPath(home))
30 require.NoError(t, err)
31 assert.Equal(t, want, string(got))
33 fileInfo, err := os.Stat(gradlePropertiesPath(home))
34 require.NoError(t, err)
35 assert.Equal(t, os.FileMode(0o600), fileInfo.Mode().Perm())
37 dirInfo, err := os.Stat(filepath.Join(home, ".gradle"))
38 require.NoError(t, err)
39 assert.Equal(t, os.FileMode(0o700), dirInfo.Mode().Perm())
40}
42func TestLoginGradle_PreservesUnrelatedContent(t *testing.T) {
43 home := t.TempDir()
44 setHome(t, home)
46 fixture := "someOther.property=value\n"
47 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
48 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
50 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
52 got, err := os.ReadFile(gradlePropertiesPath(home))
53 require.NoError(t, err)
54 content := string(got)
56 assert.Contains(t, content, "someOther.property=value")
57 assert.Contains(t, content, "myAliasUrl=https://ar.example.com")
58 assert.Contains(t, content, "myAliasUsername=__token__")
59 assert.Contains(t, content, "myAliasPassword=tok1")
60}
62func TestLoginGradle_UpdatesExistingAliasInPlace(t *testing.T) {
63 home := t.TempDir()
64 setHome(t, home)
66 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
67 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok2"))
69 got, err := os.ReadFile(gradlePropertiesPath(home))
70 require.NoError(t, err)
71 content := string(got)
73 assert.Equal(t, 1, strings.Count(content, "myAliasPassword="), "must not duplicate the property")
74 assert.Contains(t, content, "myAliasPassword=tok2")
75 assert.NotContains(t, content, "myAliasPassword=tok1")
76}
78// TestLoginGradle_UpdatesSpacedAssignmentInPlace pins that a key written the
79// way java.util.Properties also accepts it, with spaces around the "=", is
80// recognized as the same property. A "key=" prefix test misses it, appends a
81// second assignment, and leaves the superseded token in the file: later
82// assignments win, so the login still works and the stale credential is easy to
83// miss.
84func TestLoginGradle_UpdatesSpacedAssignmentInPlace(t *testing.T) {
85 home := t.TempDir()
86 setHome(t, home)
88 fixture := "myAliasUrl = https://ar.example.com\n" +
89 "myAliasUsername\t=\t__token__\n" +
90 " myAliasPassword = tok1\n"
91 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
92 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
94 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok2"))
96 got, err := os.ReadFile(gradlePropertiesPath(home))
97 require.NoError(t, err)
98 content := string(got)
100 assert.Equal(t, 1, strings.Count(content, "myAliasPassword"), "must not append a second assignment")
101 assert.Contains(t, content, "myAliasPassword=tok2")
102 assert.NotContains(t, content, "tok1", "the superseded token must not survive the refresh")
103}
105// TestLoginGradle_LeavesCommentedPropertyAlone guards the other direction of
106// gradlePropertyKey: a commented-out assignment is not an assignment, so it
107// must neither be rewritten nor stop the real property from being added.
108func TestLoginGradle_LeavesCommentedPropertyAlone(t *testing.T) {
109 home := t.TempDir()
110 setHome(t, home)
112 fixture := "# myAliasPassword=not-a-real-token\n"
113 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
114 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
116 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
118 got, err := os.ReadFile(gradlePropertiesPath(home))
119 require.NoError(t, err)
120 content := string(got)
122 assert.Contains(t, content, "# myAliasPassword=not-a-real-token")
123 assert.Contains(t, content, "myAliasPassword=tok1")
124}
126func TestLoginGradle_DifferentAliasAddsSeparateEntry(t *testing.T) {
127 home := t.TempDir()
128 setHome(t, home)
130 require.NoError(t, loginGradle("https://ar1.example.com", "aliasOne", "tok1"))
131 require.NoError(t, loginGradle("https://ar2.example.com", "aliasTwo", "tok2"))
133 got, err := os.ReadFile(gradlePropertiesPath(home))
134 require.NoError(t, err)
135 content := string(got)
137 assert.Contains(t, content, "aliasOneUrl=https://ar1.example.com")
138 assert.Contains(t, content, "aliasOnePassword=tok1")
139 assert.Contains(t, content, "aliasTwoUrl=https://ar2.example.com")
140 assert.Contains(t, content, "aliasTwoPassword=tok2")
141}
143// TestLoginGradle_LeavesAContinuationLineAlone pins that a line which
144// java.util.Properties reads as the continuation of the property above it is
145// not mistaken for an assignment of its own. Rewriting it would bury the token
146// in that property's value and leave myAliasPassword unset, so the build would
147// authenticate with nothing while the login reported success.
148func TestLoginGradle_LeavesAContinuationLineAlone(t *testing.T) {
149 home := t.TempDir()
150 setHome(t, home)
152 fixture := "unrelated=first\\\n" +
153 "myAliasPassword=still-the-unrelated-value\n"
154 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
155 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
157 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
159 got, err := os.ReadFile(gradlePropertiesPath(home))
160 require.NoError(t, err)
161 content := string(got)
163 assert.Contains(t, content, "myAliasPassword=still-the-unrelated-value", "the continuation line must be left as it was")
164 assert.NotContains(t, content, "myAliasPassword=still-the-unrelated-value\\", "and must not be rewritten")
166 lines := strings.Split(strings.TrimSuffix(content, "\n"), "\n")
167 assert.Equal(t, "myAliasPassword=tok1", lines[len(lines)-1], "the real assignment is appended instead")
168}
170// TestGradleContinuedLines pins which lines count as a continuation: an even
171// number of trailing backslashes is an escaped backslash, not a join.
172func TestGradleContinuedLines(t *testing.T) {
173 lines := []string{
174 `a=1\`, // joins
175 `b=2`, // continuation of a
176 `c=3\\`, // an escaped backslash, no join
177 `d=4`, // an assignment of its own
178 "e=5\\\r", // joins, CRLF file
179 `f=6`, // continuation of e
180 }
182 continued, dangling := gradleContinuedLines(lines)
184 assert.Equal(t, []bool{false, true, false, false, false, true}, continued)
185 assert.False(t, dangling, "the last line does not leave a continuation open")
186}
188// TestLoginGradle_EscapesBackslashesInValues pins that a backslash in a value
189// is written escaped. Properties reads a lone backslash as an escape, so a
190// --registry ending in one joins the line below: the URL property would
191// swallow the username assignment, which would then never be defined, and
192// Gradle would authenticate with no username after a green check.
193func TestLoginGradle_EscapesBackslashesInValues(t *testing.T) {
194 home := t.TempDir()
195 setHome(t, home)
197 require.NoError(t, loginGradle(`https://ar.example.com/a\`, "myAlias", "tok1"))
199 got, err := os.ReadFile(gradlePropertiesPath(home))
200 require.NoError(t, err)
201 content := string(got)
203 assert.Contains(t, content, `myAliasUrl=https://ar.example.com/a\\`, "the trailing backslash must be escaped")
204 assert.Contains(t, content, "myAliasUsername=__token__", "the next line must stay an assignment of its own")
205 assert.Contains(t, content, "myAliasPassword=tok1")
206}
208// TestLoginGradle_LeavesAnAssignmentBelowAContinuedCommentAlone pins that a
209// comment does not continue into the line below it, which the Properties spec
210// is explicit about. Skipping that assignment would append a second one and
211// leave the superseded token in the file: later assignments win, so the login
212// still works, which is what makes the stale credential easy to miss.
213func TestLoginGradle_LeavesAnAssignmentBelowAContinuedCommentAlone(t *testing.T) {
214 home := t.TempDir()
215 setHome(t, home)
217 fixture := "# see docs \\\n" + "myAliasPassword=old-live-token\n"
218 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
219 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
221 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
223 got, err := os.ReadFile(gradlePropertiesPath(home))
224 require.NoError(t, err)
225 content := string(got)
227 assert.Equal(t, 1, strings.Count(content, "myAliasPassword="), "the real assignment must be refreshed, not duplicated")
228 assert.NotContains(t, content, "old-live-token")
229}
231// TestLoginGradle_ClosesADanglingContinuationBeforeAppending pins the append
232// path for a file whose last line leaves a continuation open. Appending
233// straight after it makes the first property the rest of that property's
234// value: the property is never defined and the unrelated one is corrupted,
235// which breaks every Gradle invocation, not just this login.
236func TestLoginGradle_ClosesADanglingContinuationBeforeAppending(t *testing.T) {
237 home := t.TempDir()
238 setHome(t, home)
240 fixture := "org.gradle.jvmargs=-Xmx2g \\\n"
241 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
242 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
244 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
246 got, err := os.ReadFile(gradlePropertiesPath(home))
247 require.NoError(t, err)
248 lines := strings.Split(strings.TrimSuffix(string(got), "\n"), "\n")
250 require.Greater(t, len(lines), 1)
251 assert.Equal(t, "org.gradle.jvmargs=-Xmx2g \\", lines[0], "the unrelated property must be left as it was")
252 assert.Empty(t, lines[1], "an empty line must close the continuation before the first property")
253 assert.Contains(t, string(got), "myAliasUrl=https://ar.example.com")
254}
256// TestLoginGradle_DropsTheOldValuesContinuationLines pins that refreshing a
257// property whose value spanned a continuation takes the whole logical line
258// with it. The new value is one line, so leaving the follow-on behind would
259// strand the tail of the superseded token in the file as a property of its
260// own.
261func TestLoginGradle_DropsTheOldValuesContinuationLines(t *testing.T) {
262 home := t.TempDir()
263 setHome(t, home)
265 fixture := "myAliasPassword=old-tok\\\n" +
266 "en-tail\n" +
267 "unrelated=keep-me\n"
268 require.NoError(t, os.MkdirAll(filepath.Dir(gradlePropertiesPath(home)), 0o700))
269 require.NoError(t, os.WriteFile(gradlePropertiesPath(home), []byte(fixture), 0o600))
271 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
273 got, err := os.ReadFile(gradlePropertiesPath(home))
274 require.NoError(t, err)
275 content := string(got)
277 assert.Contains(t, content, "myAliasPassword=tok1")
278 assert.NotContains(t, content, "en-tail", "the old value's continuation must go with it")
279 assert.Contains(t, content, "unrelated=keep-me", "an unrelated line must survive")
280}
282// TestLoginGradle_HonoursGradleUserHome pins that the file lands where Gradle
283// reads it. On a machine that sets GRADLE_USER_HOME, which is common in CI,
284// writing ~/.gradle leaves the credentials in a file no build opens, and the
285// command still reports success.
286func TestLoginGradle_HonoursGradleUserHome(t *testing.T) {
287 home := t.TempDir()
288 setHome(t, home)
290 gradleHome := filepath.Join(t.TempDir(), "gradle-home")
291 t.Setenv("GRADLE_USER_HOME", gradleHome)
293 require.NoError(t, loginGradle("https://ar.example.com", "myAlias", "tok1"))
295 got, err := os.ReadFile(filepath.Join(gradleHome, "gradle.properties"))
296 require.NoError(t, err)
297 assert.Contains(t, string(got), "myAliasPassword=tok1")
299 _, err = os.Stat(gradlePropertiesPath(home))
300 assert.ErrorIs(t, err, os.ErrNotExist, "nothing may be written under ~/.gradle when GRADLE_USER_HOME is set")
301}
Diagnostics8 git calls · 363 ms
CallStatusSizeTime0363 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit6 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=6 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200242 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=242 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=5da85e254ab77f5f6e7e1419be57d524a9f8208e git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=242 git.pkt.payload.max=215 git.sideband.band1.bytes=215 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=215 git.pack.trailer=bd426fb1ae09d9fdcf755902c6903cd2eea819b2Open pack map
fetchtree:0200494 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=494 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=400358373377bb8e4922d8b821520ebdf8130c4d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=494 git.pkt.payload.max=467 git.sideband.band1.bytes=467 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=467 git.pack.trailer=363b27e4d94209a9331c11b44fe1ce6e5f4e6e81Open pack map
fetch2002.7 KiB344 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=343 git.response.bytes=2769 git.response.cache=MISS git.response.duration_ms=344 git.fetch.wants=e1f79459204d4c970ade029f8174cd34d4ebefc7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2769 git.pkt.payload.max=2742 git.sideband.band1.bytes=2742 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e30a616bf8ee28
- Request
- e4ecff55…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format