github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/fuse/swarmfs_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 // 26 27 package fuse 28 29 import ( 30 "bytes" 31 "context" 32 "crypto/rand" 33 "flag" 34 "fmt" 35 "io" 36 "io/ioutil" 37 "os" 38 "path/filepath" 39 "testing" 40 41 "github.com/ethereum/go-ethereum/swarm/api" 42 "github.com/ethereum/go-ethereum/swarm/storage" 43 44 "github.com/ethereum/go-ethereum/log" 45 46 colorable "github.com/mattn/go-colorable" 47 ) 48 49 var ( 50 loglevel = flag.Int("loglevel", 4, "verbosity of logs") 51 rawlog = flag.Bool("rawlog", false, "turn off terminal formatting in logs") 52 longrunning = flag.Bool("longrunning", false, "do run long-running tests") 53 ) 54 55 func init() { 56 flag.Parse() 57 log.PrintOrigins(true) 58 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(!*rawlog)))) 59 } 60 61 type fileInfo struct { 62 perm uint64 63 uid int 64 gid int 65 contents []byte 66 } 67 68 // 69 func createTestFilesAndUploadToSwarm(t *testing.T, api *api.API, files map[string]fileInfo, uploadDir string, toEncrypt bool) string { 70 71 // 72 for fname, finfo := range files { 73 actualPath := filepath.Join(uploadDir, fname) 74 filePath := filepath.Dir(actualPath) 75 76 // 77 err := os.MkdirAll(filePath, 0777) 78 if err != nil { 79 t.Fatalf("Error creating directory '%v' : %v", filePath, err) 80 } 81 82 // 83 fd, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(finfo.perm)) 84 if err1 != nil { 85 t.Fatalf("Error creating file %v: %v", actualPath, err1) 86 } 87 88 // 89 _, err = fd.Write(finfo.contents) 90 if err != nil { 91 t.Fatalf("Error writing to file '%v' : %v", filePath, err) 92 } 93 /* 94 95 96 97 98 99 100 101 102 103 104 105 106 */ 107 108 err = fd.Chmod(os.FileMode(finfo.perm)) 109 if err != nil { 110 t.Fatalf("Error chmod file '%v' : %v", filePath, err) 111 } 112 err = fd.Sync() 113 if err != nil { 114 t.Fatalf("Error sync file '%v' : %v", filePath, err) 115 } 116 err = fd.Close() 117 if err != nil { 118 t.Fatalf("Error closing file '%v' : %v", filePath, err) 119 } 120 } 121 122 // 123 bzzhash, err := api.Upload(context.TODO(), uploadDir, "", toEncrypt) 124 if err != nil { 125 t.Fatalf("Error uploading directory %v: %vm encryption: %v", uploadDir, err, toEncrypt) 126 } 127 128 return bzzhash 129 } 130 131 // 132 func mountDir(t *testing.T, api *api.API, files map[string]fileInfo, bzzHash string, mountDir string) *SwarmFS { 133 swarmfs := NewSwarmFS(api) 134 _, err := swarmfs.Mount(bzzHash, mountDir) 135 if isFUSEUnsupportedError(err) { 136 t.Skip("FUSE not supported:", err) 137 } else if err != nil { 138 t.Fatalf("Error mounting hash %v: %v", bzzHash, err) 139 } 140 141 // 142 found := false 143 mi := swarmfs.Listmounts() 144 for _, minfo := range mi { 145 minfo.lock.RLock() 146 if minfo.MountPoint == mountDir { 147 if minfo.StartManifest != bzzHash || 148 minfo.LatestManifest != bzzHash || 149 minfo.fuseConnection == nil { 150 minfo.lock.RUnlock() 151 t.Fatalf("Error mounting: exp(%s): act(%s)", bzzHash, minfo.StartManifest) 152 } 153 found = true 154 } 155 minfo.lock.RUnlock() 156 } 157 158 // 159 if !found { 160 t.Fatalf("Error getting mounts information for %v: %v", mountDir, err) 161 } 162 163 // 164 compareGeneratedFileWithFileInMount(t, files, mountDir) 165 166 return swarmfs 167 } 168 169 // 170 func compareGeneratedFileWithFileInMount(t *testing.T, files map[string]fileInfo, mountDir string) { 171 err := filepath.Walk(mountDir, func(path string, f os.FileInfo, err error) error { 172 if f.IsDir() { 173 return nil 174 } 175 fname := path[len(mountDir)+1:] 176 if _, ok := files[fname]; !ok { 177 t.Fatalf(" file %v present in mount dir and is not expected", fname) 178 } 179 return nil 180 }) 181 if err != nil { 182 t.Fatalf("Error walking dir %v", mountDir) 183 } 184 185 for fname, finfo := range files { 186 destinationFile := filepath.Join(mountDir, fname) 187 188 dfinfo, err := os.Stat(destinationFile) 189 if err != nil { 190 t.Fatalf("Destination file %v missing in mount: %v", fname, err) 191 } 192 193 if int64(len(finfo.contents)) != dfinfo.Size() { 194 t.Fatalf("file %v Size mismatch source (%v) vs destination(%v)", fname, int64(len(finfo.contents)), dfinfo.Size()) 195 } 196 197 if dfinfo.Mode().Perm().String() != "-rwx------" { 198 t.Fatalf("file %v Permission mismatch source (-rwx------) vs destination(%v)", fname, dfinfo.Mode().Perm()) 199 } 200 201 fileContents, err := ioutil.ReadFile(filepath.Join(mountDir, fname)) 202 if err != nil { 203 t.Fatalf("Could not readfile %v : %v", fname, err) 204 } 205 if !bytes.Equal(fileContents, finfo.contents) { 206 t.Fatalf("File %v contents mismatch: %v , %v", fname, fileContents, finfo.contents) 207 } 208 // 209 } 210 } 211 212 // 213 func checkFile(t *testing.T, testMountDir, fname string, contents []byte) { 214 destinationFile := filepath.Join(testMountDir, fname) 215 dfinfo, err1 := os.Stat(destinationFile) 216 if err1 != nil { 217 t.Fatalf("Could not stat file %v", destinationFile) 218 } 219 if dfinfo.Size() != int64(len(contents)) { 220 t.Fatalf("Mismatch in size actual(%v) vs expected(%v)", dfinfo.Size(), int64(len(contents))) 221 } 222 223 fd, err2 := os.OpenFile(destinationFile, os.O_RDONLY, os.FileMode(0665)) 224 if err2 != nil { 225 t.Fatalf("Could not open file %v", destinationFile) 226 } 227 newcontent := make([]byte, len(contents)) 228 _, err := fd.Read(newcontent) 229 if err != nil { 230 t.Fatalf("Could not read from file %v", err) 231 } 232 err = fd.Close() 233 if err != nil { 234 t.Fatalf("Could not close file %v", err) 235 } 236 237 if !bytes.Equal(contents, newcontent) { 238 t.Fatalf("File content mismatch expected (%v): received (%v) ", contents, newcontent) 239 } 240 } 241 242 func getRandomBytes(size int) []byte { 243 contents := make([]byte, size) 244 rand.Read(contents) 245 return contents 246 } 247 248 func isDirEmpty(name string) bool { 249 f, err := os.Open(name) 250 if err != nil { 251 return false 252 } 253 defer f.Close() 254 255 _, err = f.Readdirnames(1) 256 257 return err == io.EOF 258 } 259 260 type testAPI struct { 261 api *api.API 262 } 263 264 type testData struct { 265 testDir string 266 testUploadDir string 267 testMountDir string 268 bzzHash string 269 files map[string]fileInfo 270 toEncrypt bool 271 swarmfs *SwarmFS 272 } 273 274 // 275 func (ta *testAPI) initSubtest(name string) (*testData, error) { 276 var err error 277 d := &testData{} 278 d.testDir, err = ioutil.TempDir(os.TempDir(), name) 279 if err != nil { 280 return nil, fmt.Errorf("Couldn't create test dir: %v", err) 281 } 282 return d, nil 283 } 284 285 // 286 func (ta *testAPI) uploadAndMount(dat *testData, t *testing.T) (*testData, error) { 287 // 288 err := os.MkdirAll(dat.testUploadDir, 0777) 289 if err != nil { 290 return nil, fmt.Errorf("Couldn't create upload dir: %v", err) 291 } 292 // 293 err = os.MkdirAll(dat.testMountDir, 0777) 294 if err != nil { 295 return nil, fmt.Errorf("Couldn't create mount dir: %v", err) 296 } 297 // 298 dat.bzzHash = createTestFilesAndUploadToSwarm(t, ta.api, dat.files, dat.testUploadDir, dat.toEncrypt) 299 log.Debug("Created test files and uploaded to Swarm") 300 // 301 dat.swarmfs = mountDir(t, ta.api, dat.files, dat.bzzHash, dat.testMountDir) 302 log.Debug("Mounted swarm fs") 303 return dat, nil 304 } 305 306 // 307 func addDir(root string, name string) (string, error) { 308 d := filepath.Join(root, name) 309 err := os.MkdirAll(d, 0777) 310 if err != nil { 311 return "", fmt.Errorf("Couldn't create dir inside test dir: %v", err) 312 } 313 return d, nil 314 } 315 316 func (ta *testAPI) mountListAndUnmountEncrypted(t *testing.T) { 317 log.Debug("Starting mountListAndUnmountEncrypted test") 318 ta.mountListAndUnmount(t, true) 319 log.Debug("Test mountListAndUnmountEncrypted terminated") 320 } 321 322 func (ta *testAPI) mountListAndUnmountNonEncrypted(t *testing.T) { 323 log.Debug("Starting mountListAndUnmountNonEncrypted test") 324 ta.mountListAndUnmount(t, false) 325 log.Debug("Test mountListAndUnmountNonEncrypted terminated") 326 } 327 328 // 329 func (ta *testAPI) mountListAndUnmount(t *testing.T, toEncrypt bool) { 330 dat, err := ta.initSubtest("mountListAndUnmount") 331 if err != nil { 332 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 333 } 334 defer os.RemoveAll(dat.testDir) 335 336 dat.toEncrypt = toEncrypt 337 dat.testUploadDir = filepath.Join(dat.testDir, "testUploadDir") 338 dat.testMountDir = filepath.Join(dat.testDir, "testMountDir") 339 dat.files = make(map[string]fileInfo) 340 341 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 342 dat.files["2.txt"] = fileInfo{0711, 333, 444, getRandomBytes(10)} 343 dat.files["3.txt"] = fileInfo{0622, 333, 444, getRandomBytes(100)} 344 dat.files["4.txt"] = fileInfo{0533, 333, 444, getRandomBytes(1024)} 345 dat.files["5.txt"] = fileInfo{0544, 333, 444, getRandomBytes(10)} 346 dat.files["6.txt"] = fileInfo{0555, 333, 444, getRandomBytes(10)} 347 dat.files["7.txt"] = fileInfo{0666, 333, 444, getRandomBytes(10)} 348 dat.files["8.txt"] = fileInfo{0777, 333, 333, getRandomBytes(10)} 349 dat.files["11.txt"] = fileInfo{0777, 333, 444, getRandomBytes(10)} 350 dat.files["111.txt"] = fileInfo{0777, 333, 444, getRandomBytes(10)} 351 dat.files["two/2.txt"] = fileInfo{0777, 333, 444, getRandomBytes(10)} 352 dat.files["two/2/2.txt"] = fileInfo{0777, 333, 444, getRandomBytes(10)} 353 dat.files["two/2./2.txt"] = fileInfo{0777, 444, 444, getRandomBytes(10)} 354 dat.files["twice/2.txt"] = fileInfo{0777, 444, 333, getRandomBytes(200)} 355 dat.files["one/two/three/four/five/six/seven/eight/nine/10.txt"] = fileInfo{0777, 333, 444, getRandomBytes(10240)} 356 dat.files["one/two/three/four/five/six/six"] = fileInfo{0777, 333, 444, getRandomBytes(10)} 357 358 dat, err = ta.uploadAndMount(dat, t) 359 if err != nil { 360 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 361 } 362 defer dat.swarmfs.Stop() 363 // 364 _, err = dat.swarmfs.Unmount(dat.testMountDir) 365 if err != nil { 366 t.Fatalf("could not unmount %v", dat.bzzHash) 367 } 368 log.Debug("Unmount successful") 369 if !isDirEmpty(dat.testMountDir) { 370 t.Fatalf("unmount didnt work for %v", dat.testMountDir) 371 } 372 log.Debug("subtest terminated") 373 } 374 375 func (ta *testAPI) maxMountsEncrypted(t *testing.T) { 376 log.Debug("Starting maxMountsEncrypted test") 377 ta.runMaxMounts(t, true) 378 log.Debug("Test maxMountsEncrypted terminated") 379 } 380 381 func (ta *testAPI) maxMountsNonEncrypted(t *testing.T) { 382 log.Debug("Starting maxMountsNonEncrypted test") 383 ta.runMaxMounts(t, false) 384 log.Debug("Test maxMountsNonEncrypted terminated") 385 } 386 387 // 388 func (ta *testAPI) runMaxMounts(t *testing.T, toEncrypt bool) { 389 dat, err := ta.initSubtest("runMaxMounts") 390 if err != nil { 391 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 392 } 393 defer os.RemoveAll(dat.testDir) 394 395 dat.toEncrypt = toEncrypt 396 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload1") 397 dat.testMountDir = filepath.Join(dat.testDir, "max-mount1") 398 dat.files = make(map[string]fileInfo) 399 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 400 401 dat, err = ta.uploadAndMount(dat, t) 402 if err != nil { 403 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 404 } 405 defer dat.swarmfs.Stop() 406 407 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload2") 408 dat.testMountDir = filepath.Join(dat.testDir, "max-mount2") 409 dat.files["2.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 410 411 dat, err = ta.uploadAndMount(dat, t) 412 if err != nil { 413 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 414 } 415 416 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload3") 417 dat.testMountDir = filepath.Join(dat.testDir, "max-mount3") 418 dat.files["3.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 419 420 dat, err = ta.uploadAndMount(dat, t) 421 if err != nil { 422 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 423 } 424 425 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload4") 426 dat.testMountDir = filepath.Join(dat.testDir, "max-mount4") 427 dat.files["4.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 428 429 dat, err = ta.uploadAndMount(dat, t) 430 if err != nil { 431 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 432 } 433 434 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload5") 435 dat.testMountDir = filepath.Join(dat.testDir, "max-mount5") 436 dat.files["5.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 437 438 dat, err = ta.uploadAndMount(dat, t) 439 if err != nil { 440 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 441 } 442 443 // 444 testUploadDir6 := filepath.Join(dat.testDir, "max-upload6") 445 err = os.MkdirAll(testUploadDir6, 0777) 446 if err != nil { 447 t.Fatalf("Couldn't create upload dir 6: %v", err) 448 } 449 dat.files["6.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 450 testMountDir6 := filepath.Join(dat.testDir, "max-mount6") 451 err = os.MkdirAll(testMountDir6, 0777) 452 if err != nil { 453 t.Fatalf("Couldn't create mount dir 5: %v", err) 454 } 455 bzzHash6 := createTestFilesAndUploadToSwarm(t, ta.api, dat.files, testUploadDir6, toEncrypt) 456 log.Debug("Created test files and uploaded to swarm with uploadDir6") 457 _, err = dat.swarmfs.Mount(bzzHash6, testMountDir6) 458 if err == nil { 459 t.Fatalf("Expected this mount to fail due to exceeding max number of allowed mounts, but succeeded. %v", bzzHash6) 460 } 461 log.Debug("Maximum mount reached, additional mount failed. Correct.") 462 } 463 464 func (ta *testAPI) remountEncrypted(t *testing.T) { 465 log.Debug("Starting remountEncrypted test") 466 ta.remount(t, true) 467 log.Debug("Test remountEncrypted terminated") 468 } 469 func (ta *testAPI) remountNonEncrypted(t *testing.T) { 470 log.Debug("Starting remountNonEncrypted test") 471 ta.remount(t, false) 472 log.Debug("Test remountNonEncrypted terminated") 473 } 474 475 // 476 func (ta *testAPI) remount(t *testing.T, toEncrypt bool) { 477 dat, err := ta.initSubtest("remount") 478 if err != nil { 479 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 480 } 481 defer os.RemoveAll(dat.testDir) 482 483 dat.toEncrypt = toEncrypt 484 dat.testUploadDir = filepath.Join(dat.testDir, "remount-upload1") 485 dat.testMountDir = filepath.Join(dat.testDir, "remount-mount1") 486 dat.files = make(map[string]fileInfo) 487 488 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 489 490 dat, err = ta.uploadAndMount(dat, t) 491 if err != nil { 492 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 493 } 494 defer dat.swarmfs.Stop() 495 496 // 497 testMountDir2, err2 := addDir(dat.testDir, "remount-mount2") 498 if err2 != nil { 499 t.Fatalf("Error creating second mount dir: %v", err2) 500 } 501 _, err2 = dat.swarmfs.Mount(dat.bzzHash, testMountDir2) 502 if err2 != nil { 503 t.Fatalf("Error mounting hash second time on different dir %v", dat.bzzHash) 504 } 505 506 // 507 dat.files["2.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 508 testUploadDir2, err3 := addDir(dat.testDir, "remount-upload2") 509 if err3 != nil { 510 t.Fatalf("Error creating second upload dir: %v", err3) 511 } 512 bzzHash2 := createTestFilesAndUploadToSwarm(t, ta.api, dat.files, testUploadDir2, toEncrypt) 513 _, err = swarmfs.Mount(bzzHash2, dat.testMountDir) 514 if err == nil { 515 t.Fatalf("Error mounting hash %v", bzzHash2) 516 } 517 log.Debug("Mount on existing mount point failed. Correct.") 518 519 // 520 failDir, err3 := addDir(dat.testDir, "remount-fail") 521 if err3 != nil { 522 t.Fatalf("Error creating remount dir: %v", bzzHash2) 523 } 524 failHash := "0xfea11223344" 525 _, err = swarmfs.Mount(failHash, failDir) 526 if err == nil { 527 t.Fatalf("Expected this mount to fail due to non existing hash. But succeeded %v", failHash) 528 } 529 log.Debug("Nonexistent hash hasn't been mounted. Correct.") 530 } 531 532 func (ta *testAPI) unmountEncrypted(t *testing.T) { 533 log.Debug("Starting unmountEncrypted test") 534 ta.unmount(t, true) 535 log.Debug("Test unmountEncrypted terminated") 536 } 537 538 func (ta *testAPI) unmountNonEncrypted(t *testing.T) { 539 log.Debug("Starting unmountNonEncrypted test") 540 ta.unmount(t, false) 541 log.Debug("Test unmountNonEncrypted terminated") 542 } 543 544 // 545 func (ta *testAPI) unmount(t *testing.T, toEncrypt bool) { 546 dat, err := ta.initSubtest("unmount") 547 if err != nil { 548 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 549 } 550 defer os.RemoveAll(dat.testDir) 551 552 dat.toEncrypt = toEncrypt 553 dat.testUploadDir = filepath.Join(dat.testDir, "ex-upload1") 554 dat.testMountDir = filepath.Join(dat.testDir, "ex-mount1") 555 dat.files = make(map[string]fileInfo) 556 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 557 558 dat, err = ta.uploadAndMount(dat, t) 559 if err != nil { 560 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 561 } 562 defer dat.swarmfs.Stop() 563 564 _, err = dat.swarmfs.Unmount(dat.testMountDir) 565 if err != nil { 566 t.Fatalf("could not unmount %v", dat.bzzHash) 567 } 568 log.Debug("Unmounted Dir") 569 570 mi := swarmfs.Listmounts() 571 log.Debug("Going to list mounts") 572 for _, minfo := range mi { 573 log.Debug("Mount point in list: ", "point", minfo.MountPoint) 574 if minfo.MountPoint == dat.testMountDir { 575 t.Fatalf("mount state not cleaned up in unmount case %v", dat.testMountDir) 576 } 577 } 578 log.Debug("subtest terminated") 579 } 580 581 func (ta *testAPI) unmountWhenResourceBusyEncrypted(t *testing.T) { 582 log.Debug("Starting unmountWhenResourceBusyEncrypted test") 583 ta.unmountWhenResourceBusy(t, true) 584 log.Debug("Test unmountWhenResourceBusyEncrypted terminated") 585 } 586 func (ta *testAPI) unmountWhenResourceBusyNonEncrypted(t *testing.T) { 587 log.Debug("Starting unmountWhenResourceBusyNonEncrypted test") 588 ta.unmountWhenResourceBusy(t, false) 589 log.Debug("Test unmountWhenResourceBusyNonEncrypted terminated") 590 } 591 592 // 593 func (ta *testAPI) unmountWhenResourceBusy(t *testing.T, toEncrypt bool) { 594 dat, err := ta.initSubtest("unmountWhenResourceBusy") 595 if err != nil { 596 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 597 } 598 defer os.RemoveAll(dat.testDir) 599 600 dat.toEncrypt = toEncrypt 601 dat.testUploadDir = filepath.Join(dat.testDir, "ex-upload1") 602 dat.testMountDir = filepath.Join(dat.testDir, "ex-mount1") 603 dat.files = make(map[string]fileInfo) 604 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 605 606 dat, err = ta.uploadAndMount(dat, t) 607 if err != nil { 608 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 609 } 610 defer dat.swarmfs.Stop() 611 612 // 613 actualPath := filepath.Join(dat.testMountDir, "2.txt") 614 // 615 d, err := os.Create(actualPath) 616 if err != nil { 617 t.Fatalf("Couldn't create new file: %v", err) 618 } 619 // 620 // 621 defer d.Close() 622 _, err = d.Write(getRandomBytes(10)) 623 if err != nil { 624 t.Fatalf("Couldn't write to file: %v", err) 625 } 626 log.Debug("Bytes written") 627 628 _, err = dat.swarmfs.Unmount(dat.testMountDir) 629 if err == nil { 630 t.Fatalf("Expected mount to fail due to resource busy, but it succeeded...") 631 } 632 // 633 err = d.Close() 634 if err != nil { 635 t.Fatalf("Couldn't close file! %v", dat.bzzHash) 636 } 637 log.Debug("File closed") 638 639 // 640 _, err = dat.swarmfs.Unmount(dat.testMountDir) 641 if err != nil { 642 t.Fatalf("Expected mount to succeed after freeing resource, but it failed: %v", err) 643 } 644 // 645 mi := dat.swarmfs.Listmounts() 646 log.Debug("Going to list mounts") 647 for _, minfo := range mi { 648 log.Debug("Mount point in list: ", "point", minfo.MountPoint) 649 if minfo.MountPoint == dat.testMountDir { 650 t.Fatalf("mount state not cleaned up in unmount case %v", dat.testMountDir) 651 } 652 } 653 log.Debug("subtest terminated") 654 } 655 656 func (ta *testAPI) seekInMultiChunkFileEncrypted(t *testing.T) { 657 log.Debug("Starting seekInMultiChunkFileEncrypted test") 658 ta.seekInMultiChunkFile(t, true) 659 log.Debug("Test seekInMultiChunkFileEncrypted terminated") 660 } 661 662 func (ta *testAPI) seekInMultiChunkFileNonEncrypted(t *testing.T) { 663 log.Debug("Starting seekInMultiChunkFileNonEncrypted test") 664 ta.seekInMultiChunkFile(t, false) 665 log.Debug("Test seekInMultiChunkFileNonEncrypted terminated") 666 } 667 668 // 669 func (ta *testAPI) seekInMultiChunkFile(t *testing.T, toEncrypt bool) { 670 dat, err := ta.initSubtest("seekInMultiChunkFile") 671 if err != nil { 672 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 673 } 674 defer os.RemoveAll(dat.testDir) 675 676 dat.toEncrypt = toEncrypt 677 dat.testUploadDir = filepath.Join(dat.testDir, "seek-upload1") 678 dat.testMountDir = filepath.Join(dat.testDir, "seek-mount") 679 dat.files = make(map[string]fileInfo) 680 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10240)} 681 682 dat, err = ta.uploadAndMount(dat, t) 683 if err != nil { 684 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 685 } 686 defer dat.swarmfs.Stop() 687 688 // 689 actualPath := filepath.Join(dat.testMountDir, "1.txt") 690 d, err := os.OpenFile(actualPath, os.O_RDONLY, os.FileMode(0700)) 691 if err != nil { 692 t.Fatalf("Couldn't open file: %v", err) 693 } 694 log.Debug("Opened file") 695 defer func() { 696 err := d.Close() 697 if err != nil { 698 t.Fatalf("Error closing file! %v", err) 699 } 700 }() 701 702 _, err = d.Seek(5000, 0) 703 if err != nil { 704 t.Fatalf("Error seeking in file: %v", err) 705 } 706 707 contents := make([]byte, 1024) 708 _, err = d.Read(contents) 709 if err != nil { 710 t.Fatalf("Error reading file: %v", err) 711 } 712 log.Debug("Read contents") 713 finfo := dat.files["1.txt"] 714 715 if !bytes.Equal(finfo.contents[:6024][5000:], contents) { 716 t.Fatalf("File seek contents mismatch") 717 } 718 log.Debug("subtest terminated") 719 } 720 721 func (ta *testAPI) createNewFileEncrypted(t *testing.T) { 722 log.Debug("Starting createNewFileEncrypted test") 723 ta.createNewFile(t, true) 724 log.Debug("Test createNewFileEncrypted terminated") 725 } 726 727 func (ta *testAPI) createNewFileNonEncrypted(t *testing.T) { 728 log.Debug("Starting createNewFileNonEncrypted test") 729 ta.createNewFile(t, false) 730 log.Debug("Test createNewFileNonEncrypted terminated") 731 } 732 733 // 734 // 735 func (ta *testAPI) createNewFile(t *testing.T, toEncrypt bool) { 736 dat, err := ta.initSubtest("createNewFile") 737 if err != nil { 738 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 739 } 740 defer os.RemoveAll(dat.testDir) 741 742 dat.toEncrypt = toEncrypt 743 dat.testUploadDir = filepath.Join(dat.testDir, "create-upload1") 744 dat.testMountDir = filepath.Join(dat.testDir, "create-mount") 745 dat.files = make(map[string]fileInfo) 746 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 747 dat.files["five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 748 dat.files["six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 749 750 dat, err = ta.uploadAndMount(dat, t) 751 if err != nil { 752 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 753 } 754 defer dat.swarmfs.Stop() 755 756 // 757 actualPath := filepath.Join(dat.testMountDir, "2.txt") 758 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665)) 759 if err1 != nil { 760 t.Fatalf("Could not open file %s : %v", actualPath, err1) 761 } 762 defer d.Close() 763 log.Debug("Opened file") 764 contents := make([]byte, 11) 765 _, err = rand.Read(contents) 766 if err != nil { 767 t.Fatalf("Could not rand read contents %v", err) 768 } 769 log.Debug("content read") 770 _, err = d.Write(contents) 771 if err != nil { 772 t.Fatalf("Couldn't write contents: %v", err) 773 } 774 log.Debug("content written") 775 err = d.Close() 776 if err != nil { 777 t.Fatalf("Couldn't close file: %v", err) 778 } 779 log.Debug("file closed") 780 781 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 782 if err2 != nil { 783 t.Fatalf("Could not unmount %v", err2) 784 } 785 log.Debug("Directory unmounted") 786 787 testMountDir2, err3 := addDir(dat.testDir, "create-mount2") 788 if err3 != nil { 789 t.Fatalf("Error creating mount dir2: %v", err3) 790 } 791 // 792 dat.files["2.txt"] = fileInfo{0700, 333, 444, contents} 793 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2) 794 log.Debug("Directory mounted again") 795 796 checkFile(t, testMountDir2, "2.txt", contents) 797 _, err2 = dat.swarmfs.Unmount(testMountDir2) 798 if err2 != nil { 799 t.Fatalf("Could not unmount %v", err2) 800 } 801 log.Debug("subtest terminated") 802 } 803 804 func (ta *testAPI) createNewFileInsideDirectoryEncrypted(t *testing.T) { 805 log.Debug("Starting createNewFileInsideDirectoryEncrypted test") 806 ta.createNewFileInsideDirectory(t, true) 807 log.Debug("Test createNewFileInsideDirectoryEncrypted terminated") 808 } 809 810 func (ta *testAPI) createNewFileInsideDirectoryNonEncrypted(t *testing.T) { 811 log.Debug("Starting createNewFileInsideDirectoryNonEncrypted test") 812 ta.createNewFileInsideDirectory(t, false) 813 log.Debug("Test createNewFileInsideDirectoryNonEncrypted terminated") 814 } 815 816 // 817 func (ta *testAPI) createNewFileInsideDirectory(t *testing.T, toEncrypt bool) { 818 dat, err := ta.initSubtest("createNewFileInsideDirectory") 819 if err != nil { 820 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 821 } 822 defer os.RemoveAll(dat.testDir) 823 824 dat.toEncrypt = toEncrypt 825 dat.testUploadDir = filepath.Join(dat.testDir, "createinsidedir-upload") 826 dat.testMountDir = filepath.Join(dat.testDir, "createinsidedir-mount") 827 dat.files = make(map[string]fileInfo) 828 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 829 830 dat, err = ta.uploadAndMount(dat, t) 831 if err != nil { 832 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 833 } 834 defer dat.swarmfs.Stop() 835 836 // 837 dirToCreate := filepath.Join(dat.testMountDir, "one") 838 actualPath := filepath.Join(dirToCreate, "2.txt") 839 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665)) 840 if err1 != nil { 841 t.Fatalf("Could not create file %s : %v", actualPath, err1) 842 } 843 defer d.Close() 844 log.Debug("File opened") 845 contents := make([]byte, 11) 846 _, err = rand.Read(contents) 847 if err != nil { 848 t.Fatalf("Error filling random bytes into byte array %v", err) 849 } 850 log.Debug("Content read") 851 _, err = d.Write(contents) 852 if err != nil { 853 t.Fatalf("Error writing random bytes into file %v", err) 854 } 855 log.Debug("Content written") 856 err = d.Close() 857 if err != nil { 858 t.Fatalf("Error closing file %v", err) 859 } 860 log.Debug("File closed") 861 862 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 863 if err2 != nil { 864 t.Fatalf("Could not unmount %v", err2) 865 } 866 log.Debug("Directory unmounted") 867 868 testMountDir2, err3 := addDir(dat.testDir, "createinsidedir-mount2") 869 if err3 != nil { 870 t.Fatalf("Error creating mount dir2: %v", err3) 871 } 872 // 873 dat.files["one/2.txt"] = fileInfo{0700, 333, 444, contents} 874 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2) 875 log.Debug("Directory mounted again") 876 877 checkFile(t, testMountDir2, "one/2.txt", contents) 878 _, err = dat.swarmfs.Unmount(testMountDir2) 879 if err != nil { 880 t.Fatalf("could not unmount %v", dat.bzzHash) 881 } 882 log.Debug("subtest terminated") 883 } 884 885 func (ta *testAPI) createNewFileInsideNewDirectoryEncrypted(t *testing.T) { 886 log.Debug("Starting createNewFileInsideNewDirectoryEncrypted test") 887 ta.createNewFileInsideNewDirectory(t, true) 888 log.Debug("Test createNewFileInsideNewDirectoryEncrypted terminated") 889 } 890 891 func (ta *testAPI) createNewFileInsideNewDirectoryNonEncrypted(t *testing.T) { 892 log.Debug("Starting createNewFileInsideNewDirectoryNonEncrypted test") 893 ta.createNewFileInsideNewDirectory(t, false) 894 log.Debug("Test createNewFileInsideNewDirectoryNonEncrypted terminated") 895 } 896 897 // 898 func (ta *testAPI) createNewFileInsideNewDirectory(t *testing.T, toEncrypt bool) { 899 dat, err := ta.initSubtest("createNewFileInsideNewDirectory") 900 if err != nil { 901 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 902 } 903 defer os.RemoveAll(dat.testDir) 904 905 dat.toEncrypt = toEncrypt 906 dat.testUploadDir = filepath.Join(dat.testDir, "createinsidenewdir-upload") 907 dat.testMountDir = filepath.Join(dat.testDir, "createinsidenewdir-mount") 908 dat.files = make(map[string]fileInfo) 909 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 910 911 dat, err = ta.uploadAndMount(dat, t) 912 if err != nil { 913 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 914 } 915 defer dat.swarmfs.Stop() 916 917 // 918 dirToCreate, err2 := addDir(dat.testMountDir, "one") 919 if err2 != nil { 920 t.Fatalf("Error creating mount dir2: %v", err2) 921 } 922 actualPath := filepath.Join(dirToCreate, "2.txt") 923 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665)) 924 if err1 != nil { 925 t.Fatalf("Could not create file %s : %v", actualPath, err1) 926 } 927 defer d.Close() 928 log.Debug("File opened") 929 contents := make([]byte, 11) 930 _, err = rand.Read(contents) 931 if err != nil { 932 t.Fatalf("Error writing random bytes to byte array: %v", err) 933 } 934 log.Debug("content read") 935 _, err = d.Write(contents) 936 if err != nil { 937 t.Fatalf("Error writing to file: %v", err) 938 } 939 log.Debug("content written") 940 err = d.Close() 941 if err != nil { 942 t.Fatalf("Error closing file: %v", err) 943 } 944 log.Debug("File closed") 945 946 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 947 if err2 != nil { 948 t.Fatalf("Could not unmount %v", err2) 949 } 950 log.Debug("Directory unmounted") 951 952 // 953 dat.files["one/2.txt"] = fileInfo{0700, 333, 444, contents} 954 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir) 955 log.Debug("Directory mounted again") 956 957 checkFile(t, dat.testMountDir, "one/2.txt", contents) 958 _, err2 = dat.swarmfs.Unmount(dat.testMountDir) 959 if err2 != nil { 960 t.Fatalf("Could not unmount %v", err2) 961 } 962 log.Debug("subtest terminated") 963 } 964 965 func (ta *testAPI) removeExistingFileEncrypted(t *testing.T) { 966 log.Debug("Starting removeExistingFileEncrypted test") 967 ta.removeExistingFile(t, true) 968 log.Debug("Test removeExistingFileEncrypted terminated") 969 } 970 971 func (ta *testAPI) removeExistingFileNonEncrypted(t *testing.T) { 972 log.Debug("Starting removeExistingFileNonEncrypted test") 973 ta.removeExistingFile(t, false) 974 log.Debug("Test removeExistingFileNonEncrypted terminated") 975 } 976 977 // 978 func (ta *testAPI) removeExistingFile(t *testing.T, toEncrypt bool) { 979 dat, err := ta.initSubtest("removeExistingFile") 980 if err != nil { 981 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 982 } 983 defer os.RemoveAll(dat.testDir) 984 985 dat.toEncrypt = toEncrypt 986 dat.testUploadDir = filepath.Join(dat.testDir, "remove-upload") 987 dat.testMountDir = filepath.Join(dat.testDir, "remove-mount") 988 dat.files = make(map[string]fileInfo) 989 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 990 dat.files["five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 991 dat.files["six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 992 993 dat, err = ta.uploadAndMount(dat, t) 994 if err != nil { 995 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 996 } 997 defer dat.swarmfs.Stop() 998 999 // 1000 actualPath := filepath.Join(dat.testMountDir, "five.txt") 1001 err = os.Remove(actualPath) 1002 if err != nil { 1003 t.Fatalf("Error removing file! %v", err) 1004 } 1005 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1006 if err2 != nil { 1007 t.Fatalf("Could not unmount %v", err2) 1008 } 1009 log.Debug("Directory unmounted") 1010 1011 // 1012 delete(dat.files, "five.txt") 1013 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir) 1014 _, err = os.Stat(actualPath) 1015 if err == nil { 1016 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1017 } 1018 _, err2 = dat.swarmfs.Unmount(dat.testMountDir) 1019 if err2 != nil { 1020 t.Fatalf("Could not unmount %v", err2) 1021 } 1022 log.Debug("subtest terminated") 1023 } 1024 1025 func (ta *testAPI) removeExistingFileInsideDirEncrypted(t *testing.T) { 1026 log.Debug("Starting removeExistingFileInsideDirEncrypted test") 1027 ta.removeExistingFileInsideDir(t, true) 1028 log.Debug("Test removeExistingFileInsideDirEncrypted terminated") 1029 } 1030 1031 func (ta *testAPI) removeExistingFileInsideDirNonEncrypted(t *testing.T) { 1032 log.Debug("Starting removeExistingFileInsideDirNonEncrypted test") 1033 ta.removeExistingFileInsideDir(t, false) 1034 log.Debug("Test removeExistingFileInsideDirNonEncrypted terminated") 1035 } 1036 1037 // 1038 func (ta *testAPI) removeExistingFileInsideDir(t *testing.T, toEncrypt bool) { 1039 dat, err := ta.initSubtest("removeExistingFileInsideDir") 1040 if err != nil { 1041 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1042 } 1043 defer os.RemoveAll(dat.testDir) 1044 1045 dat.toEncrypt = toEncrypt 1046 dat.testUploadDir = filepath.Join(dat.testDir, "remove-upload") 1047 dat.testMountDir = filepath.Join(dat.testDir, "remove-mount") 1048 dat.files = make(map[string]fileInfo) 1049 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1050 dat.files["one/five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1051 dat.files["one/six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1052 1053 dat, err = ta.uploadAndMount(dat, t) 1054 if err != nil { 1055 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1056 } 1057 defer dat.swarmfs.Stop() 1058 1059 // 1060 actualPath := filepath.Join(dat.testMountDir, "one") 1061 actualPath = filepath.Join(actualPath, "five.txt") 1062 err = os.Remove(actualPath) 1063 if err != nil { 1064 t.Fatalf("Error removing file! %v", err) 1065 } 1066 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1067 if err2 != nil { 1068 t.Fatalf("Could not unmount %v", err2) 1069 } 1070 log.Debug("Directory unmounted") 1071 1072 // 1073 delete(dat.files, "one/five.txt") 1074 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir) 1075 _, err = os.Stat(actualPath) 1076 if err == nil { 1077 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1078 } 1079 1080 okPath := filepath.Join(dat.testMountDir, "one") 1081 okPath = filepath.Join(okPath, "six.txt") 1082 _, err = os.Stat(okPath) 1083 if err != nil { 1084 t.Fatal("Expected file to be present in re-mount after removal, but it is not there") 1085 } 1086 _, err2 = dat.swarmfs.Unmount(dat.testMountDir) 1087 if err2 != nil { 1088 t.Fatalf("Could not unmount %v", err2) 1089 } 1090 log.Debug("subtest terminated") 1091 } 1092 1093 func (ta *testAPI) removeNewlyAddedFileEncrypted(t *testing.T) { 1094 log.Debug("Starting removeNewlyAddedFileEncrypted test") 1095 ta.removeNewlyAddedFile(t, true) 1096 log.Debug("Test removeNewlyAddedFileEncrypted terminated") 1097 } 1098 1099 func (ta *testAPI) removeNewlyAddedFileNonEncrypted(t *testing.T) { 1100 log.Debug("Starting removeNewlyAddedFileNonEncrypted test") 1101 ta.removeNewlyAddedFile(t, false) 1102 log.Debug("Test removeNewlyAddedFileNonEncrypted terminated") 1103 } 1104 1105 // 1106 func (ta *testAPI) removeNewlyAddedFile(t *testing.T, toEncrypt bool) { 1107 dat, err := ta.initSubtest("removeNewlyAddedFile") 1108 if err != nil { 1109 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1110 } 1111 defer os.RemoveAll(dat.testDir) 1112 1113 dat.toEncrypt = toEncrypt 1114 dat.testUploadDir = filepath.Join(dat.testDir, "removenew-upload") 1115 dat.testMountDir = filepath.Join(dat.testDir, "removenew-mount") 1116 dat.files = make(map[string]fileInfo) 1117 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1118 dat.files["five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1119 dat.files["six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1120 1121 dat, err = ta.uploadAndMount(dat, t) 1122 if err != nil { 1123 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1124 } 1125 defer dat.swarmfs.Stop() 1126 1127 // 1128 dirToCreate := filepath.Join(dat.testMountDir, "one") 1129 err = os.MkdirAll(dirToCreate, os.FileMode(0665)) 1130 if err != nil { 1131 t.Fatalf("Error creating dir in mounted dir: %v", err) 1132 } 1133 actualPath := filepath.Join(dirToCreate, "2.txt") 1134 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665)) 1135 if err1 != nil { 1136 t.Fatalf("Could not create file %s : %v", actualPath, err1) 1137 } 1138 defer d.Close() 1139 log.Debug("file opened") 1140 contents := make([]byte, 11) 1141 _, err = rand.Read(contents) 1142 if err != nil { 1143 t.Fatalf("Error writing random bytes to byte array: %v", err) 1144 } 1145 log.Debug("content read") 1146 _, err = d.Write(contents) 1147 if err != nil { 1148 t.Fatalf("Error writing random bytes to file: %v", err) 1149 } 1150 log.Debug("content written") 1151 err = d.Close() 1152 if err != nil { 1153 t.Fatalf("Error closing file: %v", err) 1154 } 1155 log.Debug("file closed") 1156 1157 checkFile(t, dat.testMountDir, "one/2.txt", contents) 1158 log.Debug("file checked") 1159 1160 err = os.Remove(actualPath) 1161 if err != nil { 1162 t.Fatalf("Error removing file: %v", err) 1163 } 1164 log.Debug("file removed") 1165 1166 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1167 if err2 != nil { 1168 t.Fatalf("Could not unmount %v", err2) 1169 } 1170 log.Debug("Directory unmounted") 1171 1172 testMountDir2, err3 := addDir(dat.testDir, "removenew-mount2") 1173 if err3 != nil { 1174 t.Fatalf("Error creating mount dir2: %v", err3) 1175 } 1176 // 1177 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2) 1178 log.Debug("Directory mounted again") 1179 1180 if dat.bzzHash != mi.LatestManifest { 1181 t.Fatalf("same contents different hash orig(%v): new(%v)", dat.bzzHash, mi.LatestManifest) 1182 } 1183 _, err2 = dat.swarmfs.Unmount(testMountDir2) 1184 if err2 != nil { 1185 t.Fatalf("Could not unmount %v", err2) 1186 } 1187 log.Debug("subtest terminated") 1188 } 1189 1190 func (ta *testAPI) addNewFileAndModifyContentsEncrypted(t *testing.T) { 1191 log.Debug("Starting addNewFileAndModifyContentsEncrypted test") 1192 ta.addNewFileAndModifyContents(t, true) 1193 log.Debug("Test addNewFileAndModifyContentsEncrypted terminated") 1194 } 1195 1196 func (ta *testAPI) addNewFileAndModifyContentsNonEncrypted(t *testing.T) { 1197 log.Debug("Starting addNewFileAndModifyContentsNonEncrypted test") 1198 ta.addNewFileAndModifyContents(t, false) 1199 log.Debug("Test addNewFileAndModifyContentsNonEncrypted terminated") 1200 } 1201 1202 // 1203 func (ta *testAPI) addNewFileAndModifyContents(t *testing.T, toEncrypt bool) { 1204 dat, err := ta.initSubtest("addNewFileAndModifyContents") 1205 if err != nil { 1206 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1207 } 1208 defer os.RemoveAll(dat.testDir) 1209 1210 dat.toEncrypt = toEncrypt 1211 dat.testUploadDir = filepath.Join(dat.testDir, "modifyfile-upload") 1212 dat.testMountDir = filepath.Join(dat.testDir, "modifyfile-mount") 1213 dat.files = make(map[string]fileInfo) 1214 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1215 dat.files["five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1216 dat.files["six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1217 1218 dat, err = ta.uploadAndMount(dat, t) 1219 if err != nil { 1220 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1221 } 1222 defer dat.swarmfs.Stop() 1223 1224 // 1225 actualPath := filepath.Join(dat.testMountDir, "2.txt") 1226 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665)) 1227 if err1 != nil { 1228 t.Fatalf("Could not create file %s : %v", actualPath, err1) 1229 } 1230 defer d.Close() 1231 // 1232 log.Debug("file opened") 1233 line1 := []byte("Line 1") 1234 _, err = rand.Read(line1) 1235 if err != nil { 1236 t.Fatalf("Error writing random bytes to byte array: %v", err) 1237 } 1238 log.Debug("line read") 1239 _, err = d.Write(line1) 1240 if err != nil { 1241 t.Fatalf("Error writing random bytes to file: %v", err) 1242 } 1243 log.Debug("line written") 1244 err = d.Close() 1245 if err != nil { 1246 t.Fatalf("Error closing file: %v", err) 1247 } 1248 log.Debug("file closed") 1249 1250 // 1251 mi1, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1252 if err2 != nil { 1253 t.Fatalf("Could not unmount %v", err2) 1254 } 1255 log.Debug("Directory unmounted") 1256 1257 // 1258 testMountDir2, err3 := addDir(dat.testDir, "modifyfile-mount2") 1259 if err3 != nil { 1260 t.Fatalf("Error creating mount dir2: %v", err3) 1261 } 1262 dat.files["2.txt"] = fileInfo{0700, 333, 444, line1} 1263 _ = mountDir(t, ta.api, dat.files, mi1.LatestManifest, testMountDir2) 1264 log.Debug("Directory mounted again") 1265 1266 checkFile(t, testMountDir2, "2.txt", line1) 1267 log.Debug("file checked") 1268 1269 // 1270 mi2, err4 := dat.swarmfs.Unmount(testMountDir2) 1271 if err4 != nil { 1272 t.Fatalf("Could not unmount %v", err4) 1273 } 1274 log.Debug("Directory unmounted again") 1275 1276 // 1277 // 1278 err = os.RemoveAll(dat.testMountDir) 1279 if err != nil { 1280 t.Fatalf("Error cleaning up mount dir: %v", err) 1281 } 1282 // 1283 err = os.MkdirAll(dat.testMountDir, 0777) 1284 if err != nil { 1285 t.Fatalf("Error re-creating mount dir: %v", err) 1286 } 1287 // 1288 _ = mountDir(t, ta.api, dat.files, mi2.LatestManifest, dat.testMountDir) 1289 log.Debug("Directory mounted yet again") 1290 1291 // 1292 fd, err5 := os.OpenFile(actualPath, os.O_RDWR|os.O_APPEND, os.FileMode(0665)) 1293 if err5 != nil { 1294 t.Fatalf("Could not create file %s : %v", actualPath, err5) 1295 } 1296 defer fd.Close() 1297 log.Debug("file opened") 1298 // 1299 line2 := []byte("Line 2") 1300 _, err = rand.Read(line2) 1301 if err != nil { 1302 t.Fatalf("Error modifying random bytes to byte array: %v", err) 1303 } 1304 log.Debug("line read") 1305 _, err = fd.Seek(int64(len(line1)), 0) 1306 if err != nil { 1307 t.Fatalf("Error seeking position for modification: %v", err) 1308 } 1309 _, err = fd.Write(line2) 1310 if err != nil { 1311 t.Fatalf("Error modifying file: %v", err) 1312 } 1313 log.Debug("line written") 1314 err = fd.Close() 1315 if err != nil { 1316 t.Fatalf("Error closing modified file; %v", err) 1317 } 1318 log.Debug("file closed") 1319 1320 // 1321 mi3, err6 := dat.swarmfs.Unmount(dat.testMountDir) 1322 if err6 != nil { 1323 t.Fatalf("Could not unmount %v", err6) 1324 } 1325 log.Debug("Directory unmounted yet again") 1326 1327 // 1328 testMountDir4, err7 := addDir(dat.testDir, "modifyfile-mount4") 1329 if err7 != nil { 1330 t.Fatalf("Could not unmount %v", err7) 1331 } 1332 b := [][]byte{line1, line2} 1333 line1and2 := bytes.Join(b, []byte("")) 1334 dat.files["2.txt"] = fileInfo{0700, 333, 444, line1and2} 1335 _ = mountDir(t, ta.api, dat.files, mi3.LatestManifest, testMountDir4) 1336 log.Debug("Directory mounted final time") 1337 1338 checkFile(t, testMountDir4, "2.txt", line1and2) 1339 _, err = dat.swarmfs.Unmount(testMountDir4) 1340 if err != nil { 1341 t.Fatalf("Could not unmount %v", err) 1342 } 1343 log.Debug("subtest terminated") 1344 } 1345 1346 func (ta *testAPI) removeEmptyDirEncrypted(t *testing.T) { 1347 log.Debug("Starting removeEmptyDirEncrypted test") 1348 ta.removeEmptyDir(t, true) 1349 log.Debug("Test removeEmptyDirEncrypted terminated") 1350 } 1351 1352 func (ta *testAPI) removeEmptyDirNonEncrypted(t *testing.T) { 1353 log.Debug("Starting removeEmptyDirNonEncrypted test") 1354 ta.removeEmptyDir(t, false) 1355 log.Debug("Test removeEmptyDirNonEncrypted terminated") 1356 } 1357 1358 // 1359 func (ta *testAPI) removeEmptyDir(t *testing.T, toEncrypt bool) { 1360 dat, err := ta.initSubtest("removeEmptyDir") 1361 if err != nil { 1362 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1363 } 1364 defer os.RemoveAll(dat.testDir) 1365 1366 dat.toEncrypt = toEncrypt 1367 dat.testUploadDir = filepath.Join(dat.testDir, "rmdir-upload") 1368 dat.testMountDir = filepath.Join(dat.testDir, "rmdir-mount") 1369 dat.files = make(map[string]fileInfo) 1370 dat.files["1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1371 dat.files["five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1372 dat.files["six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1373 1374 dat, err = ta.uploadAndMount(dat, t) 1375 if err != nil { 1376 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1377 } 1378 defer dat.swarmfs.Stop() 1379 1380 _, err2 := addDir(dat.testMountDir, "newdir") 1381 if err2 != nil { 1382 t.Fatalf("Could not unmount %v", err2) 1383 } 1384 mi, err := dat.swarmfs.Unmount(dat.testMountDir) 1385 if err != nil { 1386 t.Fatalf("Could not unmount %v", err) 1387 } 1388 log.Debug("Directory unmounted") 1389 // 1390 if dat.bzzHash != mi.LatestManifest { 1391 t.Fatalf("same contents different hash orig(%v): new(%v)", dat.bzzHash, mi.LatestManifest) 1392 } 1393 log.Debug("subtest terminated") 1394 } 1395 1396 func (ta *testAPI) removeDirWhichHasFilesEncrypted(t *testing.T) { 1397 log.Debug("Starting removeDirWhichHasFilesEncrypted test") 1398 ta.removeDirWhichHasFiles(t, true) 1399 log.Debug("Test removeDirWhichHasFilesEncrypted terminated") 1400 } 1401 func (ta *testAPI) removeDirWhichHasFilesNonEncrypted(t *testing.T) { 1402 log.Debug("Starting removeDirWhichHasFilesNonEncrypted test") 1403 ta.removeDirWhichHasFiles(t, false) 1404 log.Debug("Test removeDirWhichHasFilesNonEncrypted terminated") 1405 } 1406 1407 // 1408 func (ta *testAPI) removeDirWhichHasFiles(t *testing.T, toEncrypt bool) { 1409 dat, err := ta.initSubtest("removeDirWhichHasFiles") 1410 if err != nil { 1411 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1412 } 1413 defer os.RemoveAll(dat.testDir) 1414 1415 dat.toEncrypt = toEncrypt 1416 dat.testUploadDir = filepath.Join(dat.testDir, "rmdir-upload") 1417 dat.testMountDir = filepath.Join(dat.testDir, "rmdir-mount") 1418 dat.files = make(map[string]fileInfo) 1419 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1420 dat.files["two/five.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1421 dat.files["two/six.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1422 1423 dat, err = ta.uploadAndMount(dat, t) 1424 if err != nil { 1425 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1426 } 1427 defer dat.swarmfs.Stop() 1428 1429 // 1430 dirPath := filepath.Join(dat.testMountDir, "two") 1431 err = os.RemoveAll(dirPath) 1432 if err != nil { 1433 t.Fatalf("Error removing directory in mounted dir: %v", err) 1434 } 1435 1436 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1437 if err2 != nil { 1438 t.Fatalf("Could not unmount %v ", err2) 1439 } 1440 log.Debug("Directory unmounted") 1441 1442 // 1443 delete(dat.files, "two/five.txt") 1444 delete(dat.files, "two/six.txt") 1445 1446 // 1447 testMountDir2, err3 := addDir(dat.testDir, "remount-mount2") 1448 if err3 != nil { 1449 t.Fatalf("Could not unmount %v", err3) 1450 } 1451 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2) 1452 log.Debug("Directory mounted") 1453 actualPath := filepath.Join(dirPath, "five.txt") 1454 _, err = os.Stat(actualPath) 1455 if err == nil { 1456 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1457 } 1458 _, err = os.Stat(dirPath) 1459 if err == nil { 1460 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1461 } 1462 _, err = dat.swarmfs.Unmount(testMountDir2) 1463 if err != nil { 1464 t.Fatalf("Could not unmount %v", err) 1465 } 1466 log.Debug("subtest terminated") 1467 } 1468 1469 func (ta *testAPI) removeDirWhichHasSubDirsEncrypted(t *testing.T) { 1470 log.Debug("Starting removeDirWhichHasSubDirsEncrypted test") 1471 ta.removeDirWhichHasSubDirs(t, true) 1472 log.Debug("Test removeDirWhichHasSubDirsEncrypted terminated") 1473 } 1474 1475 func (ta *testAPI) removeDirWhichHasSubDirsNonEncrypted(t *testing.T) { 1476 log.Debug("Starting removeDirWhichHasSubDirsNonEncrypted test") 1477 ta.removeDirWhichHasSubDirs(t, false) 1478 log.Debug("Test removeDirWhichHasSubDirsNonEncrypted terminated") 1479 } 1480 1481 // 1482 func (ta *testAPI) removeDirWhichHasSubDirs(t *testing.T, toEncrypt bool) { 1483 dat, err := ta.initSubtest("removeDirWhichHasSubDirs") 1484 if err != nil { 1485 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1486 } 1487 defer os.RemoveAll(dat.testDir) 1488 1489 dat.toEncrypt = toEncrypt 1490 dat.testUploadDir = filepath.Join(dat.testDir, "rmsubdir-upload") 1491 dat.testMountDir = filepath.Join(dat.testDir, "rmsubdir-mount") 1492 dat.files = make(map[string]fileInfo) 1493 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1494 dat.files["two/three/2.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1495 dat.files["two/three/3.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1496 dat.files["two/four/5.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1497 dat.files["two/four/6.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1498 dat.files["two/four/six/7.txt"] = fileInfo{0700, 333, 444, getRandomBytes(10)} 1499 1500 dat, err = ta.uploadAndMount(dat, t) 1501 if err != nil { 1502 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1503 } 1504 defer dat.swarmfs.Stop() 1505 1506 dirPath := filepath.Join(dat.testMountDir, "two") 1507 err = os.RemoveAll(dirPath) 1508 if err != nil { 1509 t.Fatalf("Error removing directory in mounted dir: %v", err) 1510 } 1511 1512 // 1513 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir) 1514 if err2 != nil { 1515 t.Fatalf("Could not unmount %v ", err2) 1516 } 1517 log.Debug("Directory unmounted") 1518 1519 // 1520 delete(dat.files, "two/three/2.txt") 1521 delete(dat.files, "two/three/3.txt") 1522 delete(dat.files, "two/four/5.txt") 1523 delete(dat.files, "two/four/6.txt") 1524 delete(dat.files, "two/four/six/7.txt") 1525 1526 // 1527 testMountDir2, err3 := addDir(dat.testDir, "remount-mount2") 1528 if err3 != nil { 1529 t.Fatalf("Could not unmount %v", err3) 1530 } 1531 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2) 1532 log.Debug("Directory mounted again") 1533 actualPath := filepath.Join(dirPath, "three") 1534 actualPath = filepath.Join(actualPath, "2.txt") 1535 _, err = os.Stat(actualPath) 1536 if err == nil { 1537 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1538 } 1539 actualPath = filepath.Join(dirPath, "four") 1540 _, err = os.Stat(actualPath) 1541 if err == nil { 1542 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1543 } 1544 _, err = os.Stat(dirPath) 1545 if err == nil { 1546 t.Fatal("Expected file to not be present in re-mount after removal, but it is there") 1547 } 1548 _, err = dat.swarmfs.Unmount(testMountDir2) 1549 if err != nil { 1550 t.Fatalf("Could not unmount %v", err) 1551 } 1552 log.Debug("subtest terminated") 1553 } 1554 1555 func (ta *testAPI) appendFileContentsToEndEncrypted(t *testing.T) { 1556 log.Debug("Starting appendFileContentsToEndEncrypted test") 1557 ta.appendFileContentsToEnd(t, true) 1558 log.Debug("Test appendFileContentsToEndEncrypted terminated") 1559 } 1560 1561 func (ta *testAPI) appendFileContentsToEndNonEncrypted(t *testing.T) { 1562 log.Debug("Starting appendFileContentsToEndNonEncrypted test") 1563 ta.appendFileContentsToEnd(t, false) 1564 log.Debug("Test appendFileContentsToEndNonEncrypted terminated") 1565 } 1566 1567 // 1568 func (ta *testAPI) appendFileContentsToEnd(t *testing.T, toEncrypt bool) { 1569 dat, err := ta.initSubtest("appendFileContentsToEnd") 1570 if err != nil { 1571 t.Fatalf("Couldn't initialize subtest dirs: %v", err) 1572 } 1573 defer os.RemoveAll(dat.testDir) 1574 1575 dat.toEncrypt = toEncrypt 1576 dat.testUploadDir = filepath.Join(dat.testDir, "appendlargefile-upload") 1577 dat.testMountDir = filepath.Join(dat.testDir, "appendlargefile-mount") 1578 dat.files = make(map[string]fileInfo) 1579 1580 line1 := make([]byte, 10) 1581 _, err = rand.Read(line1) 1582 if err != nil { 1583 t.Fatalf("Error writing random bytes to byte array: %v", err) 1584 } 1585 1586 dat.files["1.txt"] = fileInfo{0700, 333, 444, line1} 1587 1588 dat, err = ta.uploadAndMount(dat, t) 1589 if err != nil { 1590 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err) 1591 } 1592 defer dat.swarmfs.Stop() 1593 1594 actualPath := filepath.Join(dat.testMountDir, "1.txt") 1595 fd, err4 := os.OpenFile(actualPath, os.O_RDWR|os.O_APPEND, os.FileMode(0665)) 1596 if err4 != nil { 1597 t.Fatalf("Could not create file %s : %v", actualPath, err4) 1598 } 1599 defer fd.Close() 1600 log.Debug("file opened") 1601 line2 := make([]byte, 5) 1602 _, err = rand.Read(line2) 1603 if err != nil { 1604 t.Fatalf("Error writing random bytes to byte array: %v", err) 1605 } 1606 log.Debug("line read") 1607 _, err = fd.Seek(int64(len(line1)), 0) 1608 if err != nil { 1609 t.Fatalf("Error searching for position to append: %v", err) 1610 } 1611 _, err = fd.Write(line2) 1612 if err != nil { 1613 t.Fatalf("Error appending: %v", err) 1614 } 1615 log.Debug("line written") 1616 err = fd.Close() 1617 if err != nil { 1618 t.Fatalf("Error closing file: %v", err) 1619 } 1620 log.Debug("file closed") 1621 1622 mi1, err5 := dat.swarmfs.Unmount(dat.testMountDir) 1623 if err5 != nil { 1624 t.Fatalf("Could not unmount %v ", err5) 1625 } 1626 log.Debug("Directory unmounted") 1627 1628 // 1629 b := [][]byte{line1, line2} 1630 line1and2 := bytes.Join(b, []byte("")) 1631 dat.files["1.txt"] = fileInfo{0700, 333, 444, line1and2} 1632 testMountDir2, err6 := addDir(dat.testDir, "remount-mount2") 1633 if err6 != nil { 1634 t.Fatalf("Could not unmount %v", err6) 1635 } 1636 _ = mountDir(t, ta.api, dat.files, mi1.LatestManifest, testMountDir2) 1637 log.Debug("Directory mounted") 1638 1639 checkFile(t, testMountDir2, "1.txt", line1and2) 1640 1641 _, err = dat.swarmfs.Unmount(testMountDir2) 1642 if err != nil { 1643 t.Fatalf("Could not unmount %v", err) 1644 } 1645 log.Debug("subtest terminated") 1646 } 1647 1648 // 1649 func TestFUSE(t *testing.T) { 1650 t.Skip("disable fuse tests until they are stable") 1651 // 1652 datadir, err := ioutil.TempDir("", "fuse") 1653 if err != nil { 1654 t.Fatalf("unable to create temp dir: %v", err) 1655 } 1656 defer os.RemoveAll(datadir) 1657 1658 fileStore, err := storage.NewLocalFileStore(datadir, make([]byte, 32)) 1659 if err != nil { 1660 t.Fatal(err) 1661 } 1662 ta := &testAPI{api: api.NewAPI(fileStore, nil, nil, nil)} 1663 1664 // 1665 // 1666 t.Run("mountListAndUnmountEncrypted", ta.mountListAndUnmountEncrypted) 1667 t.Run("remountEncrypted", ta.remountEncrypted) 1668 t.Run("unmountWhenResourceBusyNonEncrypted", ta.unmountWhenResourceBusyNonEncrypted) 1669 t.Run("removeExistingFileEncrypted", ta.removeExistingFileEncrypted) 1670 t.Run("addNewFileAndModifyContentsNonEncrypted", ta.addNewFileAndModifyContentsNonEncrypted) 1671 t.Run("removeDirWhichHasFilesNonEncrypted", ta.removeDirWhichHasFilesNonEncrypted) 1672 t.Run("appendFileContentsToEndEncrypted", ta.appendFileContentsToEndEncrypted) 1673 1674 // 1675 // 1676 if *longrunning { 1677 t.Run("mountListAndUnmountNonEncrypted", ta.mountListAndUnmountNonEncrypted) 1678 t.Run("maxMountsEncrypted", ta.maxMountsEncrypted) 1679 t.Run("maxMountsNonEncrypted", ta.maxMountsNonEncrypted) 1680 t.Run("remountNonEncrypted", ta.remountNonEncrypted) 1681 t.Run("unmountEncrypted", ta.unmountEncrypted) 1682 t.Run("unmountNonEncrypted", ta.unmountNonEncrypted) 1683 t.Run("unmountWhenResourceBusyEncrypted", ta.unmountWhenResourceBusyEncrypted) 1684 t.Run("unmountWhenResourceBusyNonEncrypted", ta.unmountWhenResourceBusyNonEncrypted) 1685 t.Run("seekInMultiChunkFileEncrypted", ta.seekInMultiChunkFileEncrypted) 1686 t.Run("seekInMultiChunkFileNonEncrypted", ta.seekInMultiChunkFileNonEncrypted) 1687 t.Run("createNewFileEncrypted", ta.createNewFileEncrypted) 1688 t.Run("createNewFileNonEncrypted", ta.createNewFileNonEncrypted) 1689 t.Run("createNewFileInsideDirectoryEncrypted", ta.createNewFileInsideDirectoryEncrypted) 1690 t.Run("createNewFileInsideDirectoryNonEncrypted", ta.createNewFileInsideDirectoryNonEncrypted) 1691 t.Run("createNewFileInsideNewDirectoryEncrypted", ta.createNewFileInsideNewDirectoryEncrypted) 1692 t.Run("createNewFileInsideNewDirectoryNonEncrypted", ta.createNewFileInsideNewDirectoryNonEncrypted) 1693 t.Run("removeExistingFileNonEncrypted", ta.removeExistingFileNonEncrypted) 1694 t.Run("removeExistingFileInsideDirEncrypted", ta.removeExistingFileInsideDirEncrypted) 1695 t.Run("removeExistingFileInsideDirNonEncrypted", ta.removeExistingFileInsideDirNonEncrypted) 1696 t.Run("removeNewlyAddedFileEncrypted", ta.removeNewlyAddedFileEncrypted) 1697 t.Run("removeNewlyAddedFileNonEncrypted", ta.removeNewlyAddedFileNonEncrypted) 1698 t.Run("addNewFileAndModifyContentsEncrypted", ta.addNewFileAndModifyContentsEncrypted) 1699 t.Run("removeEmptyDirEncrypted", ta.removeEmptyDirEncrypted) 1700 t.Run("removeEmptyDirNonEncrypted", ta.removeEmptyDirNonEncrypted) 1701 t.Run("removeDirWhichHasFilesEncrypted", ta.removeDirWhichHasFilesEncrypted) 1702 t.Run("removeDirWhichHasSubDirsEncrypted", ta.removeDirWhichHasSubDirsEncrypted) 1703 t.Run("removeDirWhichHasSubDirsNonEncrypted", ta.removeDirWhichHasSubDirsNonEncrypted) 1704 t.Run("appendFileContentsToEndNonEncrypted", ta.appendFileContentsToEndNonEncrypted) 1705 } 1706 }