test_pal.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #!/usr/bin/env python3
  2. import ast
  3. import collections
  4. import mmap
  5. import os
  6. import pathlib
  7. import random
  8. import shutil
  9. import string
  10. import subprocess
  11. import unittest
  12. from datetime import datetime, timedelta
  13. from regression import (
  14. HAS_SGX,
  15. RegressionTestCase,
  16. expectedFailureIf,
  17. )
  18. CPUINFO_FLAGS_WHITELIST = [
  19. 'fpu', 'vme', 'de', 'pse', 'tsc', 'msr', 'pae', 'mce', 'cx8', 'apic', 'sep',
  20. 'mtrr', 'pge', 'mca', 'cmov', 'pat', 'pse36', 'pn', 'clflush', 'dts',
  21. 'acpi', 'mmx', 'fxsr', 'sse', 'sse2', 'ss', 'ht', 'tm', 'ia64', 'pbe',
  22. ]
  23. class TC_00_Basic(RegressionTestCase):
  24. def test_000_atomic_math(self):
  25. stdout, stderr = self.run_binary(['AtomicMath'])
  26. self.assertIn('Subtract INT_MIN: Both values match 2147483648', stderr)
  27. self.assertIn('Subtract INT_MAX: Both values match -2147483647', stderr)
  28. self.assertIn('Subtract LLONG_MIN: Both values match -9223372036854775808', stderr)
  29. self.assertIn('Subtract LLONG_MAX: Both values match -9223372036854775807', stderr)
  30. def test_001_path_normalization(self):
  31. stdout, stderr = self.run_binary(['normalize_path'])
  32. self.assertIn("Success!\n", stderr)
  33. class TC_01_Bootstrap(RegressionTestCase):
  34. def test_100_basic_boostrapping(self):
  35. stdout, stderr = self.run_binary(['Bootstrap'])
  36. # Basic Bootstrapping
  37. self.assertIn('User Program Started', stderr)
  38. # Control Block: Executable Name
  39. self.assertIn('Loaded Executable: file:Bootstrap', stderr)
  40. # One Argument Given
  41. self.assertIn('# of Arguments: 1', stderr)
  42. self.assertIn('argv[0] = Bootstrap', stderr)
  43. # Control Block: Debug Stream (Inline)
  44. self.assertIn('Written to Debug Stream', stdout)
  45. # Control Block: Allocation Alignment
  46. self.assertIn('Allocation Alignment: {}'.format(mmap.ALLOCATIONGRANULARITY), stderr)
  47. # Control Block: Executable Range
  48. self.assertIn('Executable Range OK', stderr)
  49. def test_101_basic_boostrapping_five_arguments(self):
  50. stdout, stderr = self.run_binary(['Bootstrap', 'a', 'b', 'c', 'd'])
  51. # Five Arguments Given
  52. self.assertIn('# of Arguments: 5', stderr)
  53. self.assertIn('argv[1] = a', stderr)
  54. self.assertIn('argv[2] = b', stderr)
  55. self.assertIn('argv[3] = c', stderr)
  56. self.assertIn('argv[4] = d', stderr)
  57. def test_102_cpuinfo(self):
  58. with open('/proc/cpuinfo') as file:
  59. cpuinfo = file.read().strip().split('\n\n')[-1]
  60. cpuinfo = dict(map(str.strip, line.split(':'))
  61. for line in cpuinfo.split('\n'))
  62. if 'flags' in cpuinfo:
  63. cpuinfo['flags'] = ' '.join(flag for flag in cpuinfo['flags']
  64. if flag in CPUINFO_FLAGS_WHITELIST)
  65. stdout, stderr = self.run_binary(['Bootstrap'])
  66. self.assertIn('CPU num: {}'.format(int(cpuinfo['processor']) + 1),
  67. stderr)
  68. self.assertIn('CPU vendor: {[vendor_id]}'.format(cpuinfo), stderr)
  69. self.assertIn('CPU brand: {[model name]}'.format(cpuinfo), stderr)
  70. self.assertIn('CPU family: {[cpu family]}'.format(cpuinfo), stderr)
  71. self.assertIn('CPU model: {[model]}'.format(cpuinfo), stderr)
  72. self.assertIn('CPU stepping: {[stepping]}'.format(cpuinfo), stderr)
  73. self.assertIn('CPU flags: {[flags]}'.format(cpuinfo), stderr)
  74. def test_103_dotdot(self):
  75. stdout, stderr = self.run_binary(['..Bootstrap'])
  76. self.assertIn('User Program Started', stderr)
  77. def test_104_manifest_as_executable_name(self):
  78. manifest = self.get_manifest('Bootstrap2')
  79. stdout, stderr = self.run_binary([manifest])
  80. self.assertIn('User Program Started', stderr)
  81. self.assertIn('Loaded Manifest: file:' + manifest, stderr)
  82. def test_105_manifest_as_argument(self):
  83. manifest = self.get_manifest('Bootstrap4')
  84. stdout, stderr = self.run_binary([manifest])
  85. self.assertIn('Loaded Manifest: file:' + manifest, stderr)
  86. self.assertIn('Loaded Executable: file:Bootstrap', stderr)
  87. def test_106_manifest_with_shebang(self):
  88. manifest = self.get_manifest('Bootstrap4')
  89. stdout, stderr = self.run_binary(['./' + manifest])
  90. self.assertIn('Loaded Manifest: file:' + manifest, stderr)
  91. self.assertIn('Loaded Executable: file:Bootstrap', stderr)
  92. self.assertIn('argv[0] = Bootstrap', stderr)
  93. @unittest.skipUnless(HAS_SGX, 'need SGX')
  94. def test_107_manifest_with_nonelf_binary(self):
  95. manifest = self.get_manifest('nonelf_binary')
  96. #Expect return code is -ENOEXEC(248 as unsigned char)
  97. with self.expect_returncode(248):
  98. self.run_binary([manifest])
  99. def test_110_preload_libraries(self):
  100. stdout, stderr = self.run_binary(['Bootstrap3'])
  101. self.assertIn('Binary 1 Preloaded', stderr)
  102. self.assertIn('Binary 2 Preloaded', stderr)
  103. self.assertIn('Preloaded Function 1 Called', stderr)
  104. self.assertIn('Preloaded Function 2 Called', stderr)
  105. def test_111_preload_libraries(self):
  106. # Bootstrap without Executable but Preload Libraries
  107. stdout, stderr = self.run_binary([self.get_manifest('Bootstrap5')])
  108. self.assertIn('Binary 1 Preloaded', stderr)
  109. self.assertIn('Binary 2 Preloaded', stderr)
  110. @unittest.skipUnless(HAS_SGX, 'this test requires SGX')
  111. def test_120_8gb_enclave(self):
  112. manifest = self.get_manifest('Bootstrap6')
  113. stdout, stderr = self.run_binary([manifest], timeout=360)
  114. self.assertIn('Loaded Manifest: file:' + manifest, stderr)
  115. self.assertIn('Executable Range OK', stderr)
  116. def test_130_large_number_of_items_in_manifest(self):
  117. stdout, stderr = self.run_binary([self.get_manifest('Bootstrap7')])
  118. self.assertIn('key1000=na', stderr)
  119. self.assertIn('key1=na', stderr)
  120. @unittest.skip('this is broken on non-SGX, see #860')
  121. def test_140_missing_executable_and_manifest(self):
  122. try:
  123. stdout, stderr = self.run_binary(['fakenews'])
  124. self.fail(
  125. 'expected non-zero returncode, stderr: {!r}'.format(stderr))
  126. except subprocess.CalledProcessError as e:
  127. self.assertIn('USAGE: ', e.stderr.decode())
  128. class TC_02_Symbols(RegressionTestCase):
  129. ALL_SYMBOLS = [
  130. 'DkVirtualMemoryAlloc',
  131. 'DkVirtualMemoryFree',
  132. 'DkVirtualMemoryProtect',
  133. 'DkProcessCreate',
  134. 'DkProcessExit',
  135. 'DkStreamOpen',
  136. 'DkStreamWaitForClient',
  137. 'DkStreamRead',
  138. 'DkStreamWrite',
  139. 'DkStreamDelete',
  140. 'DkStreamMap',
  141. 'DkStreamUnmap',
  142. 'DkStreamSetLength',
  143. 'DkStreamFlush',
  144. 'DkSendHandle',
  145. 'DkReceiveHandle',
  146. 'DkStreamAttributesQuery',
  147. 'DkStreamAttributesQueryByHandle',
  148. 'DkStreamAttributesSetByHandle',
  149. 'DkStreamGetName',
  150. 'DkStreamChangeName',
  151. 'DkThreadCreate',
  152. 'DkThreadDelayExecution',
  153. 'DkThreadYieldExecution',
  154. 'DkThreadExit',
  155. 'DkThreadResume',
  156. 'DkSetExceptionHandler',
  157. 'DkExceptionReturn',
  158. 'DkMutexCreate',
  159. 'DkMutexRelease',
  160. 'DkNotificationEventCreate',
  161. 'DkSynchronizationEventCreate',
  162. 'DkEventSet',
  163. 'DkEventClear',
  164. 'DkObjectsWaitAny',
  165. 'DkObjectClose',
  166. 'DkSystemTimeQuery',
  167. 'DkRandomBitsRead',
  168. 'DkInstructionCacheFlush',
  169. 'DkSegmentRegister',
  170. 'DkMemoryAvailableQuota',
  171. 'DkCreatePhysicalMemoryChannel',
  172. 'DkPhysicalMemoryCommit',
  173. 'DkPhysicalMemoryMap',
  174. ]
  175. def test_000_symbols(self):
  176. stdout, stderr = self.run_binary(['Symbols'])
  177. found_symbols = dict(line.split(' = ')
  178. for line in stderr.strip().split('\n') if line.startswith('Dk'))
  179. self.assertCountEqual(found_symbols, self.ALL_SYMBOLS)
  180. for k, v in found_symbols.items():
  181. v = ast.literal_eval(v)
  182. self.assertNotEqual(v, 0, 'symbol {} has value 0'.format(k))
  183. class TC_10_Exception(RegressionTestCase):
  184. def test_000_exception(self):
  185. stdout, stderr = self.run_binary(['Exception'])
  186. # Exception Handling (Div-by-Zero)
  187. self.assertIn('Arithmetic Exception Handler', stderr)
  188. # Exception Handling (Memory Fault)
  189. self.assertIn('Memory Fault Exception Handler', stderr)
  190. # Exception Handler Swap
  191. self.assertIn('Arithmetic Exception Handler 1', stderr)
  192. self.assertIn('Arithmetic Exception Handler 2', stderr)
  193. # Exception Handling (Set Context)
  194. self.assertIn('Arithmetic Exception Handler 1', stderr)
  195. # Exception Handling (Red zone)
  196. self.assertIn('Red zone test ok.', stderr)
  197. class TC_20_SingleProcess(RegressionTestCase):
  198. def test_000_exit_code(self):
  199. with self.expect_returncode(112):
  200. self.run_binary(['Exit'])
  201. def test_100_file(self):
  202. try:
  203. pathlib.Path('file_nonexist.tmp').unlink()
  204. except FileNotFoundError:
  205. pass
  206. pathlib.Path('file_delete.tmp').touch()
  207. with open('File', 'rb') as file:
  208. file_exist = file.read()
  209. stdout, stderr = self.run_binary(['File'])
  210. # Basic File Opening
  211. self.assertIn('File Open Test 1 OK', stderr)
  212. self.assertIn('File Open Test 2 OK', stderr)
  213. self.assertIn('File Open Test 3 OK', stderr)
  214. # Basic File Creation
  215. self.assertIn('File Creation Test 1 OK', stderr)
  216. self.assertIn('File Creation Test 2 OK', stderr)
  217. self.assertIn('File Creation Test 3 OK', stderr)
  218. # File Reading
  219. self.assertIn('Read Test 1 (0th - 40th): {}'.format(
  220. file_exist[0:40].hex()), stderr)
  221. self.assertIn('Read Test 2 (0th - 40th): {}'.format(
  222. file_exist[0:40].hex()), stderr)
  223. self.assertIn('Read Test 3 (200th - 240th): {}'.format(
  224. file_exist[200:240].hex()), stderr)
  225. # File Writing
  226. with open('file_nonexist.tmp', 'rb') as file:
  227. file_nonexist = file.read()
  228. self.assertEqual(file_exist[0:40], file_nonexist[200:240])
  229. self.assertEqual(file_exist[200:240], file_nonexist[0:40])
  230. # File Attribute Query
  231. self.assertIn('Query: type = ', stderr)
  232. self.assertIn(', size = {}'.format(len(file_exist)), stderr)
  233. # File Attribute Query by Handle
  234. self.assertIn('Query by Handle: type = ', stderr)
  235. self.assertIn(', size = {}'.format(len(file_exist)), stderr)
  236. # File Mapping
  237. self.assertIn(
  238. 'Map Test 1 (0th - 40th): {}'.format(file_exist[0:40].hex()),
  239. stderr)
  240. self.assertIn(
  241. 'Map Test 2 (200th - 240th): {}'.format(file_exist[200:240].hex()),
  242. stderr)
  243. self.assertIn(
  244. 'Map Test 3 (4096th - 4136th): {}'.format(file_exist[4096:4136].hex()),
  245. stderr)
  246. self.assertIn(
  247. 'Map Test 4 (4296th - 4336th): {}'.format(file_exist[4296:4336].hex()),
  248. stderr)
  249. # Set File Length
  250. self.assertEqual(
  251. pathlib.Path('file_nonexist.tmp').stat().st_size,
  252. mmap.ALLOCATIONGRANULARITY)
  253. # File Deletion
  254. self.assertFalse(pathlib.Path('file_delete.tmp').exists())
  255. @unittest.skipUnless(HAS_SGX, 'this test requires SGX')
  256. def test_101_nonexist_file(self):
  257. # Explicitly remove the file file_nonexist_disallowed.tmp before
  258. # running binary. Otherwise this test will fail if these tests are
  259. # run repeatedly.
  260. os.remove('file_nonexist_disallowed.tmp')
  261. stdout, stderr = self.run_binary(['File'])
  262. # Run file creation for non-existing file. This behavior is
  263. # disallowed unless sgx.allow_file_creation is explicitly set to 1.
  264. self.assertIn('File Creation Test 4 OK', stderr)
  265. def test_110_directory(self):
  266. for path in ['dir_exist.tmp', 'dir_nonexist.tmp', 'dir_delete.tmp']:
  267. try:
  268. shutil.rmtree(path)
  269. except FileNotFoundError:
  270. pass
  271. path = pathlib.Path('dir_exist.tmp')
  272. files = [path / ''.join(random.choice(string.ascii_letters)
  273. for j in range(8))
  274. for i in range(5)]
  275. path.mkdir()
  276. for p in files:
  277. p.touch()
  278. pathlib.Path('dir_delete.tmp').mkdir()
  279. stdout, stderr = self.run_binary(['Directory'])
  280. # Basic Directory Opening
  281. self.assertIn('Directory Open Test 1 OK', stderr)
  282. self.assertIn('Directory Open Test 2 OK', stderr)
  283. self.assertIn('Directory Open Test 3 OK', stderr)
  284. # Basic Directory Creation
  285. self.assertIn('Directory Creation Test 1 OK', stderr)
  286. self.assertIn('Directory Creation Test 2 OK', stderr)
  287. self.assertIn('Directory Creation Test 3 OK', stderr)
  288. # Directory Reading
  289. for p in files:
  290. self.assertIn('Read Directory: {}'.format(p.name), stderr)
  291. # Directory Attribute Query
  292. self.assertIn('Query: type = ', stderr)
  293. # Directory Attribute Query by Handle
  294. self.assertIn('Query by Handle: type = ', stderr)
  295. # Directory Deletion
  296. self.assertFalse(pathlib.Path('dir_delete.tmp').exists())
  297. def test_200_event(self):
  298. stdout, stderr = self.run_binary(['Event'])
  299. self.assertIn('Wait with too short timeout ok.', stderr)
  300. self.assertIn('Wait with long enough timeout ok.', stderr)
  301. def test_210_semaphore(self):
  302. stdout, stderr = self.run_binary(['Semaphore'])
  303. # Semaphore: Timeout on Locked Semaphores
  304. self.assertIn('Locked binary semaphore timed out (1000).', stderr)
  305. self.assertIn('Locked binary semaphore timed out (0).', stderr)
  306. # Semaphore: Acquire Unlocked Semaphores
  307. self.assertIn('Locked binary semaphore successfully (-1).', stderr)
  308. self.assertIn('Locked binary semaphore successfully (0).', stderr)
  309. def test_300_memory(self):
  310. stdout, stderr = self.run_binary(['Memory'])
  311. # Memory Allocation
  312. self.assertIn('Memory Allocation OK', stderr)
  313. # Memory Allocation with Address
  314. self.assertIn('Memory Allocation with Address OK', stderr)
  315. # Get Memory Total Quota
  316. self.assertIn('Total Memory:', stderr)
  317. for line in stderr.split('\n'):
  318. if line.startswith('Total Memory:'):
  319. self.assertNotEqual(line, 'Total Memory: 0')
  320. # Get Memory Available Quota
  321. self.assertIn('Get Memory Available Quota OK', stderr)
  322. @expectedFailureIf(HAS_SGX)
  323. def test_301_memory_nosgx(self):
  324. stdout, stderr = self.run_binary(['Memory'])
  325. # SGX1 does not support unmapping a page or changing its permission
  326. # after enclave init. Therefore the memory protection and deallocation
  327. # tests will fail. By utilizing SGX2 it's possibile to fix this.
  328. # Memory Protection
  329. self.assertIn('Memory Allocation Protection (RW) OK', stderr)
  330. self.assertIn('Memory Protection (R) OK', stderr)
  331. # Memory Deallocation
  332. self.assertIn('Memory Deallocation OK', stderr)
  333. def test_400_pipe(self):
  334. stdout, stderr = self.run_binary(['Pipe'])
  335. # Pipe Creation
  336. self.assertIn('Pipe Creation 1 OK', stderr)
  337. # Pipe Attributes
  338. self.assertIn('Pipe Attribute Query 1 on pipesrv returned OK', stderr)
  339. # Pipe Connection
  340. self.assertIn('Pipe Connection 1 OK', stderr)
  341. # Pipe Transmission
  342. self.assertIn('Pipe Write 1 OK', stderr)
  343. self.assertIn('Pipe Read 1: Hello World 1', stderr)
  344. self.assertIn('Pipe Write 2 OK', stderr)
  345. self.assertIn('Pipe Read 2: Hello World 2', stderr)
  346. def test_410_socket(self):
  347. stdout, stderr = self.run_binary(['Socket'])
  348. # TCP Socket Creation
  349. self.assertIn('TCP Creation 1 OK', stderr)
  350. # TCP Socket Connection
  351. self.assertIn('TCP Connection 1 OK', stderr)
  352. # TCP Socket Transmission
  353. self.assertIn('TCP Write 1 OK', stderr)
  354. self.assertIn('TCP Read 1: Hello World 1', stderr)
  355. self.assertIn('TCP Write 2 OK', stderr)
  356. self.assertIn('TCP Read 2: Hello World 2', stderr)
  357. # UDP Socket Creation
  358. self.assertIn('UDP Creation 1 OK', stderr)
  359. # UDP Socket Connection
  360. self.assertIn('UDP Connection 1 OK', stderr)
  361. # UDP Socket Transmission
  362. self.assertIn('UDP Write 1 OK', stderr)
  363. self.assertIn('UDP Read 1: Hello World 1', stderr)
  364. self.assertIn('UDP Write 2 OK', stderr)
  365. self.assertIn('UDP Read 2: Hello World 2', stderr)
  366. # Bound UDP Socket Transmission
  367. self.assertIn('UDP Write 3 OK', stderr)
  368. self.assertIn('UDP Read 3: Hello World 1', stderr)
  369. self.assertIn('UDP Write 4 OK', stderr)
  370. self.assertIn('UDP Read 4: Hello World 2', stderr)
  371. def test_500_thread(self):
  372. stdout, stderr = self.run_binary(['Thread'])
  373. # Thread Creation
  374. self.assertIn('Child Thread Created', stderr)
  375. self.assertIn('Run in Child Thread: Hello World', stderr)
  376. # Multiple Threads Run in Parallel
  377. self.assertIn('Threads Run in Parallel OK', stderr)
  378. # Set Thread Private Segment Register
  379. self.assertIn('Private Message (FS Segment) 1: Hello World 1', stderr)
  380. self.assertIn('Private Message (FS Segment) 2: Hello World 2', stderr)
  381. # Thread Exit
  382. self.assertIn('Child Thread Exited', stderr)
  383. def test_510_thread2(self):
  384. stdout, stderr = self.run_binary(['Thread2'])
  385. # Thread Cleanup: Exit by return.
  386. self.assertIn('Thread 2 ok.', stderr)
  387. # Thread Cleanup: Exit by DkThreadExit.
  388. self.assertIn('Thread 3 ok.', stderr)
  389. self.assertNotIn('Exiting thread 3 failed.', stderr)
  390. # Thread Cleanup: Can still start threads.
  391. self.assertIn('Thread 4 ok.', stderr)
  392. def test_900_misc(self):
  393. stdout, stderr = self.run_binary(['Misc'])
  394. # Query System Time
  395. self.assertIn('Query System Time OK', stderr)
  396. # Delay Execution for 10000 Microseconds
  397. self.assertIn('Delay Execution for 10000 Microseconds OK', stderr)
  398. # Delay Execution for 3 Seconds
  399. self.assertIn('Delay Execution for 3 Seconds OK', stderr)
  400. # Generate Random Bits
  401. self.assertIn('Generate Random Bits OK', stderr)
  402. def test_910_hex(self):
  403. stdout, stderr = self.run_binary(['Hex'])
  404. # Hex 2 String Helper Function
  405. self.assertIn('Hex test 1 is deadbeef', stderr)
  406. self.assertIn('Hex test 2 is cdcdcdcdcdcdcdcd', stderr)
  407. class TC_21_ProcessCreation(RegressionTestCase):
  408. def test_100_process(self):
  409. stdout, stderr = self.run_binary(['Process'], timeout=8)
  410. counter = collections.Counter(stderr.split('\n'))
  411. # Process Creation
  412. self.assertEqual(counter['Child Process Created'], 3)
  413. # Process Creation Arguments
  414. self.assertEqual(counter['argv[0] = Process'], 3)
  415. self.assertEqual(counter['argv[1] = Child'], 3)
  416. # Process Channel Transmission
  417. self.assertEqual(counter['Process Write 1 OK'], 3)
  418. self.assertEqual(counter['Process Read 1: Hello World 1'], 3)
  419. self.assertEqual(counter['Process Write 2 OK'], 3)
  420. self.assertEqual(counter['Process Read 2: Hello World 2'], 3)
  421. def test_110_process_broadcast(self):
  422. stdout, stderr = self.run_binary(['Process'], timeout=8)
  423. counter = collections.Counter(stderr.split('\n'))
  424. # Multi-Process Broadcast Channel Transmission
  425. if ('Warning: broadcast stream is not open. '
  426. 'Do you have a multicast route configured?') in stderr:
  427. self.skipTest('Could not open broadcast stream. '
  428. 'Do you have a multicast route configured?')
  429. self.assertEqual(counter['Broadcast Write OK'], 1)
  430. self.assertEqual(counter['Broadcast Read: Hello World 1'], 3)
  431. def test_200_process2(self):
  432. # Process Creation with a Different Binary
  433. stdout, stderr = self.run_binary(['Process2'])
  434. counter = collections.Counter(stderr.split('\n'))
  435. self.assertEqual(counter['User Program Started'], 1)
  436. def test_300_process3(self):
  437. # Process Creation without Executable
  438. stdout, stderr = self.run_binary(['Process3'])
  439. counter = collections.Counter(stderr.split('\n'))
  440. self.assertEqual(counter['Binary 1 Preloaded'], 2)
  441. self.assertEqual(counter['Binary 2 Preloaded'], 2)
  442. @unittest.skipIf(HAS_SGX, 'GIPC not supported on SGX')
  443. ## XXX Should really be running these tests as part of CI
  444. @unittest.skipUnless(pathlib.Path('/dev/gipc').exists(), 'GIPC not loaded')
  445. class TC_22_GIPC(RegressionTestCase):
  446. def test_000_gipc(self):
  447. with open('ipc_mapping.tmp', 'w') as file:
  448. file.write('Hello World')
  449. os.ftruncate(file.fileno(), mmap.PAGESIZE)
  450. stdout, stderr = self.run_binary(['Ipc'])
  451. counter = collections.Counter(stderr.split('\n'))
  452. # Create and Join Physical Memory Bulk Copy Store
  453. self.assertEqual(counter['Create Physical Memory Store OK'], 5)
  454. self.assertEqual(counter['Join Physical Memory Store OK'], 5)
  455. # Map and Commit Anonymous Physical Memory
  456. self.assertIn('[Test 1] Physical Memory Commit OK', stderr)
  457. self.assertIn('[Test 1] Physical Memory Map : Hello World', stderr)
  458. # Transfer Anonymous Physical Memory as Copy-on-Write
  459. self.assertIn('[Test 1] Sender After Commit: Hello World, Alice', stderr)
  460. self.assertIn('[Test 1] Sender Before Map : Alice, Hello World', stderr)
  461. self.assertIn('[Test 1] Receiver After Map : Hello World, Bob', stderr)
  462. self.assertIn('[Test 1] Sender After Map : Alice, Hello World', stderr)
  463. # Map and Commit Untouched Physical Memory
  464. self.assertIn('[Test 2] Physical Memory Commit OK', stderr)
  465. self.assertIn('[Test 2] Physical Memory Map : ', stderr)
  466. self.assertIn('[Test 2] Sender After Commit: Hello World, Alice', stderr)
  467. self.assertIn('[Test 2] Sender Before Map : Alice, Hello World', stderr)
  468. self.assertIn('[Test 2] Receiver After Map : Hello World, Bob', stderr)
  469. self.assertIn('[Test 2] Sender After Map : Alice, Hello World', stderr)
  470. # Map and Commit File-Backed Physical Memory
  471. self.assertIn('[Test 3] Physical Memory Commit OK', stderr)
  472. self.assertIn('[Test 3] Physical Memory Map : Hello World', stderr)
  473. self.assertIn('[Test 3] Sender After Commit: Hello World', stderr)
  474. self.assertIn('[Test 3] Receiver After Map : Hello World, Bob', stderr)
  475. self.assertIn('[Test 3] Sender After Map : Hello World', stderr)
  476. # Map and Commit File-Backed Physical Memory Beyond File Size
  477. self.assertIn('[Test 4] Physical Memory Commit OK', stderr)
  478. self.assertIn('[Test 4] Physical Memory Map : Memory Fault', stderr)
  479. # Map and Commit Huge Physical Memory
  480. self.assertIn('[Test 5] Physical Memory Commit OK', stderr)
  481. self.assertIn('[Test 5] Physical Memory Map : Hello World', stderr)
  482. class TC_23_SendHandle(RegressionTestCase):
  483. def test_000_send_handle(self):
  484. stdout, stderr = self.run_binary(['SendHandle'])
  485. counter = collections.Counter(stderr.split('\n'))
  486. # Send and Receive Handles across Processes
  487. self.assertEqual(counter['Send Handle OK'], 3)
  488. self.assertEqual(counter['Receive Handle OK'], 3)
  489. # Send Pipe Handle
  490. self.assertEqual(counter['Receive Pipe Handle: Hello World'], 1)
  491. # Send Socket Handle
  492. self.assertEqual(counter['Receive Socket Handle: Hello World'], 1)
  493. # Send File Handle
  494. self.assertEqual(counter['Receive File Handle: Hello World'], 1)
  495. @unittest.skipUnless(HAS_SGX, 'need SGX')
  496. class TC_40_AVXDisable(RegressionTestCase):
  497. @unittest.expectedFailure
  498. def test_000_avx_disable(self):
  499. # Disable AVX bit in XFRM
  500. stdout, stderr = self.run_binary(['AvxDisable'])
  501. self.assertIn('Illegal instruction executed in enclave', stderr)
  502. @unittest.skipUnless(HAS_SGX, 'need SGX')
  503. class TC_50_Attestation(RegressionTestCase):
  504. def test_000_remote_attestation(self):
  505. stdout, stderr = self.run_binary(["Attestation"])
  506. for line in stderr.split("\n"):
  507. # Check the attestation status
  508. if line.startswith("Attestation status:"):
  509. status = line[19:].strip()
  510. self.assertIn(status, ["OK", "GROUP_OUT_OF_DATE"])
  511. # Check the timestamp
  512. if line.startswith("Attestation timestamp:"):
  513. timestamp = datetime.strptime(line[22:].strip(), "%Y-%m-%dT%H:%M:%S.%f")
  514. # The timestamp may be in another time zone, but should be
  515. # within 24 hours of the current time.
  516. self.assertTrue(datetime.now() - timedelta(hours=24) <= timestamp and \
  517. datetime.now() + timedelta(hours=24) >= timestamp);