basicfs_test.go 12 KB

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