http_test.go 7.1 KB

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