snmp_v3_2042_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package adapters
  2. import (
  3. "slices"
  4. "testing"
  5. "github.com/gosnmp/gosnmp"
  6. )
  7. // Valores sintéticos de baja entropía: son passphrases de test, no
  8. // credenciales, y así no disparan el escáner de secretos del CI.
  9. const (
  10. testV3Auth = "aaaaaaaa"
  11. testV3Priv = "bbbbbbbb"
  12. )
  13. // #2042: el adapter traduce la configuración de la unidad al contrato del
  14. // poller. En v3 no debe arrastrar la comunidad y en v1/v2c no debe mandar
  15. // credenciales USM.
  16. func TestSnmpSessionConfigVersionMapping(t *testing.T) {
  17. cfg := RouterConfig{
  18. Host: "10.0.0.9", SnmpPort: 1161, SnmpCommunity: "public",
  19. SnmpVersion: "3", SnmpV3User: "monitor", SnmpV3SecurityLevel: "authPriv",
  20. SnmpV3AuthProto: "SHA256", SnmpV3AuthPass: testV3Auth,
  21. SnmpV3PrivProto: "AES256", SnmpV3PrivPass: testV3Priv,
  22. }
  23. got := snmpSessionConfig(cfg)
  24. if got.Version != gosnmp.Version3 {
  25. t.Fatalf("version = %v; want Version3", got.Version)
  26. }
  27. if got.Community != "" {
  28. t.Fatalf("community en v3 = %q; want vacía", got.Community)
  29. }
  30. if got.V3User != "monitor" || got.V3AuthProto != "SHA256" || got.V3PrivProto != "AES256" {
  31. t.Fatalf("credenciales v3 no propagadas: %+v", got)
  32. }
  33. if !slices.Equal([]string{got.V3AuthPass, got.V3PrivPass}, []string{testV3Auth, testV3Priv}) {
  34. t.Fatal("passphrases v3 no propagadas")
  35. }
  36. for _, v := range []string{"", "2c"} {
  37. cfg.SnmpVersion = v
  38. got = snmpSessionConfig(cfg)
  39. if got.Version != gosnmp.Version2c {
  40. t.Fatalf("%q → version %v; want Version2c", v, got.Version)
  41. }
  42. if got.Community != "public" {
  43. t.Fatalf("%q → community %q; want public", v, got.Community)
  44. }
  45. if got.V3User != "" || got.V3AuthPass != "" || got.V3PrivPass != "" {
  46. t.Fatalf("%q → credenciales v3 coladas: %+v", v, got)
  47. }
  48. }
  49. cfg.SnmpVersion = "1"
  50. if got = snmpSessionConfig(cfg); got.Version != gosnmp.Version1 {
  51. t.Fatalf("v1 → version %v", got.Version)
  52. }
  53. // El puerto y el host se pasan siempre.
  54. if got.Host != "10.0.0.9" || got.Port != 1161 {
  55. t.Fatalf("host/puerto = %q:%d", got.Host, got.Port)
  56. }
  57. }