branch_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 rufs
  11. import (
  12. "bytes"
  13. "crypto/tls"
  14. "flag"
  15. "fmt"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strings"
  22. "testing"
  23. "time"
  24. "devt.de/krotik/common/cryptutil"
  25. "devt.de/krotik/common/errorutil"
  26. "devt.de/krotik/common/fileutil"
  27. "devt.de/krotik/rufs/config"
  28. )
  29. const certdir = "certs" // Directory for certificates
  30. var portCount = 0 // Port assignment counter for Branch ports
  31. var footest, bartest *Branch // Branches
  32. var branchConfigs = map[string]map[string]interface{}{} // All branch configs
  33. var clientCert *tls.Certificate
  34. func TestReadOnlyBranch(t *testing.T) {
  35. x, err := createBranch("footest3", "foo3", true)
  36. if err != nil {
  37. t.Error(err)
  38. return
  39. }
  40. defer os.RemoveAll("foo3")
  41. if err := x.WriteFileFromBuffer("", nil); err == nil || err.Error() != "Branch footest3 is read-only" {
  42. t.Error("Unepxected result:", err)
  43. return
  44. }
  45. if _, err := x.WriteFile("", nil, 0); err == nil || err.Error() != "Branch footest3 is read-only" {
  46. t.Error("Unepxected result:", err)
  47. return
  48. }
  49. if _, err := x.ItemOp("", nil); err == nil || err.Error() != "Branch footest3 is read-only" {
  50. t.Error("Unepxected result:", err)
  51. return
  52. }
  53. if res, _, err := x.Dir("/", "(", false, false); err == nil || err.Error() != "error parsing regexp: missing closing ): `(`" {
  54. t.Error("Unepxected result:", res, err)
  55. return
  56. }
  57. }
  58. func TestTreeTraversal(t *testing.T) {
  59. // Test create and shutdown
  60. x, err := createBranch("footest2", "foo2", false)
  61. if err != nil {
  62. t.Error(err)
  63. return
  64. }
  65. if footest.SSLFingerprint() == "" {
  66. t.Error("Branch should have a SSL fingerprint")
  67. return
  68. }
  69. if res := footest.Name(); res != "footest" {
  70. t.Error("Unexpected result:", res)
  71. return
  72. }
  73. x.Shutdown()
  74. os.RemoveAll("foo2")
  75. // Build up a tree from one branch
  76. cfg := map[string]interface{}{
  77. config.TreeSecret: "123",
  78. config.EnableReadOnly: false,
  79. }
  80. tree, _ := NewTree(cfg, clientCert)
  81. branchRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  82. if err := tree.AddBranch("footest", branchRPC, ""); err != nil {
  83. t.Error(err)
  84. return
  85. }
  86. if err := tree.AddMapping("/1", "footest", false); err != nil {
  87. t.Error(err)
  88. return
  89. }
  90. if err := tree.AddMapping("/1/sub1", "footest", false); err != nil {
  91. t.Error(err)
  92. return
  93. }
  94. if err := tree.AddMapping("/2/3/4", "footest", false); err != nil {
  95. t.Error(err)
  96. return
  97. }
  98. if err := tree.AddMapping("/2/3", "footest", false); err != nil {
  99. t.Error(err)
  100. return
  101. }
  102. // We should now have the following structure:
  103. //
  104. // /1/test1
  105. // /1/test2
  106. // /1/sub1/test3
  107. // /1/sub1/test1
  108. // /1/sub1/test2
  109. // /1/sub1/sub1/test3
  110. // /2/3/test1
  111. // /2/3/test2
  112. // /2/3/sub1/test3
  113. // /2/3/4/test1
  114. // /2/3/4/test2
  115. // /2/3/4/sub1/test3
  116. if res := fmt.Sprint(tree); res != `
  117. /:
  118. 1/: footest(r)
  119. sub1/: footest(r)
  120. 2/:
  121. 3/: footest(r)
  122. 4/: footest(r)
  123. `[1:] {
  124. t.Error("Unexpected result:", res)
  125. return
  126. }
  127. // Test tree traversal (non recursive)
  128. var res string
  129. treeVisitor := func(item *treeItem, treePath string, branchPath []string, branches []string, writable []bool) {
  130. // treePath is used for the result (to present to the user)
  131. // branchPath is send to the branch
  132. // branches are the branches on the current level
  133. res += fmt.Sprintf("(%v) /%v: %v\n", treePath, strings.Join(branchPath, "/"), branches)
  134. }
  135. res = ""
  136. tree.root.findPathBranches("/", createMappingPath("/"), false, treeVisitor)
  137. if res != `(/) /: []
  138. ` {
  139. t.Error("Unexpected result:", res)
  140. return
  141. }
  142. res = ""
  143. tree.root.findPathBranches("/", createMappingPath("/1"), false, treeVisitor)
  144. if res != `(/) /1: []
  145. (/1) /: [footest]
  146. ` {
  147. t.Error("Unexpected result:", res)
  148. return
  149. }
  150. res = ""
  151. tree.root.findPathBranches("/", createMappingPath("/1/sub1"), false, treeVisitor)
  152. if res != `(/) /1/sub1: []
  153. (/1) /sub1: [footest]
  154. (/1/sub1) /: [footest]
  155. ` {
  156. t.Error("Unexpected result:", res)
  157. return
  158. }
  159. res = ""
  160. tree.root.findPathBranches("/", createMappingPath("/2/"), false, treeVisitor)
  161. if res != `(/) /2: []
  162. (/2) /: []
  163. ` {
  164. t.Error("Unexpected result:", res)
  165. return
  166. }
  167. res = ""
  168. tree.root.findPathBranches("/", createMappingPath("/2/3/4/5/6"), false, treeVisitor)
  169. if res != `(/) /2/3/4/5/6: []
  170. (/2) /3/4/5/6: []
  171. (/2/3) /4/5/6: [footest]
  172. (/2/3/4) /5/6: [footest]
  173. ` {
  174. t.Error("Unexpected result:", res)
  175. return
  176. }
  177. // Test tree traversal (recursive)
  178. res = ""
  179. tree.root.findPathBranches("/", createMappingPath("/1"), true, treeVisitor)
  180. if res != `(/) /1: []
  181. (/1) /: [footest]
  182. (/1/sub1) /: [footest]
  183. ` {
  184. t.Error("Unexpected result:", res)
  185. return
  186. }
  187. }
  188. func TestMain(m *testing.M) {
  189. flag.Parse()
  190. unitTestModes = true // Unit tests will have standard file modes
  191. defer func() {
  192. unitTestModes = false
  193. }()
  194. // Create a ssl certificate directory
  195. if res, _ := fileutil.PathExists(certdir); res {
  196. os.RemoveAll(certdir)
  197. }
  198. err := os.Mkdir(certdir, 0770)
  199. if err != nil {
  200. fmt.Print("Could not create test directory:", err.Error())
  201. os.Exit(1)
  202. }
  203. // Create client certificate
  204. certFile := fmt.Sprintf("cert-client.pem")
  205. keyFile := fmt.Sprintf("key-client.pem")
  206. host := "localhost"
  207. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  208. if err != nil {
  209. panic(err)
  210. }
  211. cert, err := tls.LoadX509KeyPair(path.Join(certdir, certFile), path.Join(certdir, keyFile))
  212. if err != nil {
  213. panic(err)
  214. }
  215. clientCert = &cert
  216. // Ensure logging is discarded
  217. log.SetOutput(ioutil.Discard)
  218. // Set up test branches
  219. b1, err := createBranch("footest", "foo", false)
  220. errorutil.AssertOk(err)
  221. b2, err := createBranch("bartest", "bar", false)
  222. errorutil.AssertOk(err)
  223. footest = b1
  224. bartest = b2
  225. // Create some test files
  226. ioutil.WriteFile("foo/test1", []byte("Test1 file"), 0770)
  227. ioutil.WriteFile("foo/test2", []byte("Test2 file"), 0770)
  228. os.Mkdir("foo/sub1", 0770)
  229. ioutil.WriteFile("foo/sub1/test3", []byte("Sub dir test file"), 0770)
  230. ioutil.WriteFile("bar/test1", []byte("Test3 file"), 0770)
  231. // Run the tests
  232. res := m.Run()
  233. // Shutdown the branches
  234. errorutil.AssertOk(b1.Shutdown())
  235. errorutil.AssertOk(b2.Shutdown())
  236. // Remove all directories again
  237. if err = os.RemoveAll(certdir); err != nil {
  238. fmt.Print("Could not remove test directory:", err.Error())
  239. }
  240. if err = os.RemoveAll("foo"); err != nil {
  241. fmt.Print("Could not remove test directory:", err.Error())
  242. }
  243. if err = os.RemoveAll("bar"); err != nil {
  244. fmt.Print("Could not remove test directory:", err.Error())
  245. }
  246. os.Exit(res)
  247. }
  248. /*
  249. createBranch creates a new branch.
  250. */
  251. func createBranch(name, dir string, readionly bool) (*Branch, error) {
  252. // Create the path directory
  253. if res, _ := fileutil.PathExists(dir); res {
  254. os.RemoveAll(dir)
  255. }
  256. err := os.Mkdir(dir, 0770)
  257. if err != nil {
  258. fmt.Print("Could not create test directory:", err.Error())
  259. os.Exit(1)
  260. }
  261. // Create the certificate
  262. portCount++
  263. host := fmt.Sprintf("localhost:%v", 9020+portCount)
  264. // Generate a certificate and private key
  265. certFile := fmt.Sprintf("cert-%v.pem", portCount)
  266. keyFile := fmt.Sprintf("key-%v.pem", portCount)
  267. err = cryptutil.GenCert(certdir, certFile, keyFile, host, "", 365*24*time.Hour, true, 2048, "")
  268. if err != nil {
  269. panic(err)
  270. }
  271. cert, err := tls.LoadX509KeyPair(filepath.Join(certdir, certFile), filepath.Join(certdir, keyFile))
  272. if err != nil {
  273. panic(err)
  274. }
  275. // Create the Branch
  276. config := map[string]interface{}{
  277. config.BranchName: name,
  278. config.BranchSecret: "123",
  279. config.EnableReadOnly: readionly,
  280. config.RPCHost: "localhost",
  281. config.RPCPort: fmt.Sprint(9020 + portCount),
  282. config.LocalFolder: dir,
  283. }
  284. branchConfigs[name] = config
  285. return NewBranch(config, &cert)
  286. }
  287. /*
  288. dirLocal reads a local directory and returns all found file names as a string.
  289. */
  290. func dirLocal(dir string) string {
  291. var buf bytes.Buffer
  292. fis, err := ioutil.ReadDir(dir)
  293. errorutil.AssertOk(err)
  294. for _, fi := range fis {
  295. buf.WriteString(fi.Name())
  296. if fi.IsDir() {
  297. buf.WriteString("(dir)")
  298. } else {
  299. buf.WriteString(fmt.Sprintf("(%v)", fi.Size()))
  300. }
  301. buf.WriteString(fmt.Sprintln())
  302. }
  303. return buf.String()
  304. }