github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/swarm/api/swarmfs_unix_test.go (about)

     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  
    17  // +build linux darwin
    18  
    19  package api
    20  
    21  import (
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  )
    27  
    28  var testUploadDir, _ = ioutil.TempDir(os.TempDir(), "fuse-source")
    29  var testMountDir, _ = ioutil.TempDir(os.TempDir(), "fuse-dest")
    30  
    31  func testFuseFileSystem(t *testing.T, f func(*FileSystem)) {
    32  	testApi(t, func(api *Api) {
    33  		f(NewFileSystem(api))
    34  	})
    35  }
    36  
    37  func createTestFiles(t *testing.T, files []string) {
    38  
    39  	os.RemoveAll(testUploadDir)
    40  	os.RemoveAll(testMountDir)
    41  	defer os.MkdirAll(testMountDir, 0777)
    42  
    43  	for f := range files {
    44  		actualPath := filepath.Join(testUploadDir, files[f])
    45  		filePath := filepath.Dir(actualPath)
    46  
    47  		err := os.MkdirAll(filePath, 0777)
    48  		if err != nil {
    49  			t.Fatalf("Error creating directory '%v' : %v", filePath, err)
    50  		}
    51  
    52  		_, err1 := os.OpenFile(actualPath, os.O_RDONLY|os.O_CREATE, 0666)
    53  		if err1 != nil {
    54  			t.Fatalf("Error creating file %v: %v", actualPath, err1)
    55  		}
    56  	}
    57  
    58  }
    59  
    60  func compareFiles(t *testing.T, files []string) {
    61  
    62  	for f := range files {
    63  
    64  		sourceFile := filepath.Join(testUploadDir, files[f])
    65  		destinationFile := filepath.Join(testMountDir, files[f])
    66  
    67  		sfinfo, err := os.Stat(sourceFile)
    68  		if err != nil {
    69  			t.Fatalf("Source file %v missing in mount: %v", files[f], err)
    70  		}
    71  
    72  		dfinfo, err := os.Stat(destinationFile)
    73  		if err != nil {
    74  			t.Fatalf("Destination file %v missing in mount: %v", files[f], err)
    75  		}
    76  
    77  		if sfinfo.Size() != dfinfo.Size() {
    78  			t.Fatalf("Size mismatch  source (%v) vs destination(%v)", sfinfo.Size(), dfinfo.Size())
    79  		}
    80  
    81  		if dfinfo.Mode().Perm().String() != "-r-x------" {
    82  			t.Fatalf("Permission is not 0500for file: %v", err)
    83  		}
    84  
    85  	}
    86  }
    87  
    88  func doHashTest(fs *FileSystem, t *testing.T, ensName string, files ...string) {
    89  
    90  	createTestFiles(t, files)
    91  	bzzhash, err := fs.Upload(testUploadDir, "")
    92  	if err != nil {
    93  		t.Fatalf("Error uploading directory %v: %v", testUploadDir, err)
    94  	}
    95  
    96  	swarmfs := NewSwarmFS(fs.api)
    97  	_ ,err1 := swarmfs.Mount(bzzhash, testMountDir)
    98  	if err1 != nil {
    99  
   100  		t.Fatalf("Error mounting hash  %v: %v", bzzhash, err)
   101  	}
   102  	compareFiles(t, files)
   103  	_, err2 := swarmfs.Unmount(testMountDir)
   104  	if err2 != nil {
   105  		t.Fatalf("Error unmounting path  %v: %v", testMountDir, err)
   106  	}
   107  	swarmfs.Stop()
   108  
   109  }
   110  
   111  // mounting with manifest Hash
   112  func TestFuseMountingScenarios(t *testing.T) {
   113  	testFuseFileSystem(t, func(fs *FileSystem) {
   114  
   115  		//doHashTest(fs,t, "test","1.txt")
   116  		doHashTest(fs, t, "", "1.txt")
   117  		doHashTest(fs, t, "", "1.txt", "11.txt", "111.txt", "two/2.txt", "two/two/2.txt", "three/3.txt")
   118  		doHashTest(fs, t, "", "1/2/3/4/5/6/7/8/9/10/11/12/1.txt")
   119  		doHashTest(fs, t, "", "one/one.txt", "one.txt", "once/one.txt", "one/one/one.txt")
   120  
   121  	})
   122  }