@@ -97,6 +97,38 @@ func TestNewChunkedReader(t *testing.T) {
97
97
wantChunks : []string {strings .Repeat ("a" , 2048 ), strings .Repeat ("a" , 2048 ), strings .Repeat ("a" , 2048 ), strings .Repeat ("a" , 1024 )},
98
98
wantErr : false ,
99
99
},
100
+ {
101
+ name : "binary data - bin" ,
102
+ input : string (generateBinaryContent ("bin" )),
103
+ chunkSize : DefaultChunkSize ,
104
+ peekSize : DefaultPeekSize ,
105
+ wantChunks : []string {"TuffleHog" },
106
+ wantErr : false ,
107
+ },
108
+ {
109
+ name : "binary data - exe" ,
110
+ input : string (generateBinaryContent ("exe" )),
111
+ chunkSize : DefaultChunkSize ,
112
+ peekSize : DefaultPeekSize ,
113
+ wantChunks : []string {"MZ\x90 \x03 \x00 \x04 \x00 \xff \x00 \xb8 :\xf2 ~\x11 ]\x9b \xc8 O\xa1 g0\xeb \x94 ,\r zV\x88 \xfa \x19 +\xc3 \xd0 nTuffleHog\xab \xcd 8\x04 W\xf1 j\x9e \x03 \xd8 A\xb6 /u\xcc \v \x94 \xe7 P8\xad \x1f c{\x0e \xf5 )\xc4 m\x82 \x10 " },
114
+ wantErr : false ,
115
+ },
116
+ {
117
+ name : "binary data - dmg" ,
118
+ input : string (generateBinaryContent ("dmg" )),
119
+ chunkSize : DefaultChunkSize ,
120
+ peekSize : DefaultPeekSize ,
121
+ wantChunks : []string {"\x00 \x00 \x00 \x00 TruffleHog\x00 \x00 \x00 \x00 koly" },
122
+ wantErr : false ,
123
+ },
124
+ {
125
+ name : "binary data - tag.gz" ,
126
+ input : string (generateBinaryContent ("tar.gz" )),
127
+ chunkSize : DefaultChunkSize ,
128
+ peekSize : DefaultPeekSize ,
129
+ wantChunks : []string {"\x1f \x8b \b this is binary content - trufflehog\x00 \x00 \x00 \x00 " },
130
+ wantErr : false ,
131
+ },
100
132
}
101
133
102
134
for _ , tt := range tests {
@@ -212,3 +244,43 @@ func TestReadInChunksWithCancellation(t *testing.T) {
212
244
}
213
245
}
214
246
}
247
+
248
+ // https://en.wikipedia.org/wiki/List_of_file_signatures
249
+ func generateBinaryContent (contentType string ) []byte {
250
+ switch contentType {
251
+ case "tar.gz" :
252
+ return []byte {
253
+ 0x1F , 0x8B , 0x08 , // GZIP magic + compression method
254
+ 0x74 , 0x68 , 0x69 , 0x73 , 0x20 , 0x69 , 0x73 , 0x20 ,
255
+ 0x62 , 0x69 , 0x6E , 0x61 , 0x72 , 0x79 , 0x20 , 0x63 ,
256
+ 0x6F , 0x6E , 0x74 , 0x65 , 0x6E , 0x74 , 0x20 , 0x2D ,
257
+ 0x20 , 0x74 , 0x72 , 0x75 , 0x66 , 0x66 , 0x6C , 0x65 ,
258
+ 0x68 , 0x6F , 0x67 , 0x00 , 0x00 , 0x00 , 0x00 ,
259
+ }
260
+ case "exe" :
261
+ return []byte {
262
+ // https://superuser.com/questions/1334140/how-to-check-if-a-binary-is-16-bit-on-windows
263
+ 0x4D , 0x5A , // 'MZ' magic number for EXE
264
+ 0x90 , 0x03 , 0x00 , 0x04 , 0x00 , 0xff , 0x00 , 0xb8 ,
265
+ 0x3a , 0xf2 , 0x7e , 0x11 , 0x5d , 0x9b , 0xc8 , 0x4f ,
266
+ 0xa1 , 0x67 , 0x30 , 0xeb , 0x94 , 0x2c , 0x0d , 0x7a ,
267
+ 0x56 , 0x88 , 0xfa , 0x19 , 0x2b , 0xc3 , 0xd0 , 0x6e ,
268
+ 0x54 , 0x75 , 0x66 , 0x66 , 0x6C , 0x65 , 0x48 , 0x6F ,
269
+ 0x67 , 0xab , 0xcd , 0x38 , 0x04 , 0x57 , 0xf1 , 0x6a ,
270
+ 0x9e , 0x03 , 0xd8 , 0x41 , 0xb6 , 0x2f , 0x75 , 0xcc ,
271
+ 0x0b , 0x94 , 0xe7 , 0x50 , 0x38 , 0xad , 0x1f , 0x63 ,
272
+ 0x7b , 0x0e , 0xf5 , 0x29 , 0xc4 , 0x6d , 0x82 , 0x10 ,
273
+ }
274
+ case "bin" :
275
+ return []byte {0x54 , 0x75 , 0x66 , 0x66 , 0x6C , 0x65 , 0x48 , 0x6F , 0x67 }
276
+ case "dmg" :
277
+ return []byte {
278
+ 0x00 , 0x00 , 0x00 , 0x00 ,
279
+ 0x54 , 0x72 , 0x75 , 0x66 , 0x66 , 0x6C , 0x65 , 0x48 , 0x6F , 0x67 ,
280
+ 0x00 , 0x00 , 0x00 , 0x00 ,
281
+ 0x6B , 0x6F , 0x6C , 0x79 , // "koly" magic number for dmg
282
+ }
283
+ }
284
+
285
+ return nil
286
+ }
0 commit comments