-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable building without netsnmp shared library
- Loading branch information
Showing
14 changed files
with
272 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package mib | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type DisplayHint uint8 | ||
|
||
const ( | ||
DisplayHintUnknown = DisplayHint(iota) | ||
DisplayHintString | ||
DisplayHintHexadecimal | ||
) | ||
|
||
type DisplayHints map[string]DisplayHint | ||
|
||
type Parser interface { | ||
Parse() (DisplayHints, error) | ||
} | ||
|
||
type DataProvider struct { | ||
displayHints DisplayHints | ||
} | ||
|
||
func (p *DataProvider) GetDisplayHint(oid string) DisplayHint { | ||
for length := len(oid); length > 7; length = strings.LastIndex(oid[:length], ".") { | ||
if displayHint, ok := p.displayHints[oid[:length]]; ok { | ||
return displayHint | ||
} | ||
} | ||
|
||
return DisplayHintUnknown | ||
} | ||
|
||
func NewDataProvider(displayHints DisplayHints) *DataProvider { | ||
return &DataProvider{displayHints: displayHints} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package mib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grongor/go-snmp-proxy/snmpproxy/mib" | ||
) | ||
|
||
func TestMibDataProvider_GetStringType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
stringTypes mib.DisplayHints | ||
oid string | ||
expected mib.DisplayHint | ||
}{ | ||
{ | ||
name: "exact match", | ||
stringTypes: mib.DisplayHints{ | ||
".1.2.3.1.1.5.15.2": mib.DisplayHintString, | ||
".1.2.3.1.1.5.15": mib.DisplayHintHexadecimal, | ||
}, | ||
oid: ".1.2.3.1.1.5.15.2", | ||
expected: mib.DisplayHintString, | ||
}, | ||
{ | ||
name: "parent matched", | ||
stringTypes: mib.DisplayHints{ | ||
".1.2.3.1.1.5.15.22": mib.DisplayHintHexadecimal, | ||
".1.2.3.1.1.5.15": mib.DisplayHintString, | ||
}, | ||
oid: ".1.2.3.1.1.5.15.2", | ||
expected: mib.DisplayHintString, | ||
}, | ||
{ | ||
name: "parent matched (multiple steps back)", | ||
stringTypes: mib.DisplayHints{ | ||
".1.2.3.1.1.5.15.22": mib.DisplayHintString, | ||
".1.2.3.1.1.5.1": mib.DisplayHintString, | ||
".1.2.3.1.1.1": mib.DisplayHintString, | ||
".1.2.3.1.11": mib.DisplayHintString, | ||
".1.2.3.1": mib.DisplayHintHexadecimal, | ||
}, | ||
oid: ".1.2.3.1.1.5.15.2", | ||
expected: mib.DisplayHintHexadecimal, | ||
}, | ||
{ | ||
name: "OID is too short to consider -> unknown type", | ||
stringTypes: mib.DisplayHints{".1.2.3": mib.DisplayHintHexadecimal}, | ||
oid: ".1.2.3", | ||
expected: mib.DisplayHintUnknown, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.expected, mib.NewDataProvider(test.stringTypes).GetDisplayHint(test.oid)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build !nonetsnmp | ||
|
||
package mib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/grongor/go-snmp-proxy/snmpproxy/mib" | ||
) | ||
|
||
func TestNetsnmpMibParser_Parse(t *testing.T) { | ||
require := require.New(t) | ||
|
||
mibParser := mib.NewNetsnmpMibParser(zap.NewNop().Sugar(), false) | ||
displayHints, err := mibParser.Parse() | ||
|
||
require.NoError(err) | ||
require.NotEmpty(displayHints) | ||
require.Equal(mib.DisplayHintString, displayHints[".1.3.6.1.2.1.2.2.1.2"]) | ||
require.Equal(mib.DisplayHintHexadecimal, displayHints[".1.3.6.1.2.1.4.22.1.2"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// +build nonetsnmp | ||
|
||
package mib | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
) | ||
|
||
type NopMibParser struct { | ||
} | ||
|
||
func (n NopMibParser) Parse() (DisplayHints, error) { | ||
return nil, nil | ||
} | ||
|
||
func NewNopMibParser() *NopMibParser { | ||
return &NopMibParser{} | ||
} | ||
|
||
func NewNetsnmpMibParser(*zap.SugaredLogger, bool) Parser { | ||
return NewNopMibParser() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build nonetsnmp | ||
|
||
package mib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grongor/go-snmp-proxy/snmpproxy/mib" | ||
) | ||
|
||
func TestNopMibParser_Parse(t *testing.T) { | ||
parser := mib.NewNetsnmpMibParser(nil, false) | ||
hints, err := parser.Parse() | ||
require.Nil(t, hints) | ||
require.Nil(t, err) | ||
} |
Oops, something went wrong.