swarmfs_test.go 56 KB

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