From b023f0d3f1e1e1f79f6e1fbf12107da819ef6719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 5 Oct 2022 20:28:52 +0300 Subject: [PATCH] Adding tests in go --- Makefile | 30 +++++++--- cmd/client_server/validators.go | 22 +++++++- cmd/client_server/validators_test.go | 82 ++++++++++++++++++++++++++++ internal/json/valid_response.json | 29 ++++++++++ 4 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 cmd/client_server/validators_test.go create mode 100644 internal/json/valid_response.json diff --git a/Makefile b/Makefile index 72e04fb..9be2c9e 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ moc_server_address := 127.0.0.1:5059 bold := \033[1m normal := \033[0m +good := \033[1m\033[0;32m help: @echo "$(bold)Makefile commands$(normal)" @@ -21,35 +22,48 @@ run: go run cmd/main.go -p $(port) -d "$(moc_server_address)" test-ip: - go run cmd/main.go -p $(port) -d "$(moc_server_address),localhost:5059" + @echo + @go run cmd/main.go -p $(port) -d "$(moc_server_address),localhost:5059" || \ + { echo "\n[+] PASS wrong IP address test"; exit 0; } test-port: - go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:as" + @echo + @go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:as" || \ + { echo "\n[+] PASS wrong port test"; exit 0; } test-port-max: - go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:65537" + @echo + @go run cmd/main.go -p $(port) -d "$(moc_server_address),127.0.0.1:65537" || \ + { echo "\n[+] PASS port too big test"; exit 0; } test-port-endpoint: - go run cmd/main.go -p $(port) -d "127.0.0.1:9001/bid_request" + @echo + @go run cmd/main.go -p $(port) -d "127.0.0.1:9001/bid_request" || \ + { echo "\n[+] PASS endpoint with address test"; exit 0; } build: go build -o bin/simple-choose-ad cmd/main.go start-moc-server: - @echo "[!] Starting moc server on $(moc_server_address) ..." + @echo "[!] Starting up moc-server on $(moc_server_address) ..." @go run internal/moc_server.go -l $(moc_server_address) & stop-moc-server: - @echo "[!] Killing moc server" + @echo "[!] Stopping moc-server ..." @curl -s -o /dev/null "$(moc_server_address)/exit" & test-server: - @echo "Testing server..." + @echo + @echo "Check response from moc-server " @$(MAKE) start-moc-server @cd "cmd/client_server/"; \ - go test + go test -v @$(MAKE) stop-moc-server tests: + # @$(MAKE) test-ip + # @$(MAKE) test-port + # @$(MAKE) test-port-max + # @$(MAKE) test-port-endpoint @$(MAKE) test-server diff --git a/cmd/client_server/validators.go b/cmd/client_server/validators.go index 432a1fa..093f7ef 100644 --- a/cmd/client_server/validators.go +++ b/cmd/client_server/validators.go @@ -10,7 +10,10 @@ import ( "strings" ) -const MAX_PORT_NUM = 65535 +const ( + MAX_PORT_NUM = 65535 + MIN_PORT_NUM = 1024 +) // Returns false if ipv4 `correct`. func wrongIPAddresFormat(ipv4 string) bool { @@ -31,21 +34,34 @@ func throwHTTPError(err_text string, code int, w *http.ResponseWriter) error { // and `8080` port. If ip or port has wrong format, returns error. func ParsePartnersAddress(ipAndPort string) (string, int64, error) { var err error + var ip string + var port int64 + iap := strings.Split(ipAndPort, ":") - ip := iap[0] + if len(iap) != 2 { + err = errors.New(fmt.Sprintf("Wrong partners 'ip:port' format: %v", ipAndPort)) + return ip, port, err + } + + ip = iap[0] if wrongIPAddresFormat(ip) { err = errors.New(fmt.Sprintf("Wrong ip address format in partner ip: %v", ip)) } - port, e := strconv.ParseInt(iap[1], 10, 32) + port, e := strconv.ParseInt(iap[1], 10, 64) if e != nil { err = errors.New(fmt.Sprintf("Wrong port format in partner ip: %v", e)) + return ip, port, err } if port > MAX_PORT_NUM { err = errors.New(fmt.Sprintf("Wrong port in partner ip: grater than %v", MAX_PORT_NUM)) } + if port < MIN_PORT_NUM { + err = errors.New(fmt.Sprintf("Wrong port in partner ip: %v lower than %v", port, MIN_PORT_NUM)) + } + return ip, port, err } diff --git a/cmd/client_server/validators_test.go b/cmd/client_server/validators_test.go new file mode 100644 index 0000000..7c5743d --- /dev/null +++ b/cmd/client_server/validators_test.go @@ -0,0 +1,82 @@ +package clientserver + +import ( + "testing" +) + +// Wants: 10.10.10.10:5050 +// Gets : localhost:5050 +func TestIPAddresFormat_DomainName(t *testing.T) { + addres := "localhost:5050" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10: +func TestIPAddresFormat_OnlyIpAndColon(t *testing.T) { + addres := "10.10.10.10:" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10 +func TestIPAddresFormat_OnlyIp(t *testing.T) { + addres := "10.10.10.10" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10:65537 +func TestIPAddresFormat_IncorrectPortValue_TooBig(t *testing.T) { + addres := "10.10.10.10:65537" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10:1000 +func TestIPAddresFormat_IncorrectPortValue_TooSmall(t *testing.T) { + addres := "10.10.10.10:1000" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10:as +func TestIPAddresFormat_IncorrectPortValue_NotANumber(t *testing.T) { + addres := "10.10.10.10:as" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} + +// Wants: 10.10.10.10:5050 +// Gets : 10.10.10.10:5050/bid_request +func TestIPAddresFormat_AddressWithEndpoint(t *testing.T) { + addres := "10.10.10.10:5050/bid_request" + _, _, e := ParsePartnersAddress(addres) + if e == nil { + t.Error("Must be an error, when parsing ", addres) + } + t.Log(e) +} diff --git a/internal/json/valid_response.json b/internal/json/valid_response.json new file mode 100644 index 0000000..b8433a7 --- /dev/null +++ b/internal/json/valid_response.json @@ -0,0 +1,29 @@ +{ + "id": "123", + "imp": [ + { + "id": 123, + "width": 144, + "height": 122, + "title": "example1", + "url": "example.com", + "price": 123.5 + }, + { + "id": 123, + "width": 155, + "height": 133, + "title": "bestoption", + "url": "bestoption.com", + "price": 143.8 + }, + { + "id": 123, + "width": 155, + "height": 133, + "title": "notabestoption", + "url": "notabestoption.com", + "price": 100.8 + } + ] +}