github.com/Serizao/go-winio@v0.0.0-20230906082528-f02f7f4ad6e8/pkg/etwlogrus/hook_test.go (about) 1 //go:build windows 2 // +build windows 3 4 package etwlogrus 5 6 import ( 7 "testing" 8 9 "github.com/sirupsen/logrus" 10 ) 11 12 func fireEvent(name string, value interface{}) { 13 logrus.WithField("Field", value).Info(name) 14 } 15 16 // The purpose of this test is to log lots of different field types, to test the 17 // logic that converts them to ETW. Because we don't have a way to 18 // programatically validate the ETW events, this test has two main purposes: (1) 19 // validate nothing causes a panic while logging (2) allow manual validation that 20 // the data is logged correctly (through a tool like WPA). 21 func TestFieldLogging(t *testing.T) { 22 // Sample WPRP to collect this provider is included in HookTest.wprp. 23 // 24 // Start collection: 25 // wpr -start HookTest.wprp -filemode 26 // 27 // Stop collection: 28 // wpr -stop HookTest.etl 29 h, err := NewHook("HookTest") 30 if err != nil { 31 t.Fatal(err) 32 } 33 logrus.AddHook(h) 34 35 fireEvent("Bool", true) 36 fireEvent("BoolSlice", []bool{true, false, true}) 37 fireEvent("EmptyBoolSlice", []bool{}) 38 fireEvent("String", "teststring") 39 fireEvent("StringSlice", []string{"sstr1", "sstr2", "sstr3"}) 40 fireEvent("EmptyStringSlice", []string{}) 41 fireEvent("Int", int(1)) 42 fireEvent("IntSlice", []int{2, 3, 4}) 43 fireEvent("EmptyIntSlice", []int{}) 44 fireEvent("Int8", int8(5)) 45 fireEvent("Int8Slice", []int8{6, 7, 8}) 46 fireEvent("EmptyInt8Slice", []int8{}) 47 fireEvent("Int16", int16(9)) 48 fireEvent("Int16Slice", []int16{10, 11, 12}) 49 fireEvent("EmptyInt16Slice", []int16{}) 50 fireEvent("Int32", int32(13)) 51 fireEvent("Int32Slice", []int32{14, 15, 16}) 52 fireEvent("EmptyInt32Slice", []int32{}) 53 fireEvent("Int64", int64(17)) 54 fireEvent("Int64Slice", []int64{18, 19, 20}) 55 fireEvent("EmptyInt64Slice", []int64{}) 56 fireEvent("Uint", uint(21)) 57 fireEvent("UintSlice", []uint{22, 23, 24}) 58 fireEvent("EmptyUintSlice", []uint{}) 59 fireEvent("Uint8", uint8(25)) 60 fireEvent("Uint8Slice", []uint8{26, 27, 28}) 61 fireEvent("EmptyUint8Slice", []uint8{}) 62 fireEvent("Uint16", uint16(29)) 63 fireEvent("Uint16Slice", []uint16{30, 31, 32}) 64 fireEvent("EmptyUint16Slice", []uint16{}) 65 fireEvent("Uint32", uint32(33)) 66 fireEvent("Uint32Slice", []uint32{34, 35, 36}) 67 fireEvent("EmptyUint32Slice", []uint32{}) 68 fireEvent("Uint64", uint64(37)) 69 fireEvent("Uint64Slice", []uint64{38, 39, 40}) 70 fireEvent("EmptyUint64Slice", []uint64{}) 71 fireEvent("Uintptr", uintptr(41)) 72 fireEvent("UintptrSlice", []uintptr{42, 43, 44}) 73 fireEvent("EmptyUintptrSlice", []uintptr{}) 74 fireEvent("Float32", float32(45.46)) 75 fireEvent("Float32Slice", []float32{47.48, 49.50, 51.52}) 76 fireEvent("EmptyFloat32Slice", []float32{}) 77 fireEvent("Float64", float64(53.54)) 78 fireEvent("Float64Slice", []float64{55.56, 57.58, 59.60}) 79 fireEvent("EmptyFloat64Slice", []float64{}) 80 81 type struct1 struct { 82 A float32 83 priv int 84 B []uint 85 } 86 type struct2 struct { 87 A int 88 B int 89 } 90 type struct3 struct { 91 struct2 92 A int 93 B string 94 priv string 95 C struct1 96 D uint16 97 } 98 // Unexported fields, and fields in embedded structs, should not log. 99 fireEvent("Struct", struct3{struct2{-1, -2}, 1, "2s", "-3s", struct1{3.4, -4, []uint{5, 6, 7}}, 8}) 100 }