From 9c38baeb9e790aecf779fcf6b8625c63a49b7815 Mon Sep 17 00:00:00 2001 From: yuyulei Date: Thu, 18 May 2017 23:58:58 +0800 Subject: [PATCH] add ut --- utils/util/util_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/utils/util/util_test.go b/utils/util/util_test.go index 8210c61..17d7754 100644 --- a/utils/util/util_test.go +++ b/utils/util/util_test.go @@ -20,3 +20,67 @@ func TestGetAuthKey(t *testing.T) { t.Log(key) assert.Equal("6df41a43725f0c770fd56379e12acf8c", key) } + +func TestGetPortRanges(t *testing.T) { + assert := assert.New(t) + + rangesStr := "2000-3000,3001,4000-50000" + expect := [][2]int64{ + [2]int64{2000, 3000}, + [2]int64{3001, 3001}, + [2]int64{4000, 50000}, + } + actual, err := GetPortRanges(rangesStr) + assert.Nil(err) + t.Log(actual) + assert.Equal(expect, actual) +} + +func TestContainsPort(t *testing.T) { + assert := assert.New(t) + + rangesStr := "2000-3000,3001,4000-50000" + portRanges, err := GetPortRanges(rangesStr) + assert.Nil(err) + + type Case struct { + Port int64 + Answer bool + } + cases := []Case{ + Case{ + Port: 3001, + Answer: true, + }, + Case{ + Port: 3002, + Answer: false, + }, + Case{ + Port: 44444, + Answer: true, + }, + } + for _, elem := range cases { + ok := ContainsPort(portRanges, elem.Port) + assert.Equal(elem.Answer, ok) + } +} + +func TestPortRangesCut(t *testing.T) { + assert := assert.New(t) + + rangesStr := "2000-3000,3001,4000-50000" + portRanges, err := GetPortRanges(rangesStr) + assert.Nil(err) + + expect := [][2]int64{ + [2]int64{2000, 3000}, + [2]int64{3001, 3001}, + [2]int64{4000, 44443}, + [2]int64{44445, 50000}, + } + actual := PortRangesCut(portRanges, 44444) + t.Log(actual) + assert.Equal(expect, actual) +}