|
| 1 | +// Copyright © 2018 Banzai Cloud |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package api |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/http/httptest" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/gin-gonic/gin" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +func TestMain(m *testing.M) { |
| 28 | + gin.SetMode(gin.TestMode) |
| 29 | + m.Run() |
| 30 | +} |
| 31 | + |
| 32 | +func TestPprofAuth_RejectsMissingAndInvalidToken(t *testing.T) { |
| 33 | + router := gin.New() |
| 34 | + router.GET("/debug/pprof/heap", pprofAuth("secret-token"), func(c *gin.Context) { |
| 35 | + c.Status(http.StatusOK) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("missing token", func(t *testing.T) { |
| 39 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 40 | + rec := httptest.NewRecorder() |
| 41 | + router.ServeHTTP(rec, req) |
| 42 | + assert.Equal(t, http.StatusForbidden, rec.Code) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("wrong token", func(t *testing.T) { |
| 46 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 47 | + req.Header.Set("Token", "not-the-secret") |
| 48 | + rec := httptest.NewRecorder() |
| 49 | + router.ServeHTTP(rec, req) |
| 50 | + assert.Equal(t, http.StatusForbidden, rec.Code) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestPprofAuth_AllowsMatchingToken(t *testing.T) { |
| 55 | + router := gin.New() |
| 56 | + router.GET("/debug/pprof/heap", pprofAuth("secret-token"), func(c *gin.Context) { |
| 57 | + c.String(http.StatusOK, "ok") |
| 58 | + }) |
| 59 | + |
| 60 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 61 | + req.Header.Set("Token", "secret-token") |
| 62 | + rec := httptest.NewRecorder() |
| 63 | + router.ServeHTTP(rec, req) |
| 64 | + |
| 65 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 66 | + assert.Equal(t, "ok", rec.Body.String()) |
| 67 | +} |
| 68 | + |
| 69 | +func TestAttachPprof_ProtectsHeapEndpoint(t *testing.T) { |
| 70 | + router := gin.New() |
| 71 | + attachPprof(router, "secret-token") |
| 72 | + |
| 73 | + t.Run("unauthenticated", func(t *testing.T) { |
| 74 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 75 | + rec := httptest.NewRecorder() |
| 76 | + router.ServeHTTP(rec, req) |
| 77 | + assert.Equal(t, http.StatusForbidden, rec.Code) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("authenticated", func(t *testing.T) { |
| 81 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 82 | + req.Header.Set("Token", "secret-token") |
| 83 | + rec := httptest.NewRecorder() |
| 84 | + router.ServeHTTP(rec, req) |
| 85 | + require.Equal(t, http.StatusOK, rec.Code) |
| 86 | + assert.NotEmpty(t, rec.Body.Bytes()) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func TestAttachPprof_SkipsHandlersWhenTokenUnset(t *testing.T) { |
| 91 | + router := gin.New() |
| 92 | + attachPprof(router, "") |
| 93 | + |
| 94 | + for _, route := range router.Routes() { |
| 95 | + assert.NotContains(t, route.Path, "/debug/pprof") |
| 96 | + } |
| 97 | + |
| 98 | + req := httptest.NewRequest(http.MethodGet, "/debug/pprof/heap", nil) |
| 99 | + req.Header.Set("Token", "anything") |
| 100 | + rec := httptest.NewRecorder() |
| 101 | + router.ServeHTTP(rec, req) |
| 102 | + |
| 103 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 104 | +} |
| 105 | + |
| 106 | +func TestPprofRoutes(t *testing.T) { |
| 107 | + routes := pprofRoutes() |
| 108 | + require.NotEmpty(t, routes) |
| 109 | + |
| 110 | + seen := make(map[string]bool, len(routes)) |
| 111 | + for _, route := range routes { |
| 112 | + assert.NotEmpty(t, route.pattern) |
| 113 | + assert.NotNil(t, route.handler) |
| 114 | + assert.False(t, seen[route.pattern], "duplicate pprof route %s", route.pattern) |
| 115 | + seen[route.pattern] = true |
| 116 | + } |
| 117 | + |
| 118 | + assert.True(t, seen["/debug/pprof/heap"]) |
| 119 | + assert.True(t, seen["/debug/pprof/goroutine"]) |
| 120 | +} |
| 121 | + |
| 122 | +func TestTokenEquals(t *testing.T) { |
| 123 | + assert.True(t, tokenEquals("abc", "abc")) |
| 124 | + assert.False(t, tokenEquals("abc", "abd")) |
| 125 | + assert.False(t, tokenEquals("abc", "abcd")) |
| 126 | + assert.False(t, tokenEquals("", "a")) |
| 127 | +} |
0 commit comments