aapt_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. return nil
  24. }
  25. res := aapt.ParseAppInfoDumpFromBytes(b)
  26. return &res
  27. }
  28. func TestAaptVersion(t *testing.T) {
  29. t.Parallel()
  30. aw := newAapt(t, t.Name())
  31. b, err := aw.Version()
  32. if !testErr(t, t.Name(), err) {
  33. return
  34. }
  35. if !bytes.HasPrefix(b, []byte("Android Asset Packaging Tool, v")) {
  36. t.Errorf("%v: cannot find version in %q", t.Name(), b)
  37. }
  38. }
  39. func TestAaptMinSdk(t *testing.T) {
  40. t.Parallel()
  41. info := aaptInfo(t, goodApkFile(), t.Name())
  42. if info.MinSDKVersion != 23 {
  43. t.Errorf("%v: min sdk version is %v", t.Name(), info.MinSDKVersion)
  44. }
  45. }
  46. func TestAaptTargetSdk(t *testing.T) {
  47. t.Parallel()
  48. info := aaptInfo(t, goodApkFile(), t.Name())
  49. if info.TargetSDKVersion != 32 {
  50. t.Errorf("%v: target sdk version is %v", t.Name(), info.TargetSDKVersion)
  51. }
  52. }
  53. func TestAaptRemoveSign(t *testing.T) {
  54. t.Parallel()
  55. temp, err := os.CreateTemp("", "aapt-*")
  56. if !testErr(t, t.Name(), err) {
  57. return
  58. }
  59. defer removeOsFile(t, t.Name(), temp.Name())
  60. if !makeCopy(t, t.Name(), temp, goodApkFile()) {
  61. return
  62. }
  63. a := newAapt(t, t.Name())
  64. if _, err = a.RemoveSignature(temp.Name()); err != nil {
  65. t.Fatal(err)
  66. }
  67. tmpReader, err := zip.OpenReader(temp.Name())
  68. if !testErr(t, t.Name(), err) {
  69. return
  70. }
  71. defer closeZipFile(t, t.Name(), tmpReader)
  72. for _, fileName := range tmpReader.File {
  73. if fileName.Name == "META-INF/CERT.SF" ||
  74. fileName.Name == "META-INF/CERT.DSA" ||
  75. fileName.Name == "META-INF/MANIFEST.MF" {
  76. t.Errorf("File %q was not removed", fileName.Name)
  77. }
  78. }
  79. }
  80. func TestAaptJson(t *testing.T) {
  81. t.Parallel()
  82. info := aaptInfo(t, split_base(), t.Name())
  83. b, err := json.MarshalIndent(info, "", "\t")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. t.Logf("%s", b)
  88. }