tlcl_tests.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Tests for TPM lite library
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "host_common.h"
  12. #include "test_common.h"
  13. #include "tlcl.h"
  14. #include "tlcl_internal.h"
  15. #include "vboot_common.h"
  16. /* Mock data */
  17. static char debug_info[4096];
  18. static VbError_t mock_retval;
  19. /* Call to mocked VbExTpmSendReceive() */
  20. struct srcall
  21. {
  22. const uint8_t *req; /* Request */
  23. uint8_t *rsp; /* Response */
  24. uint8_t rsp_buf[32]; /* Default response buffer, if not overridden */
  25. int req_size; /* Request size */
  26. uint32_t req_cmd; /* Request command code */
  27. int rsp_size; /* Response size */
  28. VbError_t retval; /* Value to return */
  29. };
  30. #define MAXCALLS 8
  31. static struct srcall calls[MAXCALLS];
  32. static int ncalls;
  33. /**
  34. * Reset mock data (for use before each test)
  35. */
  36. static void ResetMocks(void)
  37. {
  38. int i;
  39. *debug_info = 0;
  40. mock_retval = VBERROR_SUCCESS;
  41. memset(calls, 0, sizeof(calls));
  42. for (i = 0; i < MAXCALLS; i++)
  43. calls[i].rsp = calls[i].rsp_buf;
  44. ncalls = 0;
  45. }
  46. /**
  47. * Set response code and length for call <call_idx>.
  48. */
  49. static void SetResponse(int call_idx, uint32_t response_code, int rsp_size)
  50. {
  51. struct srcall *c = calls + call_idx;
  52. c->rsp_size = rsp_size;
  53. ToTpmUint32(c->rsp_buf + 6, response_code);
  54. }
  55. /* Mocks */
  56. VbError_t VbExTpmInit(void)
  57. {
  58. return mock_retval;
  59. }
  60. VbError_t VbExTpmClose(void)
  61. {
  62. return mock_retval;
  63. }
  64. VbError_t VbExTpmSendReceive(const uint8_t *request, uint32_t request_length,
  65. uint8_t *response, uint32_t *response_length)
  66. {
  67. struct srcall *c = calls + ncalls++;
  68. c->req = request;
  69. c->req_size = request_length;
  70. /* Parse out the command code */
  71. FromTpmUint32(request + 6, &c->req_cmd);
  72. // KLUDGE - remove
  73. printf("TSR [%d] 0x%x\n", ncalls-1, c->req_cmd);
  74. memset(response, 0, *response_length);
  75. if (c->rsp_size)
  76. memcpy(response, c->rsp, c->rsp_size);
  77. *response_length = c->rsp_size;
  78. return c->retval;
  79. }
  80. /**
  81. * Test assorted tlcl functions
  82. */
  83. static void TlclTest(void)
  84. {
  85. uint8_t buf[32], buf2[32];
  86. ResetMocks();
  87. TEST_EQ(TlclLibInit(), VBERROR_SUCCESS, "Init");
  88. ResetMocks();
  89. mock_retval = VBERROR_SIMULATED;
  90. TEST_EQ(TlclLibInit(), mock_retval, "Init bad");
  91. ResetMocks();
  92. TEST_EQ(TlclLibClose(), VBERROR_SUCCESS, "Close");
  93. ResetMocks();
  94. mock_retval = VBERROR_SIMULATED;
  95. TEST_EQ(TlclLibClose(), mock_retval, "Close bad");
  96. ResetMocks();
  97. ToTpmUint32(buf + 2, 123);
  98. TEST_EQ(TlclPacketSize(buf), 123, "TlclPacketSize");
  99. ResetMocks();
  100. ToTpmUint32(buf + 2, 10);
  101. TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), 0, "SendReceive");
  102. TEST_PTR_EQ(calls[0].req, buf, "SendReceive req ptr");
  103. TEST_EQ(calls[0].req_size, 10, "SendReceive size");
  104. ResetMocks();
  105. calls[0].retval = VBERROR_SIMULATED;
  106. ToTpmUint32(buf + 2, 10);
  107. TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), VBERROR_SIMULATED,
  108. "SendReceive fail");
  109. ResetMocks();
  110. SetResponse(0, 123, 10);
  111. ToTpmUint32(buf + 2, 10);
  112. TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), 123,
  113. "SendReceive error response");
  114. // TODO: continue self test (if needed or doing)
  115. // TODO: then retry doing self test
  116. }
  117. /**
  118. * Test send-command functions
  119. */
  120. static void SendCommandTest(void)
  121. {
  122. ResetMocks();
  123. TEST_EQ(TlclStartup(), 0, "SaveState");
  124. TEST_EQ(calls[0].req_cmd, TPM_ORD_Startup, " cmd");
  125. ResetMocks();
  126. TEST_EQ(TlclSaveState(), 0, "SaveState");
  127. TEST_EQ(calls[0].req_cmd, TPM_ORD_SaveState, " cmd");
  128. ResetMocks();
  129. TEST_EQ(TlclResume(), 0, "Resume");
  130. TEST_EQ(calls[0].req_cmd, TPM_ORD_Startup, " cmd");
  131. ResetMocks();
  132. TEST_EQ(TlclSelfTestFull(), 0, "SelfTestFull");
  133. TEST_EQ(calls[0].req_cmd, TPM_ORD_SelfTestFull, " cmd");
  134. ResetMocks();
  135. TEST_EQ(TlclContinueSelfTest(), 0, "ContinueSelfTest");
  136. TEST_EQ(calls[0].req_cmd, TPM_ORD_ContinueSelfTest, " cmd");
  137. ResetMocks();
  138. TEST_EQ(TlclAssertPhysicalPresence(), 0,
  139. "AssertPhysicalPresence");
  140. TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, " cmd");
  141. ResetMocks();
  142. TEST_EQ(TlclPhysicalPresenceCMDEnable(), 0,
  143. "PhysicalPresenceCMDEnable");
  144. TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, " cmd");
  145. ResetMocks();
  146. TEST_EQ(TlclFinalizePhysicalPresence(), 0,
  147. "FinalizePhysicalPresence");
  148. TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, " cmd");
  149. ResetMocks();
  150. TEST_EQ(TlclAssertPhysicalPresenceResult(), 0,
  151. "AssertPhysicalPresenceResult");
  152. TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, " cmd");
  153. ResetMocks();
  154. TEST_EQ(TlclLockPhysicalPresence(), 0,
  155. "LockPhysicalPresence");
  156. TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, " cmd");
  157. ResetMocks();
  158. TEST_EQ(TlclIsOwned(), 0, "IsOwned");
  159. TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, " cmd");
  160. ResetMocks();
  161. calls[0].retval = VBERROR_SIMULATED;
  162. TEST_NEQ(TlclIsOwned(), 0, "IsOwned");
  163. ResetMocks();
  164. TEST_EQ(TlclForceClear(), 0, "ForceClear");
  165. TEST_EQ(calls[0].req_cmd, TPM_ORD_ForceClear, " cmd");
  166. ResetMocks();
  167. TEST_EQ(TlclSetEnable(), 0, "SetEnable");
  168. TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalEnable, " cmd");
  169. ResetMocks();
  170. TEST_EQ(TlclClearEnable(), 0, "ClearEnable");
  171. TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalDisable, " cmd");
  172. ResetMocks();
  173. TEST_EQ(TlclSetDeactivated(0), 0, "SetDeactivated");
  174. TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalSetDeactivated, " cmd");
  175. }
  176. /**
  177. * NV spaces test
  178. *
  179. * TODO: check params/data read/written.
  180. */
  181. static void ReadWriteTest(void)
  182. {
  183. uint8_t buf[32];
  184. ResetMocks();
  185. TEST_EQ(TlclDefineSpace(1, 2, 3), 0, "DefineSpace");
  186. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_DefineSpace, " cmd");
  187. ResetMocks();
  188. TEST_EQ(TlclSetNvLocked(), 0, "SetNvLocked");
  189. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_DefineSpace, " cmd");
  190. ResetMocks();
  191. TEST_EQ(TlclWrite(1, buf, 3), 0, "Write");
  192. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, " cmd");
  193. ResetMocks();
  194. TEST_EQ(TlclRead(1, buf, 3), 0, "Read");
  195. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_ReadValue, " cmd");
  196. ResetMocks();
  197. TEST_EQ(TlclWriteLock(1), 0, "WriteLock");
  198. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, " cmd");
  199. ResetMocks();
  200. TEST_EQ(TlclReadLock(1), 0, "ReadLock");
  201. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_ReadValue, " cmd");
  202. ResetMocks();
  203. TEST_EQ(TlclSetGlobalLock(), 0, "SetGlobalLock");
  204. TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, " cmd");
  205. }
  206. /**
  207. * Test PCR funcs
  208. *
  209. * TODO: check params/data read/written.
  210. */
  211. static void PcrTest(void)
  212. {
  213. uint8_t buf[kPcrDigestLength], buf2[kPcrDigestLength];
  214. ResetMocks();
  215. TEST_EQ(TlclPCRRead(1, buf, kPcrDigestLength), 0, "PCRRead");
  216. TEST_EQ(calls[0].req_cmd, TPM_ORD_PcrRead, " cmd");
  217. ResetMocks();
  218. TEST_EQ(TlclPCRRead(1, buf, kPcrDigestLength - 1), TPM_E_IOERROR,
  219. "PCRRead too small");
  220. ResetMocks();
  221. TEST_EQ(TlclExtend(1, buf, buf2), 0, "Extend");
  222. TEST_EQ(calls[0].req_cmd, TPM_ORD_Extend, " cmd");
  223. }
  224. /**
  225. * Test flags / capabilities
  226. *
  227. * TODO: check params/data read/written.
  228. */
  229. static void FlagsTest(void)
  230. {
  231. TPM_PERMANENT_FLAGS pflags;
  232. TPM_STCLEAR_FLAGS vflags;
  233. uint8_t disable = 0, deactivated = 0, nvlocked = 0;
  234. uint32_t u;
  235. uint8_t buf[32];
  236. ResetMocks();
  237. TEST_EQ(TlclGetPermanentFlags(&pflags), 0, "GetPermanentFlags");
  238. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, " cmd");
  239. ResetMocks();
  240. TEST_EQ(TlclGetSTClearFlags(&vflags), 0, "GetSTClearFlags");
  241. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, " cmd");
  242. ResetMocks();
  243. TEST_EQ(TlclGetFlags(NULL, NULL, NULL), 0, "GetFlags NULL");
  244. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, " cmd");
  245. ResetMocks();
  246. TEST_EQ(TlclGetFlags(&disable, &deactivated, &nvlocked), 0, "GetFlags");
  247. ResetMocks();
  248. TEST_EQ(TlclGetPermissions(1, &u), 0, "GetPermissions");
  249. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, " cmd");
  250. ResetMocks();
  251. TEST_EQ(TlclGetOwnership(buf), 0, "GetOwnership");
  252. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, " cmd");
  253. }
  254. /**
  255. * Test random
  256. *
  257. * TODO: check params/data read/written.
  258. * TODO: check overflow tests.
  259. */
  260. static void RandomTest(void)
  261. {
  262. uint8_t buf[32];
  263. uint32_t size;
  264. ResetMocks();
  265. size = sizeof(buf);
  266. TEST_EQ(TlclGetRandom(buf, sizeof(buf), &size), 0, "GetRandom");
  267. TEST_EQ(calls[0].req_cmd, TPM_ORD_GetRandom, " cmd");
  268. TEST_EQ(size, 0, " size 0");
  269. }
  270. int main(void)
  271. {
  272. TlclTest();
  273. SendCommandTest();
  274. ReadWriteTest();
  275. PcrTest();
  276. FlagsTest();
  277. RandomTest();
  278. return gTestSuccess ? 0 : 255;
  279. }