|
|
@@ -1,30 +1,84 @@
|
|
|
package snmp
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"math/big"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
|
|
|
+ "github.com/gnacho/netpulse/server-go/internal/logonce"
|
|
|
"github.com/gosnmp/gosnmp"
|
|
|
)
|
|
|
|
|
|
// #1036: la MAC base del bridge se normaliza a MAYÚSCULAS (#960) o "" si
|
|
|
-// el dato no es usable (OID ausente, payload corto).
|
|
|
-func TestBridgeMacFromPDU(t *testing.T) {
|
|
|
- good := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: []byte{0x04, 0x95, 0xe6, 0x76, 0x55, 0xa1}}
|
|
|
- if got := bridgeMacFromPDU(good); got != "04:95:E6:76:55:A1" {
|
|
|
- t.Errorf("bytes: got %q", got)
|
|
|
+// el dato no es usable (OID ausente, vacío o longitud distinta de 6 octetos).
|
|
|
+func TestMacFromPDU(t *testing.T) {
|
|
|
+ good := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x01}}
|
|
|
+ if got := macFromPDU(good); got != "02:00:00:00:00:01" {
|
|
|
+ t.Errorf("bytes: obtuve %q, esperaba 02:00:00:00:00:01", got)
|
|
|
}
|
|
|
- str := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: string([]byte{0xa4, 0x7e, 0xfa, 0x65, 0x0c, 0xaa})}
|
|
|
- if got := bridgeMacFromPDU(str); got != "A4:7E:FA:65:0C:AA" {
|
|
|
- t.Errorf("string: got %q", got)
|
|
|
+ str := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: string([]byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x02})}
|
|
|
+ if got := macFromPDU(str); got != "02:00:00:00:00:02" {
|
|
|
+ t.Errorf("string: obtuve %q, esperaba 02:00:00:00:00:02", got)
|
|
|
}
|
|
|
short := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: []byte{0x01, 0x02}}
|
|
|
- if got := bridgeMacFromPDU(short); got != "" {
|
|
|
- t.Errorf("short: got %q, want vacío", got)
|
|
|
+ if got := macFromPDU(short); got != "" {
|
|
|
+ t.Errorf("PDU corta: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+ long := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x99}}
|
|
|
+ if got := macFromPDU(long); got != "" {
|
|
|
+ t.Errorf("PDU de 7 bytes: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+ empty := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.OctetString, Value: []byte{}}
|
|
|
+ if got := macFromPDU(empty); got != "" {
|
|
|
+ t.Errorf("PDU vacía: obtuve %q, esperaba vacío", got)
|
|
|
}
|
|
|
noObj := gosnmp.SnmpPDU{Name: OidDot1dBaseBridgeAddress, Type: gosnmp.NoSuchObject, Value: nil}
|
|
|
- if got := bridgeMacFromPDU(noObj); got != "" {
|
|
|
- t.Errorf("nosuchobject: got %q, want vacío", got)
|
|
|
+ if got := macFromPDU(noObj); got != "" {
|
|
|
+ t.Errorf("nosuchobject: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// TestMacFromPDUWarnsOnce verifica que una PDU de longitud distinta de 6 bytes
|
|
|
+// registra un único aviso con prefijo [netpulse:snmp] nombrando su OID y
|
|
|
+// longitud, que un segundo sondeo idéntico no repite el aviso y que una PDU
|
|
|
+// vacía se descarta en silencio.
|
|
|
+func TestMacFromPDUWarnsOnce(t *testing.T) {
|
|
|
+ var lines []string
|
|
|
+ old := snmpWarn
|
|
|
+ snmpWarn = logonce.NewWithLogf("[netpulse:snmp] ", func(f string, a ...any) {
|
|
|
+ lines = append(lines, fmt.Sprintf(f, a...))
|
|
|
+ })
|
|
|
+ t.Cleanup(func() { snmpWarn = old })
|
|
|
+
|
|
|
+ pdu := gosnmp.SnmpPDU{
|
|
|
+ Name: OidDot1dBaseBridgeAddress,
|
|
|
+ Type: gosnmp.OctetString,
|
|
|
+ Value: []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x99},
|
|
|
+ }
|
|
|
+ empty := gosnmp.SnmpPDU{
|
|
|
+ Name: OidDot1dBaseBridgeAddress,
|
|
|
+ Type: gosnmp.OctetString,
|
|
|
+ Value: []byte{},
|
|
|
+ }
|
|
|
+
|
|
|
+ if got := macFromPDU(empty); got != "" {
|
|
|
+ t.Fatalf("PDU vacía: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+ if got := macFromPDU(pdu); got != "" {
|
|
|
+ t.Fatalf("primera PDU de 7 bytes: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+ if got := macFromPDU(pdu); got != "" {
|
|
|
+ t.Fatalf("segunda PDU de 7 bytes: obtuve %q, esperaba vacío", got)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(lines) != 1 {
|
|
|
+ t.Fatalf("avisos = %d (%q), esperaba exactamente 1", len(lines), lines)
|
|
|
+ }
|
|
|
+ if !strings.HasPrefix(lines[0], "[netpulse:snmp] ") ||
|
|
|
+ !strings.Contains(lines[0], OidDot1dBaseBridgeAddress) ||
|
|
|
+ !strings.Contains(lines[0], "len=7") {
|
|
|
+ t.Fatalf("aviso inesperado: %q (esperaba prefijo [netpulse:snmp], OID %s y len=7)", lines[0], OidDot1dBaseBridgeAddress)
|
|
|
}
|
|
|
}
|
|
|
|