Skip to content

Commit

Permalink
lib: add a from optarg attribute friendly converter
Browse files Browse the repository at this point in the history
Object attributes from the command line can be in hex
or friendly name attribute list, try to convert hex
before handing off to the friendly conversion routine.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Nov 7, 2017
1 parent f1c64d5 commit cbda767
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/tpm2_attr_util.c
Expand Up @@ -632,3 +632,13 @@ char *tpm2_attr_util_nv_attrtostr(TPMA_NV nvattrs) {
char *tpm2_attr_util_obj_attrtostr(TPMA_OBJECT objattrs) {
return tpm2_attr_util_common_attrtostr(objattrs.val, obj_attr_table, ARRAY_LEN(obj_attr_table));
}

bool tpm2_attr_util_obj_from_optarg(char *argvalue, TPMA_OBJECT *objattrs) {

bool res = tpm2_util_string_to_uint32(argvalue, &objattrs->val);
if (!res) {
res = tpm2_attr_util_obj_strtoattr(argvalue, objattrs);
}

return res;
}
13 changes: 13 additions & 0 deletions lib/tpm2_attr_util.h
Expand Up @@ -62,6 +62,19 @@ bool tpm2_attr_util_nv_strtoattr(char *attribute_list, TPMA_NV *nvattrs);
*/
bool tpm2_attr_util_obj_strtoattr(char *attribute_list, TPMA_OBJECT *objattrs);

/**
* Converts a numerical or friendly string described object attribute into the
* TPMA_OBJECT. Similar to tpm2_alg_util_from_optarg().
* @param argvalue
* Either a raw numeric for a UINT32 or a friendly name object attribute list
* as in tpm2_attr_util_nv_strtoattr().
* @param objattrs
* The converted bits for a TPMA_OBJECT
* @return
* true on success or false on error.
*/
bool tpm2_attr_util_obj_from_optarg(char *argvalue, TPMA_OBJECT *objattrs);

/**
* Converts a TPMA_NV structure to a friendly name style string.
* @param nvattrs
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test_tpm2_attr_util.c
Expand Up @@ -356,6 +356,22 @@ static void test_tpm2_attr_util_obj_strtoattr_token_unknown(void **state) {
res = tpm2_attr_util_obj_strtoattr(arg1, &objattrrs);
assert_false(res);
}

static void test_tpm2_attr_util_obj_from_optarg_good(void **state) {
(void) state;

TPMA_OBJECT objattrs = { .val = 0, };
bool res = tpm2_attr_util_obj_from_optarg("0x00000002", &objattrs);
assert_true(res);
assert_int_equal(0x02, objattrs.val);

objattrs.val = 0;
char buf[] = "fixedtpm";
res = tpm2_attr_util_obj_from_optarg(buf, &objattrs);
assert_true(res);
assert_int_equal(TPMA_OBJECT_FIXEDTPM, objattrs.val);
}

int main(int argc, char* argv[]) {
(void) argc;
(void) argv;
Expand Down Expand Up @@ -464,6 +480,9 @@ int main(int argc, char* argv[]) {

/* negative tests */
cmocka_unit_test(test_tpm2_attr_util_obj_strtoattr_token_unknown),

/* test from an optarg */
cmocka_unit_test(test_tpm2_attr_util_obj_from_optarg_good)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit cbda767

Please sign in to comment.