forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_test.go
76 lines (61 loc) · 2.05 KB
/
csv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package json_test
import (
"bytes"
"fmt"
"testing"
"github.com/Velocidex/ordereddict"
"github.com/sebdah/goldie"
"github.com/stretchr/testify/suite"
"www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/file_store/test_utils"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/paths/artifacts"
"www.velocidex.com/golang/velociraptor/result_sets"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/velociraptor/vtesting/assert"
)
type CSVUtilsTestSuite struct {
test_utils.TestSuite
client_id, flow_id string
}
func (self *CSVUtilsTestSuite) TestCSVUtils() {
path_manager := artifacts.NewArtifactPathManagerWithMode(
self.ConfigObj, self.client_id, self.flow_id,
"Artifact", paths.MODE_CLIENT)
file_store_factory := file_store.GetFileStore(self.ConfigObj)
writer, err := result_sets.NewResultSetWriter(file_store_factory,
path_manager.Path(), json.DefaultEncOpts(),
utils.SyncCompleter, result_sets.TruncateMode)
assert.NoError(self.T(), err)
// Create a sample result set
for i := 0; i < 10; i++ {
writer.Write(ordereddict.NewDict().
Set("Col With \" 1",
fmt.Sprintf("Value \" with quotes: %d", i)).
Set("Object", ordereddict.NewDict().Set("Foo", 1)).
Set("Col2", i*2))
}
writer.Close()
reader, err := result_sets.NewResultSetReader(
file_store_factory, path_manager.Path())
assert.NoError(self.T(), err)
defer reader.Close()
json_chan, err := reader.JSON(self.Ctx)
assert.NoError(self.T(), err)
json_buffer := &bytes.Buffer{}
csv_buffer := &bytes.Buffer{}
json.ConvertJSONL(json_chan, json_buffer, csv_buffer,
ordereddict.NewDict().
Set("ClientId", "C.123").
Set("HuntId", "H.123"))
golden := fmt.Sprintf("JSONL:\n------\n%v\nCSV:\n------\n%v\n",
string(json_buffer.Bytes()), string(csv_buffer.Bytes()))
goldie.Assert(self.T(), "TestCSVUtils", []byte(golden))
}
func TestCSVUtils(t *testing.T) {
suite.Run(t, &CSVUtilsTestSuite{
client_id: "C.1234",
flow_id: "F.123",
})
}