示例#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 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)
	}
}