github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/persist/persist_test.go (about) 1 package persist 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/NebulousLabs/Sia/build" 12 ) 13 14 // TestIntegrationRandomSuffix checks that the random suffix creator creates 15 // valid files. 16 func TestIntegrationRandomSuffix(t *testing.T) { 17 tmpDir := build.TempDir(persistDir, "TestIntegrationRandomSuffix") 18 err := os.MkdirAll(tmpDir, 0700) 19 if err != nil { 20 t.Fatal(err) 21 } 22 for i := 0; i < 100; i++ { 23 suffix := RandomSuffix() 24 filename := filepath.Join(tmpDir, "test file - "+suffix+".nil") 25 file, err := os.Create(filename) 26 if err != nil { 27 t.Fatal(err) 28 } 29 file.Close() 30 } 31 } 32 33 // TestAbsolutePathSafeFile tests creating and committing safe files with 34 // absolute paths. 35 func TestAbsolutePathSafeFile(t *testing.T) { 36 tmpDir := build.TempDir(persistDir, "TestAbsolutePathSafeFile") 37 err := os.MkdirAll(tmpDir, 0700) 38 if err != nil { 39 t.Fatal(err) 40 } 41 absPath := filepath.Join(tmpDir, "test") 42 43 // Create safe file. 44 sf, err := NewSafeFile(absPath) 45 defer sf.Close() 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 // Check that the name of the file is not equal to the final name of the 51 // file. 52 if sf.Name() == absPath { 53 t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name()) 54 } 55 56 // Write random data to the file and commit. 57 data := make([]byte, 10) 58 rand.Read(data) 59 _, err = sf.Write(data) 60 if err != nil { 61 t.Fatal(err) 62 } 63 err = sf.Commit() 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 // Check that the file exists and has same data that was written to it. 69 dataRead, err := ioutil.ReadFile(absPath) 70 if err != nil { 71 t.Fatal(err) 72 } 73 if !bytes.Equal(data, dataRead) { 74 t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead) 75 } 76 } 77 78 // TestRelativePathSafeFile tests creating and committing safe files with 79 // relative paths. Specifically, we test that calling os.Chdir between creating 80 // and committing a safe file doesn't affect the safe file's final path. The 81 // relative path tested is relative to the working directory. 82 func TestRelativePathSafeFile(t *testing.T) { 83 tmpDir := build.TempDir(persistDir, "TestRelativePathSafeFile") 84 err := os.MkdirAll(tmpDir, 0700) 85 if err != nil { 86 t.Fatal(err) 87 } 88 absPath := filepath.Join(tmpDir, "test") 89 wd, err := os.Getwd() 90 if err != nil { 91 t.Fatal(err) 92 } 93 relPath, err := filepath.Rel(wd, absPath) 94 95 // Create safe file. 96 sf, err := NewSafeFile(relPath) 97 defer sf.Close() 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 // Check that the path of the file is not equal to the final path of the 103 // file. 104 if sf.Name() == absPath { 105 t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name()) 106 } 107 108 // Write random data to the file. 109 data := make([]byte, 10) 110 rand.Read(data) 111 _, err = sf.Write(data) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 // Change directories and commit. 117 tmpChdir := build.TempDir(persistDir, "TestRelativePathSafeFileTmpChdir") 118 err = os.MkdirAll(tmpChdir, 0700) 119 if err != nil { 120 t.Fatal(err) 121 } 122 os.Chdir(tmpChdir) 123 defer os.Chdir(wd) 124 err = sf.CommitSync() 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 // Check that the file exists and has same data that was written to it. 130 dataRead, err := ioutil.ReadFile(absPath) 131 if err != nil { 132 t.Fatal(err) 133 } 134 if !bytes.Equal(data, dataRead) { 135 t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead) 136 } 137 }