license_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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: "must not duplicate license_kind",
  88. fs: map[string][]byte{
  89. "top/Android.bp": []byte(`
  90. license_kind {
  91. name: "top_by_exception_only",
  92. conditions: ["by_exception_only"],
  93. visibility: ["//visibility:private"],
  94. }
  95. license_kind {
  96. name: "top_by_exception_only_2",
  97. conditions: ["by_exception_only"],
  98. visibility: ["//visibility:private"],
  99. }
  100. license {
  101. name: "top_proprietary",
  102. license_kinds: [
  103. "top_by_exception_only",
  104. "top_by_exception_only_2",
  105. "top_by_exception_only"
  106. ],
  107. visibility: ["//visibility:public"],
  108. }`),
  109. },
  110. expectedErrors: []string{
  111. `top/Android.bp:14:5: module "top_proprietary": Duplicated license kind: "top_by_exception_only"`,
  112. },
  113. },
  114. {
  115. name: "license_kind module must exist",
  116. fs: map[string][]byte{
  117. "top/Android.bp": []byte(`
  118. license {
  119. name: "top_notice_allowed",
  120. license_kinds: ["top_notice"],
  121. visibility: ["//visibility:public"],
  122. }`),
  123. },
  124. expectedErrors: []string{
  125. `top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
  126. },
  127. },
  128. {
  129. name: "public license",
  130. fs: map[string][]byte{
  131. "top/Android.bp": []byte(`
  132. license_kind {
  133. name: "top_by_exception_only",
  134. conditions: ["by_exception_only"],
  135. visibility: ["//visibility:private"],
  136. }
  137. license {
  138. name: "top_proprietary",
  139. license_kinds: ["top_by_exception_only"],
  140. visibility: ["//visibility:public"],
  141. }`),
  142. "other/Android.bp": []byte(`
  143. rule {
  144. name: "arule",
  145. licenses: ["top_proprietary"],
  146. }`),
  147. "yetmore/Android.bp": []byte(`
  148. package {
  149. default_applicable_licenses: ["top_proprietary"],
  150. }`),
  151. },
  152. },
  153. {
  154. name: "multiple licenses",
  155. fs: map[string][]byte{
  156. "top/Android.bp": []byte(`
  157. package {
  158. default_applicable_licenses: ["top_proprietary"],
  159. }
  160. license_kind {
  161. name: "top_notice",
  162. conditions: ["notice"],
  163. }
  164. license_kind {
  165. name: "top_by_exception_only",
  166. conditions: ["by_exception_only"],
  167. visibility: ["//visibility:public"],
  168. }
  169. license {
  170. name: "top_allowed_as_notice",
  171. license_kinds: ["top_notice"],
  172. }
  173. license {
  174. name: "top_proprietary",
  175. license_kinds: ["top_by_exception_only"],
  176. visibility: ["//visibility:public"],
  177. }
  178. rule {
  179. name: "myrule",
  180. licenses: ["top_allowed_as_notice", "top_proprietary"]
  181. }`),
  182. "other/Android.bp": []byte(`
  183. rule {
  184. name: "arule",
  185. licenses: ["top_proprietary"],
  186. }`),
  187. "yetmore/Android.bp": []byte(`
  188. package {
  189. default_applicable_licenses: ["top_proprietary"],
  190. }`),
  191. },
  192. },
  193. }
  194. func TestLicense(t *testing.T) {
  195. for _, test := range licenseTests {
  196. t.Run(test.name, func(t *testing.T) {
  197. // Customize the common license text fixture factory.
  198. GroupFixturePreparers(
  199. prepareForLicenseTest,
  200. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  201. ctx.RegisterModuleType("rule", newMockRuleModule)
  202. }),
  203. test.fs.AddToFixture(),
  204. ).
  205. ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
  206. RunTest(t)
  207. })
  208. }
  209. }
  210. func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
  211. }
  212. type mockRuleModule struct {
  213. ModuleBase
  214. DefaultableModuleBase
  215. }
  216. func newMockRuleModule() Module {
  217. m := &mockRuleModule{}
  218. InitAndroidModule(m)
  219. InitDefaultableModule(m)
  220. return m
  221. }
  222. func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
  223. }