http.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "bufio"
  8. "flag"
  9. "fmt"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "os"
  14. "regexp"
  15. "testing"
  16. )
  17. var (
  18. target string
  19. authUser string
  20. authPass string
  21. csrfToken string
  22. csrfFile string
  23. apiKey string
  24. )
  25. var jsonEndpoints = []string{
  26. "/rest/completion?node=I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU&repo=default",
  27. "/rest/config",
  28. "/rest/config/sync",
  29. "/rest/connections",
  30. "/rest/errors",
  31. "/rest/events",
  32. "/rest/lang",
  33. "/rest/model/version?repo=default",
  34. "/rest/model?repo=default",
  35. "/rest/need",
  36. "/rest/nodeid?id=I6KAH7666SLLLB5PFXSOAUFJCDZCYAOMLEKCP2GB32BV5RQST3PSROAU",
  37. "/rest/report",
  38. "/rest/system",
  39. }
  40. func main() {
  41. flag.StringVar(&target, "target", "localhost:8080", "Test target")
  42. flag.StringVar(&authUser, "user", "", "Username")
  43. flag.StringVar(&authPass, "pass", "", "Password")
  44. flag.StringVar(&csrfFile, "csrf", "", "CSRF token file")
  45. flag.StringVar(&apiKey, "api", "", "API key")
  46. flag.Parse()
  47. if len(csrfFile) > 0 {
  48. fd, err := os.Open(csrfFile)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. s := bufio.NewScanner(fd)
  53. for s.Scan() {
  54. csrfToken = s.Text()
  55. }
  56. fd.Close()
  57. }
  58. var tests []testing.InternalTest
  59. tests = append(tests, testing.InternalTest{"TestGetIndex", TestGetIndex})
  60. tests = append(tests, testing.InternalTest{"TestJSONEndpoints", TestJSONEndpoints})
  61. tests = append(tests, testing.InternalTest{"TestPOSTNoCSRF", TestPOSTNoCSRF})
  62. if len(authUser) > 0 {
  63. // If we expect authentication, verify that it fails with the wrong password and wrong API key
  64. tests = append(tests, testing.InternalTest{"TestJSONEndpointsNoAuth", TestJSONEndpointsNoAuth})
  65. tests = append(tests, testing.InternalTest{"TestJSONEndpointsIncorrectAuth", TestJSONEndpointsIncorrectAuth})
  66. }
  67. if len(csrfToken) > 0 {
  68. // If we have a CSRF token, verify that POST succeeds with it
  69. tests = append(tests, testing.InternalTest{"TestPOSTWithCSRF", TestPOSTWithCSRF})
  70. }
  71. fmt.Printf("Testing HTTP: CSRF=%v, API=%v, Auth=%v\n", len(csrfToken) > 0, len(apiKey) > 0, len(authUser) > 0)
  72. testing.Main(matcher, tests, nil, nil)
  73. }
  74. func matcher(s0, s1 string) (bool, error) {
  75. return true, nil
  76. }
  77. func TestGetIndex(t *testing.T) {
  78. res, err := get("/index.html")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if res.StatusCode != 200 {
  83. t.Errorf("Status %d != 200", res.StatusCode)
  84. }
  85. if res.ContentLength < 1024 {
  86. t.Errorf("Length %d < 1024", res.ContentLength)
  87. }
  88. res.Body.Close()
  89. res, err = get("/")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if res.StatusCode != 200 {
  94. t.Errorf("Status %d != 200", res.StatusCode)
  95. }
  96. if res.ContentLength < 1024 {
  97. t.Errorf("Length %d < 1024", res.ContentLength)
  98. }
  99. res.Body.Close()
  100. }
  101. func TestGetVersion(t *testing.T) {
  102. res, err := get("/rest/version")
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if res.StatusCode != 200 {
  107. t.Fatalf("Status %d != 200", res.StatusCode)
  108. }
  109. ver, err := ioutil.ReadAll(res.Body)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. res.Body.Close()
  114. if !regexp.MustCompile(`v\d+\.\d+\.\d+`).Match(ver) {
  115. t.Errorf("Invalid version %q", ver)
  116. }
  117. }
  118. func TestGetVersionNoCSRF(t *testing.T) {
  119. r, err := http.NewRequest("GET", "http://"+target+"/rest/version", nil)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. if len(authUser) > 0 {
  124. r.SetBasicAuth(authUser, authPass)
  125. }
  126. res, err := http.DefaultClient.Do(r)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if res.StatusCode != 403 {
  131. t.Fatalf("Status %d != 403", res.StatusCode)
  132. }
  133. }
  134. func TestJSONEndpoints(t *testing.T) {
  135. for _, p := range jsonEndpoints {
  136. res, err := get(p)
  137. if err != nil {
  138. t.Error(err)
  139. continue
  140. }
  141. if res.StatusCode != 200 {
  142. t.Errorf("Status %d != 200 for %q", res.StatusCode, p)
  143. continue
  144. }
  145. if ct := res.Header.Get("Content-Type"); ct != "application/json; charset=utf-8" {
  146. t.Errorf("Content-Type %q != \"application/json\" for %q", ct, p)
  147. continue
  148. }
  149. }
  150. }
  151. func TestPOSTNoCSRF(t *testing.T) {
  152. r, err := http.NewRequest("POST", "http://"+target+"/rest/error/clear", nil)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. if len(authUser) > 0 {
  157. r.SetBasicAuth(authUser, authPass)
  158. }
  159. res, err := http.DefaultClient.Do(r)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. if res.StatusCode != 403 && res.StatusCode != 401 {
  164. t.Fatalf("Status %d != 403/401 for POST", res.StatusCode)
  165. }
  166. }
  167. func TestPOSTWithCSRF(t *testing.T) {
  168. r, err := http.NewRequest("POST", "http://"+target+"/rest/error/clear", nil)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. if len(csrfToken) > 0 {
  173. r.Header.Set("X-CSRF-Token", csrfToken)
  174. }
  175. if len(authUser) > 0 {
  176. r.SetBasicAuth(authUser, authPass)
  177. }
  178. res, err := http.DefaultClient.Do(r)
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. if res.StatusCode != 200 {
  183. t.Fatalf("Status %d != 200 for POST", res.StatusCode)
  184. }
  185. }
  186. func TestJSONEndpointsNoAuth(t *testing.T) {
  187. for _, p := range jsonEndpoints {
  188. r, err := http.NewRequest("GET", "http://"+target+p, nil)
  189. if err != nil {
  190. t.Error(err)
  191. continue
  192. }
  193. if len(csrfToken) > 0 {
  194. r.Header.Set("X-CSRF-Token", csrfToken)
  195. }
  196. res, err := http.DefaultClient.Do(r)
  197. if err != nil {
  198. t.Error(err)
  199. continue
  200. }
  201. if res.StatusCode != 403 && res.StatusCode != 401 {
  202. t.Errorf("Status %d != 403/401 for %q", res.StatusCode, p)
  203. continue
  204. }
  205. }
  206. }
  207. func TestJSONEndpointsIncorrectAuth(t *testing.T) {
  208. for _, p := range jsonEndpoints {
  209. r, err := http.NewRequest("GET", "http://"+target+p, nil)
  210. if err != nil {
  211. t.Error(err)
  212. continue
  213. }
  214. if len(csrfToken) > 0 {
  215. r.Header.Set("X-CSRF-Token", csrfToken)
  216. }
  217. r.SetBasicAuth("wronguser", "wrongpass")
  218. res, err := http.DefaultClient.Do(r)
  219. if err != nil {
  220. t.Error(err)
  221. continue
  222. }
  223. if res.StatusCode != 403 && res.StatusCode != 401 {
  224. t.Errorf("Status %d != 403/401 for %q", res.StatusCode, p)
  225. continue
  226. }
  227. }
  228. }
  229. func get(path string) (*http.Response, error) {
  230. r, err := http.NewRequest("GET", "http://"+target+path, nil)
  231. if err != nil {
  232. return nil, err
  233. }
  234. if len(authUser) > 0 {
  235. r.SetBasicAuth(authUser, authPass)
  236. }
  237. if len(apiKey) > 0 {
  238. r.Header.Set("X-API-Key", apiKey)
  239. }
  240. return http.DefaultClient.Do(r)
  241. }