Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soundmonster committed Dec 19, 2024
1 parent 0e950d9 commit cb465f7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- [#227](https://github.com/deviceinsight/kafkactl/issues/227) Incorrect handling of Base64-encoded values when producing from JSON

## 5.4.0 - 2024-11-28
### Added
Expand Down
65 changes: 65 additions & 0 deletions internal/producer/MessageSerializer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package producer

import (
"reflect"
"testing"
)

func TestDecodeBytesBase64WhenInputIsNil(t *testing.T) {
got, err := decodeBytes(nil, "base64")
if got != nil {
t.Errorf("Expected nil, got %v", got)
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
}

func TestDecodeBytesBase64WhenInputIsInvalid(t *testing.T) {
got, err := decodeBytes([]byte("..this..is..not..base64.."), "base64")
if got != nil {
t.Errorf("Expected nil, got %v", got)
}
if err == nil {
t.Errorf("Expected error, got nil")
}
}

func TestDecodeBytesBase64WithRegularInput(t *testing.T) {
// length 4
got, err := decodeBytes([]byte("dGVzdA=="), "base64")
if string(got) != "test" {
t.Errorf("Expected hello, got %v", string(got))
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
// length 5
got, err = decodeBytes([]byte("aGVsbG8="), "base64")
if string(got) != "hello" {
t.Errorf("Expected hello, got %v", string(got))
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}

// length 6
got, err = decodeBytes([]byte("aGVsbG8h"), "base64")
if string(got) != "hello!" {
t.Errorf("Expected hello, got %v", string(got))
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
}

func TestDecodeBytesBase64WithZeroPaddedInput(t *testing.T) {
got, err := decodeBytes([]byte("AAAAAAAD"), "base64")
expected := []byte{0, 0, 0, 0, 0, 3}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected hello, got %v", got)
}
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
}

0 comments on commit cb465f7

Please sign in to comment.