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