1// 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	"testing"
20)
21
22func TestResolveSourcePrivacy(t *testing.T) {
23	tests := []struct {
24		name                string
25		roots               []string
26		edges               []annotated
27		expectedResolutions []res
28	}{
29		{
30			name:  "firstparty",
31			roots: []string{"apacheBin.meta_lic"},
32			edges: []annotated{
33				{"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}},
34			},
35			expectedResolutions: []res{},
36		},
37		{
38			name:  "notice",
39			roots: []string{"mitBin.meta_lic"},
40			edges: []annotated{
41				{"mitBin.meta_lic", "mitLib.meta_lic", []string{"static"}},
42			},
43			expectedResolutions: []res{},
44		},
45		{
46			name:  "lgpl",
47			roots: []string{"lgplBin.meta_lic"},
48			edges: []annotated{
49				{"lgplBin.meta_lic", "apacheLib.meta_lic", []string{"static"}},
50			},
51			expectedResolutions: []res{},
52		},
53		{
54			name:  "proprietaryonresricted",
55			roots: []string{"proprietary.meta_lic"},
56			edges: []annotated{
57				{"proprietary.meta_lic", "gplLib.meta_lic", []string{"static"}},
58			},
59			expectedResolutions: []res{
60				{"proprietary.meta_lic", "proprietary.meta_lic", "proprietary"},
61			},
62		},
63		{
64			name:  "restrictedonproprietary",
65			roots: []string{"gplBin.meta_lic"},
66			edges: []annotated{
67				{"gplBin.meta_lic", "proprietary.meta_lic", []string{"static"}},
68			},
69			expectedResolutions: []res{
70				{"gplBin.meta_lic", "proprietary.meta_lic", "proprietary"},
71			},
72		},
73	}
74	for _, tt := range tests {
75		t.Run(tt.name, func(t *testing.T) {
76			stderr := &bytes.Buffer{}
77			lg, err := toGraph(stderr, tt.roots, tt.edges)
78			if err != nil {
79				t.Errorf("unexpected test data error: got %s, want no error", err)
80				return
81			}
82			expectedRs := toResolutionSet(lg, tt.expectedResolutions)
83			actualRs := ResolveSourcePrivacy(lg)
84			checkResolves(actualRs, expectedRs, t)
85		})
86	}
87}
88