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