Skip to content

Commit 187f835

Browse files
committed
chore: add a test with binary data
1 parent a898111 commit 187f835

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

bundle/bundle_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
package bundle_test
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"crypto/sha256"
23+
"encoding/hex"
24+
"io"
25+
"os"
2126
"testing"
2227

2328
"github.com/golang/mock/gomock"
2429
"github.com/rs/zerolog"
2530
"github.com/stretchr/testify/assert"
2631
"github.com/stretchr/testify/require"
32+
"golang.org/x/net/html/charset"
2733

2834
"github.com/snyk/code-client-go/bundle"
2935
"github.com/snyk/code-client-go/internal/deepcode"
@@ -104,3 +110,36 @@ func Test_UploadBatch(t *testing.T) {
104110
assert.NotEqual(t, oldHash, newHash)
105111
})
106112
}
113+
114+
func Test_BundleEncoding(t *testing.T) {
115+
t.Run("utf-8 encoded content", func(t *testing.T) {
116+
content := []byte("hello")
117+
bundle := deepcode.BundleFileFrom(content)
118+
119+
utf8Reader, err := charset.NewReaderLabel("UTF-8", bytes.NewReader([]byte(bundle.Content)))
120+
assert.NoError(t, err)
121+
122+
actualContent, err := io.ReadAll(utf8Reader)
123+
assert.NoError(t, err)
124+
125+
actualShasum := sha256.Sum256(actualContent)
126+
assert.Equal(t, bundle.Hash, hex.EncodeToString(actualShasum[:]))
127+
})
128+
129+
t.Run("non utf-8 / binary file", func(t *testing.T) {
130+
content, err := os.ReadFile("testdata/rshell_font.php")
131+
assert.NoError(t, err)
132+
133+
bundle := deepcode.BundleFileFrom(content)
134+
135+
utf8Reader, err := charset.NewReaderLabel("UTF-8", bytes.NewReader(content))
136+
assert.NoError(t, err)
137+
138+
actualContent, err := io.ReadAll(utf8Reader)
139+
assert.NoError(t, err)
140+
141+
actualHash := sha256.Sum256(actualContent)
142+
143+
assert.Equal(t, bundle.Hash, hex.EncodeToString(actualHash[:]))
144+
})
145+
}

bundle/testdata/rshell_font.php

2.86 KB
Binary file not shown.

0 commit comments

Comments
 (0)