Debian Codegraph @

libgo/go/os/read_test.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package os_test
6
7 import (
8 "bytes"
9 . "os"
10 "path/filepath"
11 "runtime"
12 "testing"
13 )
14
15 func checkNamedSize(t *testing.T, path string, size int64) {
16 dir, err := Stat(path)
17 if err != nil {
18 t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
19 }
20 if dir.Size() != size {
21 t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
22 }
23 }
24
25 func TestReadFile(t *testing.T) {
26 filename := "rumpelstilzchen"
27 contents, err := ReadFile(filename)
28 if err == nil {
29 t.Fatalf("ReadFile %s: error expected, none found", filename)
30 }
31
32 filename = "read_test.go"
33 contents, err = ReadFile(filename)
34 if err != nil {
35 t.Fatalf("ReadFile %s: %v", filename, err)
36 }
37
38 checkNamedSize(t, filename, int64(len(contents)))
39 }
40
41 func TestWriteFile(t *testing.T) {
42 f, err := CreateTemp("", "ioutil-test")
43 if err != nil {
44 t.Fatal(err)
45 }
46 defer f.Close()
47 defer Remove(f.Name())
48
49 msg := "Programming today is a race between software engineers striving to " +
50 "build bigger and better idiot-proof programs, and the Universe trying " +
51 "to produce bigger and better idiots. So far, the Universe is winning."
52
53 if err := WriteFile(f.Name(), []byte(msg), 0644); err != nil {
54 t.Fatalf("WriteFile %s: %v", f.Name(), err)
55 }
56
57 data, err := ReadFile(f.Name())
58 if err != nil {
59 t.Fatalf("ReadFile %s: %v", f.Name(), err)
60 }
61
62 if string(data) != msg {
63 t.Fatalf("ReadFile: wrong data:\nhave %q\nwant %q", string(data), msg)
64 }
65 }
66
67 func TestReadOnlyWriteFile(t *testing.T) {
68 if Getuid() == 0 {
69 t.Skipf("Root can write to read-only files anyway, so skip the read-only test.")
70 }
71
72 // We don't want to use CreateTemp directly, since that opens a file for us as 0600.
73 tempDir, err := MkdirTemp("", t.Name())
74 if err != nil {
75 t.Fatal(err)
76 }
77 defer RemoveAll(tempDir)
78 filename := filepath.Join(tempDir, "blurp.txt")
79
80 shmorp := []byte("shmorp")
81 florp := []byte("florp")
82 err = WriteFile(filename, shmorp, 0444)
83 if err != nil {
84 t.Fatalf("WriteFile %s: %v", filename, err)
85 }
86 err = WriteFile(filename, florp, 0444)
87 if err == nil {
88 t.Fatalf("Expected an error when writing to read-only file %s", filename)
89 }
90 got, err := ReadFile(filename)
91 if err != nil {
92 t.Fatalf("ReadFile %s: %v", filename, err)
93 }
94 if !bytes.Equal(got, shmorp) {
95 t.Fatalf("want %s, got %s", shmorp, got)
96 }
97 }
98
99 func TestReadDir(t *testing.T) {
100 dirname := "rumpelstilzchen"
101 _, err := ReadDir(dirname)
102 if err == nil {
103 t.Fatalf("ReadDir %s: error expected, none found", dirname)
104 }
105
106 dirname = "."
107 list, err := ReadDir(dirname)
108 if err != nil {
109 t.Fatalf("ReadDir %s: %v", dirname, err)
110 }
111
112 foundFile := false
113 foundSubDir := false
114 for _, dir := range list {
115 switch {
116 case !dir.IsDir() && dir.Name() == "read_test.go":
117 foundFile = true
118 case dir.IsDir() && dir.Name() == "exec":
119 foundSubDir = true
120 }
121 }
122 if !foundFile {
123 t.Fatalf("ReadDir %s: read_test.go file not found", dirname)
124 }
125 if !foundSubDir {
126 // This doesn't work in the gofrontend testsuite framework.
127 if runtime.Compiler == "gc" {
128 t.Fatalf("ReadDir %s: exec directory not found", dirname)
129 }
130 }
131 }