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