Пример #1
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 snmpgo.Variable

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

	_, err = v.BigInt()
	if err == nil {
		t.Errorf("Failed to call BigInt()")
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
Пример #2
0
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 snmpgo.Variable = snmpgo.NewCounter64(expInt)

	big, err := v.BigInt()
	if err != nil {
		t.Errorf("Failed to call BigInt(): %v", err)
	}
	if expInt != big.Uint64() {
		t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, big.Int64())
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
Пример #3
0
func TestTimeTicks(t *testing.T) {
	expInt := int64(4294967295)
	expStr := "4294967295"
	expBuf := []byte{0x43, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff}
	var v snmpgo.Variable = snmpgo.NewTimeTicks(uint32(expInt))

	big, err := v.BigInt()
	if err != nil {
		t.Errorf("Failed to call BigInt(): %v", err)
	}
	if expInt != big.Int64() {
		t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, big.Int64())
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
Пример #4
0
func TestIpaddress(t *testing.T) {
	expStr := "192.168.1.1"
	expInt := int64(3232235777)
	expBuf := []byte{0x40, 0x04, 0xc0, 0xa8, 0x01, 0x01}
	var v snmpgo.Variable = snmpgo.NewIpaddress(0xc0, 0xa8, 0x01, 0x01)

	big, err := v.BigInt()
	if err != nil {
		t.Errorf("Failed to call BigInt(): %v", err)
	}
	if expInt != big.Int64() {
		t.Errorf("BigInt() - expected [%d], actual [%d]", expInt, big.Int64())
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
Пример #5
0
func TestEndOfMibView(t *testing.T) {
	expStr := ""
	expBuf := []byte{0x82, 0x00}
	var v snmpgo.Variable = snmpgo.NewEndOfMibView()

	_, err := v.BigInt()
	if err == nil {
		t.Errorf("Failed to call BigInt()")
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}
Пример #6
0
func TestOpaque(t *testing.T) {
	expStr := "54:65:73:74"
	expBuf := []byte{0x44, 0x04, 0x54, 0x65, 0x73, 0x74}
	var v snmpgo.Variable = snmpgo.NewOpaque(expBuf[2:])

	_, err := v.BigInt()
	if err == nil {
		t.Errorf("Failed to call BigInt()")
	}

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

	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, " "))
	}

	var w snmpgo.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.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() with rest - expected [%s], actual [%s]", expStr, w.String())
	}
}