term_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "bytes"
  13. "crypto/tls"
  14. "flag"
  15. "fmt"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "testing"
  22. "time"
  23. "devt.de/krotik/common/cryptutil"
  24. "devt.de/krotik/common/errorutil"
  25. "devt.de/krotik/common/fileutil"
  26. "devt.de/krotik/rufs"
  27. "devt.de/krotik/rufs/config"
  28. )
  29. func TestStatusUpdate(t *testing.T) {
  30. var buf bytes.Buffer
  31. // Build up a tree from one branch
  32. cfg := map[string]interface{}{
  33. config.TreeSecret: "123",
  34. }
  35. tree, _ := rufs.NewTree(cfg, clientCert)
  36. term := NewTreeTerm(tree, &buf)
  37. term.WriteStatus("Test")
  38. term.WriteStatus("foo")
  39. term.ClearStatus()
  40. // Check that we only clear extra characters when overwriting the status
  41. if buf.String() != "\rTest\rfoo \r \r" {
  42. t.Errorf("Unexpected result: %#v", buf.String())
  43. return
  44. }
  45. }
  46. func TestSimpleTreeExploring(t *testing.T) {
  47. // Build up a tree from one branch
  48. cfg := map[string]interface{}{
  49. config.TreeSecret: "123",
  50. }
  51. tree, _ := rufs.NewTree(cfg, clientCert)
  52. fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  53. fooFP := footest.SSLFingerprint()
  54. term := NewTreeTerm(tree, nil)
  55. term.AddCmd("unittest", "unittest [bla]", "Unit test command", func(*TreeTerm, ...string) (string, error) {
  56. return "123", nil
  57. })
  58. if res, err := term.Run("?"); err != nil || res != `
  59. Available commands:
  60. ----
  61. branch [branch name] [rpc] [fingerprint] : List all known branches or add a new branch to the tree
  62. cat <file> : Read and print the contents of a file
  63. cd [path] : Show or change the current directory
  64. checksum [path] [glob] : Show a directory listing and file checksums
  65. cp <src file/dir> <dst dir> : Copy a file or directory
  66. dir [path] [glob] : Show a directory listing
  67. get <src file> [dst local file] : Retrieve a file and store it locally (in the current directory)
  68. help [cmd] : Show general or command specific help
  69. mkdir <dir> : Create a new directory
  70. mount [path] [branch name] [ro] : List all mount points or add a new mount point to the tree
  71. ping <branch name> [rpc] : Ping a remote branch
  72. put [src local file] [dst file] : Read a local file and store it
  73. refresh : Refreshes all known branches and reconnect if possible
  74. ren <file> <newfile> : Rename a file or directory
  75. reset [mounts|brances] : Remove all mounts or all mounts and all branches
  76. rm <file> : Delete a file or directory (* all files; ** all files/recursive)
  77. sync <src dir> <dst dir> : Make sure dst has the same files and directories as src
  78. tree [path] [glob] : Show the listing of a directory and its subdirectories
  79. unittest [bla] : Unit test command
  80. `[1:] {
  81. t.Error("Unexpected result: ", res, err)
  82. return
  83. }
  84. if res := term.Cmds(); fmt.Sprint(res) != "[? branch cat cd checksum cp dir "+
  85. "get help ll mkdir mount ping put refresh ren reset rm sync tree unittest]" {
  86. t.Error("Unexpected result:", res)
  87. return
  88. }
  89. if res, err := term.Run("ll"); err != nil || res != `
  90. /
  91. `[1:] {
  92. t.Error("Unexpected result: ", res, err)
  93. return
  94. }
  95. if res, err := term.Run("mount"); err != nil || res != `
  96. /:
  97. `[1:] {
  98. t.Error("Unexpected result: ", res, err)
  99. return
  100. }
  101. if res, err := term.Run("branch"); err != nil || res != `
  102. `[1:] {
  103. t.Error("Unexpected result: ", res, err)
  104. return
  105. }
  106. if res, err := term.Run("refresh"); err != nil || res != `
  107. Done`[1:] {
  108. t.Error("Unexpected result: ", res, err)
  109. return
  110. }
  111. if _, err := term.Run(fmt.Sprintf("branch myfoo %v %v", fooRPC, fooFP)); err == nil ||
  112. err.Error() != "RufsError: Remote error (Unknown target node)" {
  113. t.Error("Unexpected result: ", err)
  114. return
  115. }
  116. if res, err := term.Run(fmt.Sprintf("ping footest %v %v", fooRPC, "")); err != nil || res != `
  117. Response ok - fingerprint: `[1:]+fooFP+`
  118. ` {
  119. t.Error("Unexpected result: ", res, err)
  120. return
  121. }
  122. if res, err := term.Run(fmt.Sprintf("branch footest %v %v", fooRPC, "")); err != nil || res != `
  123. footest [`[1:]+fooFP+`]
  124. ` {
  125. t.Error("Unexpected result: ", res, err)
  126. return
  127. }
  128. if res, err := term.Run("branch"); err != nil || res != `
  129. footest [`[1:]+fooFP+`]
  130. ` {
  131. t.Error("Unexpected result: ", res, err)
  132. return
  133. }
  134. if _, err := term.Run("mount / myfoo"); err == nil ||
  135. err.Error() != "Unknown target node" {
  136. t.Error("Unexpected result: ", err)
  137. return
  138. }
  139. // Mount the first directory
  140. if _, err := term.Run("mount / footest"); err != nil {
  141. t.Error("Unexpected result: ", err)
  142. return
  143. }
  144. // The directory listing should now return something
  145. if res, err := term.Run("ll"); err != nil || (res != `
  146. /
  147. drwxrwx--- 4.0 KiB sub1
  148. -rwxrwx--- 10 B test1
  149. -rwxrwx--- 10 B test2
  150. `[1:] && res != `
  151. /
  152. drwxr-x--- 4.0 KiB sub1
  153. -rwxr-x--- 10 B test1
  154. -rwxr-x--- 10 B test2
  155. `[1:] && res != `
  156. /
  157. drwxrwxrwx 0 B sub1
  158. -rw-rw-rw- 10 B test1
  159. -rw-rw-rw- 10 B test2
  160. `[1:]) {
  161. t.Error("Unexpected result: ", res, err)
  162. return
  163. }
  164. if res, err := term.Run("dir sub1"); err != nil || (res != `
  165. /sub1
  166. -rwxrwx--- 17 B test3
  167. `[1:] && res != `
  168. /sub1
  169. -rwxr-x--- 17 B test3
  170. `[1:] && res != `
  171. /sub1
  172. -rw-rw-rw- 17 B test3
  173. `[1:]) {
  174. t.Error("Unexpected result: ", res, err)
  175. return
  176. }
  177. if res, err := term.Run("dir . {"); err == nil || err.Error() != "Unclosed group at 1 of {" {
  178. t.Error("Unexpected result: ", res, err)
  179. return
  180. }
  181. if res, err := term.Run("checksum"); err != nil || (res != `
  182. /
  183. drwxrwx--- 4.0 KiB sub1
  184. -rwxrwx--- 10 B test1 [73b8af47]
  185. -rwxrwx--- 10 B test2 [b0c1fadd]
  186. `[1:] && res != `
  187. /
  188. drwxr-x--- 4.0 KiB sub1
  189. -rwxr-x--- 10 B test1 [73b8af47]
  190. -rwxr-x--- 10 B test2 [b0c1fadd]
  191. `[1:] && res != `
  192. /
  193. drwxrwxrwx 0 B sub1
  194. -rw-rw-rw- 10 B test1 [73b8af47]
  195. -rw-rw-rw- 10 B test2 [b0c1fadd]
  196. `[1:]) {
  197. t.Error("Unexpected result: ", res, err)
  198. return
  199. }
  200. if res, err := term.Run("tree"); err != nil || (res != `
  201. /
  202. drwxrwx--- 4.0 KiB sub1
  203. -rwxrwx--- 10 B test1
  204. -rwxrwx--- 10 B test2
  205. /sub1
  206. -rwxrwx--- 17 B test3
  207. `[1:] && res != `
  208. /
  209. drwxr-x--- 4.0 KiB sub1
  210. -rwxr-x--- 10 B test1
  211. -rwxr-x--- 10 B test2
  212. /sub1
  213. -rwxr-x--- 17 B test3
  214. `[1:] && res != `
  215. /
  216. drwxrwxrwx 0 B sub1
  217. -rw-rw-rw- 10 B test1
  218. -rw-rw-rw- 10 B test2
  219. /sub1
  220. -rw-rw-rw- 17 B test3
  221. `[1:]) {
  222. t.Error("Unexpected result: ", res, err)
  223. return
  224. }
  225. if res := term.CurrentDir(); res != "/" {
  226. t.Error("Unexpected result: ", res)
  227. return
  228. }
  229. if res, err := term.Run("cd sub1"); err != nil || res != `
  230. /sub1
  231. `[1:] {
  232. t.Error("Unexpected result: ", res, err)
  233. return
  234. }
  235. if res := term.CurrentDir(); res != "/sub1" {
  236. t.Error("Unexpected result: ", res)
  237. return
  238. }
  239. if res, err := term.Run("checksum"); err != nil || (res != `
  240. /sub1
  241. -rwxrwx--- 17 B test3 [f89782b1]
  242. `[1:] && res != `
  243. /sub1
  244. -rwxr-x--- 17 B test3 [f89782b1]
  245. `[1:] && res != `
  246. /sub1
  247. -rw-rw-rw- 17 B test3 [f89782b1]
  248. `[1:]) {
  249. t.Error("Unexpected result: ", res, err)
  250. return
  251. }
  252. if _, err := term.Run("reset"); err == nil ||
  253. err.Error() != "Can either reset all [mounts] or all [branches] which includes all mount points" {
  254. t.Error("Unexpected result: ", err)
  255. return
  256. }
  257. if res, err := term.Run("reset mounts"); err != nil || res != `
  258. Resetting all mounts
  259. `[1:] {
  260. t.Error("Unexpected result: ", res, err)
  261. return
  262. }
  263. if res, err := term.Run("mount"); err != nil || res != `
  264. /:
  265. `[1:] {
  266. t.Error("Unexpected result: ", res, err)
  267. return
  268. }
  269. if res, err := term.Run("branch"); err != nil || res != `
  270. footest [`[1:]+fooFP+`]
  271. ` {
  272. t.Error("Unexpected result: ", res, err)
  273. return
  274. }
  275. if res, err := term.Run("reset branches"); err != nil || res != `
  276. Resetting all branches and mounts
  277. `[1:] {
  278. t.Error("Unexpected result: ", res, err)
  279. return
  280. }
  281. if res, err := term.Run("branch"); err != nil || res != `
  282. `[1:] {
  283. t.Error("Unexpected result: ", res, err)
  284. return
  285. }
  286. // Error returns
  287. if _, err := term.Run("mount x"); err == nil || err.Error() != "mount requires either 2 or no parameters" {
  288. t.Error("Unexpected result: ", err)
  289. return
  290. }
  291. if _, err := term.Run("branch x"); err == nil || err.Error() != "branch requires either no or at least 2 parameters" {
  292. t.Error("Unexpected result: ", err)
  293. return
  294. }
  295. if _, err := term.Run("bla"); err == nil || err.Error() != "Unknown command: bla" {
  296. t.Error("Unexpected result: ", err)
  297. return
  298. }
  299. }
  300. const certdir = "certs" // Directory for certificates
  301. var portCount = 0 // Port assignment counter for Branch ports
  302. var footest, bartest, tmptest *rufs.Branch // Branches
  303. var branchConfigs = map[string]map[string]interface{}{} // All branch configs
  304. var clientCert *tls.Certificate
  305. func TestMain(m *testing.M) {
  306. flag.Parse()
  307. // Create a ssl certificate directory
  308. if res, _ := fileutil.PathExists(certdir); res {
  309. os.RemoveAll(certdir)
  310. }
  311. err := os.Mkdir(certdir, 0770)
  312. if err != nil {
  313. fmt.Print("Could not create test directory:", err.Error())
  314. os.Exit(1)
  315. }
  316. // Create client certificate
  317. certFile := fmt.Sprintf("cert-client.pem")
  318. keyFile := fmt.Sprintf("key-client.pem")
  319. host := "localhost"
  320. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  321. if err != nil {
  322. panic(err)
  323. }
  324. cert, err := tls.LoadX509KeyPair(path.Join(certdir, certFile), path.Join(certdir, keyFile))
  325. if err != nil {
  326. panic(err)
  327. }
  328. clientCert = &cert
  329. // Ensure logging is discarded
  330. log.SetOutput(ioutil.Discard)
  331. // Set up test branches
  332. b1, err := createBranch("footest", "foo")
  333. errorutil.AssertOk(err)
  334. b2, err := createBranch("bartest", "bar")
  335. errorutil.AssertOk(err)
  336. b3, err := createBranch("tmptest", "tmp")
  337. errorutil.AssertOk(err)
  338. footest = b1
  339. bartest = b2
  340. tmptest = b3
  341. // Create some test files
  342. ioutil.WriteFile("foo/test1", []byte("Test1 file"), 0770)
  343. ioutil.WriteFile("foo/test2", []byte("Test2 file"), 0770)
  344. os.Mkdir("foo/sub1", 0770)
  345. ioutil.WriteFile("foo/sub1/test3", []byte("Sub dir test file"), 0770)
  346. ioutil.WriteFile("bar/test1", []byte("Test3 file"), 0770)
  347. // Run the tests
  348. res := m.Run()
  349. // Shutdown the branches
  350. errorutil.AssertOk(b1.Shutdown())
  351. errorutil.AssertOk(b2.Shutdown())
  352. errorutil.AssertOk(b3.Shutdown())
  353. // Remove all directories again
  354. if err = os.RemoveAll(certdir); err != nil {
  355. fmt.Print("Could not remove test directory:", err.Error())
  356. }
  357. if err = os.RemoveAll("foo"); err != nil {
  358. fmt.Print("Could not remove test directory:", err.Error())
  359. }
  360. if err = os.RemoveAll("bar"); err != nil {
  361. fmt.Print("Could not remove test directory:", err.Error())
  362. }
  363. if err = os.RemoveAll("tmp"); err != nil {
  364. fmt.Print("Could not remove test directory:", err.Error())
  365. }
  366. os.Exit(res)
  367. }
  368. /*
  369. createBranch creates a new branch.
  370. */
  371. func createBranch(name, dir string) (*rufs.Branch, error) {
  372. // Create the path directory
  373. if res, _ := fileutil.PathExists(dir); res {
  374. os.RemoveAll(dir)
  375. }
  376. err := os.Mkdir(dir, 0770)
  377. if err != nil {
  378. fmt.Print("Could not create test directory:", err.Error())
  379. os.Exit(1)
  380. }
  381. // Create the certificate
  382. portCount++
  383. host := fmt.Sprintf("localhost:%v", 9020+portCount)
  384. // Generate a certificate and private key
  385. certFile := fmt.Sprintf("cert-%v.pem", portCount)
  386. keyFile := fmt.Sprintf("key-%v.pem", portCount)
  387. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  388. if err != nil {
  389. panic(err)
  390. }
  391. cert, err := tls.LoadX509KeyPair(filepath.Join(certdir, certFile), filepath.Join(certdir, keyFile))
  392. if err != nil {
  393. panic(err)
  394. }
  395. // Create the Branch
  396. config := map[string]interface{}{
  397. config.BranchName: name,
  398. config.BranchSecret: "123",
  399. config.EnableReadOnly: false,
  400. config.RPCHost: "localhost",
  401. config.RPCPort: fmt.Sprint(9020 + portCount),
  402. config.LocalFolder: dir,
  403. }
  404. branchConfigs[name] = config
  405. return rufs.NewBranch(config, &cert)
  406. }