|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func fakeAPK(t *testing.T, files map[string]string) string { |
| 12 | + t.Helper() |
| 13 | + path := filepath.Join(t.TempDir(), "applemusic.apk") |
| 14 | + out, err := os.Create(path) |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + z := zip.NewWriter(out) |
| 19 | + for name, contents := range files { |
| 20 | + w, err := z.Create(name) |
| 21 | + if err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + if _, err := w.Write([]byte(contents)); err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + } |
| 28 | + if err := z.Close(); err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + if err := out.Close(); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + return path |
| 35 | +} |
| 36 | + |
| 37 | +func TestInstallCoreADILibraries(t *testing.T) { |
| 38 | + for _, tc := range []struct { |
| 39 | + name string |
| 40 | + contents map[string]string |
| 41 | + wantError bool |
| 42 | + expectedOne, expectedTwo string |
| 43 | + }{ |
| 44 | + {"valid apk", map[string]string{"lib/x86_64/libstoreservicescore.so": "new-stores", "lib/x86_64/libCoreADI.so": "new-core"}, false, "new-stores", "new-core"}, |
| 45 | + {"missing library", map[string]string{"lib/x86_64/libstoreservicescore.so": "new-stores"}, true, "old-stores", "old-core"}, |
| 46 | + {"wrong architecture", map[string]string{"lib/arm64-v8a/libstoreservicescore.so": "new-stores", "lib/arm64-v8a/libCoreADI.so": "new-core"}, true, "old-stores", "old-core"}, |
| 47 | + } { |
| 48 | + t.Run(tc.name, func(t *testing.T) { |
| 49 | + dir := filepath.Join(t.TempDir(), "x86_64") |
| 50 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + for name, value := range map[string]string{"libstoreservicescore.so": "old-stores", "libCoreADI.so": "old-core"} { |
| 54 | + if err := os.WriteFile(filepath.Join(dir, name), []byte(value), 0o644); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + } |
| 58 | + err := installCoreADILibraries(fakeAPK(t, tc.contents), dir, "x86_64") |
| 59 | + if (err != nil) != tc.wantError { |
| 60 | + t.Fatalf("err=%v, wantError=%v", err, tc.wantError) |
| 61 | + } |
| 62 | + for name, want := range map[string]string{"libstoreservicescore.so": tc.expectedOne, "libCoreADI.so": tc.expectedTwo} { |
| 63 | + got, e := os.ReadFile(filepath.Join(dir, name)) |
| 64 | + if e != nil || string(got) != want { |
| 65 | + t.Fatalf("%s: %s / %v; want %s", name, got, e, want) |
| 66 | + } |
| 67 | + } |
| 68 | + entries, e := os.ReadDir(filepath.Dir(dir)) |
| 69 | + if e != nil { |
| 70 | + t.Fatal(e) |
| 71 | + } |
| 72 | + for _, entry := range entries { |
| 73 | + if strings.HasPrefix(entry.Name(), ".coreadi-") { |
| 74 | + t.Fatalf("staging directory left behind: %s", entry.Name()) |
| 75 | + } |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestCorruptAPKPreservesOriginal(t *testing.T) { |
| 82 | + dir := filepath.Join(t.TempDir(), "x86_64") |
| 83 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + old := filepath.Join(dir, "libstoreservicescore.so") |
| 87 | + if err := os.WriteFile(old, []byte("old-stores"), 0o644); err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + invalid := filepath.Join(t.TempDir(), "invalid.apk") |
| 91 | + if err := os.WriteFile(invalid, []byte("bad zip"), 0o644); err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + if err := installCoreADILibraries(invalid, dir, "x86_64"); err == nil { |
| 95 | + t.Fatal("expected invalid archive to fail") |
| 96 | + } |
| 97 | + if got, _ := os.ReadFile(old); string(got) != "old-stores" { |
| 98 | + t.Fatalf("old library modified: %s", got) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestBadSecondLibraryPreservesOriginals(t *testing.T) { |
| 103 | + first := "lib/x86_64/libstoreservicescore.so" |
| 104 | + second := "lib/x86_64/libCoreADI.so" |
| 105 | + archive := filepath.Join(t.TempDir(), "corrupt.apk") |
| 106 | + out, err := os.Create(archive) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + writer := zip.NewWriter(out) |
| 111 | + for _, file := range []struct{ name, data string }{{first, "first-library-good"}, {second, "second-library-corrupt"}} { |
| 112 | + entry, err := writer.CreateHeader(&zip.FileHeader{Name: file.name, Method: zip.Store}) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + if _, err := entry.Write([]byte(file.data)); err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + } |
| 119 | + } |
| 120 | + if err := writer.Close(); err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + if err := out.Close(); err != nil { |
| 124 | + t.Fatal(err) |
| 125 | + } |
| 126 | + bytes, err := os.ReadFile(archive) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + off := strings.Index(string(bytes), "second-library-corrupt") |
| 131 | + if off < 0 { |
| 132 | + t.Fatal("second library not found in fixture") |
| 133 | + } |
| 134 | + bytes[off] = 'X' // ZIP structure intact; CRC must fail for second file |
| 135 | + if err := os.WriteFile(archive, bytes, 0o600); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + dest := filepath.Join(t.TempDir(), "lib", "x86_64") |
| 139 | + if err := os.MkdirAll(dest, 0o755); err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + for name, value := range map[string]string{"libstoreservicescore.so": "old-one", "libCoreADI.so": "old-two"} { |
| 143 | + if err := os.WriteFile(filepath.Join(dest, name), []byte(value), 0o644); err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | + } |
| 147 | + if err := installCoreADILibraries(archive, dest, "x86_64"); err == nil { |
| 148 | + t.Fatal("want CRC error") |
| 149 | + } |
| 150 | + for name, value := range map[string]string{"libstoreservicescore.so": "old-one", "libCoreADI.so": "old-two"} { |
| 151 | + b, err := os.ReadFile(filepath.Join(dest, name)) |
| 152 | + if err != nil || string(b) != value { |
| 153 | + t.Fatalf("%s changed to %q (%v)", name, b, err) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments