blob: 6248209228d8049a7066913bec9f632edfe3695a [file] [log] [blame]
Bob Badoura99ac622021-10-25 16:21:00 -07001// Copyright 2021 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 compliance
16
17import (
18 "bytes"
19 "sort"
20 "strings"
21 "testing"
22)
23
24func TestReadLicenseGraph(t *testing.T) {
25 tests := []struct {
26 name string
27 fs *testFS
28 roots []string
29 expectedError string
30 expectedEdges []edge
31 expectedTargets []string
32 }{
33 {
34 name: "trivial",
35 fs: &testFS{
36 "app.meta_lic": []byte("package_name: \"Android\"\n"),
37 },
38 roots: []string{"app.meta_lic"},
39 expectedEdges: []edge{},
40 expectedTargets: []string{"app.meta_lic"},
41 },
42 {
43 name: "unterminated",
44 fs: &testFS{
45 "app.meta_lic": []byte("package_name: \"Android\n"),
46 },
47 roots: []string{"app.meta_lic"},
48 expectedError: `invalid character '\n' in string`,
49 },
50 {
51 name: "danglingref",
52 fs: &testFS{
53 "app.meta_lic": []byte(AOSP + "deps: {\n file: \"lib.meta_lic\"\n}\n"),
54 },
55 roots: []string{"app.meta_lic"},
56 expectedError: `unknown file "lib.meta_lic"`,
57 },
58 {
59 name: "singleedge",
60 fs: &testFS{
61 "app.meta_lic": []byte(AOSP + "deps: {\n file: \"lib.meta_lic\"\n}\n"),
62 "lib.meta_lic": []byte(AOSP),
63 },
64 roots: []string{"app.meta_lic"},
65 expectedEdges: []edge{{"app.meta_lic", "lib.meta_lic"}},
66 expectedTargets: []string{"app.meta_lic", "lib.meta_lic"},
67 },
68 {
69 name: "fullgraph",
70 fs: &testFS{
71 "apex.meta_lic": []byte(AOSP + "deps: {\n file: \"app.meta_lic\"\n}\ndeps: {\n file: \"bin.meta_lic\"\n}\n"),
72 "app.meta_lic": []byte(AOSP),
73 "bin.meta_lic": []byte(AOSP + "deps: {\n file: \"lib.meta_lic\"\n}\n"),
74 "lib.meta_lic": []byte(AOSP),
75 },
76 roots: []string{"apex.meta_lic"},
77 expectedEdges: []edge{
78 {"apex.meta_lic", "app.meta_lic"},
79 {"apex.meta_lic", "bin.meta_lic"},
80 {"bin.meta_lic", "lib.meta_lic"},
81 },
82 expectedTargets: []string{"apex.meta_lic", "app.meta_lic", "bin.meta_lic", "lib.meta_lic"},
83 },
84 }
85 for _, tt := range tests {
86 t.Run(tt.name, func(t *testing.T) {
87 stderr := &bytes.Buffer{}
88 lg, err := ReadLicenseGraph(tt.fs, stderr, tt.roots)
89 if err != nil {
90 if len(tt.expectedError) == 0 {
91 t.Errorf("unexpected error: got %w, want no error", err)
92 } else if !strings.Contains(err.Error(), tt.expectedError) {
93 t.Errorf("unexpected error: got %w, want %q", err, tt.expectedError)
94 }
95 return
96 }
97 if 0 < len(tt.expectedError) {
98 t.Errorf("unexpected success: got no error, want %q err", tt.expectedError)
99 return
100 }
101 if lg == nil {
102 t.Errorf("missing license graph: got nil, want license graph")
103 return
104 }
105 actualEdges := make([]edge, 0)
106 for _, e := range lg.Edges() {
107 actualEdges = append(actualEdges, edge{e.Target().Name(), e.Dependency().Name()})
108 }
109 sort.Sort(byEdge(tt.expectedEdges))
110 sort.Sort(byEdge(actualEdges))
111 if len(tt.expectedEdges) != len(actualEdges) {
112 t.Errorf("unexpected number of edges: got %v with %d elements, want %v with %d elements",
113 actualEdges, len(actualEdges), tt.expectedEdges, len(tt.expectedEdges))
114 } else {
115 for i := 0; i < len(actualEdges); i++ {
116 if tt.expectedEdges[i] != actualEdges[i] {
117 t.Errorf("unexpected edge at element %d: got %s, want %s", i, actualEdges[i], tt.expectedEdges[i])
118 }
119 }
120 }
121 actualTargets := make([]string, 0)
122 for _, t := range lg.Targets() {
123 actualTargets = append(actualTargets, t.Name())
124 }
125 sort.Strings(tt.expectedTargets)
126 sort.Strings(actualTargets)
127 if len(tt.expectedTargets) != len(actualTargets) {
128 t.Errorf("unexpected number of targets: got %v with %d elements, want %v with %d elements",
129 actualTargets, len(actualTargets), tt.expectedTargets, len(tt.expectedTargets))
130 } else {
131 for i := 0; i < len(actualTargets); i++ {
132 if tt.expectedTargets[i] != actualTargets[i] {
133 t.Errorf("unexpected target at element %d: got %s, want %s", i, actualTargets[i], tt.expectedTargets[i])
134 }
135 }
136 }
137 })
138 }
139}