firewall_validation_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package api
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. )
  8. func TestFirewallRejectsUnsupportedAndIncompleteRequests(t *testing.T) {
  9. s := &Server{} // Validation must complete without invoking any manager or tool.
  10. for _, tc := range []struct {
  11. handler http.HandlerFunc
  12. body string
  13. status int
  14. }{
  15. {s.handleSaveFilterRule, `{"chain":"input","action":"tarpit"}`, 422},
  16. {s.handleSaveFilterRule, `{"chain":"input","action":"drop","tls_host":"*.example.com"}`, 422},
  17. {s.handleSaveNATRule, `{"chain":"srcnat","action":"same"}`, 422},
  18. {s.handleSaveMangleRule, `{"chain":"output","action":"sniff-pc"}`, 422},
  19. {s.handleSaveFilterRule, `{"chain":"input","action":"accept","typo":true}`, 400},
  20. {s.handleKillConnection, `{}`, 422},
  21. {s.handleKillConnection, `{"protocol":"tcp","src":"192.0.2.1","dst":"198.51.100.1"}`, 422},
  22. } {
  23. r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
  24. w := httptest.NewRecorder()
  25. tc.handler(w, r)
  26. if w.Code != tc.status {
  27. t.Fatalf("%s returned %d: %s", tc.body, w.Code, w.Body.String())
  28. }
  29. }
  30. }
  31. func TestFirewallRoutePatterns(t *testing.T) {
  32. s := &Server{mux: http.NewServeMux(), fsHandler: http.NotFoundHandler()}
  33. s.registerRoutes()
  34. w := httptest.NewRecorder()
  35. s.mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/firewall/capabilities", nil))
  36. if w.Code != 200 {
  37. t.Fatal(w.Code)
  38. }
  39. }