Source file src/os/read_test.go

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

View as plain text