Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
78
plugins/common/encoding/decoder_test.go
Normal file
78
plugins/common/encoding/decoder_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDecoder(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
encoding string
|
||||
input []byte
|
||||
expected []byte
|
||||
expectedErr bool
|
||||
}{
|
||||
{
|
||||
name: "no decoder utf-8",
|
||||
encoding: "",
|
||||
input: []byte("howdy"),
|
||||
expected: []byte("howdy"),
|
||||
},
|
||||
{
|
||||
name: "utf-8 decoder",
|
||||
encoding: "utf-8",
|
||||
input: []byte("howdy"),
|
||||
expected: []byte("howdy"),
|
||||
},
|
||||
{
|
||||
name: "utf-8 decoder invalid bytes replaced with replacement char",
|
||||
encoding: "utf-8",
|
||||
input: []byte("\xff\xfe"),
|
||||
expected: []byte("\uFFFD\uFFFD"),
|
||||
},
|
||||
{
|
||||
name: "utf-16le decoder no BOM",
|
||||
encoding: "utf-16le",
|
||||
input: []byte("h\x00o\x00w\x00d\x00y\x00"),
|
||||
expected: []byte("howdy"),
|
||||
},
|
||||
{
|
||||
name: "utf-16le decoder with BOM",
|
||||
encoding: "utf-16le",
|
||||
input: []byte("\xff\xfeh\x00o\x00w\x00d\x00y\x00"),
|
||||
expected: []byte("\xef\xbb\xbfhowdy"),
|
||||
},
|
||||
{
|
||||
name: "utf-16be decoder no BOM",
|
||||
encoding: "utf-16be",
|
||||
input: []byte("\x00h\x00o\x00w\x00d\x00y"),
|
||||
expected: []byte("howdy"),
|
||||
},
|
||||
{
|
||||
name: "utf-16be decoder with BOM",
|
||||
encoding: "utf-16be",
|
||||
input: []byte("\xfe\xff\x00h\x00o\x00w\x00d\x00y"),
|
||||
expected: []byte("\xef\xbb\xbfhowdy"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
decoder, err := NewDecoder(tt.encoding)
|
||||
require.NoError(t, err)
|
||||
buf := bytes.NewBuffer(tt.input)
|
||||
r := decoder.Reader(buf)
|
||||
actual, err := io.ReadAll(r)
|
||||
if tt.expectedErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue