fuzzer.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import random
  4. from pyflashsim import FlashSim
  5. from pyringfs import RingFSFlashPartition, RingFS
  6. def compare(a, b):
  7. if a != b:
  8. print ">>> %s != %s" % (a, b)
  9. return a == b
  10. class FuzzRun(object):
  11. def __init__(self, name, version, object_size, sector_size, total_sectors, sector_offset, sector_count):
  12. print "FuzzRun[%s]: v=%08x os=%d ss=%d ts=%d so=%d sc=%d" % (name, version, object_size, sector_size,
  13. total_sectors, sector_offset, sector_count)
  14. sim = FlashSim(name, total_sectors*sector_size, sector_size)
  15. def op_sector_erase(flash, address):
  16. sim.sector_erase(address)
  17. return 0
  18. def op_program(flash, address, data):
  19. sim.program(address, data)
  20. return len(data)
  21. def op_read(flash, address, size):
  22. return sim.read(address, size)
  23. self.version = version
  24. self.object_size = object_size
  25. self.flash = RingFSFlashPartition(sector_size, sector_offset, sector_count,
  26. op_sector_erase, op_program, op_read)
  27. self.fs = RingFS(self.flash, self.version, self.object_size)
  28. def run(self):
  29. self.fs.format()
  30. self.fs.dump()
  31. def do_append():
  32. self.fs.append('x'*self.object_size)
  33. def do_fetch():
  34. self.fs.fetch()
  35. def do_rewind():
  36. self.fs.rewind()
  37. def do_discard():
  38. self.fs.discard()
  39. for i in xrange(1000):
  40. fun = random.choice([do_append]*100 + [do_fetch]*100 + [do_rewind]*10 + [do_discard]*10)
  41. print i, fun.__name__
  42. fun()
  43. # consistency check
  44. newfs = RingFS(self.flash, self.version, self.object_size)
  45. try:
  46. assert newfs.scan() == 0
  47. assert compare(newfs.ringfs.read.sector, self.fs.ringfs.read.sector)
  48. assert compare(newfs.ringfs.read.slot, self.fs.ringfs.read.slot)
  49. assert compare(newfs.ringfs.write.sector, self.fs.ringfs.write.sector)
  50. assert compare(newfs.ringfs.write.slot, self.fs.ringfs.write.slot)
  51. except AssertionError:
  52. print "self.fs:"
  53. self.fs.dump()
  54. print "newfs:"
  55. newfs.dump()
  56. raise
  57. sector_size = random.randint(16, 256)
  58. total_sectors = random.randint(2, 8)
  59. sector_offset = random.randint(0, total_sectors-2)
  60. sector_count = random.randint(2, total_sectors-sector_offset)
  61. version = random.randint(0, 0xffffffff)
  62. object_size = random.randint(1, sector_size-8)
  63. f = FuzzRun('tests/fuzzer.sim', version, object_size, sector_size, total_sectors, sector_offset, sector_count)
  64. f.run()