http_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. "sync"
  15. "testing"
  16. "time"
  17. "github.com/syncthing/protocol"
  18. )
  19. var jsonEndpoints = []string{
  20. "/rest/db/completion?device=I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU&folder=default",
  21. "/rest/db/ignores?folder=default",
  22. "/rest/db/need?folder=default",
  23. "/rest/db/status?folder=default",
  24. "/rest/db/browse?folder=default",
  25. "/rest/events?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/upgrade",
  40. "/rest/system/version",
  41. }
  42. func TestGetIndex(t *testing.T) {
  43. st := syncthingProcess{
  44. argv: []string{"-home", "h2"},
  45. port: 8082,
  46. instance: "2",
  47. }
  48. err := st.start()
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer st.stop()
  53. res, err := st.get("/index.html")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if res.StatusCode != 200 {
  58. t.Errorf("Status %d != 200", res.StatusCode)
  59. }
  60. bs, err := ioutil.ReadAll(res.Body)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if len(bs) < 1024 {
  65. t.Errorf("Length %d < 1024", len(bs))
  66. }
  67. if !bytes.Contains(bs, []byte("</html>")) {
  68. t.Error("Incorrect response")
  69. }
  70. if res.Header.Get("Set-Cookie") == "" {
  71. t.Error("No set-cookie header")
  72. }
  73. res.Body.Close()
  74. res, err = st.get("/")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if res.StatusCode != 200 {
  79. t.Errorf("Status %d != 200", res.StatusCode)
  80. }
  81. bs, err = ioutil.ReadAll(res.Body)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if len(bs) < 1024 {
  86. t.Errorf("Length %d < 1024", len(bs))
  87. }
  88. if !bytes.Contains(bs, []byte("</html>")) {
  89. t.Error("Incorrect response")
  90. }
  91. if res.Header.Get("Set-Cookie") == "" {
  92. t.Error("No set-cookie header")
  93. }
  94. res.Body.Close()
  95. }
  96. func TestGetIndexAuth(t *testing.T) {
  97. st := syncthingProcess{
  98. argv: []string{"-home", "h1"},
  99. port: 8081,
  100. instance: "1",
  101. apiKey: "abc123",
  102. }
  103. err := st.start()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. defer st.stop()
  108. // Without auth should give 401
  109. res, err := http.Get("http://127.0.0.1:8081/")
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. res.Body.Close()
  114. if res.StatusCode != 401 {
  115. t.Errorf("Status %d != 401", res.StatusCode)
  116. }
  117. // With wrong username/password should give 401
  118. req, err := http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. req.SetBasicAuth("testuser", "wrongpass")
  123. res, err = http.DefaultClient.Do(req)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. res.Body.Close()
  128. if res.StatusCode != 401 {
  129. t.Fatalf("Status %d != 401", res.StatusCode)
  130. }
  131. // With correct username/password should succeed
  132. req, err = http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. req.SetBasicAuth("testuser", "testpass")
  137. res, err = http.DefaultClient.Do(req)
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. res.Body.Close()
  142. if res.StatusCode != 200 {
  143. t.Fatalf("Status %d != 200", res.StatusCode)
  144. }
  145. }
  146. func TestGetJSON(t *testing.T) {
  147. st := syncthingProcess{
  148. argv: []string{"-home", "h2"},
  149. port: 8082,
  150. instance: "2",
  151. }
  152. err := st.start()
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. defer st.stop()
  157. for _, path := range jsonEndpoints {
  158. res, err := st.get(path)
  159. if err != nil {
  160. t.Error(err)
  161. }
  162. if ct := res.Header.Get("Content-Type"); ct != "application/json; charset=utf-8" {
  163. t.Errorf("Incorrect Content-Type %q for %q", ct, path)
  164. }
  165. var intf interface{}
  166. err = json.NewDecoder(res.Body).Decode(&intf)
  167. res.Body.Close()
  168. if err != nil {
  169. t.Error(err)
  170. }
  171. }
  172. }
  173. func TestPOSTWithoutCSRF(t *testing.T) {
  174. st := syncthingProcess{
  175. argv: []string{"-home", "h2"},
  176. port: 8082,
  177. instance: "2",
  178. }
  179. err := st.start()
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. defer st.stop()
  184. // Should fail without CSRF
  185. req, err := http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. res, err := http.DefaultClient.Do(req)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. res.Body.Close()
  194. if res.StatusCode != 403 {
  195. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  196. }
  197. // Get CSRF
  198. req, err = http.NewRequest("GET", "http://127.0.0.1:8082/", nil)
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. res, err = http.DefaultClient.Do(req)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. res.Body.Close()
  207. hdr := res.Header.Get("Set-Cookie")
  208. if !strings.Contains(hdr, "CSRF-Token") {
  209. t.Error("Missing CSRF-Token in", hdr)
  210. }
  211. // Should succeed with 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", hdr[len("CSRF-Token="):])
  217. res, err = http.DefaultClient.Do(req)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. res.Body.Close()
  222. if res.StatusCode != 200 {
  223. t.Fatalf("Status %d != 200 for POST", res.StatusCode)
  224. }
  225. // Should fail with incorrect CSRF
  226. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/system/error/clear", nil)
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. req.Header.Set("X-CSRF-Token", hdr[len("CSRF-Token="):]+"X")
  231. res, err = http.DefaultClient.Do(req)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. res.Body.Close()
  236. if res.StatusCode != 403 {
  237. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  238. }
  239. }
  240. var (
  241. initOnce sync.Once
  242. proc syncthingProcess
  243. )
  244. func setupAPIBench() {
  245. err := removeAll("s1", "s2", "h1/index*", "h2/index*")
  246. if err != nil {
  247. panic(err)
  248. }
  249. err = generateFiles("s1", 25000, 20, "../LICENSE")
  250. if err != nil {
  251. panic(err)
  252. }
  253. err = ioutil.WriteFile("s1/knownfile", []byte("somedatahere"), 0644)
  254. if err != nil {
  255. panic(err)
  256. }
  257. proc = syncthingProcess{ // id1
  258. instance: "1",
  259. argv: []string{"-home", "h1"},
  260. port: 8081,
  261. apiKey: apiKey,
  262. }
  263. err = proc.start()
  264. if err != nil {
  265. panic(err)
  266. }
  267. // Wait for one scan to succeed, or up to 20 seconds... This is to let
  268. // startup, UPnP etc complete and make sure the sender has the full index
  269. // before they connect.
  270. for i := 0; i < 20; i++ {
  271. resp, err := proc.post("/rest/scan?folder=default", nil)
  272. if err != nil {
  273. time.Sleep(time.Second)
  274. continue
  275. }
  276. if resp.StatusCode != 200 {
  277. resp.Body.Close()
  278. time.Sleep(time.Second)
  279. continue
  280. }
  281. break
  282. }
  283. }
  284. func benchmarkURL(b *testing.B, url string) {
  285. initOnce.Do(setupAPIBench)
  286. b.ResetTimer()
  287. for i := 0; i < b.N; i++ {
  288. resp, err := proc.get(url)
  289. if err != nil {
  290. b.Fatal(err)
  291. }
  292. if resp.StatusCode != 200 {
  293. b.Fatal(resp.Status)
  294. }
  295. resp.Body.Close()
  296. }
  297. }
  298. func BenchmarkAPI_db_completion(b *testing.B) {
  299. benchmarkURL(b, "/rest/db/completion?folder=default&device="+protocol.LocalDeviceID.String())
  300. }
  301. func BenchmarkAPI_db_file(b *testing.B) {
  302. benchmarkURL(b, "/rest/db/file?folder=default&file=knownfile")
  303. }
  304. func BenchmarkAPI_db_ignores(b *testing.B) {
  305. benchmarkURL(b, "/rest/db/ignores?folder=default")
  306. }
  307. func BenchmarkAPI_db_need(b *testing.B) {
  308. benchmarkURL(b, "/rest/db/need?folder=default")
  309. }
  310. func BenchmarkAPI_db_status(b *testing.B) {
  311. benchmarkURL(b, "/rest/db/status?folder=default")
  312. }
  313. func BenchmarkAPI_db_browse_dirsonly(b *testing.B) {
  314. benchmarkURL(b, "/rest/db/browse?folder=default&dirsonly=true")
  315. }