func TestOids(t *testing.T) { oids, _ := snmpclient2.NewOids([]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", }) 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", }) oids = oids.Sort() if len(expOids) != len(oids) { t.Errorf("Sort() - expected [%d], actual [%d]", len(expOids), len(oids)) } for i, o := range expOids { if !o.Equal(&oids[i]) { t.Errorf("Sort() - expected [%s], actual [%s]", o, oids[i]) } } 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", }) oids = oids.Sort().Uniq() if len(expOids) != len(oids) { t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(oids)) } for i, o := range expOids { if !o.Equal(&oids[i]) { t.Errorf("Uniq() - expected [%s], actual [%s]", o, oids[i]) } } expOids, _ = snmpclient2.NewOids([]string{ "1.3.6.1.2.1.1", }) oids = oids.Sort().UniqBase() if len(expOids) != len(oids) { t.Errorf("Uniq() - expected [%d], actual [%d]", len(expOids), len(oids)) } for i, o := range expOids { if !o.Equal(&oids[i]) { t.Errorf("Uniq() - expected [%s], actual [%s]", o, oids[i]) } } }
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()") } }
func TestSNMP(t *testing.T) { snmp, _ := snmpclient2.NewSNMP("udp", "127.0.0.1", snmpclient2.Arguments{ Version: snmpclient2.V3, UserName: "******", SecurityLevel: snmpclient2.AuthPriv, AuthPassword: "******", AuthProtocol: snmpclient2.Md5, PrivPassword: "******", PrivProtocol: snmpclient2.Des, }) pdu := snmpclient2.NewPdu(snmpclient2.V3, snmpclient2.Report) err := snmpclient2.SnmpCheckPdu(snmp, pdu) if err != nil { t.Errorf("checkPdu() - has error %v", err) } oids, _ := snmpclient2.NewOids([]string{"1.3.6.1.6.3.11.2.1.1.0"}) pdu = snmpclient2.NewPduWithOids(snmpclient2.V3, snmpclient2.Report, oids) err = snmpclient2.SnmpCheckPdu(snmp, pdu) if err == nil { t.Error("checkPdu() - report oid") } }
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) } } }