-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mock.go
55 lines (46 loc) · 1.48 KB
/
mock.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.
package formatterimpl
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"go.uber.org/fx"
"github.com/DataDog/datadog-agent/comp/snmptraps/formatter"
"github.com/DataDog/datadog-agent/comp/snmptraps/packet"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
// MockModule provides a dummy formatter that just hashes packets.
func MockModule() fxutil.Module {
return fxutil.Component(
fx.Provide(newDummy),
)
}
// newDummy creates a new dummy formatter.
func newDummy() formatter.Component {
return &dummyFormatter{}
}
// dummyFormatter is a formatter that just hashes packets.
type dummyFormatter struct{}
// FormatPacket is a dummy formatter method that hashes an SnmpPacket object
func (f dummyFormatter) FormatPacket(packet *packet.SnmpPacket) ([]byte, error) {
var b bytes.Buffer
for _, err := range []error{
gob.NewEncoder(&b).Encode(packet.Addr),
gob.NewEncoder(&b).Encode(packet.Content.Community),
gob.NewEncoder(&b).Encode(packet.Content.SnmpTrap),
gob.NewEncoder(&b).Encode(packet.Content.Variables),
gob.NewEncoder(&b).Encode(packet.Content.Version),
} {
if err != nil {
return nil, err
}
}
h := sha256.New()
h.Write(b.Bytes())
hexHash := hex.EncodeToString(h.Sum(nil))
return []byte(hexHash), nil
}