basicfs_test.go 14 KB

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