Skip to content

Commit ebe2358

Browse files
committed
fix(settings): reset anisette data with the engine command
Replace the manual deletion of state.plist, identifier and adb.pb with `plumesign anisette reset`. The engine owns the location of its provisioning data and also drops the `anisette` file, which the previous implementation left behind, so a reset could still reuse stale data. Move the operation from the service layer to the manager package, which already wraps the engine commands, and cover the endpoint with a test that asserts the exact argv.
1 parent ca8f66f commit ebe2358

5 files changed

Lines changed: 98 additions & 17 deletions

File tree

‎internal/manager/anisette.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package manager
2+
3+
import (
4+
"github.com/bitxeno/atvloadly/internal/exec"
5+
"github.com/bitxeno/atvloadly/internal/log"
6+
)
7+
8+
// ResetAnisette deletes the local Anisette provisioning data through the
9+
// engine, so it is generated again on the next login or install. The engine
10+
// owns the location of its data and drops every file that belongs to it; the
11+
// command is local, so it needs no working directory or proxy environment.
12+
func ResetAnisette() error {
13+
if _, err := exec.NewCommand("plumesign", "anisette", "reset").CombinedOutput(); err != nil {
14+
log.Err(err).Msg("Error resetting anisette data")
15+
return err
16+
}
17+
log.Infof("Reset anisette data")
18+
return nil
19+
}

‎internal/manager/anisette_test.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package manager
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
)
9+
10+
// fakeAnisetteEngine puts a plumesign on PATH that records the arguments of the
11+
// anisette command it is given and exits with exitCode.
12+
func fakeAnisetteEngine(t *testing.T, exitCode string) string {
13+
t.Helper()
14+
dir := t.TempDir()
15+
record := filepath.Join(dir, "args")
16+
script := "#!/bin/sh\n" +
17+
"printf '%s\\n' \"$@\" > " + record + "\n" +
18+
"exit " + exitCode + "\n"
19+
if err := os.WriteFile(filepath.Join(dir, "plumesign"), []byte(script), 0o755); err != nil {
20+
t.Fatal(err)
21+
}
22+
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
23+
return record
24+
}
25+
26+
// Anisette data is reset by the engine rather than by deleting files this
27+
// process guesses, so the engine owns the location of its provisioning data.
28+
func TestResetAnisetteRunsEngineCommand(t *testing.T) {
29+
record := fakeAnisetteEngine(t, "0")
30+
31+
if err := ResetAnisette(); err != nil {
32+
t.Fatalf("ResetAnisette() error = %v", err)
33+
}
34+
35+
if got := strings.Join(recordedArgs(t, record), " "); got != "anisette reset" {
36+
t.Errorf("arguments = %q, want %q", got, "anisette reset")
37+
}
38+
}
39+
40+
// A failing engine surfaces its error instead of reporting a successful reset.
41+
func TestResetAnisetteReportsEngineFailure(t *testing.T) {
42+
fakeAnisetteEngine(t, "1")
43+
44+
if err := ResetAnisette(); err == nil {
45+
t.Fatal("ResetAnisette() error = nil, want an error")
46+
}
47+
}

‎internal/service/service.go‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,6 @@ func UpdateCoreADI() error {
268268
return nil
269269
}
270270

271-
// ResetAnisette removes the Anisette provisioning state so the next login or
272-
// install provisions it again. It mirrors upstream impactor ResetAnisetteData
273-
// which deletes the provisioning files under <data_path>
274-
// (see claration/impactor@321eac4).
275-
func ResetAnisette() error {
276-
for _, name := range []string{"state.plist", "identifier", "adb.pb"} {
277-
path := filepath.Join(app.SideloadDataDir(), name)
278-
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
279-
return err
280-
}
281-
}
282-
log.Infof("Reset Anisette data. dir: %s", app.SideloadDataDir())
283-
return nil
284-
}
285-
286271
func ImportPairingFile(ip string, port string, data []byte) error {
287272
// Call manager to process the file
288273
if err := manager.ImportPairingFile(ip, port, data); err != nil {

‎web/router.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,9 +847,9 @@ func route(fi *fiber.App) {
847847
return c.Status(http.StatusOK).JSON(apiSuccess(true))
848848
})
849849

850-
// Delete the Anisette provisioning state (state.plist); it is recreated on next use
850+
// Reset the Anisette data through the engine; it is generated again on next use
851851
api.Post("/settings/reset/anisette", func(c *fiber.Ctx) error {
852-
if err := service.ResetAnisette(); err != nil {
852+
if err := manager.ResetAnisette(); err != nil {
853853
return c.Status(http.StatusOK).JSON(apiError("reset failed: " + err.Error()))
854854
}
855855
return c.Status(http.StatusOK).JSON(apiSuccess(true))

‎web/settings_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net/http"
77
"net/http/httptest"
8+
"os"
89
"path/filepath"
910
"strings"
1011
"testing"
@@ -180,3 +181,32 @@ func TestSaveUpdateSettingsReplacesAndClearsToken(t *testing.T) {
180181
t.Fatalf("masked preview accepted, code = %d", result.Code)
181182
}
182183
}
184+
185+
// The reset endpoint delegates to the engine command; the flag it reports is
186+
// the engine result, so a missing engine is reported as a failure.
187+
func TestResetAnisetteRouteRunsEngineCommand(t *testing.T) {
188+
engineDir := t.TempDir()
189+
record := filepath.Join(engineDir, "args")
190+
script := "#!/bin/sh\n" +
191+
"printf '%s\\n' \"$@\" > " + record + "\n" +
192+
"exit 0\n"
193+
if err := os.WriteFile(filepath.Join(engineDir, "plumesign"), []byte(script), 0o755); err != nil {
194+
t.Fatal(err)
195+
}
196+
t.Setenv("PATH", engineDir+string(os.PathListSeparator)+os.Getenv("PATH"))
197+
198+
server, _ := newTestServer(t)
199+
resp, err := server.Test(httptest.NewRequest(http.MethodPost, "/api/settings/reset/anisette", nil))
200+
if err != nil {
201+
t.Fatal(err)
202+
}
203+
_ = decodeAPI(t, resp)
204+
205+
data, err := os.ReadFile(record)
206+
if err != nil {
207+
t.Fatalf("the engine was not run: %v", err)
208+
}
209+
if got := strings.Join(strings.Fields(string(data)), " "); got != "anisette reset" {
210+
t.Errorf("arguments = %q, want %q", got, "anisette reset")
211+
}
212+
}

0 commit comments

Comments
 (0)
⌂ 换网址