basicfs_test.go 14 KB

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