func TestOid(t *testing.T) { expStr := "1.3.6.1.2.1.1.1.0" expBuf := []byte{0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00} var v snmpclient2.Oid v, err := snmpclient2.ParseOidFromString(expStr) if err != nil { t.Errorf("NewOid : %v", err) } exceptedError(t, &v) if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual[%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.Oid rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } oid := snmpclient2.Oid{Value: []int{1, 3, 6, 1, 4, 1, 9, 10, 138, 1, 4, 1, 2, 1, -1073741823}} exceptedOid := "1.3.6.1.4.1.9.10.138.1.4.1.2.1.3221225473" if exceptedOid != oid.ToString() { t.Error("excepted is ", exceptedOid, "actual is", oid.ToString()) } bs, e := oid.Marshal() if nil != e { t.Error(e) } exceptedBytes := []byte{6, 19, 43, 6, 1, 4, 1, 9, 10, 129, 10, 1, 4, 1, 2, 1, 140, 128, 128, 128, 1} if !bytes.Equal(bs, exceptedBytes) { t.Error("excepted is ", exceptedBytes, "actual is", bs) } }
// RFC3414 A.3 func TestPasswordToKey(t *testing.T) { password := "******" engineId := []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, } expBuf := []byte{ 0x52, 0x6f, 0x5e, 0xed, 0x9f, 0xcc, 0xe2, 0x6f, 0x89, 0x64, 0xc2, 0x93, 0x07, 0x87, 0xd8, 0x2b, } key := snmpclient2.PasswordToKey(snmpclient2.Md5, password, engineId) if !bytes.Equal(expBuf, key) { t.Errorf("PasswordToKey(Md5) - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(key, " ")) } expBuf = []byte{ 0x66, 0x95, 0xfe, 0xbc, 0x92, 0x88, 0xe3, 0x62, 0x82, 0x23, 0x5f, 0xc7, 0x15, 0x1f, 0x12, 0x84, 0x97, 0xb3, 0x8f, 0x3f, } key = snmpclient2.PasswordToKey(snmpclient2.Sha, password, engineId) if !bytes.Equal(expBuf, key) { t.Errorf("PasswordToKey(Aes) - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(key, " ")) } }
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()) } }
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()) } }
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()) } }
func TestOctetString(t *testing.T) { expStr := "Test" expBuf := []byte{0x04, 0x04, 0x54, 0x65, 0x73, 0x74} var v snmpclient2.Variable = snmpclient2.NewOctetString([]byte(expStr)) //_, err := v.BigInt() //if err == nil { // t.Errorf("Failed to call BigInt()") //} exceptedError(t, v) x, _ := hex.DecodeString(v.ToString()) if expStr != string(x) { t.Errorf("ToString() - expected [%s], actual[%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.OctetString rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } x, _ = hex.DecodeString(w.ToString()) if expStr != string(x) { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } x, _ = hex.DecodeString(w.ToString()) if expStr != string(x) { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestVarBind(t *testing.T) { var v snmpclient2.VariableBinding oid, _ := snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.0") v = snmpclient2.VariableBinding{Oid: oid} v.Variable = snmpclient2.NewInteger(-2147483648) testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "int", "Value": "-2147483648"}}`) v.Variable = snmpclient2.NewOctetString([]byte("MyHost")) testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "octets", "Value": "4d79486f7374"}}`) v.Variable = snmpclient2.NewNull() testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "null", "Value": ""}}`) v.Variable = snmpclient2.NewCounter32(uint32(4294967295)) testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "counter32", "Value": "4294967295"}}`) v.Variable = snmpclient2.NewCounter64(uint64(18446744073709551615)) testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", `+ `"Variable": {"Type": "counter64", "Value": "18446744073709551615"}}`) expBuf := []byte{0x30, 0x00} v = snmpclient2.VariableBinding{} buf, err := v.Marshal() if err != nil { t.Errorf("Marshal() : %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } buf = []byte{0x00, 0x00} _, err = (&v).Unmarshal(buf) if err == nil { t.Errorf("Unmarshal() : can not validation") } }
func TestCounter64(t *testing.T) { expInt := uint64(18446744073709551615) expStr := "18446744073709551615" expBuf := []byte{0x46, 0x09, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} var v snmpclient2.Variable = snmpclient2.NewCounter64(expInt) if expInt != v.Uint() { t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, v.Uint()) } if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual [%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.Counter64 rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestTimeTicks(t *testing.T) { expInt := int64(4294967295) expStr := "4294967295" expBuf := []byte{0x43, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff} var v snmpclient2.Variable = snmpclient2.NewTimeTicks(uint32(expInt)) if expInt != v.Int() { t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, v.Int()) } if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual [%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.TimeTicks rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestIpaddress(t *testing.T) { expStr := "192.168.1.1" expInt := int64(3232235777) expBuf := []byte{0x40, 0x04, 0xc0, 0xa8, 0x01, 0x01} var v snmpclient2.Variable = snmpclient2.NewIpaddress(0xc0, 0xa8, 0x01, 0x01) if expInt != v.Int() { t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, v.Int()) } if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual[%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.Ipaddress rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestInteger(t *testing.T) { expInt := int64(2147483647) expStr := "2147483647" expBuf := []byte{0x02, 0x04, 0x7f, 0xff, 0xff, 0xff} var v snmpclient2.Variable = snmpclient2.NewInteger(int32(expInt)) if expInt != v.Int() { t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, v.Int()) } if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual [%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.Integer rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestEndOfMibView(t *testing.T) { expStr := "" expBuf := []byte{0x82, 0x00} var v snmpclient2.Variable = snmpclient2.NewEndOfMibView() exceptedError(t, v) if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual[%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.EndOfMibView rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }
func TestOpaque(t *testing.T) { expStr := "54:65:73:74" expBuf := []byte{0x44, 0x04, 0x54, 0x65, 0x73, 0x74} var v snmpclient2.Variable = snmpclient2.NewOpaque(expBuf[2:]) exceptedError(t, v) if expStr != v.ToString() { t.Errorf("ToString() - expected [%s], actual[%s]", expStr, v.ToString()) } buf, err := v.Marshal() if err != nil { t.Errorf("Marshal(): %v", err) } if !bytes.Equal(expBuf, buf) { t.Errorf("Marshal() - expected [%s], actual [%s]", snmpclient2.ToHexStr(expBuf, " "), snmpclient2.ToHexStr(buf, " ")) } var w snmpclient2.Opaque rest, err := (&w).Unmarshal(buf) if len(rest) != 0 || err != nil { t.Errorf("Unmarshal() - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.ToString()) } buf = append(buf, 0x00) rest, err = (&w).Unmarshal(buf) if len(rest) != 1 || err != nil { t.Errorf("Unmarshal() with rest - len[%d] err[%v]", len(rest), err) } if expStr != w.ToString() { t.Errorf("Unmarshal() with rest - expected [%s], actual [%s]", expStr, w.ToString()) } }