Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 108 additions & 40 deletions cefevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import (
"testing"
)

var event = cefevent.CefEvent{
Version: "0",
DeviceVendor: "Cool Vendor",
DeviceProduct: "Cool Product",
DeviceVersion: "1.0",
DeviceEventClassId: "COOL_THING",
Name: "Something cool happened.",
Severity: "Unknown",
}

func TestCefEventExpected(t *testing.T) {

ext := make(map[string]string)
ext["sourceAddress"] = "127.0.0.1"

event := cefevent.CefEvent{
Version: "0",
DeviceVendor: "Cool Vendor",
DeviceProduct: "Cool Product",
DeviceVersion: "1.0",
DeviceEventClassId: "COOL_THING",
Name: "Something cool happened.",
Severity: "Unknown",
Extensions: ext,
}
extLocal := make(map[string]string)
extLocal["sourceAddress"] = "127.0.0.1"

expectedEvent := event
expectedEvent.Extensions = extLocal

want := "CEF:0|Cool Vendor|Cool Product|1.0|COOL_THING|Something cool happened.|Unknown|sourceAddress=127.0.0.1"
got, _ := event.Generate()
got, _ := expectedEvent.Generate()

if want != got {
t.Errorf("event.Generate() = %q, want %q", got, want)
Expand All @@ -32,46 +34,112 @@ func TestCefEventExpected(t *testing.T) {

func TestCefEventEscape(t *testing.T) {

ext := make(map[string]string)
ext["sourceAddress\\"] = "\n127.0.0.1="

event := cefevent.CefEvent{
Version: "0",
DeviceVendor: "\\Cool\nVendor|",
DeviceProduct: "Cool Product",
DeviceVersion: "1.0",
DeviceEventClassId: "COOL_THING",
Name: "Something cool happened.",
Severity: "Unknown",
Extensions: ext,
}
extLocal := make(map[string]string)
extLocal["sourceAddress\\"] = "\n127.0.0.1="

borkyEvent := event
borkyEvent.DeviceVendor = "\\Cool\nVendor|"
borkyEvent.Extensions = extLocal

want := "CEF:0|\\\\Cool\\nVendor\\||Cool Product|1.0|COOL_THING|Something cool happened.|Unknown|sourceAddress\\\\=\\n127.0.0.1\\="
got, _ := event.Generate()
got, _ := borkyEvent.Generate()

if want != got {
t.Errorf("event.Generate() = %q, want %q", got, want)
}

}

func TestCefEventMandatoryFields(t *testing.T) {
func TestCefEventMandatoryVersionField(t *testing.T) {

event := cefevent.CefEvent{
Version: "0",
DeviceVendor: "Cool Vendor",
DeviceProduct: "Cool Product",
DeviceVersion: "1.0",
DeviceEventClassId: "COOL_THING",
Name: "Something cool happened.",
Severity: "Unknown",
brokenEvent := event
brokenEvent.Version = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

noVersion := event
noVersion.Version = ""
_, err := noVersion.Generate()
func TestCefEventMandatoryDeviceVendorField(t *testing.T) {

brokenEvent := event
brokenEvent.DeviceVendor = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func TestCefEventMandatoryDeviceProductField(t *testing.T) {

brokenEvent := event
brokenEvent.DeviceProduct = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func TestCefEventMandatoryDeviceVersionField(t *testing.T) {

brokenEvent := event
brokenEvent.DeviceVersion = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func TestCefEventMandatoryDeviceEventClassIdField(t *testing.T) {

brokenEvent := event
brokenEvent.DeviceEventClassId = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func TestCefEventMandatoryNameField(t *testing.T) {

brokenEvent := event
brokenEvent.Name = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func TestCefEventMandatorySeverityField(t *testing.T) {

brokenEvent := event
brokenEvent.Severity = ""
_, err := brokenEvent.Generate()

if err == nil {
t.Errorf("%v", err)
}
}

func someImplementationOfCefEventer(e cefevent.CefEventer) bool {
return e.Validate()
}

func TestCefEventerValidate(t *testing.T) {

if !someImplementationOfCefEventer(&event) {
t.Errorf("Validation should be succesful here.")
}

noVersion := event
noVersion.Version = ""
if someImplementationOfCefEventer(&noVersion) {
t.Errorf("Validation should fail here.")
}
}