http_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration
  16. package integration
  17. import (
  18. "encoding/json"
  19. "net/http"
  20. "strings"
  21. "testing"
  22. )
  23. var jsonEndpoints = []string{
  24. "/rest/completion?device=I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU&folder=default",
  25. "/rest/config",
  26. "/rest/config/sync",
  27. "/rest/connections",
  28. "/rest/errors",
  29. "/rest/events",
  30. "/rest/lang",
  31. "/rest/model?folder=default",
  32. "/rest/need",
  33. "/rest/deviceid?id=I6KAH7666SLLLB5PFXSOAUFJCDZCYAOMLEKCP2GB32BV5RQST3PSROAU",
  34. "/rest/report",
  35. "/rest/system",
  36. }
  37. func TestGetIndex(t *testing.T) {
  38. st := syncthingProcess{
  39. argv: []string{"-home", "h2"},
  40. port: 8082,
  41. instance: "2",
  42. }
  43. err := st.start()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. defer st.stop()
  48. res, err := st.get("/index.html")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if res.StatusCode != 200 {
  53. t.Errorf("Status %d != 200", res.StatusCode)
  54. }
  55. if res.ContentLength < 1024 {
  56. t.Errorf("Length %d < 1024", res.ContentLength)
  57. }
  58. if res.Header.Get("Set-Cookie") == "" {
  59. t.Error("No set-cookie header")
  60. }
  61. res.Body.Close()
  62. res, err = st.get("/")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if res.StatusCode != 200 {
  67. t.Errorf("Status %d != 200", res.StatusCode)
  68. }
  69. if res.ContentLength < 1024 {
  70. t.Errorf("Length %d < 1024", res.ContentLength)
  71. }
  72. if res.Header.Get("Set-Cookie") == "" {
  73. t.Error("No set-cookie header")
  74. }
  75. res.Body.Close()
  76. }
  77. func TestGetIndexAuth(t *testing.T) {
  78. st := syncthingProcess{
  79. argv: []string{"-home", "h1"},
  80. port: 8081,
  81. instance: "1",
  82. }
  83. err := st.start()
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. defer st.stop()
  88. // Without auth should give 401
  89. res, err := http.Get("http://127.0.0.1:8081/")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. res.Body.Close()
  94. if res.StatusCode != 401 {
  95. t.Errorf("Status %d != 401", res.StatusCode)
  96. }
  97. // With wrong username/password should give 401
  98. req, err := http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. req.SetBasicAuth("testuser", "wrongpass")
  103. res, err = http.DefaultClient.Do(req)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. res.Body.Close()
  108. if res.StatusCode != 401 {
  109. t.Fatalf("Status %d != 401", res.StatusCode)
  110. }
  111. // With correct username/password should succeed
  112. req, err = http.NewRequest("GET", "http://127.0.0.1:8081/", nil)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. req.SetBasicAuth("testuser", "testpass")
  117. res, err = http.DefaultClient.Do(req)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. res.Body.Close()
  122. if res.StatusCode != 200 {
  123. t.Fatalf("Status %d != 200", res.StatusCode)
  124. }
  125. }
  126. func TestGetJSON(t *testing.T) {
  127. st := syncthingProcess{
  128. argv: []string{"-home", "h2"},
  129. port: 8082,
  130. instance: "2",
  131. }
  132. err := st.start()
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. defer st.stop()
  137. for _, path := range jsonEndpoints {
  138. res, err := st.get(path)
  139. if err != nil {
  140. t.Error(err)
  141. }
  142. if ct := res.Header.Get("Content-Type"); ct != "application/json; charset=utf-8" {
  143. t.Errorf("Incorrect Content-Type %q for %q", ct, path)
  144. }
  145. var intf interface{}
  146. err = json.NewDecoder(res.Body).Decode(&intf)
  147. res.Body.Close()
  148. if err != nil {
  149. t.Error(err)
  150. }
  151. }
  152. }
  153. func TestPOSTWithoutCSRF(t *testing.T) {
  154. st := syncthingProcess{
  155. argv: []string{"-home", "h2"},
  156. port: 8082,
  157. instance: "2",
  158. }
  159. err := st.start()
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. defer st.stop()
  164. // Should fail without CSRF
  165. req, err := http.NewRequest("POST", "http://127.0.0.1:8082/rest/error/clear", nil)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. res, err := http.DefaultClient.Do(req)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. res.Body.Close()
  174. if res.StatusCode != 403 {
  175. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  176. }
  177. // Get CSRF
  178. req, err = http.NewRequest("GET", "http://127.0.0.1:8082/", nil)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. res, err = http.DefaultClient.Do(req)
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. res.Body.Close()
  187. hdr := res.Header.Get("Set-Cookie")
  188. if !strings.Contains(hdr, "CSRF-Token") {
  189. t.Error("Missing CSRF-Token in", hdr)
  190. }
  191. // Should succeed with CSRF
  192. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/error/clear", nil)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. req.Header.Set("X-CSRF-Token", hdr[len("CSRF-Token="):])
  197. res, err = http.DefaultClient.Do(req)
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. res.Body.Close()
  202. if res.StatusCode != 200 {
  203. t.Fatalf("Status %d != 200 for POST", res.StatusCode)
  204. }
  205. // Should fail with incorrect CSRF
  206. req, err = http.NewRequest("POST", "http://127.0.0.1:8082/rest/error/clear", nil)
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. req.Header.Set("X-CSRF-Token", hdr[len("CSRF-Token="):]+"X")
  211. res, err = http.DefaultClient.Do(req)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. res.Body.Close()
  216. if res.StatusCode != 403 {
  217. t.Fatalf("Status %d != 403 for POST", res.StatusCode)
  218. }
  219. }