1// Copyright 2016 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cloud
16
17import (
18	"bytes"
19	"io/ioutil"
20	"os"
21	"path/filepath"
22	"strings"
23	"testing"
24)
25
26var sentinels = []string{
27	"Copyright",
28	"Google",
29	`Licensed under the Apache License, Version 2.0 (the "License");`,
30}
31
32func TestLicense(t *testing.T) {
33	t.Parallel()
34	err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
35		if err != nil {
36			return err
37		}
38
39		if ext := filepath.Ext(path); ext != ".go" && ext != ".proto" {
40			return nil
41		}
42		if strings.HasSuffix(path, ".pb.go") {
43			// .pb.go files are generated from the proto files.
44			// .proto files must have license headers.
45			return nil
46		}
47		if path == "bigtable/cmd/cbt/cbtdoc.go" {
48			// Automatically generated.
49			return nil
50		}
51		if path == "cmd/go-cloud-debug-agent/internal/debug/elf/elf.go" {
52			// BSD license, which is compatible, is embedded in the file.
53			return nil
54		}
55		src, err := ioutil.ReadFile(path)
56		if err != nil {
57			return nil
58		}
59		src = src[:300] // Ensure all of the sentinel values are at the top of the file.
60
61		// Find license
62		for _, sentinel := range sentinels {
63			if !bytes.Contains(src, []byte(sentinel)) {
64				t.Errorf("%v: license header not present. want %q", path, sentinel)
65				return nil
66			}
67		}
68
69		return nil
70	})
71	if err != nil {
72		t.Fatal(err)
73	}
74}
75