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
0b00897e
Commit
0b00897e
authored
6 years ago
by
Daniel Czerwonk
Browse files
Options
Downloads
Patches
Plain Diff
added method to parse string to BIOs IP type
parent
b7d5aa4d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
net/ip.go
+15
-0
15 additions, 0 deletions
net/ip.go
net/ip_test.go
+43
-0
43 additions, 0 deletions
net/ip_test.go
with
58 additions
and
0 deletions
net/ip.go
+
15
−
0
View file @
0b00897e
...
@@ -62,6 +62,21 @@ func IPFromBytes(b []byte) (IP, error) {
...
@@ -62,6 +62,21 @@ func IPFromBytes(b []byte) (IP, error) {
return
IP
{},
fmt
.
Errorf
(
"byte slice has an invalid legth. Expected either 4 (IPv4) or 16 (IPv6) bytes but got: %d"
,
len
(
b
))
return
IP
{},
fmt
.
Errorf
(
"byte slice has an invalid legth. Expected either 4 (IPv4) or 16 (IPv6) bytes but got: %d"
,
len
(
b
))
}
}
// IPFromString returns an IP address for a given string
func
IPFromString
(
str
string
)
(
IP
,
error
)
{
ip
:=
net
.
ParseIP
(
str
)
if
ip
==
nil
{
return
IP
{},
fmt
.
Errorf
(
"%s is not a valid IP address"
,
str
)
}
ip4
:=
ip
.
To4
()
if
ip4
!=
nil
{
return
IPFromBytes
(
ip4
)
}
return
IPFromBytes
(
ip
.
To16
())
}
// Equal returns true if ip is equal to other
// Equal returns true if ip is equal to other
func
(
ip
IP
)
Equal
(
other
IP
)
bool
{
func
(
ip
IP
)
Equal
(
other
IP
)
bool
{
return
ip
==
other
return
ip
==
other
...
...
This diff is collapsed.
Click to expand it.
net/ip_test.go
+
43
−
0
View file @
0b00897e
...
@@ -370,3 +370,46 @@ func TestBitAtPosition(t *testing.T) {
...
@@ -370,3 +370,46 @@ func TestBitAtPosition(t *testing.T) {
}
}
}
}
}
}
func
TestIPFromString
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
input
string
expected
IP
wantFail
bool
}{
{
name
:
"ipv4"
,
input
:
"192.168.1.234"
,
expected
:
IPv4FromOctets
(
192
,
168
,
1
,
234
),
},
{
name
:
"ipv6"
,
input
:
"2001:678:1e0::cafe"
,
expected
:
IPv6FromBlocks
(
0x2001
,
0x678
,
0x1e0
,
0
,
0
,
0
,
0
,
0xcafe
),
},
{
name
:
"invalid"
,
input
:
"foo"
,
wantFail
:
true
,
},
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
ip
,
err
:=
IPFromString
(
test
.
input
)
if
err
==
nil
&&
test
.
wantFail
{
t
.
Fatal
(
"expected error but got nil"
)
}
if
err
!=
nil
{
if
test
.
wantFail
{
return
}
t
.
Fatal
(
err
)
}
assert
.
Equal
(
t
,
test
.
expected
,
ip
)
})
}
}
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