func TestGauge32(t *testing.T) { expInt := int64(4294967295) expStr := "4294967295" expBuf := []byte{0x42, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff} var v snmpgo.Variable = snmpgo.NewGauge32(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.Gauge32 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()) } }
func buildVariable(kind string, value string) (val snmpgo.Variable, err error) { switch kind { case "i": var num int64 if num, err = strconv.ParseInt(value, 10, 32); err == nil { val = snmpgo.NewInteger(int32(num)) } case "u": var num uint64 if num, err = strconv.ParseUint(value, 10, 32); err == nil { val = snmpgo.NewGauge32(uint32(num)) } case "c": var num uint64 if num, err = strconv.ParseUint(value, 10, 32); err == nil { val = snmpgo.NewCounter32(uint32(num)) } case "C": var num uint64 if num, err = strconv.ParseUint(value, 10, 64); err == nil { val = snmpgo.NewCounter64(num) } case "t": var num uint64 if num, err = strconv.ParseUint(value, 10, 32); err == nil { val = snmpgo.NewTimeTicks(uint32(num)) } case "a": if ip := net.ParseIP(value); ip != nil && len(ip) == 4 { val = snmpgo.NewIpaddress(ip[0], ip[1], ip[2], ip[3]) } else { return nil, fmt.Errorf("%s: no valid IP Address", value) } case "o": val, err = snmpgo.ParseOidFromString(value) case "n": val = snmpgo.NewNull() case "s": val = snmpgo.NewOctetString([]byte(value)) case "x": var b []byte hx := hexPrefix.ReplaceAllString(value, "") if b, err = hex.DecodeString(hx); err == nil { val = snmpgo.NewOctetString(b) } else { return nil, fmt.Errorf("%s: no valid Hex String", value) } case "d": s := strings.Split(value, ".") b := make([]byte, len(s)) for i, piece := range s { var num int if num, err = strconv.Atoi(piece); err != nil || num > 0xff { return nil, fmt.Errorf("%s: no valid Decimal String", value) } b[i] = byte(num) } val = snmpgo.NewOctetString(b) default: return nil, fmt.Errorf("%s: unknown TYPE", kind) } return }