swarmfs_unix_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build linux darwin
  17. package api
  18. import (
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "testing"
  23. )
  24. var testUploadDir, _ = ioutil.TempDir(os.TempDir(), "fuse-source")
  25. var testMountDir, _ = ioutil.TempDir(os.TempDir(), "fuse-dest")
  26. func testFuseFileSystem(t *testing.T, f func(*FileSystem)) {
  27. testApi(t, func(api *Api) {
  28. f(NewFileSystem(api))
  29. })
  30. }
  31. func createTestFiles(t *testing.T, files []string) {
  32. os.RemoveAll(testUploadDir)
  33. os.RemoveAll(testMountDir)
  34. defer os.MkdirAll(testMountDir, 0777)
  35. for f := range files {
  36. actualPath := filepath.Join(testUploadDir, files[f])
  37. filePath := filepath.Dir(actualPath)
  38. err := os.MkdirAll(filePath, 0777)
  39. if err != nil {
  40. t.Fatalf("Error creating directory '%v' : %v", filePath, err)
  41. }
  42. _, err1 := os.OpenFile(actualPath, os.O_RDONLY|os.O_CREATE, 0666)
  43. if err1 != nil {
  44. t.Fatalf("Error creating file %v: %v", actualPath, err1)
  45. }
  46. }
  47. }
  48. func compareFiles(t *testing.T, files []string) {
  49. for f := range files {
  50. sourceFile := filepath.Join(testUploadDir, files[f])
  51. destinationFile := filepath.Join(testMountDir, files[f])
  52. sfinfo, err := os.Stat(sourceFile)
  53. if err != nil {
  54. t.Fatalf("Source file %v missing in mount: %v", files[f], err)
  55. }
  56. dfinfo, err := os.Stat(destinationFile)
  57. if err != nil {
  58. t.Fatalf("Destination file %v missing in mount: %v", files[f], err)
  59. }
  60. if sfinfo.Size() != dfinfo.Size() {
  61. t.Fatalf("Size mismatch source (%v) vs destination(%v)", sfinfo.Size(), dfinfo.Size())
  62. }
  63. if dfinfo.Mode().Perm().String() != "-r-x------" {
  64. t.Fatalf("Permission is not 0500for file: %v", err)
  65. }
  66. }
  67. }
  68. func doHashTest(fs *FileSystem, t *testing.T, ensName string, files ...string) {
  69. createTestFiles(t, files)
  70. bzzhash, err := fs.Upload(testUploadDir, "")
  71. if err != nil {
  72. t.Fatalf("Error uploading directory %v: %v", testUploadDir, err)
  73. }
  74. swarmfs := NewSwarmFS(fs.api)
  75. _ ,err1 := swarmfs.Mount(bzzhash, testMountDir)
  76. if err1 != nil {
  77. t.Fatalf("Error mounting hash %v: %v", bzzhash, err)
  78. }
  79. compareFiles(t, files)
  80. _, err2 := swarmfs.Unmount(testMountDir)
  81. if err2 != nil {
  82. t.Fatalf("Error unmounting path %v: %v", testMountDir, err)
  83. }
  84. swarmfs.Stop()
  85. }
  86. // mounting with manifest Hash
  87. func TestFuseMountingScenarios(t *testing.T) {
  88. testFuseFileSystem(t, func(fs *FileSystem) {
  89. //doHashTest(fs,t, "test","1.txt")
  90. doHashTest(fs, t, "", "1.txt")
  91. doHashTest(fs, t, "", "1.txt", "11.txt", "111.txt", "two/2.txt", "two/two/2.txt", "three/3.txt")
  92. doHashTest(fs, t, "", "1/2/3/4/5/6/7/8/9/10/11/12/1.txt")
  93. doHashTest(fs, t, "", "one/one.txt", "one.txt", "once/one.txt", "one/one/one.txt")
  94. })
  95. }