-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relates to #4
- Loading branch information
Showing
10 changed files
with
149 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ParseSTUN checks if a STUN addr is valid | ||
func ParseSTUN(stunAddr string) error { | ||
arr := strings.Split(stunAddr, ":") | ||
if len(arr) != 2 { | ||
return fmt.Errorf("invalid stun adress") | ||
} | ||
port, err := strconv.Atoi(arr[1]) | ||
if err != nil || (port <= 0 || port > 0xffff) { | ||
return fmt.Errorf("invalid port %v", port) | ||
} | ||
return nil | ||
} |
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,56 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_STUN_Arg(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
tests := []struct { | ||
input string | ||
err error | ||
}{ | ||
{ | ||
input: "", | ||
err: fmt.Errorf("invalid stun adress"), | ||
}, | ||
{ | ||
input: "test", | ||
err: fmt.Errorf("invalid stun adress"), | ||
}, | ||
{ | ||
input: "stun:lol:lol", | ||
err: fmt.Errorf("invalid stun adress"), | ||
}, | ||
{ | ||
input: "test:wtf", | ||
err: fmt.Errorf("invalid port 0"), | ||
}, | ||
{ | ||
input: "test:-2", | ||
err: fmt.Errorf("invalid port -2"), | ||
}, | ||
{ | ||
// 0xffff + 1 | ||
input: "test:65536", | ||
err: fmt.Errorf("invalid port 65536"), | ||
}, | ||
{ | ||
input: "test:5432", | ||
err: nil, | ||
}, | ||
{ | ||
input: "stun.l.google.com:19302", | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, cur := range tests { | ||
err := ParseSTUN(cur.input) | ||
assert.Equal(cur.err, err) | ||
} | ||
} |
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