gen_esp32part_tests.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/usr/bin/env python
  2. import unittest
  3. import struct
  4. import csv
  5. import sys
  6. import subprocess
  7. import tempfile
  8. import os
  9. sys.path.append("..")
  10. from gen_esp32part import *
  11. SIMPLE_CSV = """
  12. # Name,Type,SubType,Offset,Size,Flags
  13. factory,0,2,65536,1048576,
  14. """
  15. LONGER_BINARY_TABLE = ""
  16. # type 0x00, subtype 0x00,
  17. # offset 64KB, size 1MB
  18. LONGER_BINARY_TABLE += "\xAA\x50\x00\x00" + \
  19. "\x00\x00\x01\x00" + \
  20. "\x00\x00\x10\x00" + \
  21. "factory\0" + ("\0"*8) + \
  22. "\x00\x00\x00\x00"
  23. # type 0x01, subtype 0x20,
  24. # offset 0x110000, size 128KB
  25. LONGER_BINARY_TABLE += "\xAA\x50\x01\x20" + \
  26. "\x00\x00\x11\x00" + \
  27. "\x00\x02\x00\x00" + \
  28. "data" + ("\0"*12) + \
  29. "\x00\x00\x00\x00"
  30. # type 0x10, subtype 0x00,
  31. # offset 0x150000, size 1MB
  32. LONGER_BINARY_TABLE += "\xAA\x50\x10\x00" + \
  33. "\x00\x00\x15\x00" + \
  34. "\x00\x10\x00\x00" + \
  35. "second" + ("\0"*10) + \
  36. "\x00\x00\x00\x00"
  37. LONGER_BINARY_TABLE += "\xFF" * 32
  38. def _strip_trailing_ffs(binary_table):
  39. """
  40. Strip all FFs down to the last 32 bytes (terminating entry)
  41. """
  42. while binary_table.endswith("\xFF"*64):
  43. binary_table = binary_table[0:len(binary_table)-32]
  44. return binary_table
  45. class CSVParserTests(unittest.TestCase):
  46. def test_simple_partition(self):
  47. table = PartitionTable.from_csv(SIMPLE_CSV)
  48. self.assertEqual(len(table), 1)
  49. self.assertEqual(table[0].name, "factory")
  50. self.assertEqual(table[0].type, 0)
  51. self.assertEqual(table[0].subtype, 2)
  52. self.assertEqual(table[0].offset, 65536)
  53. self.assertEqual(table[0].size, 1048576)
  54. def test_require_type(self):
  55. csv = """
  56. # Name,Type, SubType,Offset,Size
  57. ihavenotype,
  58. """
  59. with self.assertRaisesRegexp(InputError, "type"):
  60. PartitionTable.from_csv(csv)
  61. def test_type_subtype_names(self):
  62. csv_magicnumbers = """
  63. # Name, Type, SubType, Offset, Size
  64. myapp, 0, 0,, 0x100000
  65. myota_0, 0, 0x10,, 0x100000
  66. myota_1, 0, 0x11,, 0x100000
  67. myota_15, 0, 0x1f,, 0x100000
  68. mytest, 0, 0x20,, 0x100000
  69. myota_status, 1, 0,, 0x100000
  70. """
  71. csv_nomagicnumbers = """
  72. # Name, Type, SubType, Offset, Size
  73. myapp, app, factory,, 0x100000
  74. myota_0, app, ota_0,, 0x100000
  75. myota_1, app, ota_1,, 0x100000
  76. myota_15, app, ota_15,, 0x100000
  77. mytest, app, test,, 0x100000
  78. myota_status, data, ota,, 0x100000
  79. """
  80. # make two equivalent partition tables, one using
  81. # magic numbers and one using shortcuts. Ensure they match
  82. magic = PartitionTable.from_csv(csv_magicnumbers)
  83. magic.verify()
  84. nomagic = PartitionTable.from_csv(csv_nomagicnumbers)
  85. nomagic.verify()
  86. self.assertEqual(nomagic["myapp"].type, 0)
  87. self.assertEqual(nomagic["myapp"].subtype, 0)
  88. self.assertEqual(nomagic["myapp"], magic["myapp"])
  89. self.assertEqual(nomagic["myota_0"].type, 0)
  90. self.assertEqual(nomagic["myota_0"].subtype, 0x10)
  91. self.assertEqual(nomagic["myota_0"], magic["myota_0"])
  92. self.assertEqual(nomagic["myota_15"], magic["myota_15"])
  93. self.assertEqual(nomagic["mytest"], magic["mytest"])
  94. self.assertEqual(nomagic["myota_status"], magic["myota_status"])
  95. #self.assertEqual(nomagic.to_binary(), magic.to_binary())
  96. def test_unit_suffixes(self):
  97. csv = """
  98. # Name, Type, Subtype, Offset, Size
  99. one_megabyte, app, factory, 32k, 1M
  100. """
  101. t = PartitionTable.from_csv(csv)
  102. t.verify()
  103. self.assertEqual(t[0].offset, 32*1024)
  104. self.assertEqual(t[0].size, 1*1024*1024)
  105. def test_default_offsets(self):
  106. csv = """
  107. # Name, Type, Subtype, Offset, Size
  108. first, app, factory,, 1M
  109. second, data, 0x15,, 1M
  110. minidata, data, 0x40,, 32K
  111. otherapp, app, factory,, 1M
  112. """
  113. t = PartitionTable.from_csv(csv)
  114. # 'first'
  115. self.assertEqual(t[0].offset, 0x010000) # 64KB boundary as it's an app image
  116. self.assertEqual(t[0].size, 0x100000) # Size specified in CSV
  117. # 'second'
  118. self.assertEqual(t[1].offset, 0x110000) # prev offset+size
  119. self.assertEqual(t[1].size, 0x100000) # Size specified in CSV
  120. # 'minidata'
  121. self.assertEqual(t[2].offset, 0x210000)
  122. # 'otherapp'
  123. self.assertEqual(t[3].offset, 0x220000) # 64KB boundary as it's an app image
  124. def test_negative_size_to_offset(self):
  125. csv = """
  126. # Name, Type, Subtype, Offset, Size
  127. first, app, factory, 0x10000, -2M
  128. second, data, 0x15, , 1M
  129. """
  130. t = PartitionTable.from_csv(csv)
  131. t.verify()
  132. # 'first'
  133. self.assertEqual(t[0].offset, 0x10000) # in CSV
  134. self.assertEqual(t[0].size, 0x200000 - t[0].offset) # Up to 2M
  135. # 'second'
  136. self.assertEqual(t[1].offset, 0x200000) # prev offset+size
  137. def test_overlapping_offsets_fail(self):
  138. csv = """
  139. first, app, factory, 0x100000, 2M
  140. second, app, ota_0, 0x200000, 1M
  141. """
  142. t = PartitionTable.from_csv(csv)
  143. with self.assertRaisesRegexp(InputError, "overlap"):
  144. t.verify()
  145. class BinaryOutputTests(unittest.TestCase):
  146. def test_binary_entry(self):
  147. csv = """
  148. first, 0x30, 0xEE, 0x100400, 0x300000
  149. """
  150. t = PartitionTable.from_csv(csv)
  151. tb = _strip_trailing_ffs(t.to_binary())
  152. self.assertEqual(len(tb), 64)
  153. self.assertEqual('\xAA\x50', tb[0:2]) # magic
  154. self.assertEqual('\x30\xee', tb[2:4]) # type, subtype
  155. eo, es = struct.unpack("<LL", tb[4:12])
  156. self.assertEqual(eo, 0x100400) # offset
  157. self.assertEqual(es, 0x300000) # size
  158. def test_multiple_entries(self):
  159. csv = """
  160. first, 0x30, 0xEE, 0x100400, 0x300000
  161. second,0x31, 0xEF, , 0x100000
  162. """
  163. t = PartitionTable.from_csv(csv)
  164. tb = _strip_trailing_ffs(t.to_binary())
  165. self.assertEqual(len(tb), 96)
  166. self.assertEqual('\xAA\x50', tb[0:2])
  167. self.assertEqual('\xAA\x50', tb[32:34])
  168. def test_encrypted_flag(self):
  169. csv = """
  170. # Name, Type, Subtype, Offset, Size, Flags
  171. first, app, factory,, 1M, encrypted
  172. """
  173. t = PartitionTable.from_csv(csv)
  174. self.assertTrue(t[0].encrypted)
  175. tb = _strip_trailing_ffs(t.to_binary())
  176. tr = PartitionTable.from_binary(tb)
  177. self.assertTrue(tr[0].encrypted)
  178. class BinaryParserTests(unittest.TestCase):
  179. def test_parse_one_entry(self):
  180. # type 0x30, subtype 0xee,
  181. # offset 1MB, size 2MB
  182. entry = "\xAA\x50\x30\xee" + \
  183. "\x00\x00\x10\x00" + \
  184. "\x00\x00\x20\x00" + \
  185. "0123456789abc\0\0\0" + \
  186. "\x00\x00\x00\x00" + \
  187. "\xFF" * 32
  188. # verify that parsing 32 bytes as a table
  189. # or as a single Definition are the same thing
  190. t = PartitionTable.from_binary(entry)
  191. self.assertEqual(len(t), 1)
  192. t[0].verify()
  193. e = PartitionDefinition.from_binary(entry[:32])
  194. self.assertEqual(t[0], e)
  195. e.verify()
  196. self.assertEqual(e.type, 0x30)
  197. self.assertEqual(e.subtype, 0xEE)
  198. self.assertEqual(e.offset, 0x100000)
  199. self.assertEqual(e.size, 0x200000)
  200. self.assertEqual(e.name, "0123456789abc")
  201. def test_multiple_entries(self):
  202. t = PartitionTable.from_binary(LONGER_BINARY_TABLE)
  203. t.verify()
  204. self.assertEqual(3, len(t))
  205. self.assertEqual(t[0].type, PartitionDefinition.APP_TYPE)
  206. self.assertEqual(t[0].name, "factory")
  207. self.assertEqual(t[1].type, PartitionDefinition.DATA_TYPE)
  208. self.assertEqual(t[1].name, "data")
  209. self.assertEqual(t[2].type, 0x10)
  210. self.assertEqual(t[2].name, "second")
  211. round_trip = _strip_trailing_ffs(t.to_binary())
  212. self.assertEqual(round_trip, LONGER_BINARY_TABLE)
  213. def test_bad_magic(self):
  214. bad_magic = "OHAI" + \
  215. "\x00\x00\x10\x00" + \
  216. "\x00\x00\x20\x00" + \
  217. "0123456789abc\0\0\0" + \
  218. "\x00\x00\x00\x00"
  219. with self.assertRaisesRegexp(InputError, "Invalid magic bytes"):
  220. PartitionTable.from_binary(bad_magic)
  221. def test_bad_length(self):
  222. bad_length = "OHAI" + \
  223. "\x00\x00\x10\x00" + \
  224. "\x00\x00\x20\x00" + \
  225. "0123456789"
  226. with self.assertRaisesRegexp(InputError, "32 bytes"):
  227. PartitionTable.from_binary(bad_length)
  228. class CSVOutputTests(unittest.TestCase):
  229. def test_output_simple_formatting(self):
  230. table = PartitionTable.from_csv(SIMPLE_CSV)
  231. as_csv = table.to_csv(True)
  232. c = csv.reader(as_csv.split("\n"))
  233. # first two lines should start with comments
  234. self.assertEqual(c.next()[0][0], "#")
  235. self.assertEqual(c.next()[0][0], "#")
  236. row = c.next()
  237. self.assertEqual(row[0], "factory")
  238. self.assertEqual(row[1], "0")
  239. self.assertEqual(row[2], "2")
  240. self.assertEqual(row[3], "0x10000") # reformatted as hex
  241. self.assertEqual(row[4], "0x100000") # also hex
  242. # round trip back to a PartitionTable and check is identical
  243. roundtrip = PartitionTable.from_csv(as_csv)
  244. self.assertEqual(roundtrip, table)
  245. def test_output_smart_formatting(self):
  246. table = PartitionTable.from_csv(SIMPLE_CSV)
  247. as_csv = table.to_csv(False)
  248. c = csv.reader(as_csv.split("\n"))
  249. # first two lines should start with comments
  250. self.assertEqual(c.next()[0][0], "#")
  251. self.assertEqual(c.next()[0][0], "#")
  252. row = c.next()
  253. self.assertEqual(row[0], "factory")
  254. self.assertEqual(row[1], "app")
  255. self.assertEqual(row[2], "2")
  256. self.assertEqual(row[3], "0x10000")
  257. self.assertEqual(row[4], "1M")
  258. # round trip back to a PartitionTable and check is identical
  259. roundtrip = PartitionTable.from_csv(as_csv)
  260. self.assertEqual(roundtrip, table)
  261. class CommandLineTests(unittest.TestCase):
  262. def test_basic_cmdline(self):
  263. try:
  264. binpath = tempfile.mktemp()
  265. csvpath = tempfile.mktemp()
  266. # copy binary contents to temp file
  267. with open(binpath, 'w') as f:
  268. f.write(LONGER_BINARY_TABLE)
  269. # run gen_esp32part.py to convert binary file to CSV
  270. subprocess.check_call([sys.executable, "../gen_esp32part.py",
  271. binpath, csvpath])
  272. # reopen the CSV and check the generated binary is identical
  273. with open(csvpath, 'r') as f:
  274. from_csv = PartitionTable.from_csv(f.read())
  275. self.assertEqual(_strip_trailing_ffs(from_csv.to_binary()), LONGER_BINARY_TABLE)
  276. # run gen_esp32part.py to conver the CSV to binary again
  277. subprocess.check_call([sys.executable, "../gen_esp32part.py",
  278. csvpath, binpath])
  279. # assert that file reads back as identical
  280. with open(binpath, 'rb') as f:
  281. binary_readback = f.read()
  282. binary_readback = _strip_trailing_ffs(binary_readback)
  283. self.assertEqual(binary_readback, LONGER_BINARY_TABLE)
  284. finally:
  285. for path in binpath, csvpath:
  286. try:
  287. os.remove(path)
  288. except OSError:
  289. pass
  290. if __name__ =="__main__":
  291. unittest.main()