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
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import (
"fmt";
"os";
"testing";
)
var dot = []string{
"dir_amd64_darwin.go",
"dir_amd64_linux.go",
"env.go",
"error.go",
"file.go",
"os_test.go",
"time.go",
"types.go",
"stat_amd64_darwin.go",
"stat_amd64_linux.go"
}
var etc = []string{
"group",
"hosts",
"passwd",
}
func size(name string, t *testing.T) uint64 {
file, err := Open(name, O_RDONLY, 0);
defer file.Close();
if err != nil {
t.Fatal("open failed:", err);
}
var buf [100]byte;
len := 0;
for {
n, e := file.Read(buf);
if n < 0 || e != nil {
t.Fatal("read failed:", err);
}
if n == 0 {
break
}
len += n;
}
return uint64(len)
}
func TestStat(t *testing.T) {
dir, err := Stat("/etc/passwd");
if err != nil {
t.Fatal("stat failed:", err);
}
if dir.Name != "passwd" {
t.Error("name should be passwd; is", dir.Name);
}
filesize := size("/etc/passwd", t);
if dir.Size != filesize {
t.Error("size should be ", filesize, "; is", dir.Size);
}
}
func TestFstat(t *testing.T) {
file, err1 := Open("/etc/passwd", O_RDONLY, 0);
defer file.Close();
if err1 != nil {
t.Fatal("open failed:", err1);
}
dir, err2 := file.Stat();
if err2 != nil {
t.Fatal("fstat failed:", err2);
}
if dir.Name != "passwd" {
t.Error("name should be passwd; is", dir.Name);
}
filesize := size("/etc/passwd", t);
if dir.Size != filesize {
t.Error("size should be ", filesize, "; is", dir.Size);
}
}
func TestLstat(t *testing.T) {
dir, err := Lstat("/etc/passwd");
if err != nil {
t.Fatal("lstat failed:", err);
}
if dir.Name != "passwd" {
t.Error("name should be passwd; is", dir.Name);
}
filesize := size("/etc/passwd", t);
if dir.Size != filesize {
t.Error("size should be ", filesize, "; is", dir.Size);
}
}
func testReaddirnames(dir string, contents []string, t *testing.T) {
file, err := Open(dir, O_RDONLY, 0);
defer file.Close();
if err != nil {
t.Fatalf("open %q failed: %v", dir, err);
}
s, err2 := file.Readdirnames(-1);
if err2 != nil {
t.Fatalf("readdirnames %q failed: %v", err2);
}
for i, m := range contents {
found := false;
for j, n := range s {
if n == "." || n == ".." {
t.Errorf("got %s in directory", n);
}
if m == n {
if found {
t.Error("present twice:", m);
}
found = true
}
}
if !found {
t.Error("could not find", m);
}
}
}
func testReaddir(dir string, contents []string, t *testing.T) {
file, err := Open(dir, O_RDONLY, 0);
defer file.Close();
if err != nil {
t.Fatalf("open %q failed: %v", dir, err);
}
s, err2 := file.Readdir(-1);
if err2 != nil {
t.Fatalf("readdir %q failed: %v", dir, err2);
}
for i, m := range contents {
found := false;
for j, n := range s {
if m == n.Name {
if found {
t.Error("present twice:", m);
}
found = true
}
}
if !found {
t.Error("could not find", m);
}
}
}
func TestReaddirnames(t *testing.T) {
testReaddirnames(".", dot, t);
testReaddirnames("/etc", etc, t);
}
func TestReaddir(t *testing.T) {
testReaddir(".", dot, t);
testReaddir("/etc", etc, t);
}
// Read the directory one entry at a time.
func smallReaddirnames(file *File, length int, t *testing.T) []string {
names := make([]string, length);
count := 0;
for {
d, err := file.Readdirnames(1);
if err != nil {
t.Fatalf("readdir %q failed: %v", file.Name(), err);
}
if len(d) == 0 {
break
}
names[count] = d[0];
count++;
}
return names[0:count]
}
// Check that reading a directory one entry at a time gives the same result
// as reading it all at once.
func TestReaddirnamesOneAtATime(t *testing.T) {
dir := "/usr/bin"; // big directory that doesn't change often.
file, err := Open(dir, O_RDONLY, 0);
defer file.Close();
if err != nil {
t.Fatalf("open %q failed: %v", dir, err);
}
all, err1 := file.Readdirnames(-1);
if err1 != nil {
t.Fatalf("readdirnames %q failed: %v", dir, err1);
}
file1, err2 := Open(dir, O_RDONLY, 0);
if err2 != nil {
t.Fatalf("open %q failed: %v", dir, err2);
}
small := smallReaddirnames(file1, len(all)+100, t); // +100 in case we screw up
for i, n := range all {
if small[i] != n {
t.Errorf("small read %q %q mismatch: %v", small[i], n);
}
}
}
|