Skip to content

Commit a9a6c0a

Browse files
efd6mergify-bot
authored andcommitted
winlogbeat/sys/winevent: use reflect IsZero method (#29190)
(cherry picked from commit 180e7f3)
1 parent ca309ef commit a9a6c0a

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

winlogbeat/sys/winevent/event_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"time"
2727

2828
"github.com/stretchr/testify/assert"
29+
30+
"github.com/elastic/beats/v7/libbeat/common"
2931
)
3032

3133
const allXML = `
@@ -79,9 +81,10 @@ const allXML = `
7981
func TestXML(t *testing.T) {
8082
allXMLTimeCreated, _ := time.Parse(time.RFC3339Nano, "2016-01-28T20:33:27.990735300Z")
8183

82-
var tests = []struct {
83-
xml string
84-
event Event
84+
tests := []struct {
85+
xml string
86+
event Event
87+
mapstr common.MapStr
8588
}{
8689
{
8790
xml: allXML,
@@ -150,6 +153,14 @@ func TestXML(t *testing.T) {
150153
},
151154
},
152155
},
156+
mapstr: common.MapStr{
157+
"event_id": "0",
158+
"time_created": time.Time{},
159+
"user_data": common.MapStr{
160+
"Id": "{00000000-0000-0000-0000-000000000000}",
161+
"xml_name": "Operation_ClientFailure",
162+
},
163+
},
153164
},
154165
}
155166

@@ -160,6 +171,9 @@ func TestXML(t *testing.T) {
160171
continue
161172
}
162173
assert.Equal(t, test.event, event)
174+
if test.mapstr != nil {
175+
assert.Equal(t, test.mapstr, event.Fields())
176+
}
163177

164178
if testing.Verbose() {
165179
json, err := json.MarshalIndent(event, "", " ")
@@ -174,7 +188,7 @@ func TestXML(t *testing.T) {
174188
// Tests that control characters other than CR and LF are escaped
175189
// when the event is decoded.
176190
func TestInvalidXML(t *testing.T) {
177-
evXML := strings.Replace(allXML, "%1", "\t
\n\x1b", -1)
191+
evXML := strings.ReplaceAll(allXML, "%1", "\t
\n\x1b")
178192
ev, err := UnmarshalXML([]byte(evXML))
179193
assert.Equal(t, nil, err)
180194
assert.Equal(t, "Creating WSMan shell on server with ResourceUri: \t\r\n\\u001b", ev.Message)

winlogbeat/sys/winevent/maputil.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package winevent
2020
import (
2121
"fmt"
2222
"reflect"
23+
"time"
2324

2425
"github.com/elastic/beats/v7/libbeat/common"
2526
"github.com/elastic/beats/v7/winlogbeat/sys"
@@ -80,25 +81,12 @@ func AddPairs(m common.MapStr, key string, pairs []KeyValue) common.MapStr {
8081

8182
// isZero return true if the given value is the zero value for its type.
8283
func isZero(i interface{}) bool {
83-
if i == nil {
84+
switch i := i.(type) {
85+
case nil:
8486
return true
87+
case time.Time:
88+
return false
89+
default:
90+
return reflect.ValueOf(i).IsZero()
8591
}
86-
87-
v := reflect.ValueOf(i)
88-
switch v.Kind() {
89-
case reflect.Array, reflect.String:
90-
return v.Len() == 0
91-
case reflect.Bool:
92-
return !v.Bool()
93-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
94-
return v.Int() == 0
95-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
96-
return v.Uint() == 0
97-
case reflect.Float32, reflect.Float64:
98-
return v.Float() == 0
99-
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
100-
return v.IsNil()
101-
}
102-
103-
return false
10492
}

0 commit comments

Comments
 (0)