test_pal.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. ]
  172. def test_000_symbols(self):
  173. stdout, stderr = self.run_binary(['Symbols'])
  174. found_symbols = dict(line.split(' = ')
  175. for line in stderr.strip().split('\n') if line.startswith('Dk'))
  176. self.assertCountEqual(found_symbols, self.ALL_SYMBOLS)
  177. for k, v in found_symbols.items():
  178. v = ast.literal_eval(v)
  179. self.assertNotEqual(v, 0, 'symbol {} has value 0'.format(k))
  180. class TC_10_Exception(RegressionTestCase):
  181. def test_000_exception(self):
  182. stdout, stderr = self.run_binary(['Exception'])
  183. # Exception Handling (Div-by-Zero)
  184. self.assertIn('Arithmetic Exception Handler', stderr)
  185. # Exception Handling (Memory Fault)
  186. self.assertIn('Memory Fault Exception Handler', stderr)
  187. # Exception Handler Swap
  188. self.assertIn('Arithmetic Exception Handler 1', stderr)
  189. self.assertIn('Arithmetic Exception Handler 2', stderr)
  190. # Exception Handling (Set Context)
  191. self.assertIn('Arithmetic Exception Handler 1', stderr)
  192. # Exception Handling (Red zone)
  193. self.assertIn('Red zone test ok.', stderr)
  194. class TC_20_SingleProcess(RegressionTestCase):
  195. def test_000_exit_code(self):
  196. with self.expect_returncode(112):
  197. self.run_binary(['Exit'])
  198. def test_100_file(self):
  199. try:
  200. pathlib.Path('file_nonexist.tmp').unlink()
  201. except FileNotFoundError:
  202. pass
  203. pathlib.Path('file_delete.tmp').touch()
  204. with open('File', 'rb') as file:
  205. file_exist = file.read()
  206. stdout, stderr = self.run_binary(['File'])
  207. # Basic File Opening
  208. self.assertIn('File Open Test 1 OK', stderr)
  209. self.assertIn('File Open Test 2 OK', stderr)
  210. self.assertIn('File Open Test 3 OK', stderr)
  211. # Basic File Creation
  212. self.assertIn('File Creation Test 1 OK', stderr)
  213. self.assertIn('File Creation Test 2 OK', stderr)
  214. self.assertIn('File Creation Test 3 OK', stderr)
  215. # File Reading
  216. self.assertIn('Read Test 1 (0th - 40th): {}'.format(
  217. file_exist[0:40].hex()), stderr)
  218. self.assertIn('Read Test 2 (0th - 40th): {}'.format(
  219. file_exist[0:40].hex()), stderr)
  220. self.assertIn('Read Test 3 (200th - 240th): {}'.format(
  221. file_exist[200:240].hex()), stderr)
  222. # File Writing
  223. with open('file_nonexist.tmp', 'rb') as file:
  224. file_nonexist = file.read()
  225. self.assertEqual(file_exist[0:40], file_nonexist[200:240])
  226. self.assertEqual(file_exist[200:240], file_nonexist[0:40])
  227. # File Attribute Query
  228. self.assertIn('Query: type = ', stderr)
  229. self.assertIn(', size = {}'.format(len(file_exist)), stderr)
  230. # File Attribute Query by Handle
  231. self.assertIn('Query by Handle: type = ', stderr)
  232. self.assertIn(', size = {}'.format(len(file_exist)), stderr)
  233. # File Mapping
  234. self.assertIn(
  235. 'Map Test 1 (0th - 40th): {}'.format(file_exist[0:40].hex()),
  236. stderr)
  237. self.assertIn(
  238. 'Map Test 2 (200th - 240th): {}'.format(file_exist[200:240].hex()),
  239. stderr)
  240. self.assertIn(
  241. 'Map Test 3 (4096th - 4136th): {}'.format(file_exist[4096:4136].hex()),
  242. stderr)
  243. self.assertIn(
  244. 'Map Test 4 (4296th - 4336th): {}'.format(file_exist[4296:4336].hex()),
  245. stderr)
  246. # Set File Length
  247. self.assertEqual(
  248. pathlib.Path('file_nonexist.tmp').stat().st_size,
  249. mmap.ALLOCATIONGRANULARITY)
  250. # File Deletion
  251. self.assertFalse(pathlib.Path('file_delete.tmp').exists())
  252. @unittest.skipUnless(HAS_SGX, 'this test requires SGX')
  253. def test_101_nonexist_file(self):
  254. # Explicitly remove the file file_nonexist_disallowed.tmp before
  255. # running binary. Otherwise this test will fail if these tests are
  256. # run repeatedly.
  257. os.remove('file_nonexist_disallowed.tmp')
  258. stdout, stderr = self.run_binary(['File'])
  259. # Run file creation for non-existing file. This behavior is
  260. # disallowed unless sgx.allow_file_creation is explicitly set to 1.
  261. self.assertIn('File Creation Test 4 OK', stderr)
  262. def test_110_directory(self):
  263. for path in ['dir_exist.tmp', 'dir_nonexist.tmp', 'dir_delete.tmp']:
  264. try:
  265. shutil.rmtree(path)
  266. except FileNotFoundError:
  267. pass
  268. path = pathlib.Path('dir_exist.tmp')
  269. files = [path / ''.join(random.choice(string.ascii_letters)
  270. for j in range(8))
  271. for i in range(5)]
  272. path.mkdir()
  273. for p in files:
  274. p.touch()
  275. pathlib.Path('dir_delete.tmp').mkdir()
  276. stdout, stderr = self.run_binary(['Directory'])
  277. # Basic Directory Opening
  278. self.assertIn('Directory Open Test 1 OK', stderr)
  279. self.assertIn('Directory Open Test 2 OK', stderr)
  280. self.assertIn('Directory Open Test 3 OK', stderr)
  281. # Basic Directory Creation
  282. self.assertIn('Directory Creation Test 1 OK', stderr)
  283. self.assertIn('Directory Creation Test 2 OK', stderr)
  284. self.assertIn('Directory Creation Test 3 OK', stderr)
  285. # Directory Reading
  286. for p in files:
  287. self.assertIn('Read Directory: {}'.format(p.name), stderr)
  288. # Directory Attribute Query
  289. self.assertIn('Query: type = ', stderr)
  290. # Directory Attribute Query by Handle
  291. self.assertIn('Query by Handle: type = ', stderr)
  292. # Directory Deletion
  293. self.assertFalse(pathlib.Path('dir_delete.tmp').exists())
  294. def test_200_event(self):
  295. stdout, stderr = self.run_binary(['Event'])
  296. self.assertIn('Wait with too short timeout ok.', stderr)
  297. self.assertIn('Wait with long enough timeout ok.', stderr)
  298. def test_210_semaphore(self):
  299. stdout, stderr = self.run_binary(['Semaphore'])
  300. # Semaphore: Timeout on Locked Semaphores
  301. self.assertIn('Locked binary semaphore timed out (1000).', stderr)
  302. self.assertIn('Locked binary semaphore timed out (0).', stderr)
  303. # Semaphore: Acquire Unlocked Semaphores
  304. self.assertIn('Locked binary semaphore successfully (-1).', stderr)
  305. self.assertIn('Locked binary semaphore successfully (0).', stderr)
  306. def test_300_memory(self):
  307. stdout, stderr = self.run_binary(['Memory'])
  308. # Memory Allocation
  309. self.assertIn('Memory Allocation OK', stderr)
  310. # Memory Allocation with Address
  311. self.assertIn('Memory Allocation with Address OK', stderr)
  312. # Get Memory Total Quota
  313. self.assertIn('Total Memory:', stderr)
  314. for line in stderr.split('\n'):
  315. if line.startswith('Total Memory:'):
  316. self.assertNotEqual(line, 'Total Memory: 0')
  317. # Get Memory Available Quota
  318. self.assertIn('Get Memory Available Quota OK', stderr)
  319. @expectedFailureIf(HAS_SGX)
  320. def test_301_memory_nosgx(self):
  321. stdout, stderr = self.run_binary(['Memory'])
  322. # SGX1 does not support unmapping a page or changing its permission
  323. # after enclave init. Therefore the memory protection and deallocation
  324. # tests will fail. By utilizing SGX2 it's possibile to fix this.
  325. # Memory Protection
  326. self.assertIn('Memory Allocation Protection (RW) OK', stderr)
  327. self.assertIn('Memory Protection (R) OK', stderr)
  328. # Memory Deallocation
  329. self.assertIn('Memory Deallocation OK', stderr)
  330. def test_400_pipe(self):
  331. stdout, stderr = self.run_binary(['Pipe'])
  332. # Pipe Creation
  333. self.assertIn('Pipe Creation 1 OK', stderr)
  334. # Pipe Attributes
  335. self.assertIn('Pipe Attribute Query 1 on pipesrv returned OK', stderr)
  336. # Pipe Connection
  337. self.assertIn('Pipe Connection 1 OK', stderr)
  338. # Pipe Transmission
  339. self.assertIn('Pipe Write 1 OK', stderr)
  340. self.assertIn('Pipe Read 1: Hello World 1', stderr)
  341. self.assertIn('Pipe Write 2 OK', stderr)
  342. self.assertIn('Pipe Read 2: Hello World 2', stderr)
  343. def test_410_socket(self):
  344. stdout, stderr = self.run_binary(['Socket'])
  345. # TCP Socket Creation
  346. self.assertIn('TCP Creation 1 OK', stderr)
  347. # TCP Socket Connection
  348. self.assertIn('TCP Connection 1 OK', stderr)
  349. # TCP Socket Transmission
  350. self.assertIn('TCP Write 1 OK', stderr)
  351. self.assertIn('TCP Read 1: Hello World 1', stderr)
  352. self.assertIn('TCP Write 2 OK', stderr)
  353. self.assertIn('TCP Read 2: Hello World 2', stderr)
  354. # UDP Socket Creation
  355. self.assertIn('UDP Creation 1 OK', stderr)
  356. # UDP Socket Connection
  357. self.assertIn('UDP Connection 1 OK', stderr)
  358. # UDP Socket Transmission
  359. self.assertIn('UDP Write 1 OK', stderr)
  360. self.assertIn('UDP Read 1: Hello World 1', stderr)
  361. self.assertIn('UDP Write 2 OK', stderr)
  362. self.assertIn('UDP Read 2: Hello World 2', stderr)
  363. # Bound UDP Socket Transmission
  364. self.assertIn('UDP Write 3 OK', stderr)
  365. self.assertIn('UDP Read 3: Hello World 1', stderr)
  366. self.assertIn('UDP Write 4 OK', stderr)
  367. self.assertIn('UDP Read 4: Hello World 2', stderr)
  368. def test_500_thread(self):
  369. stdout, stderr = self.run_binary(['Thread'])
  370. # Thread Creation
  371. self.assertIn('Child Thread Created', stderr)
  372. self.assertIn('Run in Child Thread: Hello World', stderr)
  373. # Multiple Threads Run in Parallel
  374. self.assertIn('Threads Run in Parallel OK', stderr)
  375. # Set Thread Private Segment Register
  376. self.assertIn('Private Message (FS Segment) 1: Hello World 1', stderr)
  377. self.assertIn('Private Message (FS Segment) 2: Hello World 2', stderr)
  378. # Thread Exit
  379. self.assertIn('Child Thread Exited', stderr)
  380. def test_510_thread2(self):
  381. stdout, stderr = self.run_binary(['Thread2'])
  382. # Thread Cleanup: Exit by return.
  383. self.assertIn('Thread 2 ok.', stderr)
  384. # Thread Cleanup: Exit by DkThreadExit.
  385. self.assertIn('Thread 3 ok.', stderr)
  386. self.assertNotIn('Exiting thread 3 failed.', stderr)
  387. # Thread Cleanup: Can still start threads.
  388. self.assertIn('Thread 4 ok.', stderr)
  389. def test_900_misc(self):
  390. stdout, stderr = self.run_binary(['Misc'])
  391. # Query System Time
  392. self.assertIn('Query System Time OK', stderr)
  393. # Delay Execution for 10000 Microseconds
  394. self.assertIn('Delay Execution for 10000 Microseconds OK', stderr)
  395. # Delay Execution for 3 Seconds
  396. self.assertIn('Delay Execution for 3 Seconds OK', stderr)
  397. # Generate Random Bits
  398. self.assertIn('Generate Random Bits OK', stderr)
  399. def test_910_hex(self):
  400. stdout, stderr = self.run_binary(['Hex'])
  401. # Hex 2 String Helper Function
  402. self.assertIn('Hex test 1 is deadbeef', stderr)
  403. self.assertIn('Hex test 2 is cdcdcdcdcdcdcdcd', stderr)
  404. class TC_21_ProcessCreation(RegressionTestCase):
  405. def test_100_process(self):
  406. stdout, stderr = self.run_binary(['Process'], timeout=8)
  407. counter = collections.Counter(stderr.split('\n'))
  408. # Process Creation
  409. self.assertEqual(counter['Child Process Created'], 3)
  410. # Process Creation Arguments
  411. self.assertEqual(counter['argv[0] = Process'], 3)
  412. self.assertEqual(counter['argv[1] = Child'], 3)
  413. # Process Channel Transmission
  414. self.assertEqual(counter['Process Write 1 OK'], 3)
  415. self.assertEqual(counter['Process Read 1: Hello World 1'], 3)
  416. self.assertEqual(counter['Process Write 2 OK'], 3)
  417. self.assertEqual(counter['Process Read 2: Hello World 2'], 3)
  418. def test_110_process_broadcast(self):
  419. stdout, stderr = self.run_binary(['Process'], timeout=8)
  420. counter = collections.Counter(stderr.split('\n'))
  421. # Multi-Process Broadcast Channel Transmission
  422. if ('Warning: broadcast stream is not open. '
  423. 'Do you have a multicast route configured?') in stderr:
  424. self.skipTest('Could not open broadcast stream. '
  425. 'Do you have a multicast route configured?')
  426. self.assertEqual(counter['Broadcast Write OK'], 1)
  427. self.assertEqual(counter['Broadcast Read: Hello World 1'], 3)
  428. def test_200_process2(self):
  429. # Process Creation with a Different Binary
  430. stdout, stderr = self.run_binary(['Process2'])
  431. counter = collections.Counter(stderr.split('\n'))
  432. self.assertEqual(counter['User Program Started'], 1)
  433. def test_300_process3(self):
  434. # Process Creation without Executable
  435. stdout, stderr = self.run_binary(['Process3'])
  436. counter = collections.Counter(stderr.split('\n'))
  437. self.assertEqual(counter['Binary 1 Preloaded'], 2)
  438. self.assertEqual(counter['Binary 2 Preloaded'], 2)
  439. class TC_23_SendHandle(RegressionTestCase):
  440. def test_000_send_handle(self):
  441. stdout, stderr = self.run_binary(['SendHandle'])
  442. counter = collections.Counter(stderr.split('\n'))
  443. # Send and Receive Handles across Processes
  444. self.assertEqual(counter['Send Handle OK'], 3)
  445. self.assertEqual(counter['Receive Handle OK'], 3)
  446. # Send Pipe Handle
  447. self.assertEqual(counter['Receive Pipe Handle: Hello World'], 1)
  448. # Send Socket Handle
  449. self.assertEqual(counter['Receive Socket Handle: Hello World'], 1)
  450. # Send File Handle
  451. self.assertEqual(counter['Receive File Handle: Hello World'], 1)
  452. @unittest.skipUnless(HAS_SGX, 'need SGX')
  453. class TC_40_AVXDisable(RegressionTestCase):
  454. @unittest.expectedFailure
  455. def test_000_avx_disable(self):
  456. # Disable AVX bit in XFRM
  457. stdout, stderr = self.run_binary(['AvxDisable'])
  458. self.assertIn('Illegal instruction executed in enclave', stderr)
  459. @unittest.skipUnless(HAS_SGX, 'need SGX')
  460. class TC_50_Attestation(RegressionTestCase):
  461. def test_000_remote_attestation(self):
  462. stdout, stderr = self.run_binary(["Attestation"])
  463. for line in stderr.split("\n"):
  464. # Check the attestation status
  465. if line.startswith("Attestation status:"):
  466. status = line[19:].strip()
  467. self.assertIn(status, ["OK", "GROUP_OUT_OF_DATE"])
  468. # Check the timestamp
  469. if line.startswith("Attestation timestamp:"):
  470. timestamp = datetime.strptime(line[22:].strip(), "%Y-%m-%dT%H:%M:%S.%f")
  471. # The timestamp may be in another time zone, but should be
  472. # within 24 hours of the current time.
  473. self.assertTrue(datetime.now() - timedelta(hours=24) <= timestamp and \
  474. datetime.now() + timedelta(hours=24) >= timestamp);