persist_test.go 3.37 KB
Newer Older
1 2 3
package persist

import (
Jordan Horwich's avatar
Jordan Horwich committed
4
	"bytes"
Jordan Horwich's avatar
Jordan Horwich committed
5
	"io/ioutil"
6 7 8 9
	"os"
	"path/filepath"
	"testing"

10 11
	"gitlab.com/NebulousLabs/Sia/build"
	"gitlab.com/NebulousLabs/fastrand"
12 13 14 15 16
)

// TestIntegrationRandomSuffix checks that the random suffix creator creates
// valid files.
func TestIntegrationRandomSuffix(t *testing.T) {
17
	tmpDir := build.TempDir(persistDir, t.Name())
18 19 20 21
	err := os.MkdirAll(tmpDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
22 23 24 25 26 27 28 29 30 31
	for i := 0; i < 100; i++ {
		suffix := RandomSuffix()
		filename := filepath.Join(tmpDir, "test file - "+suffix+".nil")
		file, err := os.Create(filename)
		if err != nil {
			t.Fatal(err)
		}
		file.Close()
	}
}
32

Jordan Horwich's avatar
Jordan Horwich committed
33 34 35
// TestAbsolutePathSafeFile tests creating and committing safe files with
// absolute paths.
func TestAbsolutePathSafeFile(t *testing.T) {
36
	tmpDir := build.TempDir(persistDir, t.Name())
Jordan Horwich's avatar
Jordan Horwich committed
37
	err := os.MkdirAll(tmpDir, 0700)
38 39 40
	if err != nil {
		t.Fatal(err)
	}
Jordan Horwich's avatar
Jordan Horwich committed
41 42 43 44
	absPath := filepath.Join(tmpDir, "test")

	// Create safe file.
	sf, err := NewSafeFile(absPath)
45
	defer sf.Close()
Jordan Horwich's avatar
Jordan Horwich committed
46 47 48
	if err != nil {
		t.Fatal(err)
	}
49 50 51

	// Check that the name of the file is not equal to the final name of the
	// file.
Jordan Horwich's avatar
Jordan Horwich committed
52 53 54 55 56
	if sf.Name() == absPath {
		t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name())
	}

	// Write random data to the file and commit.
57
	data := fastrand.Bytes(10)
Jordan Horwich's avatar
Jordan Horwich committed
58 59 60 61
	_, err = sf.Write(data)
	if err != nil {
		t.Fatal(err)
	}
62
	err = sf.CommitSync()
Jordan Horwich's avatar
Jordan Horwich committed
63 64 65 66 67
	if err != nil {
		t.Fatal(err)
	}

	// Check that the file exists and has same data that was written to it.
Jordan Horwich's avatar
Jordan Horwich committed
68
	dataRead, err := ioutil.ReadFile(absPath)
Jordan Horwich's avatar
Jordan Horwich committed
69 70 71 72 73
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(data, dataRead) {
		t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead)
74 75 76
	}
}

Jordan Horwich's avatar
Jordan Horwich committed
77
// TestRelativePathSafeFile tests creating and committing safe files with
78 79 80
// relative paths. Specifically, we test that calling os.Chdir between creating
// and committing a safe file doesn't affect the safe file's final path. The
// relative path tested is relative to the working directory.
Jordan Horwich's avatar
Jordan Horwich committed
81
func TestRelativePathSafeFile(t *testing.T) {
82
	tmpDir := build.TempDir(persistDir, t.Name())
Jordan Horwich's avatar
Jordan Horwich committed
83 84 85 86 87
	err := os.MkdirAll(tmpDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	absPath := filepath.Join(tmpDir, "test")
88
	wd, err := os.Getwd()
89 90 91
	if err != nil {
		t.Fatal(err)
	}
92
	relPath, err := filepath.Rel(wd, absPath)
93 94 95
	if err != nil {
		t.Fatal(err)
	}
Jordan Horwich's avatar
Jordan Horwich committed
96 97 98

	// Create safe file.
	sf, err := NewSafeFile(relPath)
99 100 101
	if err != nil {
		t.Fatal(err)
	}
102
	defer sf.Close()
103

Jordan Horwich's avatar
Jordan Horwich committed
104 105 106 107 108
	// Check that the path of the file is not equal to the final path of the
	// file.
	if sf.Name() == absPath {
		t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name())
	}
109

Jordan Horwich's avatar
Jordan Horwich committed
110
	// Write random data to the file.
111
	data := fastrand.Bytes(10)
Jordan Horwich's avatar
Jordan Horwich committed
112 113 114 115
	_, err = sf.Write(data)
	if err != nil {
		t.Fatal(err)
	}
116

Jordan Horwich's avatar
Jordan Horwich committed
117
	// Change directories and commit.
118
	tmpChdir := build.TempDir(persistDir, t.Name()+"2")
Jordan Horwich's avatar
Jordan Horwich committed
119 120 121 122 123 124
	err = os.MkdirAll(tmpChdir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	os.Chdir(tmpChdir)
	defer os.Chdir(wd)
125
	err = sf.CommitSync()
Jordan Horwich's avatar
Jordan Horwich committed
126 127 128
	if err != nil {
		t.Fatal(err)
	}
129

Jordan Horwich's avatar
Jordan Horwich committed
130
	// Check that the file exists and has same data that was written to it.
Jordan Horwich's avatar
Jordan Horwich committed
131
	dataRead, err := ioutil.ReadFile(absPath)
Jordan Horwich's avatar
Jordan Horwich committed
132 133 134 135 136
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(data, dataRead) {
		t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead)
137
	}
138
}