func TestMessageProcessingV1(t *testing.T) { snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V2c, Community: "public", }) mp := snmpgo.NewMessageProcessing(snmpgo.V2c) pdu := snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetRequest) msg, err := mp.PrepareOutgoingMessage(snmp, pdu) if err != nil { t.Errorf("PrepareOutgoingMessage() - has error %v", err) } if len(msg.PduBytes()) == 0 { t.Error("PrepareOutgoingMessage() - pdu bytes") } if pdu.RequestId() == 0 { t.Error("PrepareOutgoingMessage() - request id") } requestId := pdu.RequestId() _, err = mp.PrepareDataElements(snmp, msg, []byte{0x00, 0x00}) if err == nil { t.Error("PrepareDataElements() - message unmarshal error") } b, _ := msg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - pdu type check") } pdu = snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetResponse) rmsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V1, pdu)) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err == nil { t.Error("PrepareDataElements() - version check") } pdu.SetRequestId(requestId) pduBytes, _ := pdu.Marshal() rmsg = snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) rmsg.Community = []byte("public") rmsg.SetPduBytes(pduBytes) b, _ = rmsg.Marshal() _, err = mp.PrepareDataElements(snmp, msg, b) if err != nil { t.Errorf("PrepareDataElements() - has error %v", err) } }
func TestMessageV1(t *testing.T) { pdu := snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetRequest) msg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) b, _ := pdu.Marshal() msg.SetPduBytes(b) msg.Community = []byte("MyCommunity") expBuf := []byte{ 0x30, 0x1d, 0x02, 0x01, 0x01, 0x04, 0x0b, 0x4d, 0x79, 0x43, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x74, 0x79, 0xa0, 0x0b, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x00, } buf, err := msg.Marshal() if err != nil { t.Fatal("Marshal() : %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpgo.ToHexStr(expBuf, " "), snmpgo.ToHexStr(buf, " ")) } expStr := `{"Version": "2c", "Community": "MyCommunity", ` + `"Pdu": {"Type": "GetRequest", "RequestId": "0", "ErrorStatus": ` + `"NoError", "ErrorIndex": "0", "VarBinds": []}}` m := snmpgo.NewMessage(snmpgo.V2c, pdu) rest, err := m.Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != m.String() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, m.String()) } }
func TestCommunity(t *testing.T) { expCom := "public" snmp, _ := snmpgo.NewSNMP(snmpgo.SNMPArguments{ Version: snmpgo.V2c, Community: expCom, }) sec := snmpgo.NewCommunity() pdu := snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetRequest) smsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) err := sec.GenerateRequestMessage(snmp, smsg) if err != nil { t.Errorf("GenerateRequestMessage() - has error %v", err) } if !bytes.Equal(smsg.Community, []byte(expCom)) { t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]", expCom, smsg.Community) } if len(smsg.PduBytes()) == 0 { t.Error("GenerateRequestMessage() - pdu marshal") } pdu = snmpgo.NewPdu(snmpgo.V2c, snmpgo.GetResponse) rmsg := snmpgo.ToMessageV1(snmpgo.NewMessage(snmpgo.V2c, pdu)) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err == nil { t.Error("ProcessIncomingMessage() - community check") } rmsg.Community = []byte(expCom) rmsg.SetPduBytes(smsg.PduBytes()) err = sec.ProcessIncomingMessage(snmp, smsg, rmsg) if err != nil { t.Errorf("ProcessIncomingMessage() - has error %v", err) } }