Skip to content

Commit

Permalink
A test for GOSNMP Trap.
Browse files Browse the repository at this point in the history
  • Loading branch information
slayercat committed Feb 25, 2020
1 parent 6484c24 commit 853ae87
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
14 changes: 8 additions & 6 deletions agentcontrol.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package GoSNMPServer

import "time"
import "reflect"
import "github.com/shirou/gopsutil/host"
import (
"reflect"
"time"

import "github.com/slayercat/gosnmp"
import "github.com/pkg/errors"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/host"
"github.com/slayercat/gosnmp"
)

type FuncGetAuthoritativeEngineTime func() uint32

Expand Down Expand Up @@ -258,7 +260,7 @@ func (t *MasterAgent) SyncConfig() error {
return err
}

if len(current.CommunityIDs) == 0 {
if len(current.CommunityIDs) == 0 || t.SecurityConfig.NoSecurity {
if t.priv.defaultSubAgent != nil {
return errors.Errorf("SyncConfig: Config Error: duplicate default agent")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/prometheus/procfs v0.0.8
github.com/shirou/gopsutil v2.19.12+incompatible
github.com/sirupsen/logrus v1.4.2
github.com/slayercat/gosnmp v1.24.0
github.com/slayercat/gosnmp v1.24.1-0.20200225080903-29819b7774e5
github.com/stretchr/testify v1.4.1-0.20200129000828-ea72eb91592e
github.com/urfave/cli/v2 v2.1.1
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ github.com/slayercat/gosnmp v1.22.1-0.20200216031804-670503dd47da h1:9MWCKnSjrQm
github.com/slayercat/gosnmp v1.22.1-0.20200216031804-670503dd47da/go.mod h1:tB/loGhNgiSxOZzz02TRKKhUPUzDrGrkYQSXxD3KfqM=
github.com/slayercat/gosnmp v1.24.0 h1:VpnKTvW1BqG/rvQBf/I1y2t8HEnhjnF7pP7tBvz3TDk=
github.com/slayercat/gosnmp v1.24.0/go.mod h1:tB/loGhNgiSxOZzz02TRKKhUPUzDrGrkYQSXxD3KfqM=
github.com/slayercat/gosnmp v1.24.1-0.20200225080903-29819b7774e5 h1:Zq1YuJvWWpmrHqdIBK95DFgI7+pZXL/zejuLxRljL2I=
github.com/slayercat/gosnmp v1.24.1-0.20200225080903-29819b7774e5/go.mod h1:tB/loGhNgiSxOZzz02TRKKhUPUzDrGrkYQSXxD3KfqM=
github.com/soniah/gosnmp v1.22.0 h1:jVJi8+OGvR+JHIaZKMmnyNP0akJd2vEgNatybwhZvxg=
github.com/soniah/gosnmp v1.22.0/go.mod h1:DuEpAS0az51+DyVBQwITDsoq4++e3LTNckp2GoasF2I=
github.com/soniah/gosnmp v1.23.0/go.mod h1:DuEpAS0az51+DyVBQwITDsoq4++e3LTNckp2GoasF2I=
github.com/soniah/gosnmp v1.24.0/go.mod h1:DuEpAS0az51+DyVBQwITDsoq4++e3LTNckp2GoasF2I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
51 changes: 48 additions & 3 deletions snmpserverTraps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"os/exec"
"testing"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -30,6 +31,7 @@ func (suite *TrapTests) TestTraps() {
Logger: suite.Logger,
SecurityConfig: SecurityConfig{
AuthoritativeEngineBoots: 1,
NoSecurity: true,
Users: []gosnmp.UsmSecurityParameters{
{
UserName: "user",
Expand Down Expand Up @@ -67,7 +69,7 @@ func (suite *TrapTests) TestTraps() {
},
}
shandle := NewSNMPServer(master)
shandle.ListenUDP("udp4", ":11611")
shandle.ListenUDP("udp4", ":0")
var stopWaitChain = make(chan int)
go func() {
err := shandle.ServeForever()
Expand Down Expand Up @@ -115,7 +117,7 @@ func (suite *TrapTests) TestTraps() {
assert.Equal(suite.T(), "", string(result))
})
suite.Run("Trapv3OctetString", func() {
result, err := getCmdOutput("snmptrap", "-v3", "-n", "public",
result, err := getCmdOutput("snmpinform", "-v3", "-n", "public",
"-l", "authPriv", "-u", "user",
"-a", "SHA", "-A", "password",
"-x", "AES", "-X", "password",
Expand All @@ -129,7 +131,50 @@ func (suite *TrapTests) TestTraps() {
data := Asn1OctetStringUnwrap(trapDataReceived.Value)
assert.Equal(suite.T(), "1.2.3.13", data)
})
suite.Run("Trap V3", func() {

// refer: https://github.com/soniah/gosnmp/issues/145
client := &gosnmp.GoSNMP{
Target: "127.0.0.1",
Port: uint16(serverAddress.Port),
Version: gosnmp.Version3,
Timeout: time.Duration(30) * time.Second,
SecurityModel: gosnmp.UserSecurityModel,
MsgFlags: gosnmp.AuthPriv,
//ContextName: "public", //MUST have
Logger: &SnmpLoggerAdapter{suite.Logger},
SecurityParameters: &gosnmp.UsmSecurityParameters{
UserName: "user",
AuthenticationProtocol: gosnmp.SHA,
AuthenticationPassphrase: "password",
PrivacyProtocol: gosnmp.AES,
PrivacyPassphrase: "password",
Logger: &SnmpLoggerAdapter{suite.Logger},
},
}

if err := client.Connect(); err != nil {
panic(err)
}
defer client.Conn.Close()

trap := gosnmp.SnmpTrap{
Variables: []gosnmp.SnmpPDU{
gosnmp.SnmpPDU{
Name: ".1.2.4.1",
Type: gosnmp.OctetString,
Value: ".1.3.6.1.6.3.1.1.5.1",
},
},
}

if _, err := client.SendTrap(trap); err != nil {
panic(err)
}
_ = <-waiterReadyToWork
data := Asn1OctetStringUnwrap(trapDataReceived.Value)
assert.Equal(suite.T(), ".1.3.6.1.6.3.1.1.5.1", data)
})
shandle.Shutdown()
}

Expand Down Expand Up @@ -182,7 +227,7 @@ func (suite *TrapTests) TestErrorTraps() {
},
}
shandle := NewSNMPServer(master)
shandle.ListenUDP("udp4", ":11611")
shandle.ListenUDP("udp4", ":0")
var stopWaitChain = make(chan int)
go func() {
err := shandle.ServeForever()
Expand Down

0 comments on commit 853ae87

Please sign in to comment.