From 33c5a0d90b5ed89870bb9d85186220e0fb5f89a5 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Sun, 12 Feb 2023 23:58:24 -0300 Subject: [PATCH 01/18] temp --- core/protocol.go | 34 +++++++++---------- core/protocol_test.go | 78 +++++++++++++------------------------------ go.mod | 4 --- 3 files changed, 41 insertions(+), 75 deletions(-) delete mode 100644 go.mod diff --git a/core/protocol.go b/core/protocol.go index 6408f6a..133e5f2 100644 --- a/core/protocol.go +++ b/core/protocol.go @@ -3,8 +3,8 @@ package core import ( "encoding/binary" "fmt" + "io" "log" - "net" ) /* @@ -39,10 +39,10 @@ import ( const TotalLength int32 = 4 -func writeBufferSize(size int, conn net.Conn) (n int, err error) { +func writeBufferSize(size int, stream io.Writer) (n int, err error) { buffer := make([]byte, TotalLength) binary.BigEndian.PutUint32(buffer, uint32(size)) - sizeWritten, err := conn.Write(buffer) + sizeWritten, err := stream.Write(buffer) if err != nil { err := fmt.Errorf("error on write size buffer: %s", err) @@ -52,10 +52,10 @@ func writeBufferSize(size int, conn net.Conn) (n int, err error) { return sizeWritten, nil } -func readBufferSize(conn net.Conn) (size int, err error) { +func readBufferSize(stream io.Reader) (size int, err error) { bufferSize := make([]byte, TotalLength) - if _, err := conn.Read(bufferSize); err != nil { + if _, err := stream.Read(bufferSize); err != nil { err := fmt.Errorf("error on read size buffer: %s", err) return -1, err } @@ -63,14 +63,14 @@ func readBufferSize(conn net.Conn) (size int, err error) { return int(binary.BigEndian.Uint32(bufferSize)), nil } -func writePendingBytes(messageSize int, messageSizeWritten int, messageBuffer []byte, conn net.Conn) (n int, err error) { +func writePendingBytes(messageSize int, messageSizeWritten int, messageBuffer []byte, stream io.Writer) (n int, err error) { var messageSizeCurrent = messageSizeWritten for { if messageSize != messageSizeCurrent { buffer := messageBuffer[messageSizeWritten:messageSize] - currentWriteBytesSize, err := conn.Write(buffer) + currentWriteBytesSize, err := stream.Write(buffer) if err != nil { err := fmt.Errorf("error on write message buffer: %s", err) @@ -86,14 +86,14 @@ func writePendingBytes(messageSize int, messageSizeWritten int, messageBuffer [] } -func readPendingBytes(messageSize int, messageSizeRead int, messageBuffer []byte, conn net.Conn) (buffer []byte, err error) { +func readPendingBytes(messageSize int, messageSizeRead int, messageBuffer []byte, stream io.Reader) (buffer []byte, err error) { var messageSizeCurrent = messageSizeRead for { if messageSize != messageSizeCurrent { buffer := make([]byte, messageSize-messageSizeCurrent) - currentReadBytesSize, err := conn.Read(buffer) + currentReadBytesSize, err := stream.Read(buffer) messageSizeCurrent += currentReadBytesSize @@ -112,11 +112,11 @@ func readPendingBytes(messageSize int, messageSizeRead int, messageBuffer []byte } } -func WriteMessage(message string, conn net.Conn) (n int, err error) { +func WriteMessage(message string, stream io.Writer) (n int, err error) { messageBuffer := []byte(message) messageSize := len(messageBuffer) - writeBufferSize(messageSize, conn) - messageSizeWritten, err := conn.Write(messageBuffer) + writeBufferSize(messageSize, stream) + messageSizeWritten, err := stream.Write(messageBuffer) if err != nil { err := fmt.Errorf("error on write message buffer: %s", err) @@ -126,7 +126,7 @@ func WriteMessage(message string, conn net.Conn) (n int, err error) { if messageSize != messageSizeWritten { log.Println("not all bytes were written in first tentative, trying to write pending ") - byteWritten, err := writePendingBytes(messageSize, messageSizeWritten, messageBuffer, conn) + byteWritten, err := writePendingBytes(messageSize, messageSizeWritten, messageBuffer, stream) if err != nil { err := fmt.Errorf("error on write pending bytes to server: %s", err) @@ -139,10 +139,10 @@ func WriteMessage(message string, conn net.Conn) (n int, err error) { return messageSizeWritten, nil } -func ReadMessage(conn net.Conn) (message string, err error) { - messageSize, _ := readBufferSize(conn) +func ReadMessage(stream io.Reader) (message string, err error) { + messageSize, _ := readBufferSize(stream) messageBuffer := make([]byte, messageSize) - messageSizeRead, err := conn.Read(messageBuffer) + messageSizeRead, err := stream.Read(messageBuffer) if err != nil { err := fmt.Errorf("error on read message buffer: %s", err) @@ -152,7 +152,7 @@ func ReadMessage(conn net.Conn) (message string, err error) { if messageSize != messageSizeRead { log.Println("not all bytes were read in first tentative, trying to read pending") - buffer, err := readPendingBytes(messageSize, messageSizeRead, messageBuffer, conn) + buffer, err := readPendingBytes(messageSize, messageSizeRead, messageBuffer, stream) if err != nil { err := fmt.Errorf("error on read pending bytes from server: %s", err) diff --git a/core/protocol_test.go b/core/protocol_test.go index 4a63290..e4bb4a6 100644 --- a/core/protocol_test.go +++ b/core/protocol_test.go @@ -2,59 +2,27 @@ package core import ( "bytes" - "fmt" - "net" "testing" - "time" ) -// StreamConn is a fake to net.Conn for test purpose only -type StreamConn struct { - buffer bytes.Buffer +// Stream is a fake for test purpose only +type Stream struct { + buffer bytes.Buffer + limitReader int + limitWriter int } -func (stream *StreamConn) LocalAddr() net.Addr { - return nil +func NewStream() *Stream { + return &Stream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} } -func (stream *StreamConn) RemoteAddr() net.Addr { - return nil -} - -func (stream *StreamConn) Close() error { - return fmt.Errorf("stream conn fake: not implemented, for test purpose only)") -} - -func (stream *StreamConn) SetDeadline(t time.Time) error { - return fmt.Errorf("stream conn fake: not implemented, for test purpose only)") -} - -func (stream *StreamConn) SetReadDeadline(t time.Time) error { - return fmt.Errorf("stream conn fake: not implemented, for test purpose only)") -} - -func (stream *StreamConn) SetWriteDeadline(t time.Time) error { - return fmt.Errorf("stream conn fake: not implemented, for test purpose only)") -} - -func NewStreamConn() *StreamConn { - return &StreamConn{buffer: bytes.Buffer{}} -} - -func NewStreamConnWithData(defaultMessage string) *StreamConn { - conn := &StreamConn{buffer: bytes.Buffer{}} - WriteMessage(defaultMessage, conn) - - return conn -} - -func (stream *StreamConn) Read(b []byte) (n int, err error) { +func (stream *Stream) Read(b []byte) (n int, err error) { var buffer []byte currentBufferSize := len(b) // Will simulate limit size to read buffer. For test purpose only - if currentBufferSize > 20 { - buffer = make([]byte, 20) + if currentBufferSize > stream.limitReader { + buffer = make([]byte, stream.limitReader) } else { buffer = make([]byte, currentBufferSize) @@ -68,13 +36,13 @@ func (stream *StreamConn) Read(b []byte) (n int, err error) { } -func (stream *StreamConn) Write(b []byte) (n int, err error) { +func (stream *Stream) Write(b []byte) (n int, err error) { var buffer []byte currentMessageSize := len(b) // Will simulate limit size to write buffer. For test purpose only - if currentMessageSize > 20 { - buffer = make([]byte, 20) + if currentMessageSize > stream.limitWriter { + buffer = make([]byte, stream.limitWriter) } else { buffer = make([]byte, currentMessageSize) @@ -86,27 +54,28 @@ func (stream *StreamConn) Write(b []byte) (n int, err error) { } func TestWriteShortMessage(t *testing.T) { - stream := NewStreamConn() + stream := NewStream() - bytesWritter, _ := WriteMessage("hellou!", stream) + bytesWriter, _ := WriteMessage("Hello!", stream) - if bytesWritter != 7 { + if bytesWriter != 6 { + t.Log("Expected 6 bytes written, got ', bytesWriter") t.FailNow() } } func TestWriteLargeMessage(t *testing.T) { - stream := NewStreamConn() + stream := NewStream() - bytesWritter, _ := WriteMessage("message to write buffer", stream) + bytesWriter, _ := WriteMessage("message to write buffer", stream) - if bytesWritter != 23 { + if bytesWriter != 23 { t.FailNow() } } - func TestReadShortMessage(t *testing.T) { - stream := NewStreamConnWithData("Hello!") + stream := NewStream() + WriteMessage("Hello!", stream) // Write message to read message, _ := ReadMessage(stream) @@ -116,7 +85,8 @@ func TestReadShortMessage(t *testing.T) { } func TestReadLargeMessage(t *testing.T) { - stream := NewStreamConnWithData("message to read buffer") + stream := NewStream() + WriteMessage("message to read buffer", stream) // Write message to read message, _ := ReadMessage(stream) diff --git a/go.mod b/go.mod deleted file mode 100644 index 6f1793c..0000000 --- a/go.mod +++ /dev/null @@ -1,4 +0,0 @@ -module github.com/open-source-br/epp - -go 1.18 - From 2e47d649a0674bd2545c91f1e8ae43b18e342108 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Sun, 12 Feb 2023 23:59:13 -0300 Subject: [PATCH 02/18] go version --- .github/workflows/pullrequest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index b18eef9..0dfad76 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -23,7 +23,7 @@ jobs: - name: Setup GO uses: actions/setup-go@v3 with: - go-version: "1.18.x" + go-version: "1.19.x" - name: Running Lint (go vet) run: go vet -json ./core > govetoutput.json From 7e59e8a7e28e9c6c84d903fecaee72356c9eec0b Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 09:56:12 -0300 Subject: [PATCH 03/18] updates --- .github/workflows/pullrequest.yml | 5 +++-- core/go.mod | 2 -- core/protocol_test.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 0dfad76..189769d 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -21,9 +21,10 @@ jobs: fetch-depth: 0 - name: Setup GO - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: "1.19.x" + go-version-file: './go.work' + - name: Running Lint (go vet) run: go vet -json ./core > govetoutput.json diff --git a/core/go.mod b/core/go.mod index 995703f..b74b77f 100644 --- a/core/go.mod +++ b/core/go.mod @@ -1,3 +1 @@ module github.com/open-source-br/epp/core - -go 1.18 diff --git a/core/protocol_test.go b/core/protocol_test.go index e4bb4a6..9d57488 100644 --- a/core/protocol_test.go +++ b/core/protocol_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -// Stream is a fake for test purpose only +// Stream is a mock for test purpose only type Stream struct { buffer bytes.Buffer limitReader int From a47093139d5497a3c06ab40579ee4759d0febffa Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:38:21 -0300 Subject: [PATCH 04/18] rewrite path struct --- go.work | 4 ++-- {core => src/core}/go.mod | 0 {core => src/core}/protocol.go | 0 {core => src/core}/protocol_test.go | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename {core => src/core}/go.mod (100%) rename {core => src/core}/protocol.go (100%) rename {core => src/core}/protocol_test.go (100%) diff --git a/go.work b/go.work index 6e9786f..4bc6af1 100644 --- a/go.work +++ b/go.work @@ -1,3 +1,3 @@ -go 1.18 +go 1.21.6 -use ./core +use ./src/core diff --git a/core/go.mod b/src/core/go.mod similarity index 100% rename from core/go.mod rename to src/core/go.mod diff --git a/core/protocol.go b/src/core/protocol.go similarity index 100% rename from core/protocol.go rename to src/core/protocol.go diff --git a/core/protocol_test.go b/src/core/protocol_test.go similarity index 100% rename from core/protocol_test.go rename to src/core/protocol_test.go From 9b1cde63197f16e6c9fa08f492a0da5ecd6f7d00 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:08:14 -0300 Subject: [PATCH 05/18] temp --- .github/workflows/pullrequest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 189769d..7eb7947 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -27,7 +27,7 @@ jobs: - name: Running Lint (go vet) - run: go vet -json ./core > govetoutput.json + run: go vet -json ./src/core > govetoutput.json - name: Running Test run: go test ./core -coverprofile=coverage.out From 2ffb4482fdd66718b8b46e0bbed2669d44b30f62 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:15:15 -0300 Subject: [PATCH 06/18] temp --- .github/workflows/pullrequest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 7eb7947..53ec890 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -30,7 +30,7 @@ jobs: run: go vet -json ./src/core > govetoutput.json - name: Running Test - run: go test ./core -coverprofile=coverage.out + run: go test ./src/core -coverprofile=coverage.out - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master From b4de42fe23a75daa66f8d45a03a86c40029ecc18 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:15:53 -0300 Subject: [PATCH 07/18] ttemp --- sonar-project.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index 2e9b91f..797ea2a 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,8 +6,8 @@ sonar.projectName=EPP sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json -sonar.sources=. +sonar.sources=./src/ sonar.exclusions=**/*_test.go -sonar.tests=. +sonar.tests=./src/ sonar.test.inclusions=**/*_test.go \ No newline at end of file From 5f5e016a9df90bd24503904e6e1828eb1a4a289a Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:55:04 -0300 Subject: [PATCH 08/18] uPDATE FILES --- .cspell.json | 16 ++++++++++++++++ .gitattributes | 1 + .github/workflows/pullrequest.yml | 2 +- LICENSE | 21 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .cspell.json create mode 100644 .gitattributes create mode 100644 LICENSE diff --git a/.cspell.json b/.cspell.json new file mode 100644 index 0000000..d866e52 --- /dev/null +++ b/.cspell.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json", + "useGitignore": true, + "ignorePaths": [".vscode", ".gitignore"], + "dictionaries": [ + "en_US", + "companies", + "misc", + "softwareTerms", + "bash", + "filetypes", + "powershell", + "lorem-ipsum" + ], + "words": [] +} \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..4eb2283 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +text eol=lf \ No newline at end of file diff --git a/.github/workflows/pullrequest.yml b/.github/workflows/pullrequest.yml index 53ec890..cfb7acd 100644 --- a/.github/workflows/pullrequest.yml +++ b/.github/workflows/pullrequest.yml @@ -16,7 +16,7 @@ jobs: steps: - name: Clone Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9252f86 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Open Source BR + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file From 833553a78b0d3508f5c04b52d155fbe68a3f03ca Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:07:48 -0300 Subject: [PATCH 09/18] refactor --- src/core/go.mod | 2 + src/core/protocol_buffer_mock.go | 49 +++++++++++++++++++ src/core/protocol_test.go | 81 +++++++++++--------------------- 3 files changed, 78 insertions(+), 54 deletions(-) create mode 100644 src/core/protocol_buffer_mock.go diff --git a/src/core/go.mod b/src/core/go.mod index b74b77f..105f082 100644 --- a/src/core/go.mod +++ b/src/core/go.mod @@ -1 +1,3 @@ module github.com/open-source-br/epp/core + +go 1.21.6 diff --git a/src/core/protocol_buffer_mock.go b/src/core/protocol_buffer_mock.go new file mode 100644 index 0000000..695a6a0 --- /dev/null +++ b/src/core/protocol_buffer_mock.go @@ -0,0 +1,49 @@ +package core + +import ( + "bytes" +) + +// Stream is a mock for test purpose only +type MockStream struct { + buffer bytes.Buffer + limitReader int + limitWriter int +} + +func (stream *MockStream) Read(b []byte) (n int, err error) { + var buffer []byte + currentBufferSize := len(b) + + // Will simulate limit size to read buffer. For test purpose only + if currentBufferSize > stream.limitReader { + buffer = make([]byte, stream.limitReader) + + } else { + buffer = make([]byte, currentBufferSize) + } + + sizes, err := stream.buffer.Read(buffer) + + copy(b, buffer) + + return sizes, err + +} + +func (stream *MockStream) Write(b []byte) (n int, err error) { + var buffer []byte + currentMessageSize := len(b) + + // Will simulate limit size to write buffer. For test purpose only + if currentMessageSize > stream.limitWriter { + buffer = make([]byte, stream.limitWriter) + + } else { + buffer = make([]byte, currentMessageSize) + } + + copy(buffer, b) + + return stream.buffer.Write(buffer) +} diff --git a/src/core/protocol_test.go b/src/core/protocol_test.go index 9d57488..d392770 100644 --- a/src/core/protocol_test.go +++ b/src/core/protocol_test.go @@ -5,56 +5,8 @@ import ( "testing" ) -// Stream is a mock for test purpose only -type Stream struct { - buffer bytes.Buffer - limitReader int - limitWriter int -} - -func NewStream() *Stream { - return &Stream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} -} - -func (stream *Stream) Read(b []byte) (n int, err error) { - var buffer []byte - currentBufferSize := len(b) - - // Will simulate limit size to read buffer. For test purpose only - if currentBufferSize > stream.limitReader { - buffer = make([]byte, stream.limitReader) - - } else { - buffer = make([]byte, currentBufferSize) - } - - sizes, err := stream.buffer.Read(buffer) - - copy(b, buffer) - - return sizes, err - -} - -func (stream *Stream) Write(b []byte) (n int, err error) { - var buffer []byte - currentMessageSize := len(b) - - // Will simulate limit size to write buffer. For test purpose only - if currentMessageSize > stream.limitWriter { - buffer = make([]byte, stream.limitWriter) - - } else { - buffer = make([]byte, currentMessageSize) - } - - copy(buffer, b) - - return stream.buffer.Write(buffer) -} - func TestWriteShortMessage(t *testing.T) { - stream := NewStream() + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} bytesWriter, _ := WriteMessage("Hello!", stream) @@ -65,7 +17,7 @@ func TestWriteShortMessage(t *testing.T) { } func TestWriteLargeMessage(t *testing.T) { - stream := NewStream() + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} bytesWriter, _ := WriteMessage("message to write buffer", stream) @@ -74,8 +26,8 @@ func TestWriteLargeMessage(t *testing.T) { } } func TestReadShortMessage(t *testing.T) { - stream := NewStream() - WriteMessage("Hello!", stream) // Write message to read + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} + WriteMessage("Hello!", stream) // echo message, _ := ReadMessage(stream) @@ -85,8 +37,8 @@ func TestReadShortMessage(t *testing.T) { } func TestReadLargeMessage(t *testing.T) { - stream := NewStream() - WriteMessage("message to read buffer", stream) // Write message to read + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} + WriteMessage("message to read buffer", stream) // echo message, _ := ReadMessage(stream) @@ -94,3 +46,24 @@ func TestReadLargeMessage(t *testing.T) { t.FailNow() } } + +func TestReadFromEmptyStream(t *testing.T) { + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 20, limitWriter: 20} + + _, err := ReadMessage(stream) + + if err == nil { + t.Fatalf("Expected error when reading from empty stream, got nil") + } +} + +func TestReadShortMessageWithShortLimitReader(t *testing.T) { + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 3, limitWriter: 20} + WriteMessage("Hello!", stream) // echo + + message, _ := ReadMessage(stream) + + if message != "Hello!" { + t.FailNow() + } +} From 8d71b14bc9f8b35c49533be0f2422fbc8ad94822 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:24:57 -0300 Subject: [PATCH 10/18] refactor --- src/core/protocol.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/protocol.go b/src/core/protocol.go index 133e5f2..f3c9d77 100644 --- a/src/core/protocol.go +++ b/src/core/protocol.go @@ -141,6 +141,12 @@ func WriteMessage(message string, stream io.Writer) (n int, err error) { func ReadMessage(stream io.Reader) (message string, err error) { messageSize, _ := readBufferSize(stream) + + if messageSize <= 0 { + err := fmt.Errorf("error on read empty buffer: %s", err) + return "", err + } + messageBuffer := make([]byte, messageSize) messageSizeRead, err := stream.Read(messageBuffer) From 5f82bbbb7a7de83e801d90064e61bc8993e262ce Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:36:13 -0300 Subject: [PATCH 11/18] refactor --- src/core/protocol.go | 1 - src/core/protocol_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/protocol.go b/src/core/protocol.go index f3c9d77..9e7d6e9 100644 --- a/src/core/protocol.go +++ b/src/core/protocol.go @@ -135,7 +135,6 @@ func WriteMessage(message string, stream io.Writer) (n int, err error) { return byteWritten, nil } - return messageSizeWritten, nil } diff --git a/src/core/protocol_test.go b/src/core/protocol_test.go index d392770..7bbd3e3 100644 --- a/src/core/protocol_test.go +++ b/src/core/protocol_test.go @@ -58,7 +58,7 @@ func TestReadFromEmptyStream(t *testing.T) { } func TestReadShortMessageWithShortLimitReader(t *testing.T) { - stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 3, limitWriter: 20} + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 4, limitWriter: 20} WriteMessage("Hello!", stream) // echo message, _ := ReadMessage(stream) From 50fe61f07cf8359055d69af64a02624310f946f6 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:36:59 -0300 Subject: [PATCH 12/18] refactor --- src/core/protocol_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/protocol_test.go b/src/core/protocol_test.go index 7bbd3e3..cfa91e1 100644 --- a/src/core/protocol_test.go +++ b/src/core/protocol_test.go @@ -57,8 +57,8 @@ func TestReadFromEmptyStream(t *testing.T) { } } -func TestReadShortMessageWithShortLimitReader(t *testing.T) { - stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 4, limitWriter: 20} +func TestReadShortMessageWithShortLimitReaderAndWritter(t *testing.T) { + stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 4, limitWriter: 4} WriteMessage("Hello!", stream) // echo message, _ := ReadMessage(stream) From 217b68da91303ce8ad45c2a2a1e8c47177b74687 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:37:11 -0300 Subject: [PATCH 13/18] refactor --- src/core/protocol_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/protocol_test.go b/src/core/protocol_test.go index cfa91e1..d7f221e 100644 --- a/src/core/protocol_test.go +++ b/src/core/protocol_test.go @@ -57,7 +57,7 @@ func TestReadFromEmptyStream(t *testing.T) { } } -func TestReadShortMessageWithShortLimitReaderAndWritter(t *testing.T) { +func TestReadShortMessageWithShortLimitReaderAndWriter(t *testing.T) { stream := &MockStream{buffer: bytes.Buffer{}, limitReader: 4, limitWriter: 4} WriteMessage("Hello!", stream) // echo From cb445f5163418bb2b3a9669fed63200e2caaa019 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:41:06 -0300 Subject: [PATCH 14/18] sonnar exclude mock files --- sonar-project.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 797ea2a..7154f4f 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -7,7 +7,8 @@ sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json sonar.sources=./src/ -sonar.exclusions=**/*_test.go +sonar.exclusions=**/*_test.go,**/*_mock.go + sonar.tests=./src/ sonar.test.inclusions=**/*_test.go \ No newline at end of file From aef633399aef96256956aebe1be01a47b8725554 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:50:44 -0300 Subject: [PATCH 15/18] test --- sonar-project.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index 7154f4f..12b9060 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,9 +6,9 @@ sonar.projectName=EPP sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json -sonar.sources=./src/ +sonar.sources=./ sonar.exclusions=**/*_test.go,**/*_mock.go -sonar.tests=./src/ +sonar.tests=./ sonar.test.inclusions=**/*_test.go \ No newline at end of file From 0017e781d48351321c92f9610f3b206a19aa672b Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:15:51 -0300 Subject: [PATCH 16/18] test --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 12b9060..c790a61 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,7 +6,7 @@ sonar.projectName=EPP sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json -sonar.sources=./ +sonar.sources=./src/**/*.go sonar.exclusions=**/*_test.go,**/*_mock.go From 56847e9d4b57368b0c6f228ad9cc9c2a0ed9e538 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:19:38 -0300 Subject: [PATCH 17/18] temp --- sonar-project.properties | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index c790a61..2841803 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,9 +6,8 @@ sonar.projectName=EPP sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json -sonar.sources=./src/**/*.go +sonar.sources=. sonar.exclusions=**/*_test.go,**/*_mock.go - -sonar.tests=./ +sonar.tests=. sonar.test.inclusions=**/*_test.go \ No newline at end of file From d83e22d7292d430b0f12c3183d4c25a84fc0f9a0 Mon Sep 17 00:00:00 2001 From: "Alex G. Wolff" <13754094+alexgwolff@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:25:12 -0300 Subject: [PATCH 18/18] test --- sonar-project.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 2841803..093defc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,8 +6,9 @@ sonar.projectName=EPP sonar.go.coverage.reportPaths=coverage.out sonar.go.govet.reportPaths=govetoutput.json + sonar.sources=. -sonar.exclusions=**/*_test.go,**/*_mock.go +sonar.exclusions=**/*_test.go sonar.tests=. sonar.test.inclusions=**/*_test.go \ No newline at end of file