Bob Badour | 00c8a38 | 2022-01-26 17:21:39 -0800 | [diff] [blame] | 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "bytes" |
| 20 | "os" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func Test(t *testing.T) { |
| 26 | tests := []struct { |
| 27 | condition string |
| 28 | name string |
| 29 | roots []string |
| 30 | expectedOut []string |
| 31 | }{ |
| 32 | { |
| 33 | condition: "firstparty", |
| 34 | name: "apex", |
| 35 | roots: []string{"highest.apex.meta_lic"}, |
| 36 | expectedOut: []string{"Android"}, |
| 37 | }, |
| 38 | { |
| 39 | condition: "firstparty", |
| 40 | name: "container", |
| 41 | roots: []string{"container.zip.meta_lic"}, |
| 42 | expectedOut: []string{"Android"}, |
| 43 | }, |
| 44 | { |
| 45 | condition: "firstparty", |
| 46 | name: "application", |
| 47 | roots: []string{"application.meta_lic"}, |
| 48 | expectedOut: []string{"Android"}, |
| 49 | }, |
| 50 | { |
| 51 | condition: "firstparty", |
| 52 | name: "binary", |
| 53 | roots: []string{"bin/bin1.meta_lic"}, |
| 54 | expectedOut: []string{"Android"}, |
| 55 | }, |
| 56 | { |
| 57 | condition: "firstparty", |
| 58 | name: "library", |
| 59 | roots: []string{"lib/libd.so.meta_lic"}, |
| 60 | expectedOut: []string{"Android"}, |
| 61 | }, |
| 62 | { |
| 63 | condition: "notice", |
| 64 | name: "apex", |
| 65 | roots: []string{"highest.apex.meta_lic"}, |
| 66 | expectedOut: []string{"Android", "Device", "External"}, |
| 67 | }, |
| 68 | { |
| 69 | condition: "notice", |
| 70 | name: "container", |
| 71 | roots: []string{"container.zip.meta_lic"}, |
| 72 | expectedOut: []string{"Android", "Device", "External"}, |
| 73 | }, |
| 74 | { |
| 75 | condition: "notice", |
| 76 | name: "application", |
| 77 | roots: []string{"application.meta_lic"}, |
| 78 | expectedOut: []string{"Android", "Device"}, |
| 79 | }, |
| 80 | { |
| 81 | condition: "notice", |
| 82 | name: "binary", |
| 83 | roots: []string{"bin/bin1.meta_lic"}, |
| 84 | expectedOut: []string{"Android", "Device", "External"}, |
| 85 | }, |
| 86 | { |
| 87 | condition: "notice", |
| 88 | name: "library", |
| 89 | roots: []string{"lib/libd.so.meta_lic"}, |
| 90 | expectedOut: []string{"External"}, |
| 91 | }, |
| 92 | { |
| 93 | condition: "reciprocal", |
| 94 | name: "apex", |
| 95 | roots: []string{"highest.apex.meta_lic"}, |
| 96 | expectedOut: []string{"Android", "Device", "External"}, |
| 97 | }, |
| 98 | { |
| 99 | condition: "reciprocal", |
| 100 | name: "container", |
| 101 | roots: []string{"container.zip.meta_lic"}, |
| 102 | expectedOut: []string{"Android", "Device", "External"}, |
| 103 | }, |
| 104 | { |
| 105 | condition: "reciprocal", |
| 106 | name: "application", |
| 107 | roots: []string{"application.meta_lic"}, |
| 108 | expectedOut: []string{"Android", "Device"}, |
| 109 | }, |
| 110 | { |
| 111 | condition: "reciprocal", |
| 112 | name: "binary", |
| 113 | roots: []string{"bin/bin1.meta_lic"}, |
| 114 | expectedOut: []string{"Android", "Device", "External"}, |
| 115 | }, |
| 116 | { |
| 117 | condition: "reciprocal", |
| 118 | name: "library", |
| 119 | roots: []string{"lib/libd.so.meta_lic"}, |
| 120 | expectedOut: []string{"External"}, |
| 121 | }, |
| 122 | { |
| 123 | condition: "restricted", |
| 124 | name: "apex", |
| 125 | roots: []string{"highest.apex.meta_lic"}, |
| 126 | expectedOut: []string{"Android", "Device", "External"}, |
| 127 | }, |
| 128 | { |
| 129 | condition: "restricted", |
| 130 | name: "container", |
| 131 | roots: []string{"container.zip.meta_lic"}, |
| 132 | expectedOut: []string{"Android", "Device", "External"}, |
| 133 | }, |
| 134 | { |
| 135 | condition: "restricted", |
| 136 | name: "application", |
| 137 | roots: []string{"application.meta_lic"}, |
| 138 | expectedOut: []string{"Android", "Device"}, |
| 139 | }, |
| 140 | { |
| 141 | condition: "restricted", |
| 142 | name: "binary", |
| 143 | roots: []string{"bin/bin1.meta_lic"}, |
| 144 | expectedOut: []string{"Android", "Device", "External"}, |
| 145 | }, |
| 146 | { |
| 147 | condition: "restricted", |
| 148 | name: "library", |
| 149 | roots: []string{"lib/libd.so.meta_lic"}, |
| 150 | expectedOut: []string{"External"}, |
| 151 | }, |
| 152 | { |
| 153 | condition: "proprietary", |
| 154 | name: "apex", |
| 155 | roots: []string{"highest.apex.meta_lic"}, |
| 156 | expectedOut: []string{"Android", "Device", "External"}, |
| 157 | }, |
| 158 | { |
| 159 | condition: "proprietary", |
| 160 | name: "container", |
| 161 | roots: []string{"container.zip.meta_lic"}, |
| 162 | expectedOut: []string{"Android", "Device", "External"}, |
| 163 | }, |
| 164 | { |
| 165 | condition: "proprietary", |
| 166 | name: "application", |
| 167 | roots: []string{"application.meta_lic"}, |
| 168 | expectedOut: []string{"Android", "Device"}, |
| 169 | }, |
| 170 | { |
| 171 | condition: "proprietary", |
| 172 | name: "binary", |
| 173 | roots: []string{"bin/bin1.meta_lic"}, |
| 174 | expectedOut: []string{"Android", "Device", "External"}, |
| 175 | }, |
| 176 | { |
| 177 | condition: "proprietary", |
| 178 | name: "library", |
| 179 | roots: []string{"lib/libd.so.meta_lic"}, |
| 180 | expectedOut: []string{"External"}, |
| 181 | }, |
| 182 | } |
| 183 | for _, tt := range tests { |
| 184 | t.Run(tt.condition+" "+tt.name, func(t *testing.T) { |
| 185 | stdout := &bytes.Buffer{} |
| 186 | stderr := &bytes.Buffer{} |
| 187 | |
| 188 | rootFiles := make([]string, 0, len(tt.roots)) |
| 189 | for _, r := range tt.roots { |
| 190 | rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r) |
| 191 | } |
| 192 | |
| 193 | ctx := context{stdout, stderr, os.DirFS(".")} |
| 194 | |
| 195 | err := shippedLibs(&ctx, rootFiles...) |
| 196 | if err != nil { |
| 197 | t.Fatalf("shippedLibs: error = %w, stderr = %v", err, stderr) |
| 198 | return |
| 199 | } |
| 200 | if stderr.Len() > 0 { |
| 201 | t.Errorf("shippedLibs: gotStderr = %v, want none", stderr) |
| 202 | } |
| 203 | |
| 204 | t.Logf("got stdout: %s", stdout.String()) |
| 205 | |
| 206 | t.Logf("want stdout: %s", strings.Join(tt.expectedOut, "\n")) |
| 207 | |
| 208 | out := bufio.NewScanner(stdout) |
| 209 | lineno := 0 |
| 210 | for out.Scan() { |
| 211 | line := out.Text() |
| 212 | if strings.TrimLeft(line, " ") == "" { |
| 213 | continue |
| 214 | } |
| 215 | if len(tt.expectedOut) <= lineno { |
| 216 | t.Errorf("shippedLibs: unexpected output at line %d: got %q, want nothing (wanted %d lines)", lineno+1, line, len(tt.expectedOut)) |
| 217 | } else if tt.expectedOut[lineno] != line { |
| 218 | t.Errorf("shippedLibs: unexpected output at line %d: got %q, want %q", lineno+1, line, tt.expectedOut[lineno]) |
| 219 | } |
| 220 | lineno++ |
| 221 | } |
| 222 | for ; lineno < len(tt.expectedOut); lineno++ { |
| 223 | t.Errorf("shippedLibs: missing output line %d: ended early, want %q", lineno+1, tt.expectedOut[lineno]) |
| 224 | } |
| 225 | }) |
| 226 | } |
| 227 | } |