Files
uberbringer/tools/genlicenses/notices_test.go
T
57_WolveandClaude Opus 5.5 0c88b4ffd5
Security / Vulnerability check (push) Successful in 44s
Test / Lint and test (push) Successful in 2m5s
Add third-party license notices, generated and embedded in the binary
THIRD-PARTY-LICENSES.md holds the license and notice files of the Go
distribution and of the 35 modules that ./cmd/uberbringer links on
linux amd64, armv6, armv7 and arm64, as each distribution ships them.
tools/genlicenses writes it on go generate from go list -deps, so
modules only the tools and tests use stay out. Beside each module's
root license it takes NOTICE files and the license files in its linked
packages' directories, which cover vendored code in gofiber/utils,
klauspost/compress and fasthttp. Each module is listed with its
version, so any dependency change needs the file regenerated. The
generator fails on a module with no license file or a license it does
not recognize.

The same text goes to internal/app/uberbringer/licenses_embed.md, which
the binary embeds and the new `uberbringer licenses` command prints.
The release archives carry the file, and the Docker image keeps it with
LICENSE.txt in /usr/share/licenses/uberbringer/.

The Makefile's openapi and openapi-check targets become generate and
generate-check. They, the CI step and the gate in CLAUDE.md check
docs/swagger.json and both license files, and a test fails when a
committed copy is out of date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 21:58:39 -06:00

204 lines
6.7 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
)
const (
mitText = `Copyright (c) 2020 A. Author
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal`
bsd2Text = `Copyright (c) 2020 A. Author
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice.
2. Redistributions in binary form must reproduce the above copyright notice.`
bsd3Text = bsd2Text + `
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.`
apacheText = ` Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/`
)
func TestClassify(t *testing.T) {
tests := []struct {
name string
body string
want []string
}{
{"MIT", mitText, []string{"MIT"}},
{"MIT on one line", strings.Join(strings.Fields(mitText), " "), []string{"MIT"}},
{"BSD-2-Clause", bsd2Text, []string{"BSD-2-Clause"}},
{"BSD-3-Clause", bsd3Text, []string{"BSD-3-Clause"}},
{"Apache-2.0 in full", apacheText, []string{"Apache-2.0"}},
{"Apache-2.0 by reference", `Licensed under the Apache License, Version 2.0 (the "License");`, []string{"Apache-2.0"}},
{"ISC", "Permission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted", []string{"ISC"}},
{"MPL-2.0", "Mozilla Public License Version 2.0\n==================================", []string{"MPL-2.0"}},
{"Unlicense", "This is free and unencumbered software released into the public domain.", []string{"Unlicense"}},
{"in the order given", mitText + "\n\n" + apacheText, []string{"MIT", "Apache-2.0"}},
{"each named once", mitText + "\n\n" + bsd3Text + "\n\n" + mitText, []string{"MIT", "BSD-3-Clause"}},
{"BSD clauses read up to the next license", bsd2Text + "\n\n" + apacheText + "\n\n" + bsd3Text, []string{"BSD-2-Clause", "Apache-2.0", "BSD-3-Clause"}},
{"unknown", "All rights reserved.", nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := classify(tt.body); !slices.Equal(got, tt.want) {
t.Errorf("classify = %q, want %q", got, tt.want)
}
})
}
}
func TestFileNames(t *testing.T) {
tests := []struct {
name string
license, notice bool
}{
{"LICENSE", true, false},
{"License.md", true, false},
{"LICENCE.txt", true, false},
{"LICENSE-MIT", true, false},
{"LICENSE-APACHE-2.0.txt", true, false},
{"COPYING", true, false},
{"UNLICENSE", true, false},
{"NOTICE", false, true},
{"notice.txt", false, true},
{"license.go", false, false},
{"licenses_test.go", false, false},
{"README.md", false, false},
{"PATENTS", false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLicenseFile(tt.name); got != tt.license {
t.Errorf("isLicenseFile = %v, want %v", got, tt.license)
}
if got := isNoticeFile(tt.name); got != tt.notice {
t.Errorf("isNoticeFile = %v, want %v", got, tt.notice)
}
})
}
}
func TestNormalize(t *testing.T) {
got := normalize("\n\nCopyright\r\n\r\n indented\r\n\r\n")
if want := "Copyright\n\n indented"; got != want {
t.Errorf("normalize = %q, want %q", got, want)
}
}
func TestRenderFencesBackticks(t *testing.T) {
body := "Run ```go build``` and ````that````."
out := string(render([]component{{
name: "example.com/m", version: "v1.0.0", licenses: []string{"MIT"},
files: []licenseText{{path: "LICENSE", body: body}},
}}))
if want := "\n`````\n" + body + "\n`````\n"; !strings.Contains(out, want) {
t.Errorf("the text is not fenced by five backticks:\n%s", out)
}
}
func TestReadComponent(t *testing.T) {
dir := t.TempDir()
write := func(name, body string) {
t.Helper()
path := filepath.Join(dir, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
write("LICENSE", apacheText)
write("NOTICE", "Copyright 2020 A. Author")
write("vendored/LICENSE", mitText)
write("odd/LICENSE", "All rights reserved.")
t.Run("licenses from every license file", func(t *testing.T) {
c, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "NOTICE", "vendored/LICENSE"})
if err != nil {
t.Fatal(err)
}
if want := []string{"Apache-2.0", "MIT"}; !slices.Equal(c.licenses, want) {
t.Errorf("licenses = %q, want %q", c.licenses, want)
}
if len(c.files) != 3 {
t.Errorf("got %d files, want 3", len(c.files))
}
})
t.Run("unknown license", func(t *testing.T) {
if _, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "odd/LICENSE"}); err == nil {
t.Error("readComponent took a file with no license it knows")
}
})
t.Run("notice alone", func(t *testing.T) {
if _, err := readComponent("m", "v1.0.0", dir, []string{"NOTICE"}); err == nil {
t.Error("readComponent took a module with no license file")
}
})
}
// generated runs the generator once for the tests that need its output,
// which lists the linked modules with the go command.
var generated = sync.OnceValues(func() ([]byte, error) {
root, err := moduleRoot()
if err != nil {
return nil, err
}
return generate(root)
})
// TestCommittedNoticesUpToDate fails when either committed copy of the
// notices differs from what the linked modules produce: regenerate with
// `go generate ./...`.
func TestCommittedNoticesUpToDate(t *testing.T) {
out, err := generated()
if err != nil {
t.Fatal(err)
}
root, err := moduleRoot()
if err != nil {
t.Fatal(err)
}
for _, rel := range []string{noticesPath, embeddedPath} {
committed, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(rel)))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(committed, out) {
t.Errorf("%s is out of date; run: go generate ./...", rel)
}
}
}
// Modules that only the tools and the tests use are not linked into the
// binary, so they have no notices.
func TestNoticesLeaveOutUnlinkedModules(t *testing.T) {
out, err := generated()
if err != nil {
t.Fatal(err)
}
for _, path := range []string{"golang.org/x/tools", "golang.org/x/vuln", "honnef.co/go/tools", "github.com/invopop/jsonschema"} {
if bytes.Contains(out, []byte("\n## "+path+" ")) {
t.Errorf("the notices include %s", path)
}
}
if !bytes.Contains(out, []byte("\n## github.com/gofiber/fiber/v3 ")) {
t.Error("the notices lack github.com/gofiber/fiber/v3")
}
}