test_pal.py 22 KB

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