http_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. // +build integration
  7. package integration
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "io/ioutil"
  12. "net/http"
  13. "strings"
  14. "testing"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/rc"
  17. )
  18. var jsonEndpoints = []string{
  19. "/rest/db/completion?device=I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU&folder=default",
  20. "/rest/db/ignores?folder=default",
  21. "/rest/db/need?folder=default",
  22. "/rest/db/status?folder=default",
  23. "/rest/db/browse?folder=default",
  24. "/rest/events?since=-1&limit=5",
  25. "/rest/stats/device",
  26. "/rest/stats/folder",
  27. "/rest/svc/deviceid?id=I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU",
  28. "/rest/svc/lang",
  29. "/rest/svc/report",
  30. "/rest/system/browse?current=.",
  31. "/rest/system/config",
  32. "/rest/system/config/insync",
  33. "/rest/system/connections",
  34. "/rest/system/discovery",
  35. "/rest/system/error",
  36. "/rest/system/ping",
  37. "/rest/system/status",
  38. "/rest/system/version",
  39. }
  40. func TestGetIndex(t *testing.T) {
  41. p := startInstance(t, 2)
  42. defer checkedStop(t, p)
  43. // Check for explicit index.html
  44. res, err := http.Get("http://localhost:8082/index.html")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if res.StatusCode != 200 {
  49. t.Errorf("Status %d != 200", res.StatusCode)
  50. }
  51. bs, err := ioutil.ReadAll(res.Body)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if len(bs) < 1024 {
  56. t.Errorf("Length %d < 1024", len(bs))
  57. }
  58. if !bytes.Contains(bs, []byte("</html>")) {
  59. t.Error("Incorrect response")
  60. }
  61. if res.Header.Get("Set-Cookie") == "" {
  62. t.Error("No set-cookie header")
  63. }
  64. res.Body.Close()
  65. // Check for implicit index.html
  66. res, err = http.Get("http://localhost:8082/")
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if res.StatusCode != 200 {
  71. t.Errorf("Status %d != 200", res.StatusCode)
  72. }
  73. bs, err = ioutil.ReadAll(res.Body)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if len(bs) < 1024 {
  78. t.Errorf("Length %d < 1024", len(bs))
  79. }
  80. if !bytes.Contains(bs, []byte("</html>")) {
  81. t.Error("Incorrect response")
  82. }
  83. if res.Header.Get("Set-Cookie") == "" {
  84. t.Error("No set-cookie header")
  85. }
  86. res.Body.Close()
  87. }
  88. func TestGetIndexAuth(t *testing.T) {
  89. p := startInstance(t, 1)
  90. defer checkedStop(t, p)
  91. // Without auth should give 401
  92. res, err := http.Get("http://127.0.0.1:8081/")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. res.Body.Close()
  97. if res.StatusCode != 401 {
  98. t.Errorf("Status %d != 401", res.StatusCode)
  99. }
  100. // With wrong username/password should give 401
  101. req, err := http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. req.SetBasicAuth("testuser", "wrongpass")
  106. res, err = http.DefaultClient.Do(req)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. res.Body.Close()
  111. if res.StatusCode != 401 {
  112. t.Fatalf("Status %d != 401", res.StatusCode)
  113. }
  114. // With correct username/password should succeed
  115. req, err = http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. req.SetBasicAuth("testuser", "testpass")
  120. res, err = http.DefaultClient.Do(req)
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. res.Body.Close()
  125. if res.StatusCode != 200 {
  126. t.Fatalf("Status %d != 200", res.StatusCode)
  127. }
  128. }
  129. func TestGetJSON(t *testing.T) {
  130. p := startInstance(t, 2)
  131. defer checkedStop(t, p)
  132. for _, path := range jsonEndpoints {
  133. res, err := http.Get("http://127.0.0.1:8082" + path)
  134. if err != nil {
  135. t.Error(path, err)
  136. continue
  137. }
  138. if ct := res.Header.Get("Content-Type"); ct != "application/json; charset=utf-8" {
  139. t.Errorf("Incorrect Content-Type %q for %q", ct, path)
  140. continue
  141. }
  142. var intf interface{}
  143. err = json.NewDecoder(res.Body).Decode(&intf)
  144. res.Body.Close()
  145. if err != nil {
  146. t.Error(path, err)
  147. }
  148. }
  149. }
  150. func TestOptions(t *testing.T) {
  151. p := startInstance(t, 2)
  152. defer checkedStop(t, p)
  153. req, err := http.NewRequest("OPTIONS", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. res, err := http.DefaultClient.Do(req)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. res.Body.Close()
  162. if res.StatusCode != 204 {
  163. t.Fatalf("Status %d != 204 for OPTIONS", res.StatusCode)
  164. }
  165. }
  166. func TestPOSTWithoutCSRF(t *testing.T) {
  167. p := startInstance(t, 2)
  168. defer checkedStop(t, p)
  169. // Should fail without CSRF
  170. req, err := http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. res, err := http.DefaultClient.Do(req)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. res.Body.Close()
  179. if res.StatusCode != 403 {
  180. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  181. }
  182. // Get CSRF
  183. req, err = http.NewRequest("GET", "http://127.0.0.1:8082/", nil)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. res, err = http.DefaultClient.Do(req)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. res.Body.Close()
  192. hdr := res.Header.Get("Set-Cookie")
  193. id := res.Header.Get("X-Syncthing-ID")[:5]
  194. if !strings.Contains(hdr, "CSRF-Token") {
  195. t.Error("Missing CSRF-Token in", hdr)
  196. }
  197. // Should succeed with CSRF
  198. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):])
  203. res, err = http.DefaultClient.Do(req)
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. res.Body.Close()
  208. if res.StatusCode != 200 {
  209. t.Fatalf("Status %d != 200 for POST", res.StatusCode)
  210. }
  211. // Should fail with incorrect CSRF
  212. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):]+"X")
  217. res, err = http.DefaultClient.Do(req)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. res.Body.Close()
  222. if res.StatusCode != 403 {
  223. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  224. }
  225. }
  226. func setupAPIBench() *rc.Process {
  227. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  228. if err != nil {
  229. panic(err)
  230. }
  231. err = generateFiles("s1", 25000, 20, "../LICENSE")
  232. if err != nil {
  233. panic(err)
  234. }
  235. err = ioutil.WriteFile("s1/knownfile", []byte("somedatahere"), 0644)
  236. if err != nil {
  237. panic(err)
  238. }
  239. // This will panic if there is an actual failure to start, when we try to
  240. // call nil.Fatal(...)
  241. return startInstance(nil, 1)
  242. }
  243. func benchmarkURL(b *testing.B, url string) {
  244. p := setupAPIBench()
  245. defer p.Stop()
  246. b.ResetTimer()
  247. for i := 0; i < b.N; i++ {
  248. _, err := p.Get(url)
  249. if err != nil {
  250. b.Fatal(err)
  251. }
  252. }
  253. }
  254. func BenchmarkAPI_db_completion(b *testing.B) {
  255. benchmarkURL(b, "/rest/db/completion?folder=default&device="+protocol.LocalDeviceID.String())
  256. }
  257. func BenchmarkAPI_db_file(b *testing.B) {
  258. benchmarkURL(b, "/rest/db/file?folder=default&file=knownfile")
  259. }
  260. func BenchmarkAPI_db_ignores(b *testing.B) {
  261. benchmarkURL(b, "/rest/db/ignores?folder=default")
  262. }
  263. func BenchmarkAPI_db_need(b *testing.B) {
  264. benchmarkURL(b, "/rest/db/need?folder=default")
  265. }
  266. func BenchmarkAPI_db_status(b *testing.B) {
  267. benchmarkURL(b, "/rest/db/status?folder=default")
  268. }
  269. func BenchmarkAPI_db_browse_dirsonly(b *testing.B) {
  270. benchmarkURL(b, "/rest/db/browse?folder=default&dirsonly=true")
  271. }