swarmfs_test.go 56 KB

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