http_test.go 5.8 KB

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