updater_darwin_test.go 15.5 KB
Newer Older
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package updater

import (
	"archive/zip"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestDoUpgrade(t *testing.T) {
	tmpDir := t.TempDir()
	BundlePath = filepath.Join(tmpDir, "Ollama.app")
	appContents := filepath.Join(BundlePath, "Contents")
	appBackupDir = filepath.Join(tmpDir, "backup")
	appContentsOld := filepath.Join(appBackupDir, "Ollama.app", "Contents")
	UpdateStageDir = filepath.Join(tmpDir, "updates")
	UpgradeMarkerFile = filepath.Join(tmpDir, "upgraded")
	bundle := filepath.Join(UpdateStageDir, "foo", "ollama-darwin.zip")

	err := os.MkdirAll(filepath.Join(appContents, "MacOS"), 0o755)
	if err != nil {
		t.Fatal("failed to create empty dirs")
	}
	err = os.MkdirAll(filepath.Join(BundlePath, "Contents", "Resources"), 0o755)
	if err != nil {
		t.Fatal("failed to create empty dirs")
	}
	err = os.MkdirAll(filepath.Dir(bundle), 0o755)
	if err != nil {
		t.Fatal("failed to create empty dirs")
	}

	// No update file, simple failure scenario
	if err := DoUpgrade(false); err == nil {
		t.Fatal("expected failure without download")
	} else if !strings.Contains(err.Error(), "failed to lookup downloads") {
		t.Fatalf("unexpected error: %s", err.Error())
	}

	// Start with an unreadable zip file
	if err := os.WriteFile(bundle, []byte{0x4b, 0x50, 0x40, 0x03, 0x00, 0x0a, 0x00}, 0o755); err != nil {
		t.Fatalf("failed to create intentionally corrupt zip file: %s", err)
	}
	if err := DoUpgrade(false); err == nil {
		t.Fatal("expected failure with corrupt zip file")
	} else if !strings.Contains(err.Error(), "unable to open upgrade bundle") {
		t.Fatalf("unexpected error with corrupt zip file: %s", err)
	}

	// Generate valid (partial) zip file for remaining scenarios
	if err := zipCreationHelper(bundle, []testPayload{
		{
			Name: "Ollama.app/Contents/MacOS/Ollama",
			Body: []byte("would be app binary"),
		},
		{
			Name: "Ollama.app/Contents/Resources/ollama",
			Body: []byte("would be the cli"),
		},
		{
			Name: "Ollama.app/Contents/Resources/dummy",
			Body: []byte("./ollama"),
			Mode: os.ModeSymlink,
		},
	}); err != nil {
		t.Fatal(err)
	}
	// Permission failure on rename
	if err := os.Chmod(BundlePath, 0o500); err != nil {
		t.Fatal("failed to remove write permission")
	}
	if err := DoUpgrade(false); err == nil {
		t.Fatal("expected failure with no permission to rename Contents")
	} else if !strings.Contains(err.Error(), "permission problems") {
		t.Fatalf("unexpected error with permission failure: %s", err)
	}
	if err := os.Chmod(BundlePath, 0o755); err != nil {
		t.Fatal("failed to restore write permission")
	}

	// Prior failed upgrade
	if err := os.MkdirAll(appContentsOld, 0o755); err != nil {
		t.Fatal("failed to create empty dirs")
	}
	if err := DoUpgrade(false); err == nil {
		t.Fatal("expected failure with old contents existing")
	} else if !strings.Contains(err.Error(), "prior upgrade failed") {
		t.Fatalf("unexpected error with old contents: %s", err)
	}
	if err := os.RemoveAll(appBackupDir); err != nil {
		t.Fatal("failed to cleanup dir")
	}

	// TODO - a failure mode where we revert the backup

	// Happy path
	if err := DoUpgrade(false); err != nil {
		t.Fatalf("unexpected error with clean setup: %s", err)
	}
	if _, err := os.Stat(appContentsOld); err != nil {
		t.Fatalf("missing %s", appContentsOld)
	}
	if _, err := os.Stat(UpgradeMarkerFile); err != nil {
		t.Fatalf("missing marker %s", UpgradeMarkerFile)
	}
	if _, err := os.Stat(filepath.Join(BundlePath, "Contents", "MacOS", "Ollama")); err != nil {
		t.Fatalf("missing new App")
	}
	if _, err := os.Stat(filepath.Join(BundlePath, "Contents", "Resources", "ollama")); err != nil {
		t.Fatalf("missing new cli")
	}

	// Cleanup before next attempt
	if err := DoPostUpgradeCleanup(); err != nil {
		t.Fatal("failed to cleanup dir")
	}

	err = os.MkdirAll(filepath.Dir(bundle), 0o755)
	if err != nil {
		t.Fatal("failed to create empty dirs")
	}

	// Zip file with one corrupt file within to trigger a rollback
	if err := os.WriteFile(bundle, corruptZipData, 0o755); err != nil {
		t.Fatalf("failed to create intentionally corrupt zip file: %s", err)
	}
	if err := DoUpgrade(false); err == nil {
		t.Fatal("expected failure with corrupt zip file")
	} else if !strings.Contains(err.Error(), "failed to open bundle file") {
		t.Fatalf("unexpected error with corrupt zip file: %s", err)
	}
	// Make sure things were restored on partial failure
	if _, err := os.Stat(appContents); err != nil {
		t.Fatalf("missing %s", appContents)
	}
	if _, err := os.Stat(appContentsOld); err == nil {
		t.Fatal("old contents still exists")
	}
	if _, err := os.Stat(filepath.Join(BundlePath, "Contents", "MacOS", "Ollama")); err != nil {
		t.Fatalf("missing old App")
	}
	if _, err := os.Stat(filepath.Join(BundlePath, "Contents", "Resources", "ollama")); err != nil {
		t.Fatalf("missing old cli")
	}
}

func TestDoUpgradeAtStartup(t *testing.T) {
	tmpDir := t.TempDir()
	BundlePath = filepath.Join(tmpDir, "Ollama.app")
	appBackupDir = filepath.Join(tmpDir, "backup")
	UpdateStageDir = filepath.Join(tmpDir, "updates")
	UpgradeMarkerFile = filepath.Join(tmpDir, "upgraded")
	bundle := filepath.Join(UpdateStageDir, "foo", "ollama-darwin.zip")

	if err := DoUpgradeAtStartup(); err == nil {
		t.Fatal("expected failure without download")
	} else if !strings.Contains(err.Error(), "failed to lookup downloads") {
		t.Fatalf("unexpected error: %s", err.Error())
	}

	if err := os.MkdirAll(filepath.Dir(bundle), 0o755); err != nil {
		t.Fatal("failed to create empty dirs")
	}

	if err := zipCreationHelper(bundle, []testPayload{
		{
			Name: "Ollama.app/Contents/MacOS/Ollama",
			Body: []byte("would be app binary"),
		},
		{
			Name: "Ollama.app/Contents/Resources/ollama",
			Body: []byte("would be the cli"),
		},
		{
			Name: "Ollama.app/Contents/Resources/dummy",
			Body: []byte("./ollama"),
			Mode: os.ModeSymlink,
		},
	}); err != nil {
		t.Fatal(err)
	}

	if err := DoUpgradeAtStartup(); err != nil {
		t.Fatalf("unexpected error with verification failure: %s", err)
	}
	if _, err := os.Stat(bundle); err == nil {
		t.Fatalf("unverified bundle still exists %s", bundle)
	}
}

func TestVerifyDownloadFailures(t *testing.T) {
	tmpDir := t.TempDir()
	BundlePath = filepath.Join(tmpDir, "Ollama.app")
	UpdateStageDir = filepath.Join(tmpDir, "staging")
	bundle := filepath.Join(UpdateStageDir, "foo", "ollama-darwin.zip")
	if err := os.MkdirAll(filepath.Dir(bundle), 0o755); err != nil {
		t.Fatal("failed to create empty dirs")
	}
	tests := []struct {
		n        string
		in       []testPayload
		expected string
	}{
		{"breakout", []testPayload{
			{
				Name: "Ollama.app/",
				Body: []byte{},
			}, {
				Name: "Ollama.app/Resources/ollama",
				Body: []byte("cli payload here"),
			}, {
				Name: "Ollama.app/Contents/MacOS/Ollama",
				Body: []byte("../../../../breakout"),
				Mode: os.ModeSymlink,
			},
		}, "bundle contains link outside"},
		{"absolute", []testPayload{{
			Name: "Ollama.app/Contents/MacOS/Ollama",
			Body: []byte("/etc/foo"),
			Mode: os.ModeSymlink,
		}}, "bundle contains absolute"},
		{"missing", []testPayload{{
			Name: "Ollama.app/Contents/MacOS/Ollama",
			Body: []byte("../nothere"),
			Mode: os.ModeSymlink,
		}}, "no such file or directory"},
		{"unsigned", []testPayload{{
			Name: "Ollama.app/Contents/MacOS/Ollama",
			Body: []byte{0xfa, 0xcf, 0xfe, 0xed, 0x00, 0x0c, 0x01, 0x00},
		}}, "signature verification failed"},
	}

	for _, tt := range tests {
		t.Run(tt.n, func(t *testing.T) {
			_ = os.Remove(bundle)
			if err := zipCreationHelper(bundle, tt.in); err != nil {
				t.Fatal(err)
			}
			err := VerifyDownload()
			if err == nil || !strings.Contains(err.Error(), tt.expected) {
				t.Fatalf("expected \"%s\" got %s", tt.expected, err)
			}
		})
	}
}

// One file has been corrupted to cause a checksum mismatch
var corruptZipData = []byte{0x50, 0x4b, 0x3, 0x4, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0xed, 0x7e, 0x8f, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb, 0x0, 0x1c, 0x0, 0x4f, 0x6c, 0x6c, 0x61, 0x6d, 0x61, 0x2e, 0x61, 0x70, 0x70, 0x2f, 0x55, 0x54, 0x9, 0x0, 0x3, 0x6d, 0x6c, 0x5f, 0x67, 0x6e, 0x6c, 0x5f, 0x67, 0x75, 0x78, 0xb, 0x0, 0x1, 0x4, 0xf5, 0x1, 0x0, 0x0, 0x4, 0x14, 0x0, 0x0, 0x0, 0x50, 0x4b, 0x3, 0x4, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd8, 0x7e, 0x8f, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x0, 0x1c, 0x0, 0x4f, 0x6c, 0x6c, 0x61, 0x6d, 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 0x55, 0x54, 0x5, 0x0, 0x3, 0x66, 0x6c, 0x5f, 0x67, 0x75, 0x78, 0xb, 0x0, 0x1, 0x4, 0xf5, 0x1, 0x0, 0x0, 0x4, 0x14, 0x0, 0x0, 0x0, 0x50, 0x4b, 0x5, 0x6, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, 0x6, 0x0, 0x3f, 0x2, 0x0, 0x0, 0x1c, 0x2, 0x0, 0x0, 0x0, 0x0}

type testPayload struct {
	Name string
	Body []byte
	Mode fs.FileMode
}

func zipCreationHelper(filename string, files []testPayload) error {
	fd, err := os.Create(filename)
	if err != nil {
		return err
	}

	w := zip.NewWriter(fd)
	for _, file := range files {
		fh := &zip.FileHeader{
			Name:  file.Name,
			Flags: 0,
		}
		if file.Mode != 0 {
			fh.SetMode(file.Mode)
		}
		f, err := w.CreateHeader(fh)
		if err != nil {
			return err
		}
		_, err = f.Write(file.Body)
		if err != nil {
			return err
		}
	}
	return w.Close()
}

func TestAlreadyMoved(t *testing.T) {
	oldPath := SystemWidePath
	defer func() {
		SystemWidePath = oldPath
	}()
	exe, err := os.Executable()
	if err != nil {
		t.Fatal("failed to find executable path")
	}
	tmpDir := t.TempDir()
	testApp := filepath.Join(tmpDir, "Ollama.app")
	err = os.MkdirAll(filepath.Join(testApp, "Contents", "MacOS"), 0o755)
	if err != nil {
		t.Fatal("failed to create Contents dir")
	}
	SystemWidePath = testApp
	testBinary := filepath.Join(testApp, "Contents", "MacOS", "Ollama")
	if err := os.Symlink(exe, testBinary); err != nil {
		t.Fatalf("failed to create symlink to executable: %s", err)
	}

	bundle := alreadyMoved()
	if bundle != testApp {
		t.Fatalf("expected %s, got %s", testApp, bundle)
	}

	// "Keep scenario"
	testApp = filepath.Join(tmpDir, "Ollama 2.app")
	err = os.MkdirAll(filepath.Join(testApp, "Contents", "MacOS"), 0o755)
	if err != nil {
		t.Fatal("failed to create Contents dir")
	}
	testBinary = filepath.Join(testApp, "Contents", "MacOS", "Ollama")
	if err := os.Symlink(exe, testBinary); err != nil {
		t.Fatalf("failed to create symlink to executable: %s", err)
	}

	bundle = alreadyMoved()
	if bundle != testApp {
		t.Fatalf("expected %s, got %s", testApp, bundle)
	}
}