Beispiel #1
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")
	}
}
func TestCounter32(t *testing.T) {
	expInt := uint64(4294967295)
	expStr := "4294967295"
	expBuf := []byte{0x41, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff}
	var v snmpclient2.Variable = snmpclient2.NewCounter32(uint32(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.Counter32
	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())
	}
}