diff options
Diffstat (limited to 'src/lib/os/os_test.go')
-rw-r--r-- | src/lib/os/os_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lib/os/os_test.go b/src/lib/os/os_test.go new file mode 100644 index 000000000..beaa22753 --- /dev/null +++ b/src/lib/os/os_test.go @@ -0,0 +1,79 @@ +// 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"; +) + +func size(file string, t *testing.T) uint64 { + fd, err := Open(file, O_RDONLY, 0); + defer fd.Close(); + if err != nil { + t.Fatal("open failed:", err); + } + var buf [100]byte; + len := 0; + for { + n, e := fd.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) { + fd, err1 := Open("/etc/passwd", O_RDONLY, 0); + defer fd.Close(); + if err1 != nil { + t.Fatal("open failed:", err1); + } + dir, err2 := Fstat(fd); + 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); + } +} |