-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patherror_test.go
140 lines (119 loc) · 3.9 KB
/
error_test.go
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
package rsync_test
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/gokrazy/rsync/internal/rsynctest"
"github.com/google/go-cmp/cmp"
)
func TestErrors(t *testing.T) {
tmp := t.TempDir()
dest := filepath.Join(tmp, "dest")
// We configure an rsync module with a non-existant path to trigger an
// error. Removing read permission from a file is not sufficient because
// that does not actually trigger an error! See TestNoReadPermission.
nonExistant := filepath.Join(tmp, "non/existant")
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(nonExistant))
// sync into dest dir
var buf bytes.Buffer
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost/interop/", // copy contents of interop
//source+"/", // sync from local directory
dest) // directly into dest
rsync.Stdout = &buf
rsync.Stderr = &buf
if err := rsync.Run(); err == nil {
t.Fatalf("rsync unexpectedly did not return with an error exit code, output:\n%s", buf.String())
}
output := buf.String()
if want := "no such file or directory"; !strings.Contains(output, want) {
t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output)
}
if want := nonExistant; !strings.Contains(output, want) {
t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output)
}
}
func TestNoSuchModule(t *testing.T) {
tmp := t.TempDir()
dest := filepath.Join(tmp, "dest")
// start a server to sync from
srv := rsynctest.New(t, nil)
// sync into dest dir
var buf bytes.Buffer
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost/requesting-nonsense/", // copy contents of interop
//source+"/", // sync from local directory
dest) // directly into dest
rsync.Stdout = &buf
rsync.Stderr = &buf
if err := rsync.Run(); err == nil {
t.Fatalf("rsync unexpectedly did not return with an error exit code, output:\n%s", buf.String())
}
output := buf.String()
if want := "Unknown module"; !strings.Contains(output, want) {
t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output)
}
}
func TestNoReadPermission(t *testing.T) {
tmp := t.TempDir()
source := filepath.Join(tmp, "source")
dest := filepath.Join(tmp, "dest")
// create files in source to be copied
if err := os.MkdirAll(source, 0755); err != nil {
t.Fatal(err)
}
dummy := filepath.Join(source, "dummy")
if err := os.WriteFile(dummy, []byte("dummy"), 0644); err != nil {
t.Fatal(err)
}
other := filepath.Join(source, "other")
want := []byte("other file contents")
if err := os.WriteFile(other, want, 0644); err != nil {
t.Fatal(err)
}
// Remove read permission to trigger an error for one of the requested files.
if err := os.Chmod(dummy, 0); err != nil {
t.Fatal(err)
}
// start a server to sync from
srv := rsynctest.New(t, rsynctest.InteropModule(source))
// sync into dest dir
var buf bytes.Buffer
rsync := exec.Command("rsync", //"/home/michael/src/openrsync/openrsync",
// "--debug=all4",
"--archive",
"-v", "-v", "-v", "-v",
"--port="+srv.Port,
"rsync://localhost/interop/", // copy contents of interop
dest) // directly into dest
rsync.Stdout = &buf
rsync.Stderr = &buf
if err := rsync.Run(); err != nil {
t.Fatalf("%v: %v", rsync.Args, err)
}
if os.Getuid() > 0 {
// uid 0 can read the file despite chmod(0), so skip this check:
if _, err := os.ReadFile(filepath.Join(dest, "dummy")); err == nil {
t.Fatalf("dummy file unexpectedly created in the destination")
}
}
got, err := os.ReadFile(filepath.Join(dest, "other"))
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff)
}
}