config_envelope_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package middleware
  2. import (
  3. "bytes"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "github.com/gin-gonic/gin"
  10. "github.com/mhsanaei/3x-ui/v3/internal/util/wirecodec"
  11. )
  12. func envelopeTestEngine(t *testing.T, onHandler func()) *gin.Engine {
  13. t.Helper()
  14. gin.SetMode(gin.TestMode)
  15. engine := gin.New()
  16. engine.Use(ConfigEnvelopeMiddleware())
  17. engine.POST("/echo", func(c *gin.Context) {
  18. if onHandler != nil {
  19. onHandler()
  20. }
  21. b, _ := io.ReadAll(c.Request.Body)
  22. c.String(http.StatusOK, "%s", string(b))
  23. })
  24. return engine
  25. }
  26. func TestConfigEnvelope_DecompressesAndVerifies(t *testing.T) {
  27. engine := envelopeTestEngine(t, nil)
  28. orig := []byte(strings.Repeat("payload-", 200))
  29. packed := wirecodec.Compress(orig)
  30. req := httptest.NewRequest(http.MethodPost, "/echo", bytes.NewReader(packed))
  31. req.Header.Set("Content-Encoding", wirecodec.EncodingZstd)
  32. req.Header.Set(wirecodec.HashHeader, wirecodec.Sha256Hex(orig))
  33. w := httptest.NewRecorder()
  34. engine.ServeHTTP(w, req)
  35. if w.Code != http.StatusOK {
  36. t.Fatalf("status = %d, want 200", w.Code)
  37. }
  38. if w.Body.String() != string(orig) {
  39. t.Fatal("handler did not receive the decompressed body")
  40. }
  41. if !strings.Contains(w.Header().Get(wirecodec.CapsHeader), wirecodec.CapZstd) {
  42. t.Fatal("response must advertise the zstd capability")
  43. }
  44. }
  45. func TestConfigEnvelope_RejectsHashMismatch(t *testing.T) {
  46. called := false
  47. engine := envelopeTestEngine(t, func() { called = true })
  48. body := []byte("the-real-config")
  49. req := httptest.NewRequest(http.MethodPost, "/echo", bytes.NewReader(body))
  50. // Header claims a hash of a *different* body — a tampered/corrupted push.
  51. req.Header.Set(wirecodec.HashHeader, wirecodec.Sha256Hex([]byte("a-different-config")))
  52. w := httptest.NewRecorder()
  53. engine.ServeHTTP(w, req)
  54. if w.Code != http.StatusBadRequest {
  55. t.Fatalf("a hash mismatch must be rejected 4xx, got %d", w.Code)
  56. }
  57. if called {
  58. t.Fatal("the handler must NOT be invoked on a hash mismatch")
  59. }
  60. }
  61. func TestConfigEnvelope_PlainPassesThrough(t *testing.T) {
  62. engine := envelopeTestEngine(t, nil)
  63. req := httptest.NewRequest(http.MethodPost, "/echo", strings.NewReader("plain=body"))
  64. w := httptest.NewRecorder()
  65. engine.ServeHTTP(w, req)
  66. if w.Code != http.StatusOK || w.Body.String() != "plain=body" {
  67. t.Fatalf("a request with no envelope headers must pass through unchanged: code=%d body=%q", w.Code, w.Body.String())
  68. }
  69. }