basicfs_test.go 13 KB

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