1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3  * Copyright (c) 2017-2018, Intel Corporation
4  *
5  * All rights reserved.
6  ***********************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "tss2_sys.h"
16 
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "test.h"
20 
21 /**
22  * This program contains integration test for SAPI Tss2_Sys_GetRandom.
23  * First, this test is checking the return code to make sure the
24  * SAPI is executed correctly(return code should return TPM2_RC_SUCCESS).
25  * Second, the SAPI is called twice to make sure the return randomBytes
26  * are different by comparing the two randomBytes through memcmp.
27  * It might not be the best test for random bytes generator but
28  * at least this test shows the return randomBytes are different.
29  */
30 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)31 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
32 {
33     TSS2_RC rc;
34     TPM2B_DIGEST randomBytes1 = {sizeof (TPM2B_DIGEST) - 2,};
35     TPM2B_DIGEST randomBytes2 = {sizeof (TPM2B_DIGEST) - 2,};
36     int bytes = 20;
37 
38     LOG_INFO("GetRandom tests started.");
39     rc = Tss2_Sys_GetRandom(sapi_context, 0, bytes, &randomBytes1, 0);
40     if (rc != TSS2_RC_SUCCESS) {
41         LOG_ERROR("GetRandom FAILED! Response Code : %x", rc);
42         exit(1);
43     }
44     rc = Tss2_Sys_GetRandom(sapi_context, 0, bytes, &randomBytes2, 0);
45     if (rc != TSS2_RC_SUCCESS) {
46         LOG_ERROR("GetRandom FAILED! Response Code : %x", rc);
47         exit(1);
48     }
49     if(memcmp(&randomBytes1, &randomBytes2, bytes) == 0) {
50         LOG_ERROR("Comparison FAILED! randomBytes 0x%p & 0x%p are the same.", &randomBytes1, &randomBytes2);
51         exit(1);
52     }
53     LOG_INFO("GetRandom Test Passed!");
54     return 0;
55 }
56