license_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package android
  2. import (
  3. "testing"
  4. )
  5. // Common test set up for license tests.
  6. var prepareForLicenseTest = GroupFixturePreparers(
  7. // General preparers in alphabetical order.
  8. PrepareForTestWithDefaults,
  9. PrepareForTestWithLicenses,
  10. PrepareForTestWithOverrides,
  11. PrepareForTestWithPackageModule,
  12. PrepareForTestWithPrebuilts,
  13. PrepareForTestWithVisibility,
  14. // Additional test specific stuff
  15. prepareForTestWithFakePrebuiltModules,
  16. FixtureMergeEnv(map[string]string{"ANDROID_REQUIRE_LICENSES": "1"}),
  17. )
  18. var licenseTests = []struct {
  19. name string
  20. fs MockFS
  21. expectedErrors []string
  22. }{
  23. {
  24. name: "license must not accept licenses property",
  25. fs: map[string][]byte{
  26. "top/Android.bp": []byte(`
  27. license {
  28. name: "top_license",
  29. visibility: ["//visibility:private"],
  30. licenses: ["other_license"],
  31. }`),
  32. },
  33. expectedErrors: []string{
  34. `top/Android.bp:5:14: unrecognized property "licenses"`,
  35. },
  36. },
  37. {
  38. name: "private license",
  39. fs: map[string][]byte{
  40. "top/Android.bp": []byte(`
  41. license_kind {
  42. name: "top_notice",
  43. conditions: ["notice"],
  44. visibility: ["//visibility:private"],
  45. }
  46. license {
  47. name: "top_allowed_as_notice",
  48. license_kinds: ["top_notice"],
  49. visibility: ["//visibility:private"],
  50. }`),
  51. "other/Android.bp": []byte(`
  52. rule {
  53. name: "arule",
  54. licenses: ["top_allowed_as_notice"],
  55. }`),
  56. "yetmore/Android.bp": []byte(`
  57. package {
  58. default_applicable_licenses: ["top_allowed_as_notice"],
  59. }`),
  60. },
  61. expectedErrors: []string{
  62. `other/Android.bp:2:5: module "arule": depends on //top:top_allowed_as_notice ` +
  63. `which is not visible to this module`,
  64. `yetmore/Android.bp:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` +
  65. `which is not visible to this module`,
  66. },
  67. },
  68. {
  69. name: "must reference license_kind module",
  70. fs: map[string][]byte{
  71. "top/Android.bp": []byte(`
  72. rule {
  73. name: "top_by_exception_only",
  74. }
  75. license {
  76. name: "top_proprietary",
  77. license_kinds: ["top_by_exception_only"],
  78. visibility: ["//visibility:public"],
  79. }`),
  80. },
  81. expectedErrors: []string{
  82. `top/Android.bp:6:5: module "top_proprietary": license_kinds property ` +
  83. `"top_by_exception_only" is not a license_kind module`,
  84. },
  85. },
  86. {
  87. name: "license_kind module must exist",
  88. fs: map[string][]byte{
  89. "top/Android.bp": []byte(`
  90. license {
  91. name: "top_notice_allowed",
  92. license_kinds: ["top_notice"],
  93. visibility: ["//visibility:public"],
  94. }`),
  95. },
  96. expectedErrors: []string{
  97. `top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
  98. },
  99. },
  100. {
  101. name: "public license",
  102. fs: map[string][]byte{
  103. "top/Android.bp": []byte(`
  104. license_kind {
  105. name: "top_by_exception_only",
  106. conditions: ["by_exception_only"],
  107. visibility: ["//visibility:private"],
  108. }
  109. license {
  110. name: "top_proprietary",
  111. license_kinds: ["top_by_exception_only"],
  112. visibility: ["//visibility:public"],
  113. }`),
  114. "other/Android.bp": []byte(`
  115. rule {
  116. name: "arule",
  117. licenses: ["top_proprietary"],
  118. }`),
  119. "yetmore/Android.bp": []byte(`
  120. package {
  121. default_applicable_licenses: ["top_proprietary"],
  122. }`),
  123. },
  124. },
  125. {
  126. name: "multiple licenses",
  127. fs: map[string][]byte{
  128. "top/Android.bp": []byte(`
  129. package {
  130. default_applicable_licenses: ["top_proprietary"],
  131. }
  132. license_kind {
  133. name: "top_notice",
  134. conditions: ["notice"],
  135. }
  136. license_kind {
  137. name: "top_by_exception_only",
  138. conditions: ["by_exception_only"],
  139. visibility: ["//visibility:public"],
  140. }
  141. license {
  142. name: "top_allowed_as_notice",
  143. license_kinds: ["top_notice"],
  144. }
  145. license {
  146. name: "top_proprietary",
  147. license_kinds: ["top_by_exception_only"],
  148. visibility: ["//visibility:public"],
  149. }
  150. rule {
  151. name: "myrule",
  152. licenses: ["top_allowed_as_notice", "top_proprietary"]
  153. }`),
  154. "other/Android.bp": []byte(`
  155. rule {
  156. name: "arule",
  157. licenses: ["top_proprietary"],
  158. }`),
  159. "yetmore/Android.bp": []byte(`
  160. package {
  161. default_applicable_licenses: ["top_proprietary"],
  162. }`),
  163. },
  164. },
  165. }
  166. func TestLicense(t *testing.T) {
  167. for _, test := range licenseTests {
  168. t.Run(test.name, func(t *testing.T) {
  169. // Customize the common license text fixture factory.
  170. GroupFixturePreparers(
  171. prepareForLicenseTest,
  172. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  173. ctx.RegisterModuleType("rule", newMockRuleModule)
  174. }),
  175. test.fs.AddToFixture(),
  176. ).
  177. ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
  178. RunTest(t)
  179. })
  180. }
  181. }
  182. func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
  183. }
  184. type mockRuleModule struct {
  185. ModuleBase
  186. DefaultableModuleBase
  187. }
  188. func newMockRuleModule() Module {
  189. m := &mockRuleModule{}
  190. InitAndroidModule(m)
  191. InitDefaultableModule(m)
  192. return m
  193. }
  194. func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
  195. }