writer_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "crypto/x509"
  7. "encoding/pem"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "testing"
  12. )
  13. func TestWriter(t *testing.T) {
  14. if os.Getenv("GO_BUILDER_NAME") == "linux-amd64-androidemu" {
  15. t.Skip("skipping on linux-amd64-androidemu builder; see golang.org/issue/40290")
  16. }
  17. block, _ := pem.Decode([]byte(debugCert))
  18. if block == nil {
  19. t.Fatal("no cert")
  20. }
  21. privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. f, err := os.CreateTemp("", "testapk-")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. f.Close()
  30. defer os.Remove(f.Name())
  31. apkPath := f.Name() + ".apk"
  32. f, err = os.Create(apkPath)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer os.Remove(apkPath)
  37. apkw := NewWriter(f, privKey)
  38. w, err := apkw.Create("AndroidManifest.xml")
  39. if err != nil {
  40. t.Fatalf("could not create AndroidManifest.xml: %v", err)
  41. }
  42. if _, err := w.Write([]byte(androidManifest)); err != nil {
  43. t.Errorf("could not write AndroidManifest.xml: %v", err)
  44. }
  45. w, err = apkw.Create("assets/hello_world.txt")
  46. if err != nil {
  47. t.Fatalf("could not create assets/hello_world.txt: %v", err)
  48. }
  49. if _, err := w.Write([]byte("Hello, 世界")); err != nil {
  50. t.Errorf("could not write assets/hello_world.txt: %v", err)
  51. }
  52. if err := apkw.Close(); err != nil {
  53. t.Fatal(err)
  54. }
  55. if exec.Command("which", "aapt").Run() != nil {
  56. t.Skip("command aapt not found, skipping")
  57. }
  58. out, err := exec.Command("aapt", "list", "-a", apkPath).CombinedOutput()
  59. aaptGot := string(out)
  60. if err != nil {
  61. t.Logf("aapt:\n%s", aaptGot)
  62. t.Fatalf("aapt failed: %v", err)
  63. }
  64. if aaptGot != aaptWant {
  65. t.Errorf("unexpected output from aapt")
  66. d, err := diff(aaptGot, aaptWant)
  67. if err != nil {
  68. t.Errorf("diff failed: %v", err)
  69. } else {
  70. t.Logf("%s", d)
  71. }
  72. }
  73. }
  74. const aaptWant = `AndroidManifest.xml
  75. assets/hello_world.txt
  76. META-INF/MANIFEST.MF
  77. META-INF/CERT.SF
  78. META-INF/CERT.RSA
  79. Resource table:
  80. Package Groups (0)
  81. Android manifest:
  82. N: android=http://schemas.android.com/apk/res/android
  83. E: manifest (line=2)
  84. A: package="org.golang.fakeapp" (Raw: "org.golang.fakeapp")
  85. A: android:versionCode(0x0101021b)=(type 0x10)0x1
  86. A: android:versionName(0x0101021c)="1.0" (Raw: "1.0")
  87. E: uses-sdk (line=8)
  88. A: android:minSdkVersion(0x0101020c)=(type 0x10)0xf
  89. E: application (line=9)
  90. A: android:label(0x01010001)="FakeApp" (Raw: "FakeApp")
  91. A: android:hasCode(0x0101000c)=(type 0x12)0x0
  92. A: android:debuggable(0x0101000f)=(type 0x12)0xffffffff
  93. E: activity (line=10)
  94. A: android:name(0x01010003)="android.app.NativeActivity" (Raw: "android.app.NativeActivity")
  95. A: android:label(0x01010001)="FakeApp" (Raw: "FakeApp")
  96. A: android:configChanges(0x0101001f)=(type 0x11)0xa0
  97. E: intent-filter (line=14)
  98. E: action (line=15)
  99. A: android:name(0x01010003)="android.intent.action.MAIN" (Raw: "android.intent.action.MAIN")
  100. E: category (line=16)
  101. A: android:name(0x01010003)="android.intent.category.LAUNCHER" (Raw: "android.intent.category.LAUNCHER")
  102. `
  103. const androidManifest = `
  104. <manifest
  105. xmlns:android="http://schemas.android.com/apk/res/android"
  106. package="org.golang.fakeapp"
  107. android:versionCode="1"
  108. android:versionName="1.0">
  109. <application android:label="FakeApp" android:hasCode="false" android:debuggable="true">
  110. <activity android:name="android.app.NativeActivity"
  111. android:label="FakeApp"
  112. android:configChanges="orientation|keyboardHidden">
  113. <intent-filter>
  114. <action android:name="android.intent.action.MAIN" />
  115. <category android:name="android.intent.category.LAUNCHER" />
  116. </intent-filter>
  117. </activity>
  118. </application>
  119. </manifest>
  120. `
  121. func writeTempFile(data string) (string, error) {
  122. f, err := os.CreateTemp("", "gofmt")
  123. if err != nil {
  124. return "", err
  125. }
  126. _, err = io.WriteString(f, data)
  127. errc := f.Close()
  128. if err == nil {
  129. return f.Name(), errc
  130. }
  131. return f.Name(), err
  132. }
  133. func diff(got, want string) (string, error) {
  134. wantPath, err := writeTempFile(want)
  135. if err != nil {
  136. return "", err
  137. }
  138. defer os.Remove(wantPath)
  139. gotPath, err := writeTempFile(got)
  140. if err != nil {
  141. return "", err
  142. }
  143. defer os.Remove(gotPath)
  144. data, err := exec.Command("diff", "-u", wantPath, gotPath).CombinedOutput()
  145. if len(data) > 0 {
  146. // diff exits with a non-zero status when the files don't match.
  147. // Ignore that failure as long as we get output.
  148. err = nil
  149. }
  150. return string(data), err
  151. }