aapt_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package tests
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/json"
  6. "os"
  7. "testing"
  8. "notabug.org/Umnik/GoAndroidSDK/v2/components/sdk/buildTools/aapt"
  9. )
  10. func newAapt(t *testing.T, funcName string) *aapt.Aapt {
  11. t.Helper()
  12. a, err := aapt.NewAaptLastVersion(*newSdk(t, t.Name()))
  13. if !testErr(t, funcName, err) {
  14. return nil
  15. }
  16. return a
  17. }
  18. func aaptInfo(t *testing.T, file string, funcName string) *aapt.AppInfo {
  19. t.Helper()
  20. aw := newAapt(t, funcName)
  21. b, err := aw.DumpAppInfo(file)
  22. if !testErr(t, t.Name(), err) {
  23. t.Error(err)
  24. return nil
  25. }
  26. res := aapt.ParseAppInfoDumpFromBytes(b)
  27. return &res
  28. }
  29. func TestAaptVersion(t *testing.T) {
  30. t.Parallel()
  31. aw := newAapt(t, t.Name())
  32. b, err := aw.Version()
  33. if !testErr(t, t.Name(), err) {
  34. return
  35. }
  36. if !bytes.HasPrefix(b, []byte("Android Asset Packaging Tool, v")) {
  37. t.Errorf("%v: cannot find version in %q", t.Name(), b)
  38. }
  39. }
  40. func TestAaptMinSdk(t *testing.T) {
  41. t.Parallel()
  42. info := aaptInfo(t, goodApkFile, t.Name())
  43. if info.MinSDKVersion != 23 {
  44. t.Errorf("%v: min sdk version is %v", t.Name(), info.MinSDKVersion)
  45. }
  46. }
  47. func TestAaptTargetSdk(t *testing.T) {
  48. t.Parallel()
  49. info := aaptInfo(t, goodApkFile, t.Name())
  50. if info.TargetSDKVersion != 32 {
  51. t.Errorf("%v: target sdk version is %v", t.Name(), info.TargetSDKVersion)
  52. }
  53. }
  54. func TestAaptRemoveSign(t *testing.T) {
  55. t.Parallel()
  56. temp, err := os.CreateTemp("", "aapt-*")
  57. if !testErr(t, t.Name(), err) {
  58. return
  59. }
  60. defer removeOsFile(t, t.Name(), temp.Name())
  61. if !makeCopy(t, t.Name(), temp, goodApkFile) {
  62. return
  63. }
  64. a := newAapt(t, t.Name())
  65. if _, err = a.RemoveSignature(temp.Name()); err != nil {
  66. t.Fatal(err)
  67. }
  68. tmpReader, err := zip.OpenReader(temp.Name())
  69. if !testErr(t, t.Name(), err) {
  70. return
  71. }
  72. defer closeZipFile(t, t.Name(), tmpReader)
  73. for _, fileName := range tmpReader.File {
  74. if fileName.Name == "META-INF/CERT.SF" ||
  75. fileName.Name == "META-INF/CERT.DSA" ||
  76. fileName.Name == "META-INF/MANIFEST.MF" {
  77. t.Errorf("File %q was not removed", fileName.Name)
  78. }
  79. }
  80. }
  81. func TestAaptJson(t *testing.T) {
  82. t.Parallel()
  83. info := aaptInfo(t, splitBaseFile, t.Name())
  84. b, err := json.MarshalIndent(info, "", "\t")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. t.Logf("%s", b)
  89. }
  90. func TestApptEmptyVal(t *testing.T) {
  91. t.Parallel()
  92. info := aaptInfo(t, emptyValFile, t.Name())
  93. b, err := json.MarshalIndent(info, "", "\t")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if info.PackageName == "" || info.VersionName == "" || info.VersionCode < 0 {
  98. t.Error("can't parse package line")
  99. }
  100. t.Logf("%s", b)
  101. }