forked from kiethen/ipapk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
152 lines (141 loc) · 3.53 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ipapk
import (
"archive/zip"
"bytes"
"image/png"
"os"
"strings"
"testing"
)
func getAppZipReader(filename string) (*zip.Reader, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
stat, err := file.Stat()
if err != nil {
return nil, err
}
reader, err := zip.NewReader(file, stat.Size())
if err != nil {
return nil, err
}
return reader, nil
}
func getAndroidManifest() (*zip.File, error) {
reader, err := getAppZipReader("testdata/helloworld.apk")
if err != nil {
return nil, err
}
var xmlFile *zip.File
for _, f := range reader.File {
if f.Name == "AndroidManifest.xml" {
xmlFile = f
break
}
}
return xmlFile, nil
}
func TestParseAndroidManifest(t *testing.T) {
xmlFile, err := getAndroidManifest()
if err != nil {
t.Errorf("got %v want no error", err)
}
manifest, err := parseAndroidManifest(xmlFile)
if err != nil {
t.Errorf("got %v want no error", err)
}
if manifest.Package != "com.example.helloworld" {
t.Errorf("got %v want %v", manifest.Package, "com.example.helloworld")
}
if manifest.VersionName != "1.0" {
t.Errorf("got %v want %v", manifest.VersionName, "1.0")
}
if manifest.VersionCode != "1" {
t.Errorf("got %v want %v", manifest.VersionCode, "1")
}
}
func TestParseApkFile(t *testing.T) {
xmlFile, err := getAndroidManifest()
if err != nil {
t.Errorf("got %v want no error", err)
}
apk, err := parseApkFile(xmlFile)
if err != nil {
t.Errorf("got %v want no error", err)
}
if apk.BundleId != "com.example.helloworld" {
t.Errorf("got %v want %v", apk.BundleId, "com.example.helloworld")
}
if apk.Version != "1.0" {
t.Errorf("got %v want %v", apk.Version, "1.0")
}
if apk.Build != "1" {
t.Errorf("got %v want %v", apk.Build, "1")
}
}
func TestParseApkIconAndLabel(t *testing.T) {
icon, label, err := parseApkIconAndLabel("testdata/helloworld.apk")
if err != nil {
t.Errorf("got %v want no error", err)
}
buf := new(bytes.Buffer)
if err := png.Encode(buf, icon); err != nil {
t.Errorf("got %v want no error", err)
}
if len(buf.Bytes()) != 10223 {
t.Errorf("got %v want %v", len(buf.Bytes()), 10223)
}
if label != "HelloWorld" {
t.Errorf("got %v want %v", label, "HelloWorld")
}
}
func getIosPlist() (*zip.File, error) {
reader, err := getAppZipReader("testdata/helloworld.ipa")
if err != nil {
return nil, err
}
var plistFile *zip.File
for _, f := range reader.File {
if reInfoPlist.MatchString(f.Name) {
plistFile = f
break
}
}
return plistFile, nil
}
func TestParseIpaFile(t *testing.T) {
plistFile, err := getIosPlist()
if err != nil {
t.Errorf("got %v want no error", err)
}
ipa, err := parseIpaFile(plistFile)
if err != nil {
t.Errorf("got %v want no error", err)
}
if ipa.BundleId != "com.kthcorp.helloworld" {
t.Errorf("got %v want %v", ipa.BundleId, "com.kthcorp.helloworld")
}
if ipa.Version != "1.0" {
t.Errorf("got %v want %v", ipa.Version, "1.0")
}
if ipa.Build != "1.0" {
t.Errorf("got %v want %v", ipa.Build, "1.0")
}
}
func TestParseIpaIcon(t *testing.T) {
reader, err := getAppZipReader("testdata/helloworld.ipa")
if err != nil {
t.Errorf("got %v want no error", err)
}
var iconFile *zip.File
for _, f := range reader.File {
if strings.Contains(f.Name, "AppIcon60x60") {
iconFile = f
break
}
}
if _, err := parseIpaIcon(iconFile); err.Error() != "Icon is not found" {
t.Errorf("got %v want %v", err, "Icon is not found")
}
}