diff --git a/CHANGELOG.md b/CHANGELOG.md index 1579b50..4076241 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/producer/MessageSerializer_test.go b/internal/producer/MessageSerializer_test.go new file mode 100644 index 0000000..a273be7 --- /dev/null +++ b/internal/producer/MessageSerializer_test.go @@ -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) + } +}