term_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 direectory
  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. drwxrwxrwx 0 B sub1
  153. -rw-rw-rw- 10 B test1
  154. -rw-rw-rw- 10 B test2
  155. `[1:]) {
  156. t.Error("Unexpected result: ", res, err)
  157. return
  158. }
  159. if res, err := term.Run("dir sub1"); err != nil || (res != `
  160. /sub1
  161. -rwxrwx--- 17 B test3
  162. `[1:] && res != `
  163. /sub1
  164. -rw-rw-rw- 17 B test3
  165. `[1:]) {
  166. t.Error("Unexpected result: ", res, err)
  167. return
  168. }
  169. if res, err := term.Run("dir . {"); err == nil || err.Error() != "Unclosed group at 1 of {" {
  170. t.Error("Unexpected result: ", res, err)
  171. return
  172. }
  173. if res, err := term.Run("checksum"); err != nil || (res != `
  174. /
  175. drwxrwx--- 4.0 KiB sub1
  176. -rwxrwx--- 10 B test1 [73b8af47]
  177. -rwxrwx--- 10 B test2 [b0c1fadd]
  178. `[1:] && res != `
  179. /
  180. drwxrwxrwx 0 B sub1
  181. -rw-rw-rw- 10 B test1 [73b8af47]
  182. -rw-rw-rw- 10 B test2 [b0c1fadd]
  183. `[1:]) {
  184. t.Error("Unexpected result: ", res, err)
  185. return
  186. }
  187. if res, err := term.Run("tree"); err != nil || (res != `
  188. /
  189. drwxrwx--- 4.0 KiB sub1
  190. -rwxrwx--- 10 B test1
  191. -rwxrwx--- 10 B test2
  192. /sub1
  193. -rwxrwx--- 17 B test3
  194. `[1:] && res != `
  195. /
  196. drwxrwxrwx 0 B sub1
  197. -rw-rw-rw- 10 B test1
  198. -rw-rw-rw- 10 B test2
  199. /sub1
  200. -rw-rw-rw- 17 B test3
  201. `[1:]) {
  202. t.Error("Unexpected result: ", res, err)
  203. return
  204. }
  205. if res := term.CurrentDir(); res != "/" {
  206. t.Error("Unexpected result: ", res)
  207. return
  208. }
  209. if res, err := term.Run("cd sub1"); err != nil || res != `
  210. /sub1
  211. `[1:] {
  212. t.Error("Unexpected result: ", res, err)
  213. return
  214. }
  215. if res := term.CurrentDir(); res != "/sub1" {
  216. t.Error("Unexpected result: ", res)
  217. return
  218. }
  219. if res, err := term.Run("checksum"); err != nil || (res != `
  220. /sub1
  221. -rwxrwx--- 17 B test3 [f89782b1]
  222. `[1:] && res != `
  223. /sub1
  224. -rw-rw-rw- 17 B test3 [f89782b1]
  225. `[1:]) {
  226. t.Error("Unexpected result: ", res, err)
  227. return
  228. }
  229. if _, err := term.Run("reset"); err == nil ||
  230. err.Error() != "Can either reset all [mounts] or all [branches] which includes all mount points" {
  231. t.Error("Unexpected result: ", err)
  232. return
  233. }
  234. if res, err := term.Run("reset mounts"); err != nil || res != `
  235. Resetting all mounts
  236. `[1:] {
  237. t.Error("Unexpected result: ", res, err)
  238. return
  239. }
  240. if res, err := term.Run("mount"); err != nil || res != `
  241. /:
  242. `[1:] {
  243. t.Error("Unexpected result: ", res, err)
  244. return
  245. }
  246. if res, err := term.Run("branch"); err != nil || res != `
  247. footest [`[1:]+fooFP+`]
  248. ` {
  249. t.Error("Unexpected result: ", res, err)
  250. return
  251. }
  252. if res, err := term.Run("reset branches"); err != nil || res != `
  253. Resetting all branches and mounts
  254. `[1:] {
  255. t.Error("Unexpected result: ", res, err)
  256. return
  257. }
  258. if res, err := term.Run("branch"); err != nil || res != `
  259. `[1:] {
  260. t.Error("Unexpected result: ", res, err)
  261. return
  262. }
  263. // Error returns
  264. if _, err := term.Run("mount x"); err == nil || err.Error() != "mount requires either 2 or no parameters" {
  265. t.Error("Unexpected result: ", err)
  266. return
  267. }
  268. if _, err := term.Run("branch x"); err == nil || err.Error() != "branch requires either no or at least 2 parameters" {
  269. t.Error("Unexpected result: ", err)
  270. return
  271. }
  272. if _, err := term.Run("bla"); err == nil || err.Error() != "Unknown command: bla" {
  273. t.Error("Unexpected result: ", err)
  274. return
  275. }
  276. }
  277. const certdir = "certs" // Directory for certificates
  278. var portCount = 0 // Port assignment counter for Branch ports
  279. var footest, bartest, tmptest *rufs.Branch // Branches
  280. var branchConfigs = map[string]map[string]interface{}{} // All branch configs
  281. var clientCert *tls.Certificate
  282. func TestMain(m *testing.M) {
  283. flag.Parse()
  284. // Create a ssl certificate directory
  285. if res, _ := fileutil.PathExists(certdir); res {
  286. os.RemoveAll(certdir)
  287. }
  288. err := os.Mkdir(certdir, 0770)
  289. if err != nil {
  290. fmt.Print("Could not create test directory:", err.Error())
  291. os.Exit(1)
  292. }
  293. // Create client certificate
  294. certFile := fmt.Sprintf("cert-client.pem")
  295. keyFile := fmt.Sprintf("key-client.pem")
  296. host := "localhost"
  297. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  298. if err != nil {
  299. panic(err)
  300. }
  301. cert, err := tls.LoadX509KeyPair(path.Join(certdir, certFile), path.Join(certdir, keyFile))
  302. if err != nil {
  303. panic(err)
  304. }
  305. clientCert = &cert
  306. // Ensure logging is discarded
  307. log.SetOutput(ioutil.Discard)
  308. // Set up test branches
  309. b1, err := createBranch("footest", "foo")
  310. errorutil.AssertOk(err)
  311. b2, err := createBranch("bartest", "bar")
  312. errorutil.AssertOk(err)
  313. b3, err := createBranch("tmptest", "tmp")
  314. errorutil.AssertOk(err)
  315. footest = b1
  316. bartest = b2
  317. tmptest = b3
  318. // Create some test files
  319. ioutil.WriteFile("foo/test1", []byte("Test1 file"), 0770)
  320. ioutil.WriteFile("foo/test2", []byte("Test2 file"), 0770)
  321. os.Mkdir("foo/sub1", 0770)
  322. ioutil.WriteFile("foo/sub1/test3", []byte("Sub dir test file"), 0770)
  323. ioutil.WriteFile("bar/test1", []byte("Test3 file"), 0770)
  324. // Run the tests
  325. res := m.Run()
  326. // Shutdown the branches
  327. errorutil.AssertOk(b1.Shutdown())
  328. errorutil.AssertOk(b2.Shutdown())
  329. errorutil.AssertOk(b3.Shutdown())
  330. // Remove all directories again
  331. if err = os.RemoveAll(certdir); err != nil {
  332. fmt.Print("Could not remove test directory:", err.Error())
  333. }
  334. if err = os.RemoveAll("foo"); err != nil {
  335. fmt.Print("Could not remove test directory:", err.Error())
  336. }
  337. if err = os.RemoveAll("bar"); err != nil {
  338. fmt.Print("Could not remove test directory:", err.Error())
  339. }
  340. if err = os.RemoveAll("tmp"); err != nil {
  341. fmt.Print("Could not remove test directory:", err.Error())
  342. }
  343. os.Exit(res)
  344. }
  345. /*
  346. createBranch creates a new branch.
  347. */
  348. func createBranch(name, dir string) (*rufs.Branch, error) {
  349. // Create the path directory
  350. if res, _ := fileutil.PathExists(dir); res {
  351. os.RemoveAll(dir)
  352. }
  353. err := os.Mkdir(dir, 0770)
  354. if err != nil {
  355. fmt.Print("Could not create test directory:", err.Error())
  356. os.Exit(1)
  357. }
  358. // Create the certificate
  359. portCount++
  360. host := fmt.Sprintf("localhost:%v", 9020+portCount)
  361. // Generate a certificate and private key
  362. certFile := fmt.Sprintf("cert-%v.pem", portCount)
  363. keyFile := fmt.Sprintf("key-%v.pem", portCount)
  364. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  365. if err != nil {
  366. panic(err)
  367. }
  368. cert, err := tls.LoadX509KeyPair(filepath.Join(certdir, certFile), filepath.Join(certdir, keyFile))
  369. if err != nil {
  370. panic(err)
  371. }
  372. // Create the Branch
  373. config := map[string]interface{}{
  374. config.BranchName: name,
  375. config.BranchSecret: "123",
  376. config.EnableReadOnly: false,
  377. config.RPCHost: "localhost",
  378. config.RPCPort: fmt.Sprint(9020 + portCount),
  379. config.LocalFolder: dir,
  380. }
  381. branchConfigs[name] = config
  382. return rufs.NewBranch(config, &cert)
  383. }