Esempio n. 1
0
func TestVarBind(t *testing.T) {
	var v snmpgo.VarBind
	oid, _ := snmpgo.NewOid("1.3.6.1.2.1.1.1.0")
	v = snmpgo.VarBind{Oid: oid}

	v.Variable = snmpgo.NewInteger(-2147483648)
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Integer", "Value": "-2147483648"}}`)

	v.Variable = snmpgo.NewOctetString([]byte("MyHost"))
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "OctetString", "Value": "MyHost"}}`)

	v.Variable = snmpgo.NewNull()
	testVarBind(t, &v, `{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Null", "Value": ""}}`)

	v.Variable = snmpgo.NewCounter32(uint32(4294967295))
	testVarBind(t, &v,
		`{"Oid": "1.3.6.1.2.1.1.1.0", "Variable": {"Type": "Counter32", "Value": "4294967295"}}`)

	v.Variable = snmpgo.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 = snmpgo.VarBind{}
	buf, err := v.Marshal()
	if err != nil {
		t.Errorf("Marshal() : %v", err)
	}
	if !bytes.Equal(expBuf, buf) {
		t.Errorf("Marshal() - expected [%s], actual [%s]",
			snmpgo.ToHexStr(expBuf, " "), snmpgo.ToHexStr(buf, " "))
	}

	buf = []byte{0x00, 0x00}
	_, err = (&v).Unmarshal(buf)
	if err == nil {
		t.Errorf("Unmarshal() : can not validation")
	}
}
Esempio n. 2
0
func testVarBind(t *testing.T, v *snmpgo.VarBind, expStr string) {
	var w snmpgo.VarBind
	buf, err := v.Marshal()
	if err != nil {
		t.Errorf("Marshal() : %v", err)
	}
	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())
	}

	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.String() {
		t.Errorf("Unmarshal() - expected [%s], actual [%s]", expStr, w.String())
	}
}