github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/fuse/swarmfs_test.go (about)

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