File
Blob: tun/client/waf_test.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "go.miragespace.co/specter/spec/protocol" |
| 8 | "go.miragespace.co/specter/spec/rpc" |
| 9 | |
| 10 | "github.com/stretchr/testify/mock" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | "github.com/twitchtv/twirp" |
| 13 | ) |
| 14 | |
| 15 | func TestHostWAFClientServerFallbackAndUncertainSave(t *testing.T) { |
| 16 | c, service := newOutcomeTestClient(t, nil) |
| 17 | c.connections.Store("z-second.example.com", &protocol.Node{Address: "z-second.example.com"}) |
| 18 | at := func(address string) any { |
| 19 | return mock.MatchedBy(func(ctx context.Context) bool { return rpc.GetNode(ctx).GetAddress() == address }) |
| 20 | } |
| 21 | response := &protocol.HostWAFResponse{Hostname: "test", Policy: &protocol.WAFPolicy{Mode: protocol.WAFPolicy_INHERIT}} |
| 22 | service.TunnelService.On("GetHostWAF", at("gateway.example.com"), mock.Anything).Return(nil, twirp.BadRoute.Error("old server")).Once() |
| 23 | service.TunnelService.On("GetHostWAF", at("z-second.example.com"), mock.Anything).Return(response, nil).Once() |
| 24 | _, err := c.GetHostWAF(t.Context(), "test") |
| 25 | require.NoError(t, err) |
| 26 | |
| 27 | uncertain := twirp.Unavailable.Error("save result is uncertain") |
| 28 | service.TunnelService.On("SetHostWAF", at("gateway.example.com"), mock.Anything).Return(nil, uncertain).Once() |
| 29 | _, err = c.SetHostWAF(t.Context(), "test", "original-version", &protocol.WAFPolicy{Mode: protocol.WAFPolicy_OFF}) |
| 30 | require.ErrorIs(t, err, uncertain) |
| 31 | service.TunnelService.AssertNumberOfCalls(t, "SetHostWAF", 1) |
| 32 | } |