github.com/ZuluSpl0it/Sia@v1.3.7/persist/persist_test.go (about) 1 package persist 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/NebulousLabs/Sia/build" 11 "github.com/NebulousLabs/fastrand" 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, t.Name()) 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, t.Name()) 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 := fastrand.Bytes(10) 58 _, err = sf.Write(data) 59 if err != nil { 60 t.Fatal(err) 61 } 62 err = sf.CommitSync() 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 // Check that the file exists and has same data that was written to it. 68 dataRead, err := ioutil.ReadFile(absPath) 69 if err != nil { 70 t.Fatal(err) 71 } 72 if !bytes.Equal(data, dataRead) { 73 t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead) 74 } 75 } 76 77 // TestRelativePathSafeFile tests creating and committing safe files with 78 // relative paths. Specifically, we test that calling os.Chdir between creating 79 // and committing a safe file doesn't affect the safe file's final path. The 80 // relative path tested is relative to the working directory. 81 func TestRelativePathSafeFile(t *testing.T) { 82 tmpDir := build.TempDir(persistDir, t.Name()) 83 err := os.MkdirAll(tmpDir, 0700) 84 if err != nil { 85 t.Fatal(err) 86 } 87 absPath := filepath.Join(tmpDir, "test") 88 wd, err := os.Getwd() 89 if err != nil { 90 t.Fatal(err) 91 } 92 relPath, err := filepath.Rel(wd, absPath) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 // Create safe file. 98 sf, err := NewSafeFile(relPath) 99 if err != nil { 100 t.Fatal(err) 101 } 102 defer sf.Close() 103 104 // Check that the path of the file is not equal to the final path of the 105 // file. 106 if sf.Name() == absPath { 107 t.Errorf("safeFile created with filename: %s has temporary filename that is equivalent to finalName: %s\n", absPath, sf.Name()) 108 } 109 110 // Write random data to the file. 111 data := fastrand.Bytes(10) 112 _, err = sf.Write(data) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 // Change directories and commit. 118 tmpChdir := build.TempDir(persistDir, t.Name()+"2") 119 err = os.MkdirAll(tmpChdir, 0700) 120 if err != nil { 121 t.Fatal(err) 122 } 123 os.Chdir(tmpChdir) 124 defer os.Chdir(wd) 125 err = sf.CommitSync() 126 if err != nil { 127 t.Fatal(err) 128 } 129 130 // Check that the file exists and has same data that was written to it. 131 dataRead, err := ioutil.ReadFile(absPath) 132 if err != nil { 133 t.Fatal(err) 134 } 135 if !bytes.Equal(data, dataRead) { 136 t.Fatalf("Committed file has different data than was written to it: expected %v, got %v\n", data, dataRead) 137 } 138 }