| 1 | package apkparser_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/xml" |
| 7 | "fmt" |
| 8 | "github.com/avast/apkparser" |
| 9 | "io" |
| 10 | "io/ioutil" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | func Example() { |
| 18 | enc := xml.NewEncoder(os.Stdout) |
| 19 | enc.Indent("", "\t") |
| 20 | zipErr, resErr, manErr := apkparser.ParseApk(os.Args[1], enc) |
| 21 | if zipErr != nil { |
| 22 | fmt.Fprintf(os.Stderr, "Failed to open the APK: %s", zipErr.Error()) |
| 23 | os.Exit(1) |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | if resErr != nil { |
| 28 | fmt.Fprintf(os.Stderr, "Failed to parse resources: %s", resErr.Error()) |
| 29 | } |
| 30 | if manErr != nil { |
| 31 | fmt.Fprintf(os.Stderr, "Failed to parse AndroidManifest.xml: %s", manErr.Error()) |
| 32 | os.Exit(1) |
| 33 | return |
| 34 | } |
| 35 | fmt.Println() |
| 36 | } |
| 37 | |
| 38 | func testExpectedOutputFile(t *testing.T, fnBase string) { |
| 39 | in, err := os.Open(fnBase + ".bin") |
| 40 | if err != nil { |
| 41 | t.Fatalf("failed to open file %s.bin: %s", fnBase, err.Error()) |
| 42 | return |
| 43 | } |
| 44 | defer in.Close() |
| 45 | |
| 46 | hasher := sha256.New() |
| 47 | enc := xml.NewEncoder(hasher) |
| 48 | enc.Indent("", " ") |
| 49 | |
| 50 | if err := apkparser.ParseManifest(in, enc, nil); err != nil { |
| 51 | t.Fatalf("failed to parse manifest %s.bin: %s", fnBase, err.Error()) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | hasher.Write([]byte("\n")) |
| 56 | |
| 57 | currentSha256 := hasher.Sum(nil) |
| 58 | hasher.Reset() |
| 59 | |
| 60 | expected, err := os.Open(fnBase + ".xml") |
| 61 | if err != nil { |
| 62 | t.Fatalf("failed to open file %s.xml: %s", fnBase, err.Error()) |
| 63 | return |
| 64 | } |
| 65 | defer expected.Close() |
| 66 | |
| 67 | if _, err := io.Copy(hasher, expected); err != nil { |
| 68 | t.Fatalf("failed to read %s.xml: %s", fnBase, err.Error()) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | if expectedSha256 := hasher.Sum(nil); !bytes.Equal(currentSha256, expectedSha256) { |
| 73 | t.Fatalf("parsing %s.bin does not produce the expected output!", fnBase) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestExpectedOutput(t *testing.T) { |
| 78 | files, err := ioutil.ReadDir("testdata") |
| 79 | if err != nil { |
| 80 | t.Fatalf("failed to open testdata directory: %s", err.Error()) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | for _, fi := range files { |
| 85 | if fi.IsDir() || !strings.HasSuffix(fi.Name(), ".bin") { |
| 86 | continue |
| 87 | } |
| 88 | |
| 89 | base := fi.Name()[:len(fi.Name())-4] |
| 90 | t.Run(base, func(t *testing.T) { |
| 91 | testExpectedOutputFile(t, filepath.Join("testdata", base)) |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestPlainManifest(t *testing.T) { |
| 97 | plainManifests := []string{ |
| 98 | `<?xml version="1.0" encoding="utf-8" standalone="no"?>`, |
| 99 | `<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">`, |
| 100 | } |
| 101 | |
| 102 | enc := xml.NewEncoder(ioutil.Discard) |
| 103 | |
| 104 | for _, man := range plainManifests { |
| 105 | r := strings.NewReader(man) |
| 106 | if err := apkparser.ParseManifest(r, enc, nil); err != apkparser.ErrPlainTextManifest { |
| 107 | t.Fatalf("failed to produce ErrPlainTextManifest on string '%s', got '%v' instead", man, err) |
| 108 | return |
| 109 | } |
| 110 | } |
| 111 | } |