@@ -16,6 +16,7 @@ package jar
16
16
17
17
import (
18
18
"path/filepath"
19
+ "strings"
19
20
"testing"
20
21
21
22
"github.com/google/go-cmp/cmp"
@@ -177,3 +178,59 @@ func TestWalkerRewrite(t *testing.T) {
177
178
t .Errorf ("walking filesystem after rewrite returned diff (-want, +got): %s" , diff )
178
179
}
179
180
}
181
+
182
+ // TestNonDefaultParser verifies that Walker can be configured with a
183
+ // non-default Parser by scanning a large JAR file with two
184
+ // configurations: one where Parser.MaxBytes is larger than the file
185
+ // size and one where Parser.MaxBytes is smaller than the file
186
+ // size. It ensures that the first case succeeds and the second fails.
187
+ func TestNonDefaultParser (t * testing.T ) {
188
+ jar := "400mb_jar_in_jar.jar"
189
+
190
+ tempDir := t .TempDir ()
191
+ src := testdataPath (jar )
192
+ dest := filepath .Join (tempDir , jar )
193
+ cpFile (t , dest , src )
194
+
195
+ tests := []struct {
196
+ desc string
197
+ maxBytes int64
198
+ wantErr bool
199
+ }{
200
+ {
201
+ desc : "MaxBytes > JAR size" ,
202
+ maxBytes : 4 << 30 , // 4GiB
203
+ wantErr : false ,
204
+ },
205
+ {
206
+ desc : "MaxBytes < JAR size" ,
207
+ maxBytes : 4 << 20 , // 4MiB
208
+ wantErr : true ,
209
+ },
210
+ }
211
+
212
+ for _ , tc := range tests {
213
+ t .Run (tc .desc , func (t * testing.T ) {
214
+ var gotErr error
215
+
216
+ p := & Parser {MaxBytes : tc .maxBytes }
217
+ w := & Walker {
218
+ Parser : p ,
219
+ HandleError : func (path string , err error ) {
220
+ if err != nil && strings .HasSuffix (path , filepath .FromSlash ("/" + jar )) {
221
+ gotErr = err
222
+ }
223
+ },
224
+ }
225
+ if err := w .Walk (tempDir ); err != nil {
226
+ t .Errorf ("Walk returned unexpected error: %v" , err )
227
+ }
228
+
229
+ if tc .wantErr && gotErr == nil {
230
+ t .Error ("Parser failed to generate expected error when scanning JARs > MaxBytes, got nil, want error" )
231
+ } else if ! tc .wantErr && gotErr != nil {
232
+ t .Errorf ("Parser generated unexpected error when scanning JARs <= MaxBytes, got %v, want nil error" , gotErr )
233
+ }
234
+ })
235
+ }
236
+ }
0 commit comments