Welcome to mirror list, hosted at ThFree Co, Russian Federation.

license_test.go « repository « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d518ec7a35442e4320beb12739eca16ee9a450a8 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//go:build !gitaly_test_sha256

package repository

import (
	"context"
	_ "net/http/pprof"
	"os"
	"testing"

	"github.com/go-enry/go-license-detector/v4/licensedb"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/catfile"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config"
	"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/rubyserver"
	"gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testserver"
	"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
	"gitlab.com/gitlab-org/labkit/correlation"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
)

const (
	mitLicense = `MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.`
)

func testSuccessfulFindLicenseRequest(t *testing.T, cfg config.Cfg, client gitalypb.RepositoryServiceClient, rubySrv *rubyserver.Server) {
	testhelper.NewFeatureSets(featureflag.GoFindLicense).Run(t, func(t *testing.T, ctx context.Context) {
		for _, tc := range []struct {
			desc                  string
			nonExistentRepository bool
			setup                 func(t *testing.T, repoPath string)
			// expectedLicenseRuby is used to verify the response received from the Ruby side-car.
			// Also is it used if expectedLicenseGo is not set. Because the Licensee gem and
			// the github.com/go-enry/go-license-detector go package use different license databases
			// and different methods to detect the license, they will not always return the
			// same result. So we need to provide different expected results in some cases.
			expectedLicenseRuby *gitalypb.FindLicenseResponse
			expectedLicenseGo   *gitalypb.FindLicenseResponse
			errorContains       string
		}{
			{
				desc: "repository does not exist",
				setup: func(t *testing.T, repoPath string) {
					require.NoError(t, os.RemoveAll(repoPath))
				},
				errorContains: "GetRepoPath: not a git repository",
			},
			{
				desc: "empty if no license file in repo",
				setup: func(t *testing.T, repoPath string) {
					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "README.md",
								Content: "readme content",
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{},
			},
			{
				desc: "high confidence mit result and less confident mit-0 result",
				setup: func(t *testing.T, repoPath string) {
					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "LICENSE",
								Content: mitLicense,
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "mit",
					LicenseUrl:       "http://choosealicense.com/licenses/mit/",
					LicenseName:      "MIT License",
					LicensePath:      "LICENSE",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "mit",
					LicenseUrl:       "https://opensource.org/licenses/MIT",
					LicenseName:      "MIT License",
					LicensePath:      "LICENSE",
				},
			},
			{
				// test for https://gitlab.com/gitlab-org/gitaly/-/issues/4745
				desc: "ignores licenses that don't have further details",
				setup: func(t *testing.T, repoPath string) {
					licenseText := testhelper.MustReadFile(t, "testdata/linux-license.txt")

					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "COPYING",
								Content: string(licenseText),
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "other",
					LicenseName:      "Other",
					LicenseNickname:  "LICENSE",
					LicensePath:      "COPYING",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "gpl-2.0+",
					LicenseName:      "GNU General Public License v2.0 or later",
					LicenseUrl:       "https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html",
					LicensePath:      "COPYING",
				},
			},
			{
				desc: "unknown license",
				setup: func(t *testing.T, repoPath string) {
					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "LICENSE.md",
								Content: "this doesn't match any known license",
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "other",
					LicenseName:      "Other",
					LicenseNickname:  "LICENSE",
					LicensePath:      "LICENSE.md",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "other",
					LicenseName:      "Other",
					LicenseNickname:  "LICENSE",
					LicensePath:      "LICENSE.md",
				},
			},
			{
				desc: "deprecated license",
				setup: func(t *testing.T, repoPath string) {
					deprecatedLicenseData := testhelper.MustReadFile(t, "testdata/gnu_license.deprecated.txt")

					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "LICENSE",
								Content: string(deprecatedLicenseData),
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "gpl-3.0",
					LicenseUrl:       "http://choosealicense.com/licenses/gpl-3.0/",
					LicenseName:      "GNU General Public License v3.0",
					LicensePath:      "LICENSE",
					LicenseNickname:  "GNU GPLv3",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "gpl-3.0+",
					LicenseUrl:       "https://www.gnu.org/licenses/gpl-3.0-standalone.html",
					LicenseName:      "GNU General Public License v3.0 or later",
					LicensePath:      "LICENSE",
					// The nickname is not set because there is no nickname defined for gpl-3.0+ license.
				},
			},
			{
				desc: "license with nickname",
				setup: func(t *testing.T, repoPath string) {
					licenseText := testhelper.MustReadFile(t, "testdata/gpl-2.0_license.txt")

					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "LICENSE",
								Content: string(licenseText),
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "gpl-2.0",
					LicenseUrl:       "http://choosealicense.com/licenses/gpl-2.0/",
					LicenseName:      "GNU General Public License v2.0",
					LicensePath:      "LICENSE",
					LicenseNickname:  "GNU GPLv2",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "gpl-2.0",
					LicenseUrl:       "https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html",
					LicenseName:      "GNU General Public License v2.0 only",
					LicensePath:      "LICENSE",
					LicenseNickname:  "GNU GPLv2",
				},
			},
			{
				desc: "license in subdir",
				setup: func(t *testing.T, repoPath string) {
					subTree := gittest.WriteTree(t, cfg, repoPath,
						[]gittest.TreeEntry{{
							Mode:    "100644",
							Path:    "LICENSE",
							Content: mitLicense,
						}})

					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode: "040000",
								Path: "legal",
								OID:  subTree,
							}))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{},
			},
			{
				desc: "license pointing to license file",
				setup: func(t *testing.T, repoPath string) {
					gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),
						gittest.WithTreeEntries(
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "mit.txt",
								Content: mitLicense,
							},
							gittest.TreeEntry{
								Mode:    "100644",
								Path:    "LICENSE",
								Content: "mit.txt",
							},
						))
				},
				expectedLicenseRuby: &gitalypb.FindLicenseResponse{
					LicenseShortName: "other",
					LicenseName:      "Other",
					LicenseNickname:  "LICENSE",
					LicensePath:      "LICENSE",
				},
				expectedLicenseGo: &gitalypb.FindLicenseResponse{
					LicenseShortName: "mit",
					LicenseUrl:       "https://opensource.org/licenses/MIT",
					LicenseName:      "MIT License",
					LicensePath:      "mit.txt",
				},
			},
		} {
			t.Run(tc.desc, func(t *testing.T) {
				repo, repoPath := gittest.CreateRepository(t, ctx, cfg)
				tc.setup(t, repoPath)

				if _, err := os.Stat(repoPath); !os.IsNotExist(err) {
					gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/main")
				}

				resp, err := client.FindLicense(ctx, &gitalypb.FindLicenseRequest{Repository: repo})
				if tc.errorContains != "" {
					require.Error(t, err)
					require.Contains(t, err.Error(), tc.errorContains)
					return
				}

				require.NoError(t, err)
				if featureflag.GoFindLicense.IsEnabled(ctx) && tc.expectedLicenseGo != nil {
					testhelper.ProtoEqual(t, tc.expectedLicenseGo, resp)
				} else {
					testhelper.ProtoEqual(t, tc.expectedLicenseRuby, resp)
				}
			})
		}
	})
}

