http_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/protocol"
  16. "github.com/syncthing/syncthing/internal/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/upgrade",
  39. "/rest/system/version",
  40. }
  41. func TestGetIndex(t *testing.T) {
  42. p := startInstance(t, 2)
  43. defer checkedStop(t, p)
  44. // Check for explicint 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 TestPOSTWithoutCSRF(t *testing.T) {
  152. p := startInstance(t, 2)
  153. defer checkedStop(t, p)
  154. // Should fail without CSRF
  155. req, err := http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. res, err := http.DefaultClient.Do(req)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. res.Body.Close()
  164. if res.StatusCode != 403 {
  165. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  166. }
  167. // Get CSRF
  168. req, err = http.NewRequest("GET", "http://127.0.0.1:8082/", nil)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. res, err = http.DefaultClient.Do(req)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. res.Body.Close()
  177. hdr := res.Header.Get("Set-Cookie")
  178. id := res.Header.Get("X-Syncthing-ID")[:5]
  179. if !strings.Contains(hdr, "CSRF-Token") {
  180. t.Error("Missing CSRF-Token in", hdr)
  181. }
  182. // Should succeed with CSRF
  183. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):])
  188. res, err = http.DefaultClient.Do(req)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. res.Body.Close()
  193. if res.StatusCode != 200 {
  194. t.Fatalf("Status %d != 200 for POST", res.StatusCode)
  195. }
  196. // Should fail with incorrect CSRF
  197. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. req.Header.Set("X-CSRF-Token-"+id, hdr[len("CSRF-Token-"+id+"="):]+"X")
  202. res, err = http.DefaultClient.Do(req)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. res.Body.Close()
  207. if res.StatusCode != 403 {
  208. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  209. }
  210. }
  211. func setupAPIBench() *rc.Process {
  212. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  213. if err != nil {
  214. panic(err)
  215. }
  216. err = generateFiles("s1", 25000, 20, "../LICENSE")
  217. if err != nil {
  218. panic(err)
  219. }
  220. err = ioutil.WriteFile("s1/knownfile", []byte("somedatahere"), 0644)
  221. if err != nil {
  222. panic(err)
  223. }
  224. // This will panic if there is an actual failure to start, when we try to
  225. // call nil.Fatal(...)
  226. return startInstance(nil, 1)
  227. }
  228. func benchmarkURL(b *testing.B, url string) {
  229. p := setupAPIBench()
  230. defer p.Stop()
  231. b.ResetTimer()
  232. for i := 0; i < b.N; i++ {
  233. _, err := p.Get(url)
  234. if err != nil {
  235. b.Fatal(err)
  236. }
  237. }
  238. }
  239. func BenchmarkAPI_db_completion(b *testing.B) {
  240. benchmarkURL(b, "/rest/db/completion?folder=default&device="+protocol.LocalDeviceID.String())
  241. }
  242. func BenchmarkAPI_db_file(b *testing.B) {
  243. benchmarkURL(b, "/rest/db/file?folder=default&file=knownfile")
  244. }
  245. func BenchmarkAPI_db_ignores(b *testing.B) {
  246. benchmarkURL(b, "/rest/db/ignores?folder=default")
  247. }
  248. func BenchmarkAPI_db_need(b *testing.B) {
  249. benchmarkURL(b, "/rest/db/need?folder=default")
  250. }
  251. func BenchmarkAPI_db_status(b *testing.B) {
  252. benchmarkURL(b, "/rest/db/status?folder=default")
  253. }
  254. func BenchmarkAPI_db_browse_dirsonly(b *testing.B) {
  255. benchmarkURL(b, "/rest/db/browse?folder=default&dirsonly=true")
  256. }