feat: build wahrwelt A/D toolkit
This commit is contained in:
1 parent
303438d206
commit
653924d8c6
564 files changed
+36310
-49998
No files matched your search
@@ -0,0 +1,134 @@
|
||||
package exploit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
|
||||
"github.com/c4t-but-s4d/neo/v2/internal/client"
|
||||
epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
|
||||
)
|
||||
|
||||
func TestRunner_onServerStateUpdateRescalesOnTeamChange(t *testing.T) {
|
||||
st, cleanup := mockStorage()
|
||||
defer func() {
|
||||
require.NoError(t, cleanup())
|
||||
}()
|
||||
st.cache.Update([]*State{{
|
||||
ID: "check",
|
||||
Version: 1,
|
||||
RunEvery: time.Minute,
|
||||
Timeout: time.Minute,
|
||||
ScaledTimeout: time.Minute,
|
||||
}})
|
||||
|
||||
r := &Runner{
|
||||
storage: st,
|
||||
client: &client.Client{ID: "worker"},
|
||||
maxJobs: 1,
|
||||
timeoutScaleTarget: 1,
|
||||
teams: map[string]string{},
|
||||
restarts: make(chan struct{}, 1),
|
||||
metrics: &Metrics{Teams: prometheus.NewGauge(prometheus.GaugeOpts{Name: "neo_test_teams", Help: "team count"})},
|
||||
logger: logrus.NewEntry(logrus.New()),
|
||||
}
|
||||
|
||||
state := &epb.ServerState{
|
||||
Config: &epb.Config{
|
||||
FlagRegexp: ".*",
|
||||
PingEvery: durationpb.New(time.Minute),
|
||||
SubmitEvery: durationpb.New(time.Second),
|
||||
},
|
||||
ClientTeamMap: map[string]*epb.TeamBucket{
|
||||
"worker": {Teams: map[string]string{}},
|
||||
},
|
||||
Exploits: []*epb.ExploitState{{ExploitId: "check", Version: 1}},
|
||||
}
|
||||
|
||||
// Эксплойт уже загружен, поэтому повторное состояние не меняет его версию.
|
||||
require.NoError(t, r.onServerStateUpdate(context.Background(), state))
|
||||
got, ok := st.Exploit("check")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, time.Minute, got.ScaledTimeout)
|
||||
|
||||
state.ClientTeamMap["worker"] = &epb.TeamBucket{Teams: map[string]string{
|
||||
"team-a": "192.0.2.1",
|
||||
"team-b": "192.0.2.2",
|
||||
}}
|
||||
require.NoError(t, r.onServerStateUpdate(context.Background(), state))
|
||||
|
||||
got, ok = st.Exploit("check")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, time.Minute, got.Timeout)
|
||||
require.Equal(t, 30*time.Second, got.ScaledTimeout)
|
||||
}
|
||||
|
||||
func Test_retryLoop(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
retries int
|
||||
doRetries int
|
||||
returnErr error
|
||||
lastErr error
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
"simple",
|
||||
1,
|
||||
1,
|
||||
ErrFinishedUnexpectedly,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"endless retries",
|
||||
-1,
|
||||
100,
|
||||
ErrFinishedUnexpectedly,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"return last error",
|
||||
1,
|
||||
5,
|
||||
io.EOF,
|
||||
nil,
|
||||
io.EOF,
|
||||
},
|
||||
{
|
||||
"context cancelled",
|
||||
5,
|
||||
3,
|
||||
ErrFinishedUnexpectedly,
|
||||
context.Canceled,
|
||||
context.Canceled,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cnt := 0
|
||||
f := func(context.Context) error {
|
||||
cnt++
|
||||
if cnt < tt.doRetries {
|
||||
return tt.returnErr
|
||||
}
|
||||
return tt.lastErr
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
require.ErrorIs(t, retryLoop(ctx, "test", tt.retries, time.Millisecond, f), tt.wantErr)
|
||||
if tt.retries != -1 && tt.doRetries > tt.retries {
|
||||
require.Equal(t, tt.retries, cnt)
|
||||
} else {
|
||||
require.Equal(t, tt.doRetries, cnt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user