func testFindLicenseRequestEmptyRepo(t *testing.T, cfg config.Cfg, client gitalypb.RepositoryServiceClient, rubySrv *rubyserver.Server) {
	testhelper.NewFeatureSets(featureflag.GoFindLicense).Run(t, func(t *testing.T, ctx context.Context) {
		repo, _ := gittest.CreateRepository(t, ctx, cfg)

		resp, err := client.FindLicense(ctx, &gitalypb.FindLicenseRequest{Repository: repo})
		require.NoError(t, err)

		require.Empty(t, resp.GetLicenseShortName())
	})
}

func TestFindLicense_validate(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)
	cfg := testcfg.Build(t)
	client, serverSocketPath := runRepositoryService(t, cfg, nil)
	cfg.SocketPath = serverSocketPath
	_, err := client.FindLicense(ctx, &gitalypb.FindLicenseRequest{Repository: nil})
	msg := testhelper.GitalyOrPraefect("empty Repository", "repo scoped: empty Repository")
	testhelper.RequireGrpcError(t, status.Error(codes.InvalidArgument, msg), err)
}

func BenchmarkFindLicense(b *testing.B) {
	cfg := testcfg.Build(b)
	ctx := testhelper.Context(b)
	ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.GoFindLicense, true)

	gitCmdFactory := gittest.NewCountingCommandFactory(b, cfg)
	client, serverSocketPath := runRepositoryService(
		b,
		cfg,
		nil,
		testserver.WithGitCommandFactory(gitCmdFactory),
	)
	cfg.SocketPath = serverSocketPath

	// Warm up the license database
	licensedb.Preload()

	repoGitLab, _ := gittest.CreateRepository(b, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
		Seed:                   "benchmark.git",
	})

	repoStress, repoStressPath := gittest.CreateRepository(b, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})

	repoKicad, _ := gittest.CreateRepository(b, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
		Seed:                   "kicad.git",
	})

	// Based on https://github.com/go-enry/go-license-detector/blob/18a439e5437cd46905b074ac24c27cbb6cac4347/licensedb/internal/investigation.go#L28-L38
	fileNames := []string{
		"licence",
		"lisence", //nolint:misspell
		"lisense", //nolint:misspell
		"license",
		"licences",
		"lisences",
		"lisenses",
		"licenses",
		"legal",
		"copyleft",
		"copyright",
		"copying",
		"unlicense",
		"gpl-v1",
		"gpl-v2",
		"gpl-v3",
		"lgpl-v1",
		"lgpl-v2",
		"lgpl-v3",
		"bsd",
		"mit",
		"apache",
	}
	fileExtensions := []string{
		"",
		".md",
		".rst",
		".html",
		".txt",
	}

	treeEntries := make([]gittest.TreeEntry, 0, len(fileNames)*len(fileExtensions))

	for _, name := range fileNames {
		for _, ext := range fileExtensions {
			treeEntries = append(treeEntries,
				gittest.TreeEntry{
					Mode:    "100644",
					Path:    name + ext,
					Content: mitLicense + "\n" + name, // grain of salt
				})
		}
	}

	gittest.WriteCommit(b, cfg, repoStressPath, gittest.WithBranch("main"),
		gittest.WithTreeEntries(treeEntries...))
	gittest.Exec(b, cfg, "-C", repoStressPath, "symbolic-ref", "HEAD", "refs/heads/main")

	testhelper.NewFeatureSets(featureflag.LocalrepoReadObjectCached).Bench(b, func(b *testing.B, ctx context.Context) {
		ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.GoFindLicense, true)
		ctx = correlation.ContextWithCorrelation(ctx, "1")
		ctx = testhelper.MergeOutgoingMetadata(ctx,
			metadata.Pairs(catfile.SessionIDField, "1"),
		)

		for _, tc := range []struct {
			desc      string
			repo      *gitalypb.Repository
			shortname string
		}{
			{
				desc:      "gitlab-org/gitlab.git",
				repo:      repoGitLab,
				shortname: "mit",
			},
			{
				desc:      "stress.git",
				repo:      repoStress,
				shortname: "mit",
			},
			{
				desc:      "kicad.git",
				repo:      repoKicad,
				shortname: "bsl-1.0",
			},
		} {
			b.Run(tc.desc, func(b *testing.B) {
				gitCmdFactory.ResetCount()

				for i := 0; i < b.N; i++ {
					resp, err := client.FindLicense(ctx, &gitalypb.FindLicenseRequest{Repository: tc.repo})
					require.NoError(b, err)
					require.Equal(b, tc.shortname, resp.GetLicenseShortName())
				}

				if featureflag.LocalrepoReadObjectCached.IsEnabled(ctx) {
					catfileCount := gitCmdFactory.CommandCount("cat-file")
					require.LessOrEqual(b, catfileCount, uint64(1))
				}
			})
		}
	})
}