basicfs_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != mode {
  102. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  103. }
  104. if err := fs.Chmod("dir", 0555); err != nil {
  105. t.Error(err)
  106. }
  107. if stat, err := os.Stat(path); err != nil || stat.Mode()&os.ModePerm != 0555 {
  108. t.Errorf("wrong perm: %t %#o", err == nil, stat.Mode()&os.ModePerm)
  109. }
  110. }
  111. func TestChtimes(t *testing.T) {
  112. fs, dir := setup(t)
  113. path := filepath.Join(dir, "file")
  114. defer os.RemoveAll(dir)
  115. fd, err := os.Create(path)
  116. if err != nil {
  117. t.Error(err)
  118. }
  119. fd.Close()
  120. mtime := time.Now().Add(-time.Hour)
  121. fs.Chtimes("file", mtime, mtime)
  122. stat, err := os.Stat(path)
  123. if err != nil {
  124. t.Error(err)
  125. }
  126. diff := stat.ModTime().Sub(mtime)
  127. if diff > 3*time.Second || diff < -3*time.Second {
  128. t.Errorf("%s != %s", stat.Mode(), mtime)
  129. }
  130. }
  131. func TestCreate(t *testing.T) {
  132. fs, dir := setup(t)
  133. path := filepath.Join(dir, "file")
  134. defer os.RemoveAll(dir)
  135. if _, err := os.Stat(path); err == nil {
  136. t.Errorf("exists?")
  137. }
  138. fd, err := fs.Create("file")
  139. if err != nil {
  140. t.Error(err)
  141. }
  142. fd.Close()
  143. if _, err := os.Stat(path); err != nil {
  144. t.Error(err)
  145. }
  146. }
  147. func TestCreateSymlink(t *testing.T) {
  148. if runtime.GOOS == "windows" {
  149. t.Skip("windows not supported")
  150. }
  151. fs, dir := setup(t)
  152. path := filepath.Join(dir, "file")
  153. defer os.RemoveAll(dir)
  154. if err := fs.CreateSymlink("blah", "file"); err != nil {
  155. t.Error(err)
  156. }
  157. if target, err := os.Readlink(path); err != nil || target != "blah" {
  158. t.Error("target", target, "err", err)
  159. }
  160. if err := os.Remove(path); err != nil {
  161. t.Error(err)
  162. }
  163. if err := fs.CreateSymlink(filepath.Join("..", "blah"), "file"); err != nil {
  164. t.Error(err)
  165. }
  166. if target, err := os.Readlink(path); err != nil || target != filepath.Join("..", "blah") {
  167. t.Error("target", target, "err", err)
  168. }
  169. }
  170. func TestDirNames(t *testing.T) {
  171. fs, dir := setup(t)
  172. defer os.RemoveAll(dir)
  173. // Case differences
  174. testCases := []string{
  175. "a",
  176. "bC",
  177. }
  178. sort.Strings(testCases)
  179. for _, sub := range testCases {
  180. if err := os.Mkdir(filepath.Join(dir, sub), 0777); err != nil {
  181. t.Error(err)
  182. }
  183. }
  184. if dirs, err := fs.DirNames("."); err != nil || len(dirs) != len(testCases) {
  185. t.Errorf("%s %s %s", err, dirs, testCases)
  186. } else {
  187. sort.Strings(dirs)
  188. for i := range dirs {
  189. if dirs[i] != testCases[i] {
  190. t.Errorf("%s != %s", dirs[i], testCases[i])
  191. }
  192. }
  193. }
  194. }
  195. func TestNames(t *testing.T) {
  196. // Tests that all names are without the root directory.
  197. fs, dir := setup(t)
  198. defer os.RemoveAll(dir)
  199. expected := "file"
  200. fd, err := fs.Create(expected)
  201. if err != nil {
  202. t.Error(err)
  203. }
  204. defer fd.Close()
  205. if fd.Name() != expected {
  206. t.Errorf("incorrect %s != %s", fd.Name(), expected)
  207. }
  208. if stat, err := fd.Stat(); err != nil || stat.Name() != expected {
  209. t.Errorf("incorrect %s != %s (%v)", stat.Name(), expected, err)
  210. }
  211. if err := fs.Mkdir("dir", 0777); err != nil {
  212. t.Error(err)
  213. }
  214. expected = filepath.Join("dir", "file")
  215. fd, err = fs.Create(expected)
  216. if err != nil {
  217. t.Error(err)
  218. }
  219. defer fd.Close()
  220. if fd.Name() != expected {
  221. t.Errorf("incorrect %s != %s", fd.Name(), expected)
  222. }
  223. // os.fd.Stat() returns just base, so do we.
  224. if stat, err := fd.Stat(); err != nil || stat.Name() != filepath.Base(expected) {
  225. t.Errorf("incorrect %s != %s (%v)", stat.Name(), filepath.Base(expected), err)
  226. }
  227. }
  228. func TestGlob(t *testing.T) {
  229. // Tests that all names are without the root directory.
  230. fs, dir := setup(t)
  231. defer os.RemoveAll(dir)
  232. for _, dirToCreate := range []string{
  233. filepath.Join("a", "test", "b"),
  234. filepath.Join("a", "best", "b"),
  235. filepath.Join("a", "best", "c"),
  236. } {
  237. if err := fs.MkdirAll(dirToCreate, 0777); err != nil {
  238. t.Error(err)
  239. }
  240. }
  241. testCases := []struct {
  242. pattern string
  243. matches []string
  244. }{
  245. {
  246. filepath.Join("a", "?est", "?"),
  247. []string{
  248. filepath.Join("a", "test", "b"),
  249. filepath.Join("a", "best", "b"),
  250. filepath.Join("a", "best", "c"),
  251. },
  252. },
  253. {
  254. filepath.Join("a", "?est", "b"),
  255. []string{
  256. filepath.Join("a", "test", "b"),
  257. filepath.Join("a", "best", "b"),
  258. },
  259. },
  260. {
  261. filepath.Join("a", "best", "?"),
  262. []string{
  263. filepath.Join("a", "best", "b"),
  264. filepath.Join("a", "best", "c"),
  265. },
  266. },
  267. }
  268. for _, testCase := range testCases {
  269. results, err := fs.Glob(testCase.pattern)
  270. sort.Strings(results)
  271. sort.Strings(testCase.matches)
  272. if err != nil {
  273. t.Error(err)
  274. }
  275. if len(results) != len(testCase.matches) {
  276. t.Errorf("result count mismatch")
  277. }
  278. for i := range testCase.matches {
  279. if results[i] != testCase.matches[i] {
  280. t.Errorf("%s != %s", results[i], testCase.matches[i])
  281. }
  282. }
  283. }
  284. }
  285. func TestUsage(t *testing.T) {
  286. fs, dir := setup(t)
  287. defer os.RemoveAll(dir)
  288. usage, err := fs.Usage(".")
  289. if err != nil {
  290. if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" {
  291. t.Skip()
  292. }
  293. t.Errorf("Unexpected error: %s", err)
  294. }
  295. if usage.Free < 1 {
  296. t.Error("Disk is full?", usage.Free)
  297. }
  298. }
  299. func TestRooted(t *testing.T) {
  300. type testcase struct {
  301. root string
  302. rel string
  303. joined string
  304. ok bool
  305. }
  306. cases := []testcase{
  307. // Valid cases
  308. {"foo", "bar", "foo/bar", true},
  309. {"foo", "/bar", "foo/bar", true},
  310. {"foo/", "bar", "foo/bar", true},
  311. {"foo/", "/bar", "foo/bar", true},
  312. {"baz/foo", "bar", "baz/foo/bar", true},
  313. {"baz/foo", "/bar", "baz/foo/bar", true},
  314. {"baz/foo/", "bar", "baz/foo/bar", true},
  315. {"baz/foo/", "/bar", "baz/foo/bar", true},
  316. {"foo", "bar/baz", "foo/bar/baz", true},
  317. {"foo", "/bar/baz", "foo/bar/baz", true},
  318. {"foo/", "bar/baz", "foo/bar/baz", true},
  319. {"foo/", "/bar/baz", "foo/bar/baz", true},
  320. {"baz/foo", "bar/baz", "baz/foo/bar/baz", true},
  321. {"baz/foo", "/bar/baz", "baz/foo/bar/baz", true},
  322. {"baz/foo/", "bar/baz", "baz/foo/bar/baz", true},
  323. {"baz/foo/", "/bar/baz", "baz/foo/bar/baz", true},
  324. // Not escape attempts, but oddly formatted relative paths.
  325. {"foo", "", "foo", true},
  326. {"foo", "/", "foo", true},
  327. {"foo", "/..", "foo", true},
  328. {"foo", "./bar", "foo/bar", true},
  329. {"foo/", "", "foo", true},
  330. {"foo/", "/", "foo", true},
  331. {"foo/", "/..", "foo", true},
  332. {"foo/", "./bar", "foo/bar", true},
  333. {"baz/foo", "./bar", "baz/foo/bar", true},
  334. {"foo", "./bar/baz", "foo/bar/baz", true},
  335. {"baz/foo", "./bar/baz", "baz/foo/bar/baz", true},
  336. {"baz/foo", "bar/../baz", "baz/foo/baz", true},
  337. {"baz/foo", "/bar/../baz", "baz/foo/baz", true},
  338. {"baz/foo", "./bar/../baz", "baz/foo/baz", true},
  339. // Results in an allowed path, but does it by probing. Disallowed.
  340. {"foo", "../foo", "", false},
  341. {"foo", "../foo/bar", "", false},
  342. {"baz/foo", "../foo/bar", "", false},
  343. {"baz/foo", "../../baz/foo/bar", "", false},
  344. {"baz/foo", "bar/../../foo/bar", "", false},
  345. {"baz/foo", "bar/../../../baz/foo/bar", "", false},
  346. // Escape attempts.
  347. {"foo", "..", "", false},
  348. {"foo", "../", "", false},
  349. {"foo", "../bar", "", false},
  350. {"foo", "../foobar", "", false},
  351. {"foo/", "../bar", "", false},
  352. {"foo/", "../foobar", "", false},
  353. {"baz/foo", "../bar", "", false},
  354. {"baz/foo", "../foobar", "", false},
  355. {"baz/foo/", "../bar", "", false},
  356. {"baz/foo/", "../foobar", "", false},
  357. {"baz/foo/", "bar/../../quux/baz", "", false},
  358. // Empty root is a misconfiguration.
  359. {"", "/foo", "", false},
  360. {"", "foo", "", false},
  361. {"", ".", "", false},
  362. {"", "..", "", false},
  363. {"", "/", "", false},
  364. {"", "", "", false},
  365. // Root=/ is valid, and things should be verified as usual.
  366. {"/", "foo", "/foo", true},
  367. {"/", "/foo", "/foo", true},
  368. {"/", "../foo", "", false},
  369. {"/", "..", "", false},
  370. {"/", "/", "/", true},
  371. {"/", "", "/", true},
  372. // special case for filesystems to be able to MkdirAll('.') for example
  373. {"/", ".", "/", true},
  374. }
  375. if runtime.GOOS == "windows" {
  376. extraCases := []testcase{
  377. {`c:\`, `foo`, `c:\foo`, true},
  378. {`\\?\c:\`, `foo`, `\\?\c:\foo`, true},
  379. {`c:\`, `\foo`, `c:\foo`, true},
  380. {`\\?\c:\`, `\foo`, `\\?\c:\foo`, true},
  381. {`c:\`, `\\foo`, ``, false},
  382. {`c:\`, ``, `c:\`, true},
  383. {`c:\`, `\`, `c:\`, true},
  384. {`\\?\c:\`, `\\foo`, ``, false},
  385. {`\\?\c:\`, ``, `\\?\c:\`, true},
  386. {`\\?\c:\`, `\`, `\\?\c:\`, true},
  387. {`\\?\c:\test`, `.`, `\\?\c:\test`, true},
  388. {`c:\test`, `.`, `c:\test`, true},
  389. {`\\?\c:\test`, `/`, `\\?\c:\test`, true},
  390. {`c:\test`, ``, `c:\test`, true},
  391. // makes no sense, but will be treated simply as a bad filename
  392. {`c:\foo`, `d:\bar`, `c:\foo\d:\bar`, true},
  393. // special case for filesystems to be able to MkdirAll('.') for example
  394. {`c:\`, `.`, `c:\`, true},
  395. {`\\?\c:\`, `.`, `\\?\c:\`, true},
  396. }
  397. for _, tc := range cases {
  398. // Add case where root is backslashed, rel is forward slashed
  399. extraCases = append(extraCases, testcase{
  400. root: filepath.FromSlash(tc.root),
  401. rel: tc.rel,
  402. joined: tc.joined,
  403. ok: tc.ok,
  404. })
  405. // and the opposite
  406. extraCases = append(extraCases, testcase{
  407. root: tc.root,
  408. rel: filepath.FromSlash(tc.rel),
  409. joined: tc.joined,
  410. ok: tc.ok,
  411. })
  412. // and both backslashed
  413. extraCases = append(extraCases, testcase{
  414. root: filepath.FromSlash(tc.root),
  415. rel: filepath.FromSlash(tc.rel),
  416. joined: tc.joined,
  417. ok: tc.ok,
  418. })
  419. }
  420. cases = append(cases, extraCases...)
  421. }
  422. for _, tc := range cases {
  423. fs := BasicFilesystem{root: tc.root}
  424. res, err := fs.rooted(tc.rel)
  425. if tc.ok {
  426. if err != nil {
  427. t.Errorf("Unexpected error for rooted(%q, %q): %v", tc.root, tc.rel, err)
  428. continue
  429. }
  430. exp := filepath.FromSlash(tc.joined)
  431. if res != exp {
  432. t.Errorf("Unexpected result for rooted(%q, %q): %q != expected %q", tc.root, tc.rel, res, exp)
  433. }
  434. } else if err == nil {
  435. t.Errorf("Unexpected pass for rooted(%q, %q) => %q", tc.root, tc.rel, res)
  436. continue
  437. }
  438. }
  439. }
  440. func TestNewBasicFilesystem(t *testing.T) {
  441. if runtime.GOOS == "windows" {
  442. t.Skip("non-windows root paths")
  443. }
  444. testCases := []struct {
  445. input string
  446. expectedRoot string
  447. expectedURI string
  448. }{
  449. {"/foo/bar/baz", "/foo/bar/baz", "/foo/bar/baz"},
  450. {"/foo/bar/baz/", "/foo/bar/baz", "/foo/bar/baz"},
  451. {"", "/", "/"},
  452. {"/", "/", "/"},
  453. }
  454. for _, testCase := range testCases {
  455. fs := newBasicFilesystem(testCase.input)
  456. if fs.root != testCase.expectedRoot {
  457. t.Errorf("root %q != %q", fs.root, testCase.expectedRoot)
  458. }
  459. if fs.URI() != testCase.expectedURI {
  460. t.Errorf("uri %q != %q", fs.URI(), testCase.expectedURI)
  461. }
  462. }
  463. fs := newBasicFilesystem("relative/path")
  464. if fs.root == "relative/path" || !strings.HasPrefix(fs.root, string(PathSeparator)) {
  465. t.Errorf(`newBasicFilesystem("relative/path").root == %q, expected absolutification`, fs.root)
  466. }
  467. }
  468. func TestRel(t *testing.T) {
  469. testCases := []struct {
  470. root string
  471. abs string
  472. expectedRel string
  473. }{
  474. {"/", "/", ""},
  475. {"/", "/test", "test"},
  476. {"/", "/Test", "Test"},
  477. {"/Test", "/Test/test", "test"},
  478. }
  479. if runtime.GOOS == "windows" {
  480. for i := range testCases {
  481. testCases[i].root = filepath.FromSlash(testCases[i].root)
  482. testCases[i].abs = filepath.FromSlash(testCases[i].abs)
  483. testCases[i].expectedRel = filepath.FromSlash(testCases[i].expectedRel)
  484. }
  485. }
  486. for _, tc := range testCases {
  487. if res := rel(tc.abs, tc.root); res != tc.expectedRel {
  488. t.Errorf(`rel("%v", "%v") == "%v", expected "%v"`, tc.abs, tc.root, res, tc.expectedRel)
  489. }
  490. }
  491. }