Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bio-rd
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
bio-rd
Commits
1b6d5072
Commit
1b6d5072
authored
6 years ago
by
Christoph Petrausch
Browse files
Options
Downloads
Patches
Plain Diff
Added tests for large community string parsing
parent
6ee4ae92
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
protocols/bgp/packet/large_community.go
+6
-4
6 additions, 4 deletions
protocols/bgp/packet/large_community.go
protocols/bgp/packet/large_community_test.go
+86
-0
86 additions, 0 deletions
protocols/bgp/packet/large_community_test.go
with
92 additions
and
4 deletions
protocols/bgp/packet/large_community.go
+
6
−
4
View file @
1b6d5072
...
@@ -2,10 +2,13 @@ package packet
...
@@ -2,10 +2,13 @@ package packet
import
(
import
(
"fmt"
"fmt"
"regexp"
"strconv"
"strconv"
"strings"
"strings"
)
)
var
largeCommunityRegEx
=
regexp
.
MustCompile
(
"^
\\
(?
\\
d+,
\\
d+,
\\
d+
\\
)?$"
)
type
LargeCommunity
struct
{
type
LargeCommunity
struct
{
GlobalAdministrator
uint32
GlobalAdministrator
uint32
DataPart1
uint32
DataPart1
uint32
...
@@ -17,13 +20,12 @@ func (c LargeCommunity) String() string {
...
@@ -17,13 +20,12 @@ func (c LargeCommunity) String() string {
}
}
func
ParseLargeCommunityString
(
s
string
)
(
com
LargeCommunity
,
err
error
)
{
func
ParseLargeCommunityString
(
s
string
)
(
com
LargeCommunity
,
err
error
)
{
if
!
largeCommunityRegEx
.
Match
([]
byte
(
s
))
{
return
com
,
fmt
.
Errorf
(
"malformed large community string %s"
,
s
)
}
s
=
strings
.
Trim
(
s
,
"()"
)
s
=
strings
.
Trim
(
s
,
"()"
)
t
:=
strings
.
Split
(
s
,
","
)
t
:=
strings
.
Split
(
s
,
","
)
if
len
(
t
)
!=
3
{
return
com
,
fmt
.
Errorf
(
"can not parse large community %s"
,
s
)
}
v
,
err
:=
strconv
.
ParseUint
(
t
[
0
],
10
,
32
)
v
,
err
:=
strconv
.
ParseUint
(
t
[
0
],
10
,
32
)
if
err
!=
nil
{
if
err
!=
nil
{
return
com
,
err
return
com
,
err
...
...
This diff is collapsed.
Click to expand it.
protocols/bgp/packet/large_community_test.go
0 → 100644
+
86
−
0
View file @
1b6d5072
package
packet
import
(
"errors"
"testing"
"fmt"
"math"
"strconv"
"github.com/stretchr/testify/assert"
)
func
TestParseLargeCommunityString
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
in
string
expected
LargeCommunity
err
error
}{
{
name
:
"normal large community"
,
in
:
"(1,2,3)"
,
expected
:
LargeCommunity
{
GlobalAdministrator
:
1
,
DataPart1
:
2
,
DataPart2
:
3
,
},
err
:
nil
,
},
{
name
:
"too short community"
,
in
:
"(1,2)"
,
expected
:
LargeCommunity
{},
err
:
errors
.
New
(
"malformed large community string (1,2)"
),
},
{
name
:
"missing parentheses large community"
,
in
:
"1,2,3"
,
expected
:
LargeCommunity
{
GlobalAdministrator
:
1
,
DataPart1
:
2
,
DataPart2
:
3
,
},
err
:
nil
,
},
{
name
:
"malformed large community"
,
in
:
"[1,2,3]"
,
expected
:
LargeCommunity
{},
err
:
errors
.
New
(
"malformed large community string [1,2,3]"
),
},
{
name
:
"missing digit"
,
in
:
"(,2,3)"
,
expected
:
LargeCommunity
{},
err
:
errors
.
New
(
"malformed large community string (,2,3)"
),
},
{
name
:
"to big global administrator"
,
in
:
fmt
.
Sprintf
(
"(%d,1,2)"
,
math
.
MaxInt64
),
expected
:
LargeCommunity
{},
err
:
&
strconv
.
NumError
{
Func
:
"ParseUint"
,
Num
:
fmt
.
Sprintf
(
"%d"
,
math
.
MaxInt64
),
Err
:
strconv
.
ErrRange
},
},
{
name
:
"to big data part 1"
,
in
:
fmt
.
Sprintf
(
"(1,%d,2)"
,
math
.
MaxInt64
),
expected
:
LargeCommunity
{
1
,
0
,
0
},
err
:
&
strconv
.
NumError
{
Func
:
"ParseUint"
,
Num
:
fmt
.
Sprintf
(
"%d"
,
math
.
MaxInt64
),
Err
:
strconv
.
ErrRange
},
},
{
name
:
"to big data part 2"
,
in
:
fmt
.
Sprintf
(
"(1,2,%d)"
,
math
.
MaxInt64
),
expected
:
LargeCommunity
{
1
,
2
,
0
},
err
:
&
strconv
.
NumError
{
Func
:
"ParseUint"
,
Num
:
fmt
.
Sprintf
(
"%d"
,
math
.
MaxInt64
),
Err
:
strconv
.
ErrRange
},
},
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
com
,
err
:=
ParseLargeCommunityString
(
test
.
in
)
assert
.
Equal
(
t
,
test
.
err
,
err
)
assert
.
Equal
(
t
,
test
.
expected
,
com
)
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment