Skip to content

Commit

Permalink
Merge pull request #6 from zanebeckwith/output-in-test
Browse files Browse the repository at this point in the history
Save the public key and its handle to file, in the create_primary test.
  • Loading branch information
Zane Beckwith authored Dec 28, 2017
2 parents b466e04 + 80e68fd commit 751bea9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
9 changes: 8 additions & 1 deletion test/test-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

char *hostname_g = "localhost";
const char *port_g = "2321";
char *pub_key_filename_g = "pub_key.txt";
char *handle_filename_g = "handle.txt";

#define TEST_ASSERT(cond) \
do \
Expand All @@ -43,8 +45,13 @@ const char *port_g = "2321";
#define parse_cmd_args(argc, argv) \
do \
{ \
if (argc == 2) { \
if (argc >= 2) { \
hostname_g = argv[1]; \
} \
printf("Connecting to %s:%s for TPM testing\n", hostname_g, port_g); \
if (argc == 4) { \
pub_key_filename_g = argv[2]; \
handle_filename_g = argv[3]; \
} \
printf("Saving public key to %s and handle to %s\n", pub_key_filename_g, handle_filename_g);\
} while(0);
54 changes: 52 additions & 2 deletions test/tss2_sys_createprimary-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "test-utils.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct test_context {
TSS2_SYS_CONTEXT *sapi_ctx;
Expand All @@ -36,7 +38,7 @@ int main(int argc, char *argv[])
{
parse_cmd_args(argc, argv);

full_test();
full_test(pub_key_filename_g, handle_filename_g);
}

void initialize(struct test_context *ctx)
Expand Down Expand Up @@ -81,7 +83,7 @@ void cleanup(struct test_context *ctx)
}
}

void full_test()
void full_test(const char* pub_key_filename, const char* handle_filename)
{
printf("In tss2_sys_createprimary-test::full_test...\n");

Expand Down Expand Up @@ -156,8 +158,56 @@ void full_test()

TEST_ASSERT(TSS2_RC_SUCCESS == ret);

int write_ret = 0;

FILE *pub_key_file_ptr = fopen(pub_key_filename, "w");
if (NULL == pub_key_file_ptr)
return;
do {
if (fprintf(pub_key_file_ptr, "%02X", 4) != 2)
break;

for (unsigned i=0; i < public_key.publicArea.unique.ecc.x.size; i++) {
if (fprintf(pub_key_file_ptr, "%02X", public_key.publicArea.unique.ecc.x.buffer[i]) != 2) {
write_ret = -1;
break;
}
}
if (0 != write_ret)
break;

for (unsigned i=0; i < public_key.publicArea.unique.ecc.y.size; i++) {
if (fprintf(pub_key_file_ptr, "%02X", public_key.publicArea.unique.ecc.y.buffer[i]) != 2) {
write_ret = -1;
break;
}
}
if (0 != write_ret)
break;
} while(0);
(void)fclose(pub_key_file_ptr);

(void)handle_filename;
FILE *handle_file_ptr = fopen(handle_filename, "w");
if (NULL == handle_file_ptr)
return;
write_ret = 0;
do {
for (int i=(sizeof(key_handle)-1); i >= 0; i--) {
if (fprintf(handle_file_ptr, "%02X", (key_handle >> i*8) & 0xFF) != 2) {
write_ret = -1;
break;
}
}
if (0 != write_ret)
break;
} while(0);
(void)fclose(handle_file_ptr);

cleanup(&ctx);

TEST_ASSERT(0 == write_ret);

printf("ok\n");
}

0 comments on commit 751bea9

Please sign in to comment.