Esempio n. 1
0
func TestMessageProcessingV1(t *testing.T) {
	snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1", snmpclient2.Arguments{
		Version:   snmpclient2.V2c,
		Community: "public",
	})
	mp := snmpclient2.NewMessageProcessing(snmpclient2.V2c)
	pdu := snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.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 = snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetResponse)
	rmsg := snmpclient2.NewMessage(snmpclient2.V2c, pdu).(*snmpclient2.MessageV1)
	b, _ = rmsg.Marshal()
	_, err = mp.PrepareDataElements(snmp, msg, b)
	if err == nil {
		t.Error("PrepareDataElements() - version check")
	}

	pdu.SetRequestId(requestId)
	pduBytes, _ := pdu.Marshal()
	rmsg = snmpclient2.NewMessage(snmpclient2.V2c, pdu).(*snmpclient2.MessageV1)
	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)
	}
}
Esempio n. 2
0
func TestNewPdu(t *testing.T) {
	pdu := snmpclient2.NewPdu(snmpclient2.V1, snmpclient2.GetRequest)
	if _, ok := pdu.(*snmpclient2.PduV1); !ok {
		t.Errorf("NewPdu() Invalid PDU")
	}

	pdu = snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetRequest)
	if _, ok := pdu.(*snmpclient2.PduV1); !ok {
		t.Errorf("NewPdu() Invalid PDU")
	}

	pdu = snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetRequest)
	if _, ok := pdu.(*snmpclient2.ScopedPdu); !ok {
		t.Errorf("NewPdu() Invalid PDU")
	}
}
Esempio n. 3
0
func TestMessageV1(t *testing.T) {
	pdu := snmpclient2.NewPdu(snmpclient2.V2c, snmpclient2.GetRequest)
	msg := snmpclient2.NewMessage(snmpclient2.V2c, pdu).(*snmpclient2.MessageV1)
	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]",
			snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " "))
	}

	expStr := `{"Version": "2c", "Community": "MyCommunity", ` +
		`"PDU": {"Type": "GetRequest", "RequestId": "0", "ErrorStatus": ` +
		`"NoError", "ErrorIndex": "0", "VariableBindings": []}}`
	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)
	}
	if expStr != m.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, m.String())
	}
}
Esempio n. 4
0
func TestMessageV3(t *testing.T) {
	pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetRequest)
	msg := snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	b, _ := pdu.Marshal()
	msg.SetPduBytes(b)
	msg.MessageId = 123
	msg.MessageMaxSize = 321
	msg.SetReportable(true)
	msg.SetPrivacy(true)
	msg.SetAuthentication(true)
	msg.SecurityModel = 3
	msg.AuthEngineId = []byte{0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
	msg.AuthEngineBoots = 456
	msg.AuthEngineTime = 654
	msg.UserName = []byte("User")
	msg.AuthParameter = []byte{0xaa, 0xbb, 0xcc}
	msg.PrivParameter = []byte{0xdd, 0xee, 0xff}

	expBuf := []byte{
		0x30, 0x4b, 0x02, 0x01, 0x03, 0x30, 0x0d, 0x02, 0x01, 0x7b,
		0x02, 0x02, 0x01, 0x41, 0x04, 0x01, 0x07, 0x02, 0x01, 0x03,
		0x04, 0x24, 0x30, 0x22, 0x04, 0x08, 0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x02, 0x02, 0x01, 0xc8, 0x02, 0x02, 0x02, 0x8e, 0x04, 0x04, 0x55, 0x73, 0x65, 0x72,
		0x04, 0x03, 0xaa, 0xbb, 0xcc, 0x04, 0x03, 0xdd, 0xee, 0xff,
		0x30, 0x11, 0x04, 0x00, 0x04, 0x00, 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]",
			snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " "))
	}

	expStr := `{"Version": "3", "GlobalData": {"MessageId": "123", "MessageMaxSize": "321", ` +
		`"MessageFlags": "apr", "SecurityModel": "USM"}, "SecurityParameter": ` +
		`{"AuthEngineId": "8001020304050607", "AuthEngineBoots": "456", ` +
		`"AuthEngineTime": "654", "UserName": "******", "AuthParameter": "aa:bb:cc", ` +
		`"PrivParameter": "dd:ee:ff"}, "PDU": {"Type": "GetRequest", "RequestId": "0", ` +
		`"ErrorStatus": "NoError", "ErrorIndex": "0", "ContextEngineId": "", ` +
		`"ContextName": "", "VariableBindings": []}}`
	m := snmpclient2.NewMessage(snmpclient2.V3, 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())
	}
}
Esempio n. 5
0
func TestScopedPdu(t *testing.T) {
	pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetRequest)
	pdu.SetRequestId(123)
	pdu.SetErrorStatus(snmpclient2.TooBig)
	pdu.SetErrorIndex(2)

	sp := pdu.(*snmpclient2.ScopedPdu)
	sp.ContextEngineId = []byte{0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}
	sp.ContextName = []byte("MyContext")

	oid, _ := snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.0")
	pdu.AppendVariableBinding(oid, snmpclient2.NewOctetString([]byte("MyHost")))
	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.2.0")
	pdu.AppendVariableBinding(oid, snmpclient2.NewNull())
	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.3.0")
	pdu.AppendVariableBinding(oid, snmpclient2.NewTimeTicks(uint32(11111)))

	expBuf := []byte{
		0x30, 0x54, 0x04, 0x08, 0x80, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x04, 0x09, 0x4d, 0x79, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74,
		0xa0, 0x3d, 0x02, 0x01, 0x7b, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02,
		0x30, 0x32, 0x30, 0x12, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01,
		0x01, 0x01, 0x00, 0x04, 0x06, 0x4d, 0x79, 0x48, 0x6f, 0x73, 0x74,
		0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x02,
		0x00, 0x05, 0x00, 0x30, 0x0e, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02,
		0x01, 0x01, 0x03, 0x00, 0x43, 0x02, 0x2b, 0x67,
	}
	buf, err := pdu.Marshal()
	if err != nil {
		t.Fatal("Marshal() : %v", err)
	}
	if !bytes.Equal(expBuf, buf) {
		t.Errorf("Marshal() - expected [%s], actual [%s]",
			snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " "))
	}

	expStr := `{"Type": "GetRequest", "RequestId": "123", ` +
		`"ErrorStatus": "TooBig", "ErrorIndex": "2", ` +
		`"ContextEngineId": "8001020304050607", "ContextName": "MyContext", ` +
		`"VariableBindings": [` +
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "octets", "Value": "4d79486f7374"}}, ` +
		`{"Oid": "1.3.6.1.2.1.1.2.0", "Variable": {"Type": "null", "Value": ""}}, ` +
		`{"Oid": "1.3.6.1.2.1.1.3.0", "Variable": {"Type": "timeticks", "Value": "11111"}}]}`
	var w snmpclient2.ScopedPdu
	rest, err := (&w).Unmarshal(buf)
	if len(rest) != 0 || err != nil {
		t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err)
	}
	if expStr != w.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.String())
	}
}
Esempio n. 6
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)
	}
}
Esempio n. 7
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())
	}
}
Esempio n. 8
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())
	}
}
Esempio n. 9
0
func TestSNMP(t *testing.T) {
	snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1", snmpclient2.Arguments{
		Version:       snmpclient2.V3,
		UserName:      "******",
		SecurityLevel: snmpclient2.AuthPriv,
		AuthPassword:  "******",
		AuthProtocol:  snmpclient2.Md5,
		PrivPassword:  "******",
		PrivProtocol:  snmpclient2.Des,
	})

	pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.Report)
	err := snmpclient2.SnmpCheckPdu(snmp, pdu)
	if err != nil {
		t.Errorf("checkPdu() - has error %v", err)
	}

	oids, _ := snmpclient2.NewOids([]string{"1.3.6.1.6.3.11.2.1.1.0"})
	pdu = snmpclient2.NewPduWithOids(snmpclient2.V3, snmpclient2.Report, oids)
	err = snmpclient2.SnmpCheckPdu(snmp, pdu)
	if err == nil {
		t.Error("checkPdu() - report oid")
	}
}
Esempio n. 10
0
func aTestUsm(t *testing.T) {
	expUser := []byte("myUser")
	expEngId := []byte{0x80, 0x00, 0x00, 0x00, 0x01}
	expCtxId := []byte{0x80, 0x00, 0x00, 0x00, 0x05}
	expCtxName := "myName"
	snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1",
		snmpclient2.Arguments{
			Version:         snmpclient2.V3,
			UserName:        string(expUser),
			SecurityLevel:   snmpclient2.AuthPriv,
			AuthPassword:    "******",
			AuthProtocol:    snmpclient2.Md5,
			PrivPassword:    "******",
			PrivProtocol:    snmpclient2.Des,
			ContextEngineId: hex.EncodeToString(expCtxId),
			ContextName:     expCtxName,
		})
	sec := snmpclient2.NewUsm().(*snmpclient2.USM)
	pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetRequest)
	spdu := pdu.(*snmpclient2.ScopedPdu)
	smsg := snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	smsg.SetAuthentication(false)
	smsg.SetPrivacy(false)

	// Discovery
	err := sec.GenerateRequestMessage(snmpclient2.GetArgs(snmp), smsg)
	if err != nil {
		t.Errorf("GenerateRequestMessage() - has error %v", err)
	}
	if !bytes.Equal(spdu.ContextEngineId, expCtxId) {
		t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]",
			expCtxId, spdu.ContextEngineId)
	}
	if string(spdu.ContextName) != expCtxName {
		t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]",
			expCtxName, string(spdu.ContextName))
	}
	if len(smsg.PduBytes()) == 0 {
		t.Error("GenerateRequestMessage() - pdu marshal")
	}

	pdu = snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.Report)
	rmsg := snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	rmsg.SetPduBytes(smsg.PduBytes())
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err == nil {
		t.Error("ProcessIncomingMessage() - engineId check")
	}

	rmsg.AuthEngineId = expEngId
	rmsg.AuthEngineBoots = -1
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err == nil {
		t.Error("ProcessIncomingMessage() - boots check")
	}

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

	rmsg.AuthEngineTime = 1
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err != nil {
		t.Errorf("ProcessIncomingMessage() - has error %v", err)
	}
	if !bytes.Equal(sec.AuthEngineId, expEngId) {
		t.Errorf("ProcessIncomingMessage() - expected [%s], actual [%s]",
			sec.AuthEngineId, expEngId)
	}
	// if len(sec.AuthKey) == 0 {
	// 	t.Error("ProcessIncomingMessage() - authKey")
	// }
	// if len(sec.PrivKey) == 0 {
	// 	t.Error("ProcessIncomingMessage() - privKey")
	// }

	// Synchronize
	smsg.SetAuthentication(true)
	smsg.SetPrivacy(true)

	err = sec.GenerateRequestMessage(snmpclient2.GetArgs(snmp), smsg)
	if err != nil {
		t.Errorf("GenerateRequestMessage() - has error %v", err)
	}
	if !bytes.Equal(smsg.UserName, expUser) {
		t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]",
			expUser, smsg.UserName)
	}
	if !bytes.Equal(smsg.AuthEngineId, expEngId) {
		t.Errorf("GenerateRequestMessage() - expected [%s], actual [%s]",
			expEngId, smsg.AuthEngineId)
	}
	if len(smsg.PrivParameter) == 0 {
		t.Error("GenerateRequestMessage() - privParameter")
	}
	if len(smsg.AuthParameter) == 0 {
		t.Error("GenerateRequestMessage() - authParameter")
	}

	pdu = snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.Report)
	rmsg = snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	rmsg.SetAuthentication(true)
	rmsg.SetPrivacy(true)
	rmsg.SetPduBytes(smsg.PduBytes())
	rmsg.AuthEngineId = []byte("foobar")
	rmsg.AuthEngineBoots = smsg.AuthEngineBoots
	rmsg.AuthEngineTime = smsg.AuthEngineTime
	rmsg.PrivParameter = smsg.PrivParameter
	rmsg.AuthParameter = smsg.AuthParameter

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

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

	rmsg.AuthEngineId = expEngId
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err != nil {
		t.Errorf("ProcessIncomingMessage() - has error %v", err)
	}
	if sec.AuthEngineBoots != rmsg.AuthEngineBoots {
		t.Error("ProcessIncomingMessage() - engineBoots")
	}
	if sec.AuthEngineTime != rmsg.AuthEngineTime {
		t.Error("ProcessIncomingMessage() - engineTime")
	}

	// Request
	sec.AuthEngineBoots = 1
	sec.AuthEngineTime = 1

	err = sec.GenerateRequestMessage(snmpclient2.GetArgs(snmp), smsg)
	if err != nil {
		t.Errorf("GenerateRequestMessage() - has error %v", err)
	}
	if smsg.AuthEngineBoots != sec.AuthEngineBoots {
		t.Errorf("GenerateRequestMessage() - expected [%d], actual [%d]",
			sec.AuthEngineBoots, smsg.AuthEngineBoots)
	}
	if smsg.AuthEngineTime != sec.AuthEngineTime {
		t.Errorf("GenerateRequestMessage() - expected [%d], actual [%d]",
			sec.AuthEngineTime, smsg.AuthEngineTime)
	}

	pdu = snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetResponse)
	spdu = pdu.(*snmpclient2.ScopedPdu)
	rmsg = snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	rmsg.AuthEngineId = expEngId
	rmsg.AuthEngineBoots = smsg.AuthEngineBoots
	rmsg.AuthEngineTime = smsg.AuthEngineTime
	rmsg.UserName = expUser

	// set PduBytes with GetResponse
	b, _ := spdu.Marshal()
	rmsg.SetPduBytes(b)

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

	// set PduBytes with ContextEngineId
	spdu.ContextEngineId = expCtxId
	b, _ = spdu.Marshal()
	rmsg.SetPduBytes(b)
	err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	if err == nil {
		t.Error("ProcessIncomingMessage() - contextName check")
	}

	// // set PduBytes with ContextName
	// spdu.ContextName = []byte(expCtxName)
	// b, _ = spdu.Marshal()
	// rmsg.SetPduBytes(b)
	// err = sec.ProcessIncomingMessage(snmpclient2.GetArgs(snmp), rmsg)
	// if err == nil {
	// 	t.Error("ProcessIncomingMessage() - response authenticate check")
	// }
}
Esempio n. 11
0
func TestMessageProcessingV3(t *testing.T) {
	snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1", snmpclient2.Arguments{
		Version:       snmpclient2.V3,
		UserName:      "******",
		SecurityLevel: snmpclient2.AuthPriv,
		AuthPassword:  "******",
		AuthProtocol:  snmpclient2.Md5,
		PrivPassword:  "******",
		PrivProtocol:  snmpclient2.Des,
	})
	var mss snmpclient2.Message = &snmpclient2.MessageV1{}
	t.Log(mss.String())
	mp := snmpclient2.NewMessageProcessing(snmpclient2.V3)
	//usm := mp.Security().(*snmpclient2.USM)
	//usm.AuthKey = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	//usm.PrivKey = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.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")
	}
	msgv3 := msg.(*snmpclient2.MessageV3)
	if msgv3.MessageId == 0 {
		t.Error("PrepareOutgoingMessage() - message id")
	}
	if !msgv3.Reportable() || !msgv3.Authentication() || !msgv3.Privacy() {
		t.Error("PrepareOutgoingMessage() - security flag")
	}
	msgv3.SetAuthentication(false)
	msgv3.SetPrivacy(false)
	msgv3.AuthEngineId = []byte{0, 0, 0, 0, 0}
	requestId := pdu.RequestId()
	messageId := msgv3.MessageId

	_, 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 = snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.GetResponse)
	rmsg := snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	b, _ = rmsg.Marshal()
	_, err = mp.PrepareDataElements(snmp, msg, b)
	if err == nil {
		t.Error("PrepareDataElements() - message id check")
	}

	rmsg = snmpclient2.NewMessage(snmpclient2.V3, pdu).(*snmpclient2.MessageV3)
	rmsg.AuthEngineId = []byte{0, 0, 0, 0, 0}
	rmsg.MessageId = messageId
	b, _ = rmsg.Marshal()
	_, err = mp.PrepareDataElements(snmp, msg, b)
	if err == nil {
		t.Error("PrepareDataElements() - security model check")
	}

	pdu.(*snmpclient2.ScopedPdu).ContextEngineId = rmsg.AuthEngineId
	pduBytes, _ := pdu.Marshal()
	rmsg.SetPduBytes(pduBytes)
	rmsg.SecurityModel = 3
	b, _ = rmsg.Marshal()
	_, err = mp.PrepareDataElements(snmp, msg, b)
	if err == nil {
		t.Error("PrepareDataElements() - request id check")
	}

	pdu.SetRequestId(requestId)
	pduBytes, _ = pdu.Marshal()
	rmsg.SetPduBytes(pduBytes)
	rmsg.UserName = []byte(snmpclient2.GetArgs(snmp).UserName)
	b, _ = rmsg.Marshal()
	_, err = mp.PrepareDataElements(snmp, msg, b)
	if err != nil {
		t.Errorf("PrepareDataElements() - has error %v", err)
	}
}