basicfs_test.go 12 KB

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