basicfs_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // Copyright (C) 2017 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "sort"
  13. "strings"
  14. "testing"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/rand"
  17. )
  18. func setup(t *testing.T) (*BasicFilesystem, string) {
  19. t.Helper()
  20. dir, err := ioutil.TempDir("", "")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. return newBasicFilesystem(dir), dir
  25. }
  26. func TestChmodFile(t *testing.T) {
  27. fs, dir := setup(t)
  28. path := filepath.Join(dir, "file")
  29. defer os.RemoveAll(dir)
  30. defer os.Chmod(path, 0666)
  31. fd, err := os.Create(path)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. fd.Close()
  36. if err := os.Chmod(path, 0666); err != nil {
  37. t.Error(err)
  38. }
  39. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != 0666 {
  40. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  41. }
  42. if err := fs.Chmod("file", 0444); err != nil {
  43. t.Error(err)
  44. }
  45. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != 0444 {
  46. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  47. }
  48. }
  49. func TestChownFile(t *testing.T) {
  50. if runtime.GOOS == "windows" {
  51. t.Skip("Not supported on Windows")
  52. return
  53. }
  54. if os.Getuid() != 0 {
  55. // We are not root. No expectation of being able to chown. Our tests
  56. // typically don't run with CAP_FOWNER.
  57. t.Skip("Test not possible")
  58. return
  59. }
  60. fs, dir := setup(t)
  61. path := filepath.Join(dir, "file")
  62. defer os.RemoveAll(dir)
  63. defer os.Chmod(path, 0666)
  64. fd, err := os.Create(path)
  65. if err != nil {
  66. t.Error("Unexpected error:", err)
  67. }
  68. fd.Close()
  69. _, err = fs.Lstat("file")
  70. if err != nil {
  71. t.Error("Unexpected error:", err)
  72. }
  73. newUID := 1000 + rand.Intn(30000)
  74. newGID := 1000 + rand.Intn(30000)
  75. if err := fs.Lchown("file", newUID, newGID); err != nil {
  76. t.Error("Unexpected error:", err)
  77. }
  78. info, err := fs.Lstat("file")
  79. if err != nil {
  80. t.Error("Unexpected error:", err)
  81. }
  82. if info.Owner() != newUID {
  83. t.Errorf("Incorrect owner, expected %d but got %d", newUID, info.Owner())
  84. }
  85. if info.Group() != newGID {
  86. t.Errorf("Incorrect group, expected %d but got %d", newGID, info.Group())
  87. }
  88. }
  89. func TestChmodDir(t *testing.T) {
  90. fs, dir := setup(t)
  91. path := filepath.Join(dir, "dir")
  92. defer os.RemoveAll(dir)
  93. mode := os.FileMode(0755)
  94. if runtime.GOOS == "windows" {
  95. mode = os.FileMode(0777)
  96. }
  97. defer os.Chmod(path, mode)
  98. if err := os.Mkdir(path, mode); err != nil {
  99. t.Error(err)
  100. }
  101. // On UNIX, Mkdir will subtract the umask, so force desired mode explicitly
  102. if err := os.Chmod(path, mode); err != nil {
  103. t.Error(err)
  104. }
  105. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != mode {
  106. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  107. }
  108. if err := fs.Chmod("dir", 0555); err != nil {
  109. t.Error(err)
  110. }
  111. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != 0555 {
  112. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  113. }
  114. }
  115. func TestChtimes(t *testing.T) {
  116. fs, dir := setup(t)
  117. path := filepath.Join(dir, "file")
  118. defer os.RemoveAll(dir)
  119. fd, err := os.Create(path)
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. fd.Close()
  124. mtime := time.Now().Add(-time.Hour)
  125. fs.Chtimes("file", mtime, mtime)
  126. stat, err := os.Stat(path)
  127. if err != nil {
  128. t.Error(err)
  129. }
  130. diff := stat.ModTime().Sub(mtime)
  131. if diff > 3*time.Second || diff < -3*time.Second {
  132. t.Errorf("%s != %s", stat.Mode(), mtime)
  133. }
  134. }
  135. func TestCreate(t *testing.T) {
  136. fs, dir := setup(t)
  137. path := filepath.Join(dir, "file")
  138. defer os.RemoveAll(dir)
  139. if _, err := os.Stat(path); err == nil {
  140. t.Errorf("exists?")
  141. }
  142. fd, err := fs.Create("file")
  143. if err != nil {
  144. t.Error(err)
  145. }
  146. fd.Close()
  147. if _, err := os.Stat(path); err != nil {
  148. t.Error(err)
  149. }
  150. }
  151. func TestCreateSymlink(t *testing.T) {
  152. if runtime.GOOS == "windows" {
  153. t.Skip("windows not supported")
  154. }
  155. fs, dir := setup(t)
  156. path := filepath.Join(dir, "file")
  157. defer os.RemoveAll(dir)
  158. if err := fs.CreateSymlink("blah", "file"); err != nil {
  159. t.Error(err)
  160. }
  161. if target, err := os.Readlink(path); err != nil || target != "blah" {
  162. t.Error("target", target, "err", err)
  163. }
  164. if err := os.Remove(path); err != nil {
  165. t.Error(err)
  166. }
  167. if err := fs.CreateSymlink(filepath.Join("..", "blah"), "file"); err != nil {
  168. t.Error(err)
  169. }
  170. if target, err := os.Readlink(path); err != nil || target != filepath.Join("..", "blah") {
  171. t.Error("target", target, "err", err)
  172. }
  173. }
  174. func TestDirNames(t *testing.T) {
  175. fs, dir := setup(t)
  176. defer os.RemoveAll(dir)
  177. // Case differences
  178. testCases := []string{
  179. "a",
  180. "bC",
  181. }
  182. sort.Strings(testCases)
  183. for _, sub := range testCases {
  184. if err := os.Mkdir(filepath.Join(dir, sub), 0777); err != nil {
  185. t.Error(err)
  186. }
  187. }
  188. if dirs, err := fs.DirNames("."); err != nil || len(dirs) != len(testCases) {
  189. t.Errorf("%s %s %s", err, dirs, testCases)
  190. } else {
  191. sort.Strings(dirs)
  192. for i := range dirs {
  193. if dirs[i] != testCases[i] {
  194. t.Errorf("%s != %s", dirs[i], testCases[i])
  195. }
  196. }
  197. }
  198. }
  199. func TestNames(t *testing.T) {
  200. // Tests that all names are without the root directory.
  201. fs, dir := setup(t)
  202. defer os.RemoveAll(dir)
  203. expected := "file"
  204. fd, err := fs.Create(expected)
  205. if err != nil {
  206. t.Error(err)
  207. }
  208. defer fd.Close()
  209. if fd.Name() != expected {
  210. t.Errorf("incorrect %s != %s", fd.Name(), expected)
  211. }
  212. if stat, err := fd.Stat(); err != nil || stat.Name() != expected {
  213. t.Errorf("incorrect %s != %s (%v)", stat.Name(), expected, err)
  214. }
  215. if err := fs.Mkdir("dir", 0777); err != nil {
  216. t.Error(err)
  217. }
  218. expected = filepath.Join("dir", "file")
  219. fd, err = fs.Create(expected)
  220. if err != nil {
  221. t.Error(err)
  222. }
  223. defer fd.Close()
  224. if fd.Name() != expected {
  225. t.Errorf("incorrect %s != %s", fd.Name(), expected)
  226. }
  227. // os.fd.Stat() returns just base, so do we.
  228. if stat, err := fd.Stat(); err != nil || stat.Name() != filepath.Base(expected) {
  229. t.Errorf("incorrect %s != %s (%v)", stat.Name(), filepath.Base(expected), err)
  230. }
  231. }
  232. func TestGlob(t *testing.T) {
  233. // Tests that all names are without the root directory.
  234. fs, dir := setup(t)
  235. defer os.RemoveAll(dir)
  236. for _, dirToCreate := range []string{
  237. filepath.Join("a", "test", "b"),
  238. filepath.Join("a", "best", "b"),
  239. filepath.Join("a", "best", "c"),
  240. } {
  241. if err := fs.MkdirAll(dirToCreate, 0777); err != nil {
  242. t.Error(err)
  243. }
  244. }
  245. testCases := []struct {
  246. pattern string
  247. matches []string
  248. }{
  249. {
  250. filepath.Join("a", "?est", "?"),
  251. []string{
  252. filepath.Join("a", "test", "b"),
  253. filepath.Join("a", "best", "b"),
  254. filepath.Join("a", "best", "c"),
  255. },
  256. },
  257. {
  258. filepath.Join("a", "?est", "b"),
  259. []string{
  260. filepath.Join("a", "test", "b"),
  261. filepath.Join("a", "best", "b"),
  262. },
  263. },
  264. {
  265. filepath.Join("a", "best", "?"),
  266. []string{
  267. filepath.Join("a", "best", "b"),
  268. filepath.Join("a", "best", "c"),
  269. },
  270. },
  271. }
  272. for _, testCase := range testCases {
  273. results, err := fs.Glob(testCase.pattern)
  274. sort.Strings(results)
  275. sort.Strings(testCase.matches)
  276. if err != nil {
  277. t.Error(err)
  278. }
  279. if len(results) != len(testCase.matches) {
  280. t.Errorf("result count mismatch")
  281. }
  282. for i := range testCase.matches {
  283. if results[i] != testCase.matches[i] {
  284. t.Errorf("%s != %s", results[i], testCase.matches[i])
  285. }
  286. }
  287. }
  288. }
  289. func TestUsage(t *testing.T) {
  290. fs, dir := setup(t)
  291. defer os.RemoveAll(dir)
  292. usage, err := fs.Usage(".")
  293. if err != nil {
  294. if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" {
  295. t.Skip()
  296. }
  297. t.Errorf("Unexpected error: %s", err)
  298. }
  299. if usage.Free < 1 {
  300. t.Error("Disk is full?", usage.Free)
  301. }
  302. }
  303. func TestRooted(t *testing.T) {
  304. type testcase struct {
  305. root string
  306. rel string
  307. joined string
  308. ok bool
  309. }
  310. cases := []testcase{
  311. // Valid cases
  312. {"foo", "bar", "foo/bar", true},
  313. {"foo", "/bar", "foo/bar", true},
  314. {"foo/", "bar", "foo/bar", true},
  315. {"foo/", "/bar", "foo/bar", true},
  316. {"baz/foo", "bar", "baz/foo/bar", true},
  317. {"baz/foo", "/bar", "baz/foo/bar", true},
  318. {"baz/foo/", "bar", "baz/foo/bar", true},
  319. {"baz/foo/", "/bar", "baz/foo/bar", true},
  320. {"foo", "bar/baz", "foo/bar/baz", true},
  321. {"foo", "/bar/baz", "foo/bar/baz", true},
  322. {"foo/", "bar/baz", "foo/bar/baz", true},
  323. {"foo/", "/bar/baz", "foo/bar/baz", true},
  324. {"baz/foo", "bar/baz", "baz/foo/bar/baz", true},
  325. {"baz/foo", "/bar/baz", "baz/foo/bar/baz", true},
  326. {"baz/foo/", "bar/baz", "baz/foo/bar/baz", true},
  327. {"baz/foo/", "/bar/baz", "baz/foo/bar/baz", true},
  328. // Not escape attempts, but oddly formatted relative paths.
  329. {"foo", "", "foo", true},
  330. {"foo", "/", "foo", true},
  331. {"foo", "/..", "foo", true},
  332. {"foo", "./bar", "foo/bar", true},
  333. {"foo/", "", "foo", true},
  334. {"foo/", "/", "foo", true},
  335. {"foo/", "/..", "foo", true},
  336. {"foo/", "./bar", "foo/bar", true},
  337. {"baz/foo", "./bar", "baz/foo/bar", true},
  338. {"foo", "./bar/baz", "foo/bar/baz", true},
  339. {"baz/foo", "./bar/baz", "baz/foo/bar/baz", true},
  340. {"baz/foo", "bar/../baz", "baz/foo/baz", true},
  341. {"baz/foo", "/bar/../baz", "baz/foo/baz", true},
  342. {"baz/foo", "./bar/../baz", "baz/foo/baz", true},
  343. // Results in an allowed path, but does it by probing. Disallowed.
  344. {"foo", "../foo", "", false},
  345. {"foo", "../foo/bar", "", false},
  346. {"baz/foo", "../foo/bar", "", false},
  347. {"baz/foo", "../../baz/foo/bar", "", false},
  348. {"baz/foo", "bar/../../foo/bar", "", false},
  349. {"baz/foo", "bar/../../../baz/foo/bar", "", false},
  350. // Escape attempts.
  351. {"foo", "..", "", false},
  352. {"foo", "../", "", false},
  353. {"foo", "../bar", "", false},
  354. {"foo", "../foobar", "", false},
  355. {"foo/", "../bar", "", false},
  356. {"foo/", "../foobar", "", false},
  357. {"baz/foo", "../bar", "", false},
  358. {"baz/foo", "../foobar", "", false},
  359. {"baz/foo/", "../bar", "", false},
  360. {"baz/foo/", "../foobar", "", false},
  361. {"baz/foo/", "bar/../../quux/baz", "", false},
  362. // Empty root is a misconfiguration.
  363. {"", "/foo", "", false},
  364. {"", "foo", "", false},
  365. {"", ".", "", false},
  366. {"", "..", "", false},
  367. {"", "/", "", false},
  368. {"", "", "", false},
  369. // Root=/ is valid, and things should be verified as usual.
  370. {"/", "foo", "/foo", true},
  371. {"/", "/foo", "/foo", true},
  372. {"/", "../foo", "", false},
  373. {"/", "..", "", false},
  374. {"/", "/", "/", true},
  375. {"/", "", "/", true},
  376. // special case for filesystems to be able to MkdirAll('.') for example
  377. {"/", ".", "/", true},
  378. }
  379. if runtime.GOOS == "windows" {
  380. extraCases := []testcase{
  381. {`c:\`, `foo`, `c:\foo`, true},
  382. {`\\?\c:\`, `foo`, `\\?\c:\foo`, true},
  383. {`c:\`, `\foo`, `c:\foo`, true},
  384. {`\\?\c:\`, `\foo`, `\\?\c:\foo`, true},
  385. {`c:\`, `\\foo`, ``, false},
  386. {`c:\`, ``, `c:\`, true},
  387. {`c:\`, `\`, `c:\`, true},
  388. {`\\?\c:\`, `\\foo`, ``, false},
  389. {`\\?\c:\`, ``, `\\?\c:\`, true},
  390. {`\\?\c:\`, `\`, `\\?\c:\`, true},
  391. {`\\?\c:\test`, `.`, `\\?\c:\test`, true},
  392. {`c:\test`, `.`, `c:\test`, true},
  393. {`\\?\c:\test`, `/`, `\\?\c:\test`, true},
  394. {`c:\test`, ``, `c:\test`, true},
  395. // makes no sense, but will be treated simply as a bad filename
  396. {`c:\foo`, `d:\bar`, `c:\foo\d:\bar`, true},
  397. // special case for filesystems to be able to MkdirAll('.') for example
  398. {`c:\`, `.`, `c:\`, true},
  399. {`\\?\c:\`, `.`, `\\?\c:\`, true},
  400. }
  401. for _, tc := range cases {
  402. // Add case where root is backslashed, rel is forward slashed
  403. extraCases = append(extraCases, testcase{
  404. root: filepath.FromSlash(tc.root),
  405. rel: tc.rel,
  406. joined: tc.joined,
  407. ok: tc.ok,
  408. })
  409. // and the opposite
  410. extraCases = append(extraCases, testcase{
  411. root: tc.root,
  412. rel: filepath.FromSlash(tc.rel),
  413. joined: tc.joined,
  414. ok: tc.ok,
  415. })
  416. // and both backslashed
  417. extraCases = append(extraCases, testcase{
  418. root: filepath.FromSlash(tc.root),
  419. rel: filepath.FromSlash(tc.rel),
  420. joined: tc.joined,
  421. ok: tc.ok,
  422. })
  423. }
  424. cases = append(cases, extraCases...)
  425. }
  426. for _, tc := range cases {
  427. fs := BasicFilesystem{root: tc.root}
  428. res, err := fs.rooted(tc.rel)
  429. if tc.ok {
  430. if err != nil {
  431. t.Errorf("Unexpected error for rooted(%q, %q): %v", tc.root, tc.rel, err)
  432. continue
  433. }
  434. exp := filepath.FromSlash(tc.joined)
  435. if res != exp {
  436. t.Errorf("Unexpected result for rooted(%q, %q): %q != expected %q", tc.root, tc.rel, res, exp)
  437. }
  438. } else if err == nil {
  439. t.Errorf("Unexpected pass for rooted(%q, %q) => %q", tc.root, tc.rel, res)
  440. continue
  441. }
  442. }
  443. }
  444. func TestNewBasicFilesystem(t *testing.T) {
  445. if runtime.GOOS == "windows" {
  446. t.Skip("non-windows root paths")
  447. }
  448. currentDir, err := filepath.Abs(".")
  449. if err != nil {
  450. t.Fatal(err)
  451. }
  452. testCases := []struct {
  453. input string
  454. expectedRoot string
  455. expectedURI string
  456. }{
  457. {"/foo/bar/baz", "/foo/bar/baz", "/foo/bar/baz"},
  458. {"/foo/bar/baz/", "/foo/bar/baz", "/foo/bar/baz"},
  459. {"", currentDir, currentDir},
  460. {".", currentDir, currentDir},
  461. {"/", "/", "/"},
  462. }
  463. for _, testCase := range testCases {
  464. fs := newBasicFilesystem(testCase.input)
  465. if fs.root != testCase.expectedRoot {
  466. t.Errorf("root %q != %q", fs.root, testCase.expectedRoot)
  467. }
  468. if fs.URI() != testCase.expectedURI {
  469. t.Errorf("uri %q != %q", fs.URI(), testCase.expectedURI)
  470. }
  471. }
  472. fs := newBasicFilesystem("relative/path")
  473. if fs.root == "relative/path" || !strings.HasPrefix(fs.root, string(PathSeparator)) {
  474. t.Errorf(`newBasicFilesystem("relative/path").root == %q, expected absolutification`, fs.root)
  475. }
  476. }
  477. func TestRel(t *testing.T) {
  478. testCases := []struct {
  479. root string
  480. abs string
  481. expectedRel string
  482. }{
  483. {"/", "/", ""},
  484. {"/", "/test", "test"},
  485. {"/", "/Test", "Test"},
  486. {"/Test", "/Test/test", "test"},
  487. }
  488. if runtime.GOOS == "windows" {
  489. for i := range testCases {
  490. testCases[i].root = filepath.FromSlash(testCases[i].root)
  491. testCases[i].abs = filepath.FromSlash(testCases[i].abs)
  492. testCases[i].expectedRel = filepath.FromSlash(testCases[i].expectedRel)
  493. }
  494. }
  495. for _, tc := range testCases {
  496. if res := rel(tc.abs, tc.root); res != tc.expectedRel {
  497. t.Errorf(`rel("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  498. }
  499. }
  500. }
  501. func TestBasicWalkSkipSymlink(t *testing.T) {
  502. _, dir := setup(t)
  503. defer os.RemoveAll(dir)
  504. testWalkSkipSymlink(t, FilesystemTypeBasic, dir)
  505. }
  506. func TestWalkTraverseDirJunct(t *testing.T) {
  507. _, dir := setup(t)
  508. defer os.RemoveAll(dir)
  509. testWalkTraverseDirJunct(t, FilesystemTypeBasic, dir)
  510. }
  511. func TestWalkInfiniteRecursion(t *testing.T) {
  512. _, dir := setup(t)
  513. defer os.RemoveAll(dir)
  514. testWalkInfiniteRecursion(t, FilesystemTypeBasic, dir)
  515. }