flow: encode boundary as a string for JSON

This is only used for debugging.
This commit is contained in:
Vincent Bernat
2022-03-18 15:32:03 +01:00
parent c41dfcbe34
commit 154c5103fb
3 changed files with 74 additions and 0 deletions

View File

@@ -271,6 +271,8 @@ func TestCore(t *testing.T) {
"OutIfName": "Gi0/0/677",
"InIfSpeed": 1000,
"OutIfSpeed": 1000,
"InIfBoundary": "UNDEFINED",
"OutIfBoundary": "UNDEFINED",
"DstCountry": "GB",
"SrcCountry": "BT",
"SrcAS": 35908,

View File

@@ -12,6 +12,8 @@ type prettierFlowMessage struct {
PrettierSrcAddr string `json:"SrcAddr,omitempty"`
PrettierDstAddr string `json:"DstAddr,omitempty"`
PrettierSamplerAddress string `json:"SamplerAddress,omitempty"`
PrettierInIfBoundary string `json:"InIfBoundary,omitempty"`
PrettierOutIfBoundary string `json:"OutIfBoundary,omitempty"`
}
// MarshalJSON marshals a flow message to JSON. It uses a textual
@@ -23,6 +25,8 @@ func (fm FlowMessage) MarshalJSON() ([]byte, error) {
PrettierSrcAddr: net.IP(fm.SrcAddr).String(),
PrettierDstAddr: net.IP(fm.DstAddr).String(),
PrettierSamplerAddress: net.IP(fm.SamplerAddress).String(),
PrettierInIfBoundary: fm.InIfBoundary.String(),
PrettierOutIfBoundary: fm.OutIfBoundary.String(),
}
prettier.SrcAddr = nil
prettier.DstAddr = nil

68
flow/encoder_test.go Normal file
View File

@@ -0,0 +1,68 @@
package flow
import (
"akvorado/helpers"
"bytes"
"encoding/json"
"net"
"strings"
"testing"
)
func TestJSONEncoding(t *testing.T) {
flow := &FlowMessage{
TimeReceived: 200,
SequenceNum: 1000,
SamplingRate: 1000,
FlowDirection: 1,
SamplerAddress: net.ParseIP("192.0.2.42"),
TimeFlowStart: 100,
TimeFlowEnd: 200,
Bytes: 6765,
Packets: 4,
InIf: 300,
OutIf: 200,
SrcAddr: net.ParseIP("67.43.156.77"),
DstAddr: net.ParseIP("2.125.160.216"),
Etype: 0x800,
Proto: 6,
SrcPort: 8534,
DstPort: 80,
InIfProvider: "Telia",
InIfBoundary: FlowMessage_EXTERNAL,
OutIfBoundary: FlowMessage_INTERNAL,
}
buf := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buf)
encoder.SetIndent("", " ")
if err := encoder.Encode(flow); err != nil {
t.Fatalf("Encode() error:\n%+v", err)
}
got := strings.Split(buf.String(), "\n")
expected := strings.Split(`{
"TimeReceived": 200,
"SequenceNum": 1000,
"SamplingRate": 1000,
"FlowDirection": 1,
"TimeFlowStart": 100,
"TimeFlowEnd": 200,
"Bytes": 6765,
"Packets": 4,
"Etype": 2048,
"Proto": 6,
"SrcPort": 8534,
"DstPort": 80,
"InIf": 300,
"OutIf": 200,
"InIfProvider": "Telia",
"SrcAddr": "67.43.156.77",
"DstAddr": "2.125.160.216",
"SamplerAddress": "192.0.2.42",
"InIfBoundary": "EXTERNAL",
"OutIfBoundary": "INTERNAL"
}
`, "\n")
if diff := helpers.Diff(got, expected); diff != "" {
t.Errorf("Encode() (-got, +want):\n%s", diff)
}
}