| 1 |
#include <stdio.h> |
| 2 |
#include <string.h> |
| 3 |
|
| 4 |
#include "unittest.h" |
| 5 |
|
| 6 |
#include <mechtable.h> |
| 7 |
|
| 8 |
int checkstring(void) |
| 9 |
{ |
| 10 |
unsigned int i; |
| 11 |
int idx, res = 0; |
| 12 |
|
| 13 |
for (i = 0; i < MECHTABLE_NUM_ELEMS; ++i) { |
| 14 |
idx = mechtable_idx_from_string(mechtable_rows[i].string); |
| 15 |
if (idx < 0) { |
| 16 |
fprintf(stderr, "Mechanism %s not found in table!\n", |
| 17 |
mechtable_rows[i].string); |
| 18 |
res = -1; |
| 19 |
} else if ((unsigned int) idx != i) { |
| 20 |
fprintf(stderr, "Expected mechanism %s at index %u, but query returned %d!\n", |
| 21 |
mechtable_rows[i].string, i, idx); |
| 22 |
res = -1; |
| 23 |
} |
| 24 |
if (mechrow_from_string(mechtable_rows[i].string) == NULL) { |
| 25 |
fprintf(stderr, "Unable to get row reference for mechanism %s!\n", |
| 26 |
mechtable_rows[i].string); |
| 27 |
res = -1; |
| 28 |
} |
| 29 |
} |
| 30 |
return res; |
| 31 |
} |
| 32 |
|
| 33 |
int checknumeric(void) |
| 34 |
{ |
| 35 |
unsigned int i; |
| 36 |
int idx, res = 0; |
| 37 |
|
| 38 |
for (i = 0; i < MECHTABLE_NUM_ELEMS; ++i) { |
| 39 |
idx = mechtable_idx_from_numeric(mechtable_rows[i].numeric); |
| 40 |
if (idx < 0) { |
| 41 |
fprintf(stderr, "Mechanism %lu not found in table!\n", |
| 42 |
mechtable_rows[i].numeric); |
| 43 |
res = -1; |
| 44 |
} else if ((unsigned int) idx != i) { |
| 45 |
fprintf(stderr, "Expected mechanism %lu at index %u, but query returned %d!\n", |
| 46 |
mechtable_rows[i].numeric, i, idx); |
| 47 |
res = -1; |
| 48 |
} |
| 49 |
if (mechrow_from_numeric(mechtable_rows[i].numeric) == NULL) { |
| 50 |
fprintf(stderr, "Unable to get row reference for mechanism %lu!\n", |
| 51 |
mechtable_rows[i].numeric); |
| 52 |
res = -1; |
| 53 |
} |
| 54 |
} |
| 55 |
return res; |
| 56 |
} |
| 57 |
|
| 58 |
int checkalias(void) |
| 59 |
{ |
| 60 |
const struct mechrow *row = mechrow_from_string("CKM_ECDSA_KEY_PAIR_GEN"); |
| 61 |
if (row) |
| 62 |
return strcmp(row->string, "CKM_EC_KEY_PAIR_GEN"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
int checkfailure(void) |
| 67 |
{ |
| 68 |
int idxnum = mechtable_idx_from_numeric(0xffffffffu); |
| 69 |
int idxstr = mechtable_idx_from_string("CKM_DOES_NOT_EXIST"); |
| 70 |
int res = 0; |
| 71 |
|
| 72 |
if (idxnum != -1) { |
| 73 |
fprintf(stderr, "Did find 0xffffffff which should not be a valid mechanism!\n"); |
| 74 |
res = -1; |
| 75 |
} |
| 76 |
if (idxstr != -1) { |
| 77 |
fprintf(stderr, "Did find mechanism CKM_DOES_NOT_EXIST!\n"); |
| 78 |
res = -1; |
| 79 |
} |
| 80 |
return res; |
| 81 |
} |
| 82 |
|
| 83 |
int main(void) |
| 84 |
{ |
| 85 |
if (checkstring() || checknumeric() || checkalias() || checkfailure()) |
| 86 |
return TEST_FAIL; |
| 87 |
return TEST_PASS; |
| 88 |
} |