@@ -16,6 +16,8 @@ package file
16
16
17
17
import (
18
18
"fmt"
19
+ "io/ioutil"
20
+ "math/rand"
19
21
"strings"
20
22
"testing"
21
23
@@ -212,4 +214,57 @@ func TestFingerprintStartsWith(t *testing.T) {
212
214
}
213
215
}
214
216
217
+ // Generates a file filled with many random bytes, then
218
+ // writes the same bytes to a second file, one byte at a time.
219
+ // Validates, after each byte is written, that fingerprint
220
+ // matching would successfully associate the two files.
221
+ // The static file can be thought of as the present state of
222
+ // the file, while each iteration of the growing file represents
223
+ // a possible state of the same file at a previous time.
224
+ func TestFingerprintStartsWith_FromFile (t * testing.T ) {
225
+ r := rand .New (rand .NewSource (112358 ))
226
+
227
+ operator , _ , tempDir := newTestFileOperator (t , nil , nil )
228
+ operator .fingerprintSize *= 10
229
+
230
+ fileLength := 12 * operator .fingerprintSize
231
+
232
+ // Make a []byte we can write one at a time
233
+ content := make ([]byte , fileLength )
234
+ r .Read (content ) // Fill slice with random bytes
235
+
236
+ // Overwrite some bytes with \n to ensure
237
+ // we are testing a file with multiple lines
238
+ newlineMask := make ([]byte , fileLength )
239
+ r .Read (newlineMask ) // Fill slice with random bytes
240
+ for i , b := range newlineMask {
241
+ if b == 0 && i != 0 { // 1/256 chance, but never first byte
242
+ content [i ] = byte ('\n' )
243
+ }
244
+ }
245
+
246
+ fullFile , err := ioutil .TempFile (tempDir , "" )
247
+ require .NoError (t , err )
248
+ _ , err = fullFile .Write (content )
249
+ require .NoError (t , err )
250
+
251
+ fff , err := operator .NewFingerprint (fullFile )
252
+ require .NoError (t , err )
253
+
254
+ partialFile , err := ioutil .TempFile (tempDir , "" )
255
+ require .NoError (t , err )
256
+
257
+ // Write one byte at a time and validate that updated
258
+ // full fingerprint still starts with partial
259
+ for i := range content {
260
+ _ , err = partialFile .Write (content [i :i ])
261
+ require .NoError (t , err )
262
+
263
+ pff , err := operator .NewFingerprint (fullFile )
264
+ require .NoError (t , err )
265
+
266
+ require .True (t , fff .StartsWith (pff ))
267
+ }
268
+ }
269
+
215
270
// TODO TestConfig (config_test.go) - sets defaults, errors appropriately, etc
0 commit comments