1
0

basicfs_test.go 14 KB

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