4 # Usage: fileDiffer <afile> <bfile> <list of disk changes>
8 # quick regression test suite
9 # Tests LUKS images for changes at certain disk offsets
11 # Does fast python code has to look ugly or is it just me?
24 mychanges = changes();
26 mychanges.mode = 'ALLOWED'
28 mychanges.mode = 'REQUIRED'
29 mychanges.strictness = 'RANDOM'
31 mychanges.mode = 'REQUIRED'
32 mychanges.strictness = 'SEMANTIC'
34 dashIndex = i.find('-')
36 mychanges.starts = int(i[1:])
37 mychanges.ends = mychanges.starts
39 mychanges.starts = int(i[1:dashIndex])
40 mychanges.ends = int(i[dashIndex+1:])
42 changelist.append(mychanges)
43 mychanges = changes();
45 # mychanges.ends will be fixed later
46 mychanges.mode = 'FORBIDDEN'
47 changelist.append(mychanges)
48 return [aFileName, bFileName, changelist]
52 if i >= c.starts and i<=c.ends:
57 if i <= c.starts or i <= c.ends:
58 newchangelist.append(c)
61 [aFileName, bFileName, changelist] = parseArgs(sys.argv)
63 aFile = open(aFileName,'r')
64 bFile = open(bFileName,'r')
66 aString = aFile.read()
67 bString = bFile.read()
69 if len(aString) != len(bString):
70 sys.exit("Mismatch different file sizes")
72 fileLen = len(aString)
73 fileLen10th = fileLen/10
75 # Create a catch all entry
76 changelist[-1].ends = fileLen
78 print "Changes list: (FORBIDDEN default)"
79 print "start\tend\tmode\t\tstrictness"
81 if i.mode == 'REQUIRED':
82 print "%d\t%d\t%s\t%s" % (i.starts, i.ends, i.mode, i.strictness)
84 print "%d\t%d\t%s" % (i.starts, i.ends, i.mode)
94 while filepos < fileLen:
96 if modeNotTrivial == 1:
98 # print (filepos, c.mode)
99 if c.mode == 'REQUIRED':
100 if aString[filepos] == bString[filepos]:
103 if aString[filepos] != bString[filepos] and c.mode != 'ALLOWED':
104 sys.exit("Mismatch at %d: change forbidden" % filepos)
106 # Do some maintaince, print progress bar, and clean changelist
108 # Maintaining two counters appears to be faster than modulo operation
109 if fileLen10thC == fileLen10th:
111 sys.stdout.write(".")
113 changelist = cleanchanges(filepos)
114 if len(changelist) == 1:
116 filepos = filepos + 1
117 fileLen10thC = fileLen10thC + 1
120 if c.mode == 'REQUIRED':
121 if c.strictness == 'SEMANTIC' and c.miss == (c.ends-c.starts+1):
122 sys.exit("Mismatch: not even a single change in region %d-%d." % (c.starts, c.ends))
123 # This is not correct. We should do a statistical test
124 # of the sampled data against the hypothetical distribution
125 # of collision. Chi-Square Test.
126 if c.strictness == 'RANDOM' and c.miss == (c.ends-c.starts+1):
127 sys.exit("Mismatch: not even a single change in region %d-%d." % (c.starts, c.ends))
129 print ".] - everything ok"