get ready for python 3, refactor code to be easier to follow/change
This commit is contained in:
parent
494a5eaaf4
commit
5a7e6ad76b
@ -13,6 +13,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -68,6 +69,12 @@ def main():
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def writeAndRunCandidate(test):
|
||||||
|
with open(".candidate.test", 'wb') as outf:
|
||||||
|
outf.write(test)
|
||||||
|
r = runCandidate(".candidate.test")
|
||||||
|
return r
|
||||||
|
|
||||||
def structure(result):
|
def structure(result):
|
||||||
OneOfs = []
|
OneOfs = []
|
||||||
currentOneOf = []
|
currentOneOf = []
|
||||||
@ -85,90 +92,83 @@ def main():
|
|||||||
|
|
||||||
initial = runCandidate(test)
|
initial = runCandidate(test)
|
||||||
if not checks(initial):
|
if not checks(initial):
|
||||||
print "STARTING TEST DOES NOT SATISFY REDUCTION CRITERIA"
|
print("STARTING TEST DOES NOT SATISFY REDUCTION CRITERIA")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
with open(test, 'rb') as test:
|
with open(test, 'rb') as test:
|
||||||
currentTest = bytearray(test.read())
|
currentTest = bytearray(test.read())
|
||||||
|
|
||||||
print "ORIGINAL TEST HAS", len(currentTest), "BYTES"
|
print("ORIGINAL TEST HAS", len(currentTest), "BYTES")
|
||||||
|
|
||||||
s = structure(initial)
|
s = structure(initial)
|
||||||
print "LAST BYTE READ IS", s[1]
|
print("LAST BYTE READ IS", s[1])
|
||||||
|
|
||||||
if s[1] < len(currentTest):
|
if s[1] < len(currentTest):
|
||||||
print "SHRINKING TO IGNORE UNREAD BYTES"
|
print("SHRINKING TO IGNORE UNREAD BYTES")
|
||||||
currentTest = currentTest[:s[1]+1]
|
currentTest = currentTest[:s[1]+1]
|
||||||
|
|
||||||
changed = True
|
changed = True
|
||||||
while changed:
|
while changed:
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
cuts = s[0]
|
cuts = s[0]
|
||||||
for c in cuts:
|
for c in cuts:
|
||||||
newTest = currentTest[:c[0]] + currentTest[c[1]+1:]
|
newTest = currentTest[:c[0]] + currentTest[c[1]+1:]
|
||||||
with open(".candidate.test", 'wb') as outf:
|
r = writeAndRunCandidate(".candidate.test")
|
||||||
outf.write(newTest)
|
|
||||||
r = runCandidate(".candidate.test")
|
|
||||||
if checks(r):
|
if checks(r):
|
||||||
print "ONEOF REMOVAL REDUCED TEST TO", len(newTest), "BYTES"
|
print("ONEOF REMOVAL REDUCED TEST TO", len(newTest), "BYTES")
|
||||||
s = structure(r)
|
|
||||||
changed = True
|
changed = True
|
||||||
currentTest = newTest
|
|
||||||
break
|
|
||||||
if changed:
|
|
||||||
continue
|
|
||||||
for b in range(0, len(currentTest)):
|
|
||||||
for v in range(b+1, len(currentTest)):
|
|
||||||
newTest = currentTest[:b] + currentTest[v:]
|
|
||||||
with open(".candidate.test", 'wb') as outf:
|
|
||||||
outf.write(newTest)
|
|
||||||
r = runCandidate(".candidate.test")
|
|
||||||
if checks(r):
|
|
||||||
print "BYTE RANGE REMOVAL REDUCED TEST TO", len(newTest), "BYTES"
|
|
||||||
s = structure(r)
|
|
||||||
changed = True
|
|
||||||
currentTest = newTest
|
|
||||||
break
|
|
||||||
if changed:
|
|
||||||
continue
|
|
||||||
for b in range(0, len(currentTest)):
|
|
||||||
for v in range(0, currentTest[b]):
|
|
||||||
newTest = bytearray(currentTest)
|
|
||||||
newTest[b] = v
|
|
||||||
with open(".candidate.test", 'wb') as outf:
|
|
||||||
outf.write(newTest)
|
|
||||||
r = runCandidate(".candidate.test")
|
|
||||||
if checks(r):
|
|
||||||
print "BYTE REDUCTION: BYTE", b, "FROM", currentTest[b], "TO", v
|
|
||||||
s = structure(r)
|
|
||||||
changed = True
|
|
||||||
currentTest = newTest
|
|
||||||
break
|
|
||||||
for b in range(0, len(currentTest)):
|
|
||||||
if currentTest[b] == 0:
|
|
||||||
continue
|
|
||||||
newTest = bytearray(currentTest)
|
|
||||||
newTest[b] = currentTest[b]-1
|
|
||||||
newTest = newTest[:b+1] + newTest[b+2:]
|
|
||||||
with open(".candidate.test", 'wb') as outf:
|
|
||||||
outf.write(newTest)
|
|
||||||
r = runCandidate(".candidate.test")
|
|
||||||
if checks(r):
|
|
||||||
print "BYTE REDUCE AND DELETE AT BYTE", b
|
|
||||||
s = structure(r)
|
|
||||||
changed = True
|
|
||||||
currentTest = newTest
|
|
||||||
break
|
break
|
||||||
|
|
||||||
print "NO REDUCTIONS FOUND"
|
if not changed:
|
||||||
|
for b in range(0, len(currentTest)):
|
||||||
|
for v in range(b+1, len(currentTest)):
|
||||||
|
newTest = currentTest[:b] + currentTest[v:]
|
||||||
|
r = writeAndRunCandidate(".candidate.test")
|
||||||
|
if checks(r):
|
||||||
|
print("BYTE RANGE REMOVAL REDUCED TEST TO", len(newTest), "BYTES")
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not changed:
|
||||||
|
for b in range(0, len(currentTest)):
|
||||||
|
for v in range(0, currentTest[b]):
|
||||||
|
newTest = bytearray(currentTest)
|
||||||
|
newTest[b] = v
|
||||||
|
r = writeAndRunCandidate(newTest)
|
||||||
|
if checks(r):
|
||||||
|
print("BYTE REDUCTION: BYTE", b, "FROM", currentTest[b], "TO", v)
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
if changed:
|
||||||
|
break
|
||||||
|
|
||||||
|
if not changed:
|
||||||
|
for b in range(0, len(currentTest)):
|
||||||
|
if currentTest[b] == 0:
|
||||||
|
continue
|
||||||
|
newTest = bytearray(currentTest)
|
||||||
|
newTest[b] = currentTest[b]-1
|
||||||
|
newTest = newTest[:b+1] + newTest[b+2:]
|
||||||
|
r = writeAndRunCandidate(newTest)
|
||||||
|
if checks(r):
|
||||||
|
print("BYTE REDUCE AND DELETE AT BYTE", b)
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if changed:
|
||||||
|
currentTest = newTest
|
||||||
|
s = structure(r)
|
||||||
|
|
||||||
|
print("NO REDUCTIONS FOUND")
|
||||||
|
|
||||||
if (s[1] + 1) > len(currentTest):
|
if (s[1] + 1) > len(currentTest):
|
||||||
print "PADDING TEST WITH", (s[1] + 1) - len(currentTest), "ZEROS"
|
print("PADDING TEST WITH", (s[1] + 1) - len(currentTest), "ZEROS")
|
||||||
padding = bytearray('\x00' * ((s[1] + 1) - len(currentTest)))
|
padding = bytearray('\x00' * ((s[1] + 1) - len(currentTest)))
|
||||||
currentTest = currentTest + padding
|
currentTest = currentTest + padding
|
||||||
|
|
||||||
print
|
print()
|
||||||
print "WRITING REDUCED TEST TO", out
|
print("WRITING REDUCED TEST WITH", len(currentTest), "BYTES TO", out)
|
||||||
|
|
||||||
with open(out, 'wb') as outf:
|
with open(out, 'wb') as outf:
|
||||||
outf.write(currentTest)
|
outf.write(currentTest)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user