Пример #1
0
// Failed to process incoming message - Failed to Unmarshal PDU, cause `asn1: syntax error: zero length OBJECT IDENTIFIER`
func TestMessageV1Unmarshal1(t *testing.T) {
	buf := []byte{0x30, 0x82, 0x00, 0x89, 0x02, 0x01, 0x01, 0x04, 0x0a, 0x64, 0x7a, 0x67, 0x77,
		0x40, 0x6a, 0x78, 0x7a, 0x7a, 0x62, 0xa2, 0x78, 0x02, 0x03, 0x01, 0xaf, 0xa2, 0x02,
		0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x6b, 0x30, 0x1c, 0x06, 0x0f, 0x2b, 0x06, 0x01,
		0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x02, 0xa0, 0x87, 0x80, 0x0c, 0x04, 0x09,
		0x43, 0x61, 0x72, 0x64, 0x20, 0x73, 0x6c, 0x6f, 0x74, 0x30, 0x13, 0x06, 0x0f, 0x2b,
		0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x03, 0xa0, 0x87, 0x80, 0x0c,
		0x06, 0x00, 0x30, 0x14, 0x06, 0x0f, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01,
		0x01, 0x01, 0x05, 0xa0, 0x87, 0x80, 0x0c, 0x02, 0x01, 0x05, 0x30, 0x20, 0x06, 0x0f,
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x07, 0xa0, 0x87, 0x80,
		0x0c, 0x04, 0x0d, 0x43, 0x61, 0x72, 0x64, 0x20, 0x73, 0x6c, 0x6f, 0x74, 0x20, 0x30,
		0x2f, 0x37}

	expStr := `{"Version": "2c", "Community": "dzgw@jxzzb", "PDU": {"Type": "GetResponse", "RequestId": "110498", ` +
		`"ErrorStatus": "NoError", "ErrorIndex": "0", "VariableBindings": [{"Oid": "1.3.6.1.2.1.47.1.1.1.1.2.67223564", ` +
		`"Variable": {"Type": "octets", "Value": "4361726420736c6f74"}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.3.67223564", ` +
		`"Variable": {"Type": "oid", "Value": ""}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.5.67223564", "Variable": {"Type": "int", ` +
		`"Value": "5"}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.7.67223564", "Variable": {"Type": "octets", "Value": ` +
		`"4361726420736c6f7420302f37"}}]}}`

	pdu := snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetResponse)
	m := snmpclient2.NewMessage(snmpclient2.V2c, pdu)
	rest, err := m.Unmarshal(buf)
	if len(rest) != 0 || err != nil {
		t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err)
	}

	mp := snmpclient2.NewCommunity()

	if err = mp.ProcessIncomingMessage(nil, m); nil != err {
		t.Error(err)
		return
	}
	//pdu.Unmarshal()

	if expStr != m.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, m.String())
	}
}
Пример #2
0
// Failed to process incoming message - Failed to Unmarshal PDU, cause `asn1: structure error: integer not minimally-encoded`
func TestMessageV1Unmarshal2(t *testing.T) {
	buf := []byte{0x30, 0x7e, 0x02, 0x01, 0x01, 0x04, 0x0a, 0x64, 0x7a, 0x67, 0x77, 0x40,
		0x6a, 0x78, 0x7a, 0x7a, 0x62, 0xa2, 0x6d, 0x02, 0x03, 0x02, 0x14, 0x9e, 0x02, 0x01,
		0x00, 0x02, 0x01, 0x00, 0x30, 0x60, 0x30, 0x11, 0x06, 0x0c, 0x2b, 0x06, 0x01, 0x02,
		0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x06, 0x01, 0x00, 0x30, 0x11, 0x06,
		0x0c, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x04, 0x01, 0x02,
		0x01, 0x00, 0x30, 0x14, 0x06, 0x0c, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01,
		0x01, 0x01, 0x06, 0x01, 0x02, 0x04, 0xff, 0xff, 0xff, 0xff, 0x30, 0x10, 0x06, 0x0c,
		0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01, 0x08, 0x01, 0x04, 0x00,
		0x30, 0x10, 0x06, 0x0c, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x2f, 0x01, 0x01, 0x01, 0x01,
		0x0e, 0x01, 0x04, 0x00}

	expStr := `{"Version": "2c", "Community": "dzgw@jxzzb", "PDU": {"Type": "GetResponse", "RequestId": "136350",` +
		` "ErrorStatus": "NoError", "ErrorIndex": "0", "VariableBindings": [{"Oid": "1.3.6.1.2.1.47.1.1.1.1.3.1",` +
		` "Variable": {"Type": "oid", "Value": "0.0"}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.4.1", "Variable": {"Type": "int",` +
		` "Value": "0"}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.6.1", "Variable": {"Type": "int", "Value": "-1"}}, {"Oid":` +
		` "1.3.6.1.2.1.47.1.1.1.1.8.1", "Variable": {"Type": "octets", "Value": ""}}, {"Oid": "1.3.6.1.2.1.47.1.1.1.1.14.1",` +
		` "Variable": {"Type": "octets", "Value": ""}}]}}`

	pdu := snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetResponse)
	m := snmpclient2.NewMessage(snmpclient2.V2c, pdu)
	rest, err := m.Unmarshal(buf)
	if len(rest) != 0 || err != nil {
		t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err)
	}

	mp := snmpclient2.NewCommunity()

	if err = mp.ProcessIncomingMessage(nil, m); nil != err {
		t.Error(err)
		return
	}
	//pdu.Unmarshal()

	if expStr != m.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, m.String())
	}
}
Пример #3
0
func TestCommunity(t *testing.T) {
	expCom := "public"
	snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1", snmpclient2.Arguments{
		Version:   snmpclient2.V2c,
		Community: expCom,
	})
	sec := snmpclient2.NewCommunity()
	pdu := snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetRequest)
	smsg := snmpclient2.NewMessage(snmpclient2.V2c, pdu).(*snmpclient2.MessageV1)

	err := sec.GenerateRequestMessage(snmpclient2.GetArgs(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 = snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetResponse)
	rmsg := snmpclient2.NewMessage(snmpclient2.V2c, pdu).(*snmpclient2.MessageV1)

	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err == nil {
		t.Error("ProcessIncomingMessage() - community check")
	}

	rmsg.Community = []byte(expCom)
	rmsg.SetPduBytes(smsg.PduBytes())
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err != nil {
		t.Errorf("ProcessIncomingMessage() - has error %v", err)
	}
}