Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "testing" |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 7 | |
| 8 | "github.com/google/blueprint" |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | var visibilityTests = []struct { |
| 12 | name string |
| 13 | fs map[string][]byte |
| 14 | expectedErrors []string |
| 15 | }{ |
| 16 | { |
| 17 | name: "invalid visibility: empty list", |
| 18 | fs: map[string][]byte{ |
| 19 | "top/Blueprints": []byte(` |
| 20 | mock_library { |
| 21 | name: "libexample", |
| 22 | visibility: [], |
| 23 | }`), |
| 24 | }, |
| 25 | expectedErrors: []string{`visibility: must contain at least one visibility rule`}, |
| 26 | }, |
| 27 | { |
| 28 | name: "invalid visibility: empty rule", |
| 29 | fs: map[string][]byte{ |
| 30 | "top/Blueprints": []byte(` |
| 31 | mock_library { |
| 32 | name: "libexample", |
| 33 | visibility: [""], |
| 34 | }`), |
| 35 | }, |
| 36 | expectedErrors: []string{`visibility: invalid visibility pattern ""`}, |
| 37 | }, |
| 38 | { |
| 39 | name: "invalid visibility: unqualified", |
| 40 | fs: map[string][]byte{ |
| 41 | "top/Blueprints": []byte(` |
| 42 | mock_library { |
| 43 | name: "libexample", |
| 44 | visibility: ["target"], |
| 45 | }`), |
| 46 | }, |
| 47 | expectedErrors: []string{`visibility: invalid visibility pattern "target"`}, |
| 48 | }, |
| 49 | { |
| 50 | name: "invalid visibility: empty namespace", |
| 51 | fs: map[string][]byte{ |
| 52 | "top/Blueprints": []byte(` |
| 53 | mock_library { |
| 54 | name: "libexample", |
| 55 | visibility: ["//"], |
| 56 | }`), |
| 57 | }, |
| 58 | expectedErrors: []string{`visibility: invalid visibility pattern "//"`}, |
| 59 | }, |
| 60 | { |
| 61 | name: "invalid visibility: empty module", |
| 62 | fs: map[string][]byte{ |
| 63 | "top/Blueprints": []byte(` |
| 64 | mock_library { |
| 65 | name: "libexample", |
| 66 | visibility: [":"], |
| 67 | }`), |
| 68 | }, |
| 69 | expectedErrors: []string{`visibility: invalid visibility pattern ":"`}, |
| 70 | }, |
| 71 | { |
| 72 | name: "invalid visibility: empty namespace and module", |
| 73 | fs: map[string][]byte{ |
| 74 | "top/Blueprints": []byte(` |
| 75 | mock_library { |
| 76 | name: "libexample", |
| 77 | visibility: ["//:"], |
| 78 | }`), |
| 79 | }, |
| 80 | expectedErrors: []string{`visibility: invalid visibility pattern "//:"`}, |
| 81 | }, |
| 82 | { |
| 83 | name: "//visibility:unknown", |
| 84 | fs: map[string][]byte{ |
| 85 | "top/Blueprints": []byte(` |
| 86 | mock_library { |
| 87 | name: "libexample", |
| 88 | visibility: ["//visibility:unknown"], |
| 89 | }`), |
| 90 | }, |
| 91 | expectedErrors: []string{`unrecognized visibility rule "//visibility:unknown"`}, |
| 92 | }, |
| 93 | { |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 94 | name: "//visibility:xxx mixed", |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 95 | fs: map[string][]byte{ |
| 96 | "top/Blueprints": []byte(` |
| 97 | mock_library { |
| 98 | name: "libexample", |
| 99 | visibility: ["//visibility:public", "//namespace"], |
| 100 | } |
| 101 | |
| 102 | mock_library { |
| 103 | name: "libother", |
| 104 | visibility: ["//visibility:private", "//namespace"], |
| 105 | }`), |
| 106 | }, |
| 107 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 108 | `module "libother": visibility: cannot mix "//visibility:private"` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 109 | ` with any other visibility rules`, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 110 | `module "libexample": visibility: cannot mix "//visibility:public"` + |
| 111 | ` with any other visibility rules`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 112 | }, |
| 113 | }, |
| 114 | { |
| 115 | name: "//visibility:legacy_public", |
| 116 | fs: map[string][]byte{ |
| 117 | "top/Blueprints": []byte(` |
| 118 | mock_library { |
| 119 | name: "libexample", |
| 120 | visibility: ["//visibility:legacy_public"], |
| 121 | }`), |
| 122 | }, |
| 123 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 124 | `module "libexample": visibility: //visibility:legacy_public must` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 125 | ` not be used`, |
| 126 | }, |
| 127 | }, |
| 128 | { |
| 129 | // Verify that //visibility:public will allow the module to be referenced from anywhere, e.g. |
| 130 | // the current directory, a nested directory and a directory in a separate tree. |
| 131 | name: "//visibility:public", |
| 132 | fs: map[string][]byte{ |
| 133 | "top/Blueprints": []byte(` |
| 134 | mock_library { |
| 135 | name: "libexample", |
| 136 | visibility: ["//visibility:public"], |
| 137 | } |
| 138 | |
| 139 | mock_library { |
| 140 | name: "libsamepackage", |
| 141 | deps: ["libexample"], |
| 142 | }`), |
| 143 | "top/nested/Blueprints": []byte(` |
| 144 | mock_library { |
| 145 | name: "libnested", |
| 146 | deps: ["libexample"], |
| 147 | }`), |
| 148 | "other/Blueprints": []byte(` |
| 149 | mock_library { |
| 150 | name: "libother", |
| 151 | deps: ["libexample"], |
| 152 | }`), |
| 153 | }, |
| 154 | }, |
| 155 | { |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 156 | // Verify that //visibility:private allows the module to be referenced from the current |
| 157 | // directory only. |
| 158 | name: "//visibility:private", |
| 159 | fs: map[string][]byte{ |
| 160 | "top/Blueprints": []byte(` |
| 161 | mock_library { |
| 162 | name: "libexample", |
| 163 | visibility: ["//visibility:private"], |
| 164 | } |
| 165 | |
| 166 | mock_library { |
| 167 | name: "libsamepackage", |
| 168 | deps: ["libexample"], |
| 169 | }`), |
| 170 | "top/nested/Blueprints": []byte(` |
| 171 | mock_library { |
| 172 | name: "libnested", |
| 173 | deps: ["libexample"], |
| 174 | }`), |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 175 | "other/Blueprints": []byte(` |
| 176 | mock_library { |
| 177 | name: "libother", |
| 178 | deps: ["libexample"], |
| 179 | }`), |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 180 | }, |
| 181 | expectedErrors: []string{ |
| 182 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 183 | ` visible to this module`, |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 184 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 185 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 186 | }, |
| 187 | }, |
| 188 | { |
| 189 | // Verify that :__pkg__ allows the module to be referenced from the current directory only. |
| 190 | name: ":__pkg__", |
| 191 | fs: map[string][]byte{ |
| 192 | "top/Blueprints": []byte(` |
| 193 | mock_library { |
| 194 | name: "libexample", |
| 195 | visibility: [":__pkg__"], |
| 196 | } |
| 197 | |
| 198 | mock_library { |
| 199 | name: "libsamepackage", |
| 200 | deps: ["libexample"], |
| 201 | }`), |
| 202 | "top/nested/Blueprints": []byte(` |
| 203 | mock_library { |
| 204 | name: "libnested", |
| 205 | deps: ["libexample"], |
| 206 | }`), |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 207 | "other/Blueprints": []byte(` |
| 208 | mock_library { |
| 209 | name: "libother", |
| 210 | deps: ["libexample"], |
| 211 | }`), |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 212 | }, |
| 213 | expectedErrors: []string{ |
| 214 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 215 | ` visible to this module`, |
Martin Stjernholm | 8edeb63 | 2019-05-21 12:18:38 +0100 | [diff] [blame] | 216 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 217 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 218 | }, |
| 219 | }, |
| 220 | { |
| 221 | // Verify that //top/nested allows the module to be referenced from the current directory and |
| 222 | // the top/nested directory only, not a subdirectory of top/nested and not peak directory. |
| 223 | name: "//top/nested", |
| 224 | fs: map[string][]byte{ |
| 225 | "top/Blueprints": []byte(` |
| 226 | mock_library { |
| 227 | name: "libexample", |
| 228 | visibility: ["//top/nested"], |
| 229 | } |
| 230 | |
| 231 | mock_library { |
| 232 | name: "libsamepackage", |
| 233 | deps: ["libexample"], |
| 234 | }`), |
| 235 | "top/nested/Blueprints": []byte(` |
| 236 | mock_library { |
| 237 | name: "libnested", |
| 238 | deps: ["libexample"], |
| 239 | }`), |
| 240 | "top/nested/again/Blueprints": []byte(` |
| 241 | mock_library { |
| 242 | name: "libnestedagain", |
| 243 | deps: ["libexample"], |
| 244 | }`), |
| 245 | "peak/Blueprints": []byte(` |
| 246 | mock_library { |
| 247 | name: "libother", |
| 248 | deps: ["libexample"], |
| 249 | }`), |
| 250 | }, |
| 251 | expectedErrors: []string{ |
| 252 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 253 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 254 | `module "libnestedagain" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 255 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 256 | }, |
| 257 | }, |
| 258 | { |
| 259 | // Verify that :__subpackages__ allows the module to be referenced from the current directory |
| 260 | // and sub directories but nowhere else. |
| 261 | name: ":__subpackages__", |
| 262 | fs: map[string][]byte{ |
| 263 | "top/Blueprints": []byte(` |
| 264 | mock_library { |
| 265 | name: "libexample", |
| 266 | visibility: [":__subpackages__"], |
| 267 | } |
| 268 | |
| 269 | mock_library { |
| 270 | name: "libsamepackage", |
| 271 | deps: ["libexample"], |
| 272 | }`), |
| 273 | "top/nested/Blueprints": []byte(` |
| 274 | mock_library { |
| 275 | name: "libnested", |
| 276 | deps: ["libexample"], |
| 277 | }`), |
| 278 | "peak/other/Blueprints": []byte(` |
| 279 | mock_library { |
| 280 | name: "libother", |
| 281 | deps: ["libexample"], |
| 282 | }`), |
| 283 | }, |
| 284 | expectedErrors: []string{ |
| 285 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 286 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 287 | }, |
| 288 | }, |
| 289 | { |
| 290 | // Verify that //top/nested:__subpackages__ allows the module to be referenced from the current |
| 291 | // directory and sub directories but nowhere else. |
| 292 | name: "//top/nested:__subpackages__", |
| 293 | fs: map[string][]byte{ |
| 294 | "top/Blueprints": []byte(` |
| 295 | mock_library { |
| 296 | name: "libexample", |
| 297 | visibility: ["//top/nested:__subpackages__", "//other"], |
| 298 | } |
| 299 | |
| 300 | mock_library { |
| 301 | name: "libsamepackage", |
| 302 | deps: ["libexample"], |
| 303 | }`), |
| 304 | "top/nested/Blueprints": []byte(` |
| 305 | mock_library { |
| 306 | name: "libnested", |
| 307 | deps: ["libexample"], |
| 308 | }`), |
| 309 | "top/other/Blueprints": []byte(` |
| 310 | mock_library { |
| 311 | name: "libother", |
| 312 | deps: ["libexample"], |
| 313 | }`), |
| 314 | }, |
| 315 | expectedErrors: []string{ |
| 316 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 317 | ` visible to this module`, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 318 | }, |
| 319 | }, |
| 320 | { |
| 321 | // Verify that ["//top/nested", "//peak:__subpackages"] allows the module to be referenced from |
| 322 | // the current directory, top/nested and peak and all its subpackages. |
| 323 | name: `["//top/nested", "//peak:__subpackages__"]`, |
| 324 | fs: map[string][]byte{ |
| 325 | "top/Blueprints": []byte(` |
| 326 | mock_library { |
| 327 | name: "libexample", |
| 328 | visibility: ["//top/nested", "//peak:__subpackages__"], |
| 329 | } |
| 330 | |
| 331 | mock_library { |
| 332 | name: "libsamepackage", |
| 333 | deps: ["libexample"], |
| 334 | }`), |
| 335 | "top/nested/Blueprints": []byte(` |
| 336 | mock_library { |
| 337 | name: "libnested", |
| 338 | deps: ["libexample"], |
| 339 | }`), |
| 340 | "peak/other/Blueprints": []byte(` |
| 341 | mock_library { |
| 342 | name: "libother", |
| 343 | deps: ["libexample"], |
| 344 | }`), |
| 345 | }, |
| 346 | }, |
| 347 | { |
| 348 | // Verify that //vendor... cannot be used outside vendor apart from //vendor:__subpackages__ |
| 349 | name: `//vendor`, |
| 350 | fs: map[string][]byte{ |
| 351 | "top/Blueprints": []byte(` |
| 352 | mock_library { |
| 353 | name: "libexample", |
| 354 | visibility: ["//vendor:__subpackages__"], |
| 355 | } |
| 356 | |
| 357 | mock_library { |
| 358 | name: "libsamepackage", |
| 359 | visibility: ["//vendor/apps/AcmeSettings"], |
| 360 | }`), |
| 361 | "vendor/Blueprints": []byte(` |
| 362 | mock_library { |
| 363 | name: "libvendorexample", |
| 364 | deps: ["libexample"], |
| 365 | visibility: ["//vendor/nested"], |
| 366 | }`), |
| 367 | "vendor/nested/Blueprints": []byte(` |
| 368 | mock_library { |
| 369 | name: "libvendornested", |
| 370 | deps: ["libexample", "libvendorexample"], |
| 371 | }`), |
| 372 | }, |
| 373 | expectedErrors: []string{ |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 374 | `module "libsamepackage": visibility: "//vendor/apps/AcmeSettings"` + |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 375 | ` is not allowed. Packages outside //vendor cannot make themselves visible to specific` + |
| 376 | ` targets within //vendor, they can only use //vendor:__subpackages__.`, |
| 377 | }, |
| 378 | }, |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 379 | |
| 380 | // Defaults propagation tests |
| 381 | { |
| 382 | // Check that visibility is the union of the defaults modules. |
| 383 | name: "defaults union, basic", |
| 384 | fs: map[string][]byte{ |
| 385 | "top/Blueprints": []byte(` |
| 386 | mock_defaults { |
| 387 | name: "libexample_defaults", |
| 388 | visibility: ["//other"], |
| 389 | } |
| 390 | mock_library { |
| 391 | name: "libexample", |
| 392 | visibility: ["//top/nested"], |
| 393 | defaults: ["libexample_defaults"], |
| 394 | } |
| 395 | mock_library { |
| 396 | name: "libsamepackage", |
| 397 | deps: ["libexample"], |
| 398 | }`), |
| 399 | "top/nested/Blueprints": []byte(` |
| 400 | mock_library { |
| 401 | name: "libnested", |
| 402 | deps: ["libexample"], |
| 403 | }`), |
| 404 | "other/Blueprints": []byte(` |
| 405 | mock_library { |
| 406 | name: "libother", |
| 407 | deps: ["libexample"], |
| 408 | }`), |
| 409 | "outsider/Blueprints": []byte(` |
| 410 | mock_library { |
| 411 | name: "liboutsider", |
| 412 | deps: ["libexample"], |
| 413 | }`), |
| 414 | }, |
| 415 | expectedErrors: []string{ |
| 416 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 417 | ` visible to this module`, |
| 418 | }, |
| 419 | }, |
| 420 | { |
| 421 | name: "defaults union, multiple defaults", |
| 422 | fs: map[string][]byte{ |
| 423 | "top/Blueprints": []byte(` |
| 424 | mock_defaults { |
| 425 | name: "libexample_defaults_1", |
| 426 | visibility: ["//other"], |
| 427 | } |
| 428 | mock_defaults { |
| 429 | name: "libexample_defaults_2", |
| 430 | visibility: ["//top/nested"], |
| 431 | } |
| 432 | mock_library { |
| 433 | name: "libexample", |
| 434 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 435 | } |
| 436 | mock_library { |
| 437 | name: "libsamepackage", |
| 438 | deps: ["libexample"], |
| 439 | }`), |
| 440 | "top/nested/Blueprints": []byte(` |
| 441 | mock_library { |
| 442 | name: "libnested", |
| 443 | deps: ["libexample"], |
| 444 | }`), |
| 445 | "other/Blueprints": []byte(` |
| 446 | mock_library { |
| 447 | name: "libother", |
| 448 | deps: ["libexample"], |
| 449 | }`), |
| 450 | "outsider/Blueprints": []byte(` |
| 451 | mock_library { |
| 452 | name: "liboutsider", |
| 453 | deps: ["libexample"], |
| 454 | }`), |
| 455 | }, |
| 456 | expectedErrors: []string{ |
| 457 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 458 | ` visible to this module`, |
| 459 | }, |
| 460 | }, |
| 461 | { |
| 462 | name: "//visibility:public mixed with other in defaults", |
| 463 | fs: map[string][]byte{ |
| 464 | "top/Blueprints": []byte(` |
| 465 | mock_defaults { |
| 466 | name: "libexample_defaults", |
| 467 | visibility: ["//visibility:public", "//namespace"], |
| 468 | } |
| 469 | mock_library { |
| 470 | name: "libexample", |
| 471 | defaults: ["libexample_defaults"], |
| 472 | }`), |
| 473 | }, |
| 474 | expectedErrors: []string{ |
| 475 | `module "libexample_defaults": visibility: cannot mix "//visibility:public"` + |
| 476 | ` with any other visibility rules`, |
| 477 | }, |
| 478 | }, |
| 479 | { |
| 480 | name: "//visibility:public overriding defaults", |
| 481 | fs: map[string][]byte{ |
| 482 | "top/Blueprints": []byte(` |
| 483 | mock_defaults { |
| 484 | name: "libexample_defaults", |
| 485 | visibility: ["//namespace"], |
| 486 | } |
| 487 | mock_library { |
| 488 | name: "libexample", |
| 489 | visibility: ["//visibility:public"], |
| 490 | defaults: ["libexample_defaults"], |
| 491 | }`), |
| 492 | "outsider/Blueprints": []byte(` |
| 493 | mock_library { |
| 494 | name: "liboutsider", |
| 495 | deps: ["libexample"], |
| 496 | }`), |
| 497 | }, |
| 498 | }, |
| 499 | { |
| 500 | name: "//visibility:public mixed with other from different defaults 1", |
| 501 | fs: map[string][]byte{ |
| 502 | "top/Blueprints": []byte(` |
| 503 | mock_defaults { |
| 504 | name: "libexample_defaults_1", |
| 505 | visibility: ["//namespace"], |
| 506 | } |
| 507 | mock_defaults { |
| 508 | name: "libexample_defaults_2", |
| 509 | visibility: ["//visibility:public"], |
| 510 | } |
| 511 | mock_library { |
| 512 | name: "libexample", |
| 513 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 514 | }`), |
| 515 | "outsider/Blueprints": []byte(` |
| 516 | mock_library { |
| 517 | name: "liboutsider", |
| 518 | deps: ["libexample"], |
| 519 | }`), |
| 520 | }, |
| 521 | }, |
| 522 | { |
| 523 | name: "//visibility:public mixed with other from different defaults 2", |
| 524 | fs: map[string][]byte{ |
| 525 | "top/Blueprints": []byte(` |
| 526 | mock_defaults { |
| 527 | name: "libexample_defaults_1", |
| 528 | visibility: ["//visibility:public"], |
| 529 | } |
| 530 | mock_defaults { |
| 531 | name: "libexample_defaults_2", |
| 532 | visibility: ["//namespace"], |
| 533 | } |
| 534 | mock_library { |
| 535 | name: "libexample", |
| 536 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 537 | }`), |
| 538 | "outsider/Blueprints": []byte(` |
| 539 | mock_library { |
| 540 | name: "liboutsider", |
| 541 | deps: ["libexample"], |
| 542 | }`), |
| 543 | }, |
| 544 | }, |
| 545 | { |
| 546 | name: "//visibility:private in defaults", |
| 547 | fs: map[string][]byte{ |
| 548 | "top/Blueprints": []byte(` |
| 549 | mock_defaults { |
| 550 | name: "libexample_defaults", |
| 551 | visibility: ["//visibility:private"], |
| 552 | } |
| 553 | mock_library { |
| 554 | name: "libexample", |
| 555 | defaults: ["libexample_defaults"], |
| 556 | } |
| 557 | mock_library { |
| 558 | name: "libsamepackage", |
| 559 | deps: ["libexample"], |
| 560 | }`), |
| 561 | "top/nested/Blueprints": []byte(` |
| 562 | mock_library { |
| 563 | name: "libnested", |
| 564 | deps: ["libexample"], |
| 565 | }`), |
| 566 | "other/Blueprints": []byte(` |
| 567 | mock_library { |
| 568 | name: "libother", |
| 569 | deps: ["libexample"], |
| 570 | }`), |
| 571 | }, |
| 572 | expectedErrors: []string{ |
| 573 | `module "libnested" variant "android_common": depends on //top:libexample which is not` + |
| 574 | ` visible to this module`, |
| 575 | `module "libother" variant "android_common": depends on //top:libexample which is not` + |
| 576 | ` visible to this module`, |
| 577 | }, |
| 578 | }, |
| 579 | { |
| 580 | name: "//visibility:private mixed with other in defaults", |
| 581 | fs: map[string][]byte{ |
| 582 | "top/Blueprints": []byte(` |
| 583 | mock_defaults { |
| 584 | name: "libexample_defaults", |
| 585 | visibility: ["//visibility:private", "//namespace"], |
| 586 | } |
| 587 | mock_library { |
| 588 | name: "libexample", |
| 589 | defaults: ["libexample_defaults"], |
| 590 | }`), |
| 591 | }, |
| 592 | expectedErrors: []string{ |
| 593 | `module "libexample_defaults": visibility: cannot mix "//visibility:private"` + |
| 594 | ` with any other visibility rules`, |
| 595 | }, |
| 596 | }, |
| 597 | { |
| 598 | name: "//visibility:private overriding defaults", |
| 599 | fs: map[string][]byte{ |
| 600 | "top/Blueprints": []byte(` |
| 601 | mock_defaults { |
| 602 | name: "libexample_defaults", |
| 603 | visibility: ["//namespace"], |
| 604 | } |
| 605 | mock_library { |
| 606 | name: "libexample", |
| 607 | visibility: ["//visibility:private"], |
| 608 | defaults: ["libexample_defaults"], |
| 609 | }`), |
| 610 | }, |
| 611 | expectedErrors: []string{ |
| 612 | `module "libexample": visibility: cannot mix "//visibility:private"` + |
| 613 | ` with any other visibility rules`, |
| 614 | }, |
| 615 | }, |
| 616 | { |
| 617 | name: "//visibility:private in defaults overridden", |
| 618 | fs: map[string][]byte{ |
| 619 | "top/Blueprints": []byte(` |
| 620 | mock_defaults { |
| 621 | name: "libexample_defaults", |
| 622 | visibility: ["//visibility:private"], |
| 623 | } |
| 624 | mock_library { |
| 625 | name: "libexample", |
| 626 | visibility: ["//namespace"], |
| 627 | defaults: ["libexample_defaults"], |
| 628 | }`), |
| 629 | }, |
| 630 | expectedErrors: []string{ |
| 631 | `module "libexample": visibility: cannot mix "//visibility:private"` + |
| 632 | ` with any other visibility rules`, |
| 633 | }, |
| 634 | }, |
| 635 | { |
| 636 | name: "//visibility:private mixed with itself", |
| 637 | fs: map[string][]byte{ |
| 638 | "top/Blueprints": []byte(` |
| 639 | mock_defaults { |
| 640 | name: "libexample_defaults_1", |
| 641 | visibility: ["//visibility:private"], |
| 642 | } |
| 643 | mock_defaults { |
| 644 | name: "libexample_defaults_2", |
| 645 | visibility: ["//visibility:private"], |
| 646 | } |
| 647 | mock_library { |
| 648 | name: "libexample", |
| 649 | visibility: ["//visibility:private"], |
| 650 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 651 | }`), |
| 652 | "outsider/Blueprints": []byte(` |
| 653 | mock_library { |
| 654 | name: "liboutsider", |
| 655 | deps: ["libexample"], |
| 656 | }`), |
| 657 | }, |
| 658 | expectedErrors: []string{ |
| 659 | `module "liboutsider" variant "android_common": depends on //top:libexample which is not` + |
| 660 | ` visible to this module`, |
| 661 | }, |
| 662 | }, |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | func TestVisibility(t *testing.T) { |
| 666 | buildDir, err := ioutil.TempDir("", "soong_neverallow_test") |
| 667 | if err != nil { |
| 668 | t.Fatal(err) |
| 669 | } |
| 670 | defer os.RemoveAll(buildDir) |
| 671 | |
| 672 | for _, test := range visibilityTests { |
| 673 | t.Run(test.name, func(t *testing.T) { |
| 674 | _, errs := testVisibility(buildDir, test.fs) |
| 675 | |
| 676 | expectedErrors := test.expectedErrors |
| 677 | if expectedErrors == nil { |
| 678 | FailIfErrored(t, errs) |
| 679 | } else { |
| 680 | for _, expectedError := range expectedErrors { |
| 681 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 682 | } |
| 683 | if len(errs) > len(expectedErrors) { |
| 684 | t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) |
| 685 | for i, expectedError := range expectedErrors { |
| 686 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 687 | } |
| 688 | for i, err := range errs { |
| 689 | t.Errorf("errs[%d] = %s", i, err) |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | }) |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | func testVisibility(buildDir string, fs map[string][]byte) (*TestContext, []error) { |
| 698 | |
| 699 | // Create a new config per test as visibility information is stored in the config. |
| 700 | config := TestArchConfig(buildDir, nil) |
| 701 | |
| 702 | ctx := NewTestArchContext() |
| 703 | ctx.RegisterModuleType("mock_library", ModuleFactoryAdaptor(newMockLibraryModule)) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 704 | ctx.RegisterModuleType("mock_defaults", ModuleFactoryAdaptor(defaultsFactory)) |
| 705 | ctx.PreArchMutators(registerVisibilityRuleChecker) |
| 706 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 707 | ctx.PreArchMutators(registerVisibilityRuleGatherer) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 708 | ctx.PostDepsMutators(registerVisibilityRuleEnforcer) |
| 709 | ctx.Register() |
| 710 | |
| 711 | ctx.MockFileSystem(fs) |
| 712 | |
| 713 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 714 | if len(errs) > 0 { |
| 715 | return ctx, errs |
| 716 | } |
| 717 | |
| 718 | _, errs = ctx.PrepareBuildActions(config) |
| 719 | return ctx, errs |
| 720 | } |
| 721 | |
| 722 | type mockLibraryProperties struct { |
| 723 | Deps []string |
| 724 | } |
| 725 | |
| 726 | type mockLibraryModule struct { |
| 727 | ModuleBase |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 728 | DefaultableModuleBase |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 729 | properties mockLibraryProperties |
| 730 | } |
| 731 | |
| 732 | func newMockLibraryModule() Module { |
| 733 | m := &mockLibraryModule{} |
| 734 | m.AddProperties(&m.properties) |
| 735 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon) |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 736 | InitDefaultableModule(m) |
Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 737 | return m |
| 738 | } |
| 739 | |
| 740 | type dependencyTag struct { |
| 741 | blueprint.BaseDependencyTag |
| 742 | name string |
| 743 | } |
| 744 | |
| 745 | func (j *mockLibraryModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 746 | ctx.AddVariationDependencies(nil, dependencyTag{name: "mockdeps"}, j.properties.Deps...) |
| 747 | } |
| 748 | |
| 749 | func (p *mockLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 750 | } |
Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame^] | 751 | |
| 752 | type mockDefaults struct { |
| 753 | ModuleBase |
| 754 | DefaultsModuleBase |
| 755 | } |
| 756 | |
| 757 | func defaultsFactory() Module { |
| 758 | m := &mockDefaults{} |
| 759 | InitDefaultsModule(m) |
| 760 | return m |
| 761 | } |
| 762 | |
| 763 | func (*mockDefaults) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 764 | } |