Skip to content

Commit 2663ed7

Browse files
committed
chore: add a test with binary data
1 parent a898111 commit 2663ed7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

bundle/bundle_test.go

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

bundle/testdata/rshell_font.php

2.86 KB
Binary file not shown.

0 commit comments

Comments
 (0)