xref: /aosp_15_r20/external/licenseclassifier/v2/trace_test.go (revision 46c4c49da23cae783fa41bf46525a6505638499a)
1*46c4c49dSIbrahim Kanouche// Copyright 2020 Google Inc.
2*46c4c49dSIbrahim Kanouche//
3*46c4c49dSIbrahim Kanouche// Licensed under the Apache License, Version 2.0 (the "License");
4*46c4c49dSIbrahim Kanouche// you may not use this file except in compliance with the License.
5*46c4c49dSIbrahim Kanouche// You may obtain a copy of the License at
6*46c4c49dSIbrahim Kanouche//
7*46c4c49dSIbrahim Kanouche//     http://www.apache.org/licenses/LICENSE-2.0
8*46c4c49dSIbrahim Kanouche//
9*46c4c49dSIbrahim Kanouche// Unless required by applicable law or agreed to in writing, software
10*46c4c49dSIbrahim Kanouche// distributed under the License is distributed on an "AS IS" BASIS,
11*46c4c49dSIbrahim Kanouche// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*46c4c49dSIbrahim Kanouche// See the License for the specific language governing permissions and
13*46c4c49dSIbrahim Kanouche// limitations under the License.
14*46c4c49dSIbrahim Kanouche
15*46c4c49dSIbrahim Kanouchepackage classifier
16*46c4c49dSIbrahim Kanouche
17*46c4c49dSIbrahim Kanoucheimport (
18*46c4c49dSIbrahim Kanouche	"testing"
19*46c4c49dSIbrahim Kanouche
20*46c4c49dSIbrahim Kanouche	"github.com/google/go-cmp/cmp"
21*46c4c49dSIbrahim Kanouche)
22*46c4c49dSIbrahim Kanouche
23*46c4c49dSIbrahim Kanouchefunc TestInitTrace(t *testing.T) {
24*46c4c49dSIbrahim Kanouche	tests := []struct {
25*46c4c49dSIbrahim Kanouche		name, licFlag, phaseFlag string
26*46c4c49dSIbrahim Kanouche		expectedLics             map[string]bool
27*46c4c49dSIbrahim Kanouche		expectedPhases           map[string]bool
28*46c4c49dSIbrahim Kanouche	}{
29*46c4c49dSIbrahim Kanouche		{
30*46c4c49dSIbrahim Kanouche			name:           "empty flags",
31*46c4c49dSIbrahim Kanouche			licFlag:        "",
32*46c4c49dSIbrahim Kanouche			phaseFlag:      "",
33*46c4c49dSIbrahim Kanouche			expectedLics:   map[string]bool{},
34*46c4c49dSIbrahim Kanouche			expectedPhases: map[string]bool{},
35*46c4c49dSIbrahim Kanouche		},
36*46c4c49dSIbrahim Kanouche		{
37*46c4c49dSIbrahim Kanouche			name:           "single entries",
38*46c4c49dSIbrahim Kanouche			licFlag:        "one_license",
39*46c4c49dSIbrahim Kanouche			phaseFlag:      "setup",
40*46c4c49dSIbrahim Kanouche			expectedLics:   map[string]bool{"one_license": true},
41*46c4c49dSIbrahim Kanouche			expectedPhases: map[string]bool{"setup": true},
42*46c4c49dSIbrahim Kanouche		},
43*46c4c49dSIbrahim Kanouche		{
44*46c4c49dSIbrahim Kanouche			name:           "multiple entries",
45*46c4c49dSIbrahim Kanouche			licFlag:        "one_license,two_license",
46*46c4c49dSIbrahim Kanouche			phaseFlag:      "setup,teardown",
47*46c4c49dSIbrahim Kanouche			expectedLics:   map[string]bool{"one_license": true, "two_license": true},
48*46c4c49dSIbrahim Kanouche			expectedPhases: map[string]bool{"setup": true, "teardown": true},
49*46c4c49dSIbrahim Kanouche		},
50*46c4c49dSIbrahim Kanouche	}
51*46c4c49dSIbrahim Kanouche
52*46c4c49dSIbrahim Kanouche	for _, test := range tests {
53*46c4c49dSIbrahim Kanouche		t.Run(test.name, func(t *testing.T) {
54*46c4c49dSIbrahim Kanouche			tc := &TraceConfiguration{
55*46c4c49dSIbrahim Kanouche				TraceLicenses: test.licFlag,
56*46c4c49dSIbrahim Kanouche				TracePhases:   test.phaseFlag,
57*46c4c49dSIbrahim Kanouche			}
58*46c4c49dSIbrahim Kanouche			tc.init()
59*46c4c49dSIbrahim Kanouche			if !cmp.Equal(tc.traceLicenses, test.expectedLics) {
60*46c4c49dSIbrahim Kanouche				t.Errorf("got %v want %v", traceLicenses, test.expectedLics)
61*46c4c49dSIbrahim Kanouche			}
62*46c4c49dSIbrahim Kanouche			if !cmp.Equal(tc.tracePhases, test.expectedPhases) {
63*46c4c49dSIbrahim Kanouche				t.Errorf("got %v want %v", traceLicenses, test.expectedPhases)
64*46c4c49dSIbrahim Kanouche			}
65*46c4c49dSIbrahim Kanouche		})
66*46c4c49dSIbrahim Kanouche	}
67*46c4c49dSIbrahim Kanouche}
68*46c4c49dSIbrahim Kanouche
69*46c4c49dSIbrahim Kanouchefunc TestPhaseWildcardMatching(t *testing.T) {
70*46c4c49dSIbrahim Kanouche	tests := []struct {
71*46c4c49dSIbrahim Kanouche		name   string
72*46c4c49dSIbrahim Kanouche		phases string
73*46c4c49dSIbrahim Kanouche		hits   []string
74*46c4c49dSIbrahim Kanouche		misses []string
75*46c4c49dSIbrahim Kanouche	}{
76*46c4c49dSIbrahim Kanouche		{
77*46c4c49dSIbrahim Kanouche			name:   "exact match",
78*46c4c49dSIbrahim Kanouche			phases: "scoring",
79*46c4c49dSIbrahim Kanouche			hits:   []string{"scoring"},
80*46c4c49dSIbrahim Kanouche			misses: []string{"tokenize"},
81*46c4c49dSIbrahim Kanouche		},
82*46c4c49dSIbrahim Kanouche		{
83*46c4c49dSIbrahim Kanouche			name:   "all match",
84*46c4c49dSIbrahim Kanouche			phases: "*",
85*46c4c49dSIbrahim Kanouche			hits:   []string{"scoring", "tokenize"},
86*46c4c49dSIbrahim Kanouche			misses: nil,
87*46c4c49dSIbrahim Kanouche		},
88*46c4c49dSIbrahim Kanouche	}
89*46c4c49dSIbrahim Kanouche	for _, test := range tests {
90*46c4c49dSIbrahim Kanouche		t.Run(test.name, func(t *testing.T) {
91*46c4c49dSIbrahim Kanouche			tc := &TraceConfiguration{
92*46c4c49dSIbrahim Kanouche				TracePhases: test.phases,
93*46c4c49dSIbrahim Kanouche			}
94*46c4c49dSIbrahim Kanouche			tc.init()
95*46c4c49dSIbrahim Kanouche			for _, h := range test.hits {
96*46c4c49dSIbrahim Kanouche				if !tc.shouldTrace(h) {
97*46c4c49dSIbrahim Kanouche					t.Errorf("unexpected miss on phase %s", h)
98*46c4c49dSIbrahim Kanouche				}
99*46c4c49dSIbrahim Kanouche			}
100*46c4c49dSIbrahim Kanouche
101*46c4c49dSIbrahim Kanouche			for _, m := range test.misses {
102*46c4c49dSIbrahim Kanouche				if tc.shouldTrace(m) {
103*46c4c49dSIbrahim Kanouche					t.Errorf("unexpected hit on phase %s", m)
104*46c4c49dSIbrahim Kanouche				}
105*46c4c49dSIbrahim Kanouche			}
106*46c4c49dSIbrahim Kanouche		})
107*46c4c49dSIbrahim Kanouche	}
108*46c4c49dSIbrahim Kanouche}
109*46c4c49dSIbrahim Kanouche
110*46c4c49dSIbrahim Kanouchefunc TestLicenseWildcardMatching(t *testing.T) {
111*46c4c49dSIbrahim Kanouche	tests := []struct {
112*46c4c49dSIbrahim Kanouche		name     string
113*46c4c49dSIbrahim Kanouche		licenses string
114*46c4c49dSIbrahim Kanouche		hits     []string
115*46c4c49dSIbrahim Kanouche		misses   []string
116*46c4c49dSIbrahim Kanouche	}{
117*46c4c49dSIbrahim Kanouche		{
118*46c4c49dSIbrahim Kanouche			name:     "exact match",
119*46c4c49dSIbrahim Kanouche			hits:     []string{"GPL-2.0"},
120*46c4c49dSIbrahim Kanouche			misses:   []string{"Apache-2.0", "GPL-3.0"},
121*46c4c49dSIbrahim Kanouche			licenses: "GPL-2.0",
122*46c4c49dSIbrahim Kanouche		},
123*46c4c49dSIbrahim Kanouche		{
124*46c4c49dSIbrahim Kanouche			name:     "prefix match",
125*46c4c49dSIbrahim Kanouche			hits:     []string{"GPL-2.0", "GPL-3.0"},
126*46c4c49dSIbrahim Kanouche			misses:   []string{"Apache-2.0"},
127*46c4c49dSIbrahim Kanouche			licenses: "GPL-*",
128*46c4c49dSIbrahim Kanouche		},
129*46c4c49dSIbrahim Kanouche		{
130*46c4c49dSIbrahim Kanouche			name:     "all match",
131*46c4c49dSIbrahim Kanouche			hits:     []string{"GPL-2.0", "GPL-3.0", "Apache-2.0"},
132*46c4c49dSIbrahim Kanouche			misses:   nil,
133*46c4c49dSIbrahim Kanouche			licenses: "*",
134*46c4c49dSIbrahim Kanouche		},
135*46c4c49dSIbrahim Kanouche	}
136*46c4c49dSIbrahim Kanouche
137*46c4c49dSIbrahim Kanouche	for _, test := range tests {
138*46c4c49dSIbrahim Kanouche		t.Run(test.name, func(t *testing.T) {
139*46c4c49dSIbrahim Kanouche			tc := &TraceConfiguration{
140*46c4c49dSIbrahim Kanouche				TraceLicenses: test.licenses,
141*46c4c49dSIbrahim Kanouche			}
142*46c4c49dSIbrahim Kanouche			tc.init()
143*46c4c49dSIbrahim Kanouche			for _, h := range test.hits {
144*46c4c49dSIbrahim Kanouche				if !tc.isTraceLicense(h) {
145*46c4c49dSIbrahim Kanouche					t.Errorf("unexpected miss on license %s", h)
146*46c4c49dSIbrahim Kanouche				}
147*46c4c49dSIbrahim Kanouche			}
148*46c4c49dSIbrahim Kanouche
149*46c4c49dSIbrahim Kanouche			for _, m := range test.misses {
150*46c4c49dSIbrahim Kanouche				if tc.isTraceLicense(m) {
151*46c4c49dSIbrahim Kanouche					t.Errorf("unexpected hit on license %s", m)
152*46c4c49dSIbrahim Kanouche				}
153*46c4c49dSIbrahim Kanouche			}
154*46c4c49dSIbrahim Kanouche		})
155*46c4c49dSIbrahim Kanouche	}
156*46c4c49dSIbrahim Kanouche}
157*46c4c49dSIbrahim Kanouche
158*46c4c49dSIbrahim Kanouche// The TraceConfiguration is only explicitly initialized and propagated to a
159*46c4c49dSIbrahim Kanouche// variety of helper structs. For convenience, we just make it work safely in
160*46c4c49dSIbrahim Kanouche// the case the pointer is nil. This test ensures that behavior so users of the
161*46c4c49dSIbrahim Kanouche// TraceConfiguration don't need to explicitly initialize it.
162*46c4c49dSIbrahim Kanouchefunc TestNilSafety(t *testing.T) {
163*46c4c49dSIbrahim Kanouche	var tc *TraceConfiguration
164*46c4c49dSIbrahim Kanouche	tc.init()
165*46c4c49dSIbrahim Kanouche	if tc.isTraceLicense("GPL-2.0") {
166*46c4c49dSIbrahim Kanouche		t.Errorf("unexpected hit on license")
167*46c4c49dSIbrahim Kanouche	}
168*46c4c49dSIbrahim Kanouche
169*46c4c49dSIbrahim Kanouche	if tc.shouldTrace("scoring") {
170*46c4c49dSIbrahim Kanouche		t.Errorf("unexpected hit on phase")
171*46c4c49dSIbrahim Kanouche	}
172*46c4c49dSIbrahim Kanouche}
173