-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_test.go
More file actions
158 lines (148 loc) · 4.09 KB
/
Copy pathverify_test.go
File metadata and controls
158 lines (148 loc) · 4.09 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
package pin
import (
"context"
"os"
"path/filepath"
"testing"
)
func setupSynced(t *testing.T) (dir string, srv string) {
t.Helper()
srvT := fakeNPM(t, "demo", "1.0.0", map[string]string{
"dist/a.js": "alpha",
"dist/b.css": "beta",
})
dir = t.TempDir()
writeManifest(t, dir, `out: "v"
assets:
- name: "demo"
version: "1.0.0"
files: ["dist/a.js", "dist/b.css"]
`)
if _, err := Sync(context.Background(), SyncOptions{Dir: dir, RegistryURL: srvT.URL}); err != nil {
t.Fatal(err)
}
return dir, srvT.URL
}
func TestVerifyClean(t *testing.T) {
dir, _ := setupSynced(t)
res, err := Verify(VerifyOptions{Dir: dir})
if err != nil {
t.Fatal(err)
}
if res.Failed() {
t.Errorf("clean verify failed: %+v", res)
}
if len(res.OK) != 2 {
t.Errorf("OK = %d, want 2", len(res.OK))
}
if len(res.Extra) != 0 {
t.Errorf("Extra = %v", res.Extra)
}
}
func TestVerifyMissing(t *testing.T) {
dir, _ := setupSynced(t)
if err := os.Remove(filepath.Join(dir, "v/demo/a.js")); err != nil {
t.Fatal(err)
}
res, err := Verify(VerifyOptions{Dir: dir})
if err != nil {
t.Fatal(err)
}
if !res.Failed() {
t.Error("missing file should fail verify")
}
if len(res.Missing) != 1 || res.Missing[0] != "demo/a.js" {
t.Errorf("Missing = %v", res.Missing)
}
if len(res.OK) != 1 {
t.Errorf("OK = %d, want 1", len(res.OK))
}
}
func TestVerifyDrifted(t *testing.T) {
dir, _ := setupSynced(t)
if err := os.WriteFile(filepath.Join(dir, "v/demo/a.js"), []byte("tampered"), 0o644); err != nil {
t.Fatal(err)
}
res, err := Verify(VerifyOptions{Dir: dir})
if err != nil {
t.Fatal(err)
}
if !res.Failed() {
t.Error("drifted file should fail verify")
}
if len(res.Drifted) != 1 || res.Drifted[0].Out != "demo/a.js" {
t.Errorf("Drifted = %v", res.Drifted)
}
if res.Drifted[0].Expected == res.Drifted[0].Actual {
t.Error("expected != actual")
}
}
func TestVerifyExtra(t *testing.T) {
dir, _ := setupSynced(t)
if err := os.WriteFile(filepath.Join(dir, "v/demo/leftover.js"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
res, err := Verify(VerifyOptions{Dir: dir})
if err != nil {
t.Fatal(err)
}
if res.Failed() {
t.Error("extra files should not fail verify by default")
}
if len(res.Extra) != 1 || res.Extra[0] != "demo/leftover.js" {
t.Errorf("Extra = %v", res.Extra)
}
}
func TestVerifyNoLockfile(t *testing.T) {
dir := t.TempDir()
if _, err := Verify(VerifyOptions{Dir: dir}); err == nil {
t.Fatal("verify without lockfile should error")
}
}
// TestVerifyStrict exercises the --strict re-derive path: the inner
// verifyStrictNPM that re-fetches each npm package's tarball and
// recomputes per-file SHA-384 against the lockfile-recorded integrity.
func TestVerifyStrict(t *testing.T) {
dir, srvURL := setupSynced(t)
res, err := Verify(VerifyOptions{Dir: dir, Strict: true, RegistryURL: srvURL})
if err != nil {
t.Fatalf("Verify --strict: %v", err)
}
if res.Failed() {
t.Errorf("clean --strict verify failed: %+v", res)
}
}
// TestVerifyStrict_TarballMismatch covers the case where the
// registry-side bytes drift from what the lockfile recorded. We swap
// in a fakeNPM that returns a different tarball than the one we
// originally synced from; --strict catches it.
func TestVerifyStrict_TarballMismatch(t *testing.T) {
dir, _ := setupSynced(t)
// Different files = different per-file SHA-384, but the lockfile
// still has the original integrity. --strict re-derives from the
// new tarball and sees the mismatch.
srvNew := fakeNPM(t, "demo", "1.0.0", map[string]string{
"dist/a.js": "ALPHA-MODIFIED",
"dist/b.css": "BETA-MODIFIED",
})
res, err := Verify(VerifyOptions{Dir: dir, Strict: true, RegistryURL: srvNew.URL})
if err != nil {
t.Fatalf("Verify --strict: %v", err)
}
if !res.Failed() {
t.Error("--strict against a drifted tarball should fail")
}
}
func TestVerifySummary(t *testing.T) {
r := &VerifyResult{
OK: []string{"a", "b"},
Missing: []string{"c"},
Drifted: []Drift{{Out: "d"}},
Extra: []string{"e", "f"},
}
got := r.Summary()
want := "2 ok, 1 missing, 1 drifted, 2 extra"
if got != want {
t.Errorf("Summary() = %q, want %q", got, want)
}
}