1// +build ignore
2
3package mib
4
5import (
6	"os"
7	"strings"
8	"testing"
9
10	"bosun.org/snmp/asn1"
11)
12
13type LookupTest struct {
14	prefix string
15	result asn1.ObjectIdentifier
16}
17
18var lookupTests = []LookupTest{
19	{"SNMPv2-MIB::sysName.0", asn1.ObjectIdentifier{1, 3, 6, 1, 2, 1, 1, 5, 0}},
20	{"1.3.6.1.2.1.1.5.0", asn1.ObjectIdentifier{1, 3, 6, 1, 2, 1, 1, 5, 0}},
21	{".1.3.6.1.2.1.1.5.0", asn1.ObjectIdentifier{1, 3, 6, 1, 2, 1, 1, 5, 0}},
22}
23
24func TestLookup(t *testing.T) {
25	for _, test := range lookupTests {
26		result, err := Lookup(test.prefix)
27		if err != nil {
28			t.Errorf("Lookup(%q) error: %v", test.prefix, err)
29			continue
30		}
31		if !result.Equal(test.result) {
32			t.Errorf("Lookup(%q)", test.prefix)
33			t.Errorf("  want=%v", test.result)
34			t.Errorf("  have=%v", result)
35		}
36	}
37}
38
39type LookupError struct {
40	prefix string
41	expect string
42}
43
44var lookupErrors = []LookupError{
45	{"", "exit status 2"},
46	{"foo", "exit status 2"},
47	{"sysName.0", "exit status 2"},
48}
49
50func TestLookupErrors(t *testing.T) {
51	for _, test := range lookupErrors {
52		_, err := Lookup(test.prefix)
53		if err == nil {
54			t.Errorf("expected error for %q", test.prefix)
55		} else if strings.Index(err.Error(), test.expect) < 0 {
56			t.Errorf("expected error with %q for %q; got %s", test.expect, test.prefix, err)
57		}
58	}
59}
60
61func TestCacheWarm(t *testing.T) {
62	// clear the cache
63	cache.lookup = make(map[string]asn1.ObjectIdentifier)
64
65	// warm it up
66	_, err := Lookup("SNMPv2-MIB::sysName.0")
67	if err != nil {
68		t.Errorf("unexpected error: %v", err)
69		return
70	}
71
72	// break snmptranslate
73	defer os.Setenv("PATH", os.Getenv("PATH"))
74	os.Setenv("PATH", "")
75
76	// simulate query
77	if _, err := Lookup("SNMPv2-MIB::sysName.0"); err != nil {
78		t.Errorf("unexpected error: %v", err)
79	}
80}
81
82func TestCacheCold(t *testing.T) {
83	// clear the cache
84	cache.lookup = make(map[string]asn1.ObjectIdentifier)
85
86	// break snmptranslate
87	defer os.Setenv("PATH", os.Getenv("PATH"))
88	os.Setenv("PATH", "")
89
90	// simulate query
91	if _, err := Lookup("SNMPv2-MIB::sysName.0"); err == nil {
92		t.Errorf("unexpected success")
93	}
94}
95