|
17 | 17 | package bundle_test
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "bytes" |
20 | 21 | "context"
|
| 22 | + "crypto/sha256" |
| 23 | + "encoding/hex" |
| 24 | + "io" |
| 25 | + "os" |
21 | 26 | "testing"
|
22 | 27 |
|
23 | 28 | "github.com/golang/mock/gomock"
|
24 | 29 | "github.com/rs/zerolog"
|
25 | 30 | "github.com/stretchr/testify/assert"
|
26 | 31 | "github.com/stretchr/testify/require"
|
| 32 | + "golang.org/x/net/html/charset" |
27 | 33 |
|
28 | 34 | "github.com/snyk/code-client-go/bundle"
|
29 | 35 | "github.com/snyk/code-client-go/internal/deepcode"
|
@@ -104,3 +110,36 @@ func Test_UploadBatch(t *testing.T) {
|
104 | 110 | assert.NotEqual(t, oldHash, newHash)
|
105 | 111 | })
|
106 | 112 | }
|
| 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 | +} |
0 commit comments