예제 #1
0
func TestParseOidFromString(t *testing.T) {
	expStr := ".1.3.6.1.2.1.1.1.0"
	var v snmpclient2.Oid

	v, err := snmpclient2.ParseOidFromString(expStr)
	if err != nil {
		t.Errorf("NewOid : %v", err)
	}

	if expStr[1:] != v.ToString() {
		t.Errorf("ToString() - expected [%s], actual[%s]", expStr[1:], v.ToString())
	}

	var s []string
	for i := 0; i <= 128; i++ {
		s = append(s, strconv.Itoa(i))
	}
	// expStr = strings.Join(s, ".")
	// v, err = snmpclient2.ParseOidFromString(expStr)
	// if err == nil {
	// 	t.Errorf("NewOid sub-identifiers size")
	// }

	expStr = "1.3.6.1.2.1.-1.0"
	v, err = snmpclient2.ParseOidFromString(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifier range")
	}

	expStr = "1.3.6.1.2.1.4294967296.0"
	v, err = snmpclient2.ParseOidFromString(expStr)
	if err == nil {
		t.Errorf("NewOid sub-identifier range")
	}

	// expStr = "3.3.6.1.2.1.1.1.0"
	// v, err = snmpclient2.ParseOidFromString(expStr)
	// if err == nil {
	// 	t.Errorf("NewOid first sub-identifier range")
	// }

	// expStr = "1"
	// v, err = snmpclient2.ParseOidFromString(expStr)
	// if err == nil {
	// 	t.Errorf("NewOid sub-identifiers size")
	// }

	// expStr = "1.40.6.1.2.1.1.1.0"
	// v, err = snmpclient2.ParseOidFromString(expStr)
	// if err == nil {
	// 	t.Errorf("NewOid first sub-identifier range")
	// }
}
예제 #2
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())
	}
}
예제 #3
0
func TestOidOperation(t *testing.T) {
	oid, _ := snmpclient2.ParseOidFromString("1.2.3.4.5.6.7")

	oids, _ := snmpclient2.NewOids([]string{"1.2.3.4", "1.2.3.4.5.6.7",
		"1.2.3.4.5.6.7.8", "1.1.3.4", "1.3.3.4"})

	if !oid.Contains(&oids[0]) || !oid.Contains(&oids[1]) || oid.Contains(&oids[2]) ||
		oid.Contains(&oids[3]) || oid.Contains(&oids[4]) {
		t.Errorf("Failed to Contains()")
	}

	if oid.Compare(&oids[0]) != 1 || oid.Compare(&oids[1]) != 0 || oid.Compare(&oids[2]) != -1 ||
		oid.Compare(&oids[3]) != 1 || oid.Compare(&oids[4]) != -1 {
		t.Errorf("Failed to Compare()")
	}

	if oid.Equal(&oids[0]) || !oid.Equal(&oids[1]) || oid.Equal(&oids[2]) ||
		oid.Equal(&oids[3]) || oid.Equal(&oids[4]) {
		t.Errorf("Failed to Contains()")
	}

	oid = oid.AppendSubIds([]int{8, 9, 10})
	if oid.ToString() != "1.2.3.4.5.6.7.8.9.10" {
		t.Errorf("Failed to AppendSubIds()")
	}
}
예제 #4
0
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)
	}
}
예제 #5
0
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")
	}
}
예제 #6
0
func TestVarBinds(t *testing.T) {
	var v snmpclient2.VariableBindings

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

	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.0")
	VariableBinding := v.MatchOid(oid)
	if VariableBinding == nil || !VariableBinding.Oid.Equal(&oid) {
		t.Errorf("Failed to MatchOid()")
	}
	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.1")
	VariableBinding = v.MatchOid(oid)
	if VariableBinding != nil {
		t.Errorf("Failed to MatchOid() - no match")
	}
	VariableBinding = v.MatchOid(snmpclient2.EmptyOID)
	if VariableBinding != nil {
		t.Errorf("Failed to MatchOid() - nil")
	}

	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1")
	VariableBindings := v.MatchBaseOids(oid)
	if len(VariableBindings) != 3 {
		t.Errorf("Failed to MatchBaseOids()")
	}
	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.0")
	VariableBindings = v.MatchBaseOids(oid)
	if len(VariableBindings) != 1 || !VariableBindings[0].Oid.Equal(&oid) {
		t.Errorf("Failed to MatchBaseOids() - one")
	}
	oid, _ = snmpclient2.ParseOidFromString("1.3.6.1.2.1.1.1.1")
	VariableBindings = v.MatchBaseOids(oid)
	if len(VariableBindings) != 0 {
		t.Errorf("Failed to MatchBaseOids() - no match")
	}
	VariableBindings = v.MatchBaseOids(snmpclient2.EmptyOID)
	if len(VariableBindings) != 0 {
		t.Errorf("Failed to MatchBaseOids() - nil")
	}

	var w snmpclient2.VariableBindings
	for _, o := range []string{
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.3.0",
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
	} {
		oid, _ = snmpclient2.ParseOidFromString(o)
		w = append(w, snmpclient2.NewVarBind(oid, snmpclient2.NewNull()))
	}

	expOids, _ := snmpclient2.NewOids([]string{
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.3.0",
	})
	w = w.Sort()
	if len(expOids) != len(w) {
		t.Errorf("Sort() - expected [%d], actual [%d]", len(expOids), len(w))
	}
	for i, o := range expOids {
		if !o.Equal(&w[i].Oid) {
			t.Errorf("Sort() - expected [%s], actual [%s]", o, w[i].Oid)
		}
	}

	expOids, _ = snmpclient2.NewOids([]string{
		"1.3.6.1.2.1.1",
		"1.3.6.1.2.1.1.1.0",
		"1.3.6.1.2.1.1.2.0",
		"1.3.6.1.2.1.1.3.0",
	})
	w = w.Sort().Uniq()
	if len(expOids) != len(w) {
		t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(w))
		return
	}
	for i, o := range expOids {
		if !o.Equal(&w[i].Oid) {
			t.Errorf("Uniq() - expected [%s], actual [%s]", o, w[i].Oid)
		}
	}
}