-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomicwrite_test.go
More file actions
336 lines (250 loc) · 7.25 KB
/
Copy pathatomicwrite_test.go
File metadata and controls
336 lines (250 loc) · 7.25 KB
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
327
328
329
330
331
332
333
334
335
336
package atomicwrite
import (
"errors"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"testing"
)
func tempFile(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "testfile")
err := os.WriteFile(path, []byte(content), 0o644) //nolint:gosec // test fixture uses 0644 inside t.TempDir
if err != nil {
t.Fatal(err)
}
return path
}
func assertFileContent(t *testing.T, path, want string) {
t.Helper()
data, err := os.ReadFile(path) //nolint:gosec // test reads from t.TempDir
if err != nil {
t.Fatalf("reading file: %v", err)
}
if string(data) != want {
t.Errorf("content = %q, want %q", string(data), want)
}
}
func TestFingerprintFromBytes(t *testing.T) {
t.Parallel()
data := []byte("hello world")
fingerprint := FingerprintFromBytes(data)
if fingerprint.IsZero() {
t.Error("fingerprint should not be zero for non-empty data")
}
if !fingerprint.Matches(data) {
t.Error("fingerprint should match the same data")
}
if fingerprint.Matches([]byte("different")) {
t.Error("fingerprint should not match different data")
}
}
func TestFingerprintIsZero(t *testing.T) {
t.Parallel()
var zero Fingerprint
if !zero.IsZero() {
t.Error("zero-value Fingerprint should be zero")
}
nonZero := FingerprintFromBytes([]byte("data"))
if nonZero.IsZero() {
t.Error("fingerprint from data should not be zero")
}
}
func TestFingerprintFile(t *testing.T) {
t.Parallel()
path := tempFile(t, "content")
fingerprint, err := FingerprintFile(path)
if err != nil {
t.Fatalf("FingerprintFile: %v", err)
}
if fingerprint.IsZero() {
t.Error("fingerprint should not be zero for existing file")
}
if !fingerprint.Matches([]byte("content")) {
t.Error("fingerprint should match file content")
}
}
func TestFingerprintFileNonexistent(t *testing.T) {
t.Parallel()
fingerprint, err := FingerprintFile("/nonexistent/path/file")
if err != nil {
t.Fatalf("FingerprintFile nonexistent: %v", err)
}
if !fingerprint.IsZero() {
t.Error("nonexistent file should return zero fingerprint")
}
}
func TestWriteFirstRun(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "testfile")
err := Write(path, []byte("hello"), Fingerprint{})
if err != nil {
t.Fatalf("Write first run: %v", err)
}
assertFileContent(t, path, "hello")
_, statErr := os.Stat(path + ".bak")
if !os.IsNotExist(statErr) {
t.Error("expected no .bak file on first run")
}
}
func TestWriteWithFingerprint(t *testing.T) {
t.Parallel()
path := tempFile(t, "original")
fingerprint := FingerprintFromBytes([]byte("original"))
err := Write(path, []byte("updated"), fingerprint)
if err != nil {
t.Fatalf("Write: %v", err)
}
assertFileContent(t, path, "updated")
}
func TestWriteRejectsConcurrentModification(t *testing.T) {
t.Parallel()
path := tempFile(t, "original")
fingerprint := FingerprintFromBytes([]byte("original"))
modified := "modified"
err := os.WriteFile(path, []byte(modified), 0o644) //nolint:gosec // test fixture in t.TempDir
if err != nil {
t.Fatalf("modify file: %v", err)
}
writeErr := Write(path, []byte("updated"), fingerprint)
if writeErr == nil {
t.Fatal("expected error for concurrent modification, got nil")
}
if !errors.Is(writeErr, ErrConcurrentModification) {
t.Errorf("expected ErrConcurrentModification, got: %v", writeErr)
}
_, statErr := os.Stat(path + ".tmp")
if statErr == nil {
t.Error("temp file should be cleaned up on error")
}
}
func TestWritePreservesPermissions(t *testing.T) {
t.Parallel()
path := tempFile(t, "original")
err := os.Chmod(path, 0o600)
if err != nil {
t.Fatalf("chmod: %v", err)
}
fingerprint := FingerprintFromBytes([]byte("original"))
writeErr := Write(path, []byte("updated"), fingerprint)
if writeErr != nil {
t.Fatalf("Write: %v", writeErr)
}
info, statErr := os.Stat(path)
if statErr != nil {
t.Fatalf("stat: %v", statErr)
}
if perm := info.Mode().Perm(); perm != 0o600 {
t.Errorf("permissions = %o, want 0o600", perm)
}
}
func TestWriteLeavesNoLeftoverFiles(t *testing.T) {
t.Parallel()
originalContent := "original"
path := tempFile(t, originalContent)
fingerprint := FingerprintFromBytes([]byte(originalContent))
err := Write(path, []byte("updated"), fingerprint)
if err != nil {
t.Fatalf("Write: %v", err)
}
assertFileContent(t, path, "updated")
_, bakErr := os.Stat(path + ".bak")
if !os.IsNotExist(bakErr) {
t.Error("expected no .bak file after write")
}
tmpMatches, globErr := filepath.Glob(filepath.Join(filepath.Dir(path), "*.tmp"))
if globErr != nil {
t.Fatalf("glob: %v", globErr)
}
if len(tmpMatches) > 0 {
t.Errorf("expected no temp files after write, found: %v", tmpMatches)
}
}
func TestTempFileCleanedUpOnError(t *testing.T) {
t.Parallel()
path := tempFile(t, "original")
fingerprint := FingerprintFromBytes([]byte("original"))
err := os.Remove(path)
if err != nil {
t.Fatalf("remove original: %v", err)
}
writeErr := Write(path, []byte("updated"), fingerprint)
if writeErr == nil {
t.Fatal("expected error when original file deleted during verification, got nil")
}
tmpMatches, globErr := filepath.Glob(filepath.Join(filepath.Dir(path), "testfile.*.tmp"))
if globErr != nil {
t.Fatalf("glob: %v", globErr)
}
if len(tmpMatches) > 0 {
t.Errorf("temp files should be cleaned up on error, found: %v", tmpMatches)
}
}
func TestConcurrentWriteRACE(t *testing.T) {
t.Parallel()
path := tempFile(t, "original")
var successes, conflicts atomic.Int32
var waitGroup sync.WaitGroup
const writers = 10
for writerIndex := range writers {
waitGroup.Go(func() {
fingerprint, fpErr := FingerprintFile(path)
if fpErr != nil {
t.Logf("FingerprintFile: %v", fpErr)
return
}
payload := "writer-" + strconv.Itoa(writerIndex)
writeErr := Write(path, []byte(payload), fingerprint)
if writeErr == nil {
successes.Add(1)
} else if errors.Is(writeErr, ErrConcurrentModification) {
conflicts.Add(1)
}
})
}
waitGroup.Wait()
total := successes.Load() + conflicts.Load()
if int(total) != writers {
t.Errorf("expected %d total outcomes, got %d (successes=%d, conflicts=%d)",
writers, total, successes.Load(), conflicts.Load())
}
if successes.Load() < 1 {
t.Fatal("expected at least one successful write")
}
if conflicts.Load() < 1 {
t.Log("no conflicts detected — race window may be too narrow")
}
data, readErr := os.ReadFile(path) //nolint:gosec // test reads from t.TempDir
if readErr != nil {
t.Fatalf("reading final file: %v", readErr)
}
content := string(data)
validPayload := false
for writerIndex := range writers {
if content == "writer-"+strconv.Itoa(writerIndex) {
validPayload = true
break
}
}
if !validPayload {
t.Errorf("final content %q is not a valid writer payload — possible corruption", content)
}
}
func TestAtomicRenameReportsErrorOnFailure(t *testing.T) {
t.Parallel()
dir := t.TempDir()
ghostPath := filepath.Join(dir, "nonexistent", "dir", "testfile")
tmpPath := filepath.Join(dir, "temp-file")
err := os.WriteFile(tmpPath, []byte("content"), 0o644) //nolint:gosec // test fixture in t.TempDir
if err != nil {
t.Fatalf("write temp: %v", err)
}
renameErr := atomicRename(ghostPath, tmpPath)
if renameErr == nil {
t.Fatal("expected error for rename into nonexistent directory, got nil")
}
}