Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scudette committed Nov 28, 2024
1 parent d832293 commit c8b304f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func csrfProtect(config_obj *config_proto.Config,
if pres && disable_csrf == "1" {
logger := logging.GetLogger(config_obj, &logging.GUIComponent)
logger.Info("Disabling CSRF protection because environment VELOCIRAPTOR_DISABLE_CSRF is set")
return parent
return api_utils.HandlerFunc(parent, parent.ServeHTTP)
}

// Derive a CSRF key from the hash of the server's public key.
Expand Down
39 changes: 39 additions & 0 deletions file_store/test_utils/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/Velocidex/yaml/v2"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
flows_proto "www.velocidex.com/golang/velociraptor/flows/proto"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/velociraptor/vql/acl_managers"
"www.velocidex.com/golang/velociraptor/vtesting/assert"

artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
Expand Down Expand Up @@ -110,6 +114,41 @@ type TestSuite struct {
Services *orgs.ServiceContainer
}

func (self *TestSuite) CreateClient(client_id string) {
client_info_manager, err := services.GetClientInfoManager(self.ConfigObj)
assert.NoError(self.T(), err)

err = client_info_manager.Set(self.Ctx, &services.ClientInfo{
actions_proto.ClientInfo{
ClientId: client_id,
}})
assert.NoError(self.T(), err)
}

func (self *TestSuite) CreateFlow(client_id, flow_id string) {
defer utils.SetFlowIdForTests(flow_id)()

launcher, err := services.GetLauncher(self.ConfigObj)
assert.NoError(self.T(), err)

manager, err := services.GetRepositoryManager(self.ConfigObj)
assert.NoError(self.T(), err)

repository, err := manager.GetGlobalRepository(self.ConfigObj)
require.NoError(self.T(), err)

_, err = launcher.ScheduleArtifactCollection(
self.Ctx,
self.ConfigObj,
acl_managers.NullACLManager{},
repository,
&flows_proto.ArtifactCollectorArgs{
ClientId: client_id,
Artifacts: []string{"Generic.Client.Info"},
}, nil)
assert.NoError(self.T(), err)
}

func (self *TestSuite) LoadConfig() *config_proto.Config {
os.Setenv("VELOCIRAPTOR_CONFIG", SERVER_CONFIG)
config_obj, err := new(config.Loader).
Expand Down
2 changes: 1 addition & 1 deletion http_comms/comms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (self *FakeServer) Log(fmtstring string, args ...interface{}) {

func NewFakeServer() *FakeServer {
self := &FakeServer{}
self.server = httptest.NewServer(api_utils.HandlerFunc(
self.server = httptest.NewServer(api_utils.HandlerFunc(nil,
func(rw http.ResponseWriter, req *http.Request) {
self.Log("request: %v", req.URL.String())

Expand Down
3 changes: 2 additions & 1 deletion services/client_info/client_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ package client_info
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -286,7 +287,7 @@ func (self *ClientInfoManager) ProcessFlowCompletion(
return self.storage.Modify(ctx, config_obj, client_id,
func(client_info *services.ClientInfo) (*services.ClientInfo, error) {
if client_info == nil {
return nil, utils.NotFoundError
return nil, fmt.Errorf("Client %v: %w", client_id, utils.NotFoundError)
}

if client_info.InFlightFlows != nil {
Expand Down
4 changes: 4 additions & 0 deletions services/vfs_service/vfs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var definitions = []string{`
name: System.VFS.ListDirectory
`, `
name: System.VFS.DownloadFile
`, `
name: System.Flow.Completion
type: CLIENT_EVENT
`,
}

Expand Down Expand Up @@ -58,6 +61,7 @@ func (self *VFSServiceTestSuite) SetupTest() {
self.ConfigObj, utils.GetSuperuserName(self.ConfigObj))

self.closer = utils.MockTime(utils.NewMockClock(time.Unix(1000, 0)))
self.CreateClient(self.client_id)
}

func (self *VFSServiceTestSuite) TearDownTest() {
Expand Down
9 changes: 9 additions & 0 deletions vql/server/clients/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func (self *DeleteTestSuite) SetupTest() {

self.client_id = "C.12312"
self.TestSuite.SetupTest()

client_info_manager, err := services.GetClientInfoManager(self.ConfigObj)
assert.NoError(self.T(), err)

err = client_info_manager.Set(self.Ctx, &services.ClientInfo{
actions_proto.ClientInfo{
ClientId: self.client_id,
}})
assert.NoError(self.T(), err)
}

func (self *DeleteTestSuite) TearDownTest() {
Expand Down
6 changes: 0 additions & 6 deletions vql/server/clients/fixtures/TestDeleteClient.golden
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@
"/clients"
],
"Files deleted": [
{
"client_id": "C.12312",
"type": "",
"vfs_path": "",
"error": "client_delete: clearIndexer: file does not exist"
},
{
"client_id": "C.12312",
"type": "Filestore",
Expand Down
2 changes: 2 additions & 0 deletions vql/server/downloads/downloads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ sources:

self.acl_manager = acl_managers.NewServerACLManager(
self.ConfigObj, "admin")

self.CreateClient(self.client_id)
}

func (self *TestSuite) TestExportCollectionServerArtifact() {
Expand Down
2 changes: 2 additions & 0 deletions vql/tools/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ func (self *TestSuite) TestCollectionWithUpload() {
golden := ordereddict.NewDict().
Set("zip_contents", zip_contents)

self.CreateClient("C.30b949dd33e1330a")

import_func := collector.ImportCollectionFunction{}
result := import_func.Call(self.Ctx, scope,
ordereddict.NewDict().
Expand Down
2 changes: 2 additions & 0 deletions vql/tools/collector/import_hunt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ func (self *TestSuite) snapshotHuntFlow() *ordereddict.Dict {
}

func (self *TestSuite) TestImportHuntFromFixture() {
self.CreateFlow("server", "F.1234")

defer utils.SetFlowIdForTests("F.1234XX")()
defer utils.MockTime(utils.NewMockClock(time.Unix(10, 10)))()

Expand Down
1 change: 1 addition & 0 deletions vql/tools/collector/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (self *TestSuite) TestCreateAndImportCollection() {
}

func (self *TestSuite) TestImportCollectionFromFixture() {
self.CreateFlow("server", "F.1234")
defer utils.SetFlowIdForTests("F.1234")()

manager, _ := services.GetRepositoryManager(self.ConfigObj)
Expand Down

0 comments on commit c8b304f

Please sign in to comment.