Skip to content

Commit

Permalink
Added server tests. (Velocidex#141)
Browse files Browse the repository at this point in the history
Refactored internal grpc connection so it can be mocked.
Memory based file store and data store allow easier testing.
  • Loading branch information
scudette authored Oct 28, 2019
1 parent c64d8cd commit 0ff07b2
Show file tree
Hide file tree
Showing 35 changed files with 2,079 additions and 467 deletions.
19 changes: 11 additions & 8 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type ApiServer struct {
config *config_proto.Config
server_obj *server.Server
ca_pool *x509.CertPool

api_client_factory grpc_client.APIClientFactory
}

func (self *ApiServer) CancelFlow(
Expand All @@ -74,7 +76,9 @@ func (self *ApiServer) CancelFlow(
}
}

result, err := flows.CancelFlow(self.config, in.ClientId, in.FlowId, user)
result, err := flows.CancelFlow(
self.config, in.ClientId, in.FlowId, user,
self.api_client_factory)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -161,15 +165,13 @@ func (self *ApiServer) CollectArtifact(
result.FlowId = flow_id

// Notify the client if it is listenning.
channel := grpc_client.GetChannel(self.config)
defer channel.Close()
client, cancel := self.api_client_factory.GetAPIClient(self.config)
defer cancel()

client := api_proto.NewAPIClient(channel)
_, err = client.NotifyClients(ctx, &api_proto.NotificationRequest{
ClientId: in.ClientId,
})
if err != nil {
fmt.Printf("Cant connect: %v\n", err)
return nil, err
}

Expand Down Expand Up @@ -710,9 +712,10 @@ func StartServer(config_obj *config_proto.Config, server_obj *server.Server) err
api_proto.RegisterAPIServer(
grpcServer,
&ApiServer{
config: config_obj,
server_obj: server_obj,
ca_pool: CA_Pool,
config: config_obj,
server_obj: server_obj,
ca_pool: CA_Pool,
api_client_factory: grpc_client.GRPCAPIClient{},
},
)
// Register reflection service.
Expand Down
22 changes: 12 additions & 10 deletions api/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,25 @@ func MakeCollectorRequest(
Artifacts: &flows_proto.Artifacts{
Names: []string{artifact_name},
},
Parameters: &flows_proto.ArtifactParameters{},
},
}

if len(parameters)%2 != 0 {
parameters = parameters[:len(parameters)-len(parameters)%2]
}

for i := 0; i < len(parameters); {
k := parameters[i]
i++
v := parameters[i]
i++
result.Request.Parameters.Env = append(result.Request.Parameters.Env,
&actions_proto.VQLEnv{
Key: k, Value: v,
})
if parameters != nil {
result.Request.Parameters = &flows_proto.ArtifactParameters{}
for i := 0; i < len(parameters); {
k := parameters[i]
i++
v := parameters[i]
i++
result.Request.Parameters.Env = append(result.Request.Parameters.Env,
&actions_proto.VQLEnv{
Key: k, Value: v,
})
}
}

return result
Expand Down
6 changes: 3 additions & 3 deletions api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func downloadFlowToZip(
for _, artifact_source := range flow_details.Context.ArtifactsWithResults {
artifact, source := artifacts.SplitFullSourceName(artifact_source)
copier(artifacts.GetCSVPath(
client_id, "", path.Base(flow_id),
client_id, "", flow_id,
artifact, source, artifacts.MODE_CLIENT))
}

Expand All @@ -112,7 +112,7 @@ func downloadFlowToZip(
}

// File uploads are stored in their own CSV file.
file_path := artifacts.GetUploadsFile(client_id, path.Base(flow_id), "", "")
file_path := artifacts.GetUploadsFile(client_id, flow_id, "", "")
fd, err := file_store_factory.ReadFile(file_path)
if err != nil {
return err
Expand Down Expand Up @@ -274,7 +274,7 @@ func huntResultDownloadHandler(
returnError(w, 404, "Hunt id should be specified.")
return
}
hunt_id := path.Base(hunt_ids[0])
hunt_id := hunt_ids[0]

hunt_details, err := flows.GetHunt(
config_obj,
Expand Down
Loading

0 comments on commit 0ff07b2

Please sign in to comment.