File: mechtabletest.c

package info (click to toggle)
opencryptoki 3.23.0%2Bdfsg-0.3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,604 kB
  • sloc: ansic: 214,248; sh: 2,759; makefile: 289; yacc: 242; pascal: 152; exp: 126; lex: 93; cpp: 9
file content (88 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <string.h>

#include "unittest.h"

#include <mechtable.h>

int checkstring(void)
{
    unsigned int i;
    int idx, res = 0;

    for (i = 0; i < MECHTABLE_NUM_ELEMS; ++i) {
        idx = mechtable_idx_from_string(mechtable_rows[i].string);
        if (idx < 0) {
            fprintf(stderr, "Mechanism %s not found in table!\n",
                    mechtable_rows[i].string);
            res = -1;
        } else if ((unsigned int) idx != i) {
            fprintf(stderr, "Expected mechanism %s at index %u, but query returned %d!\n",
                    mechtable_rows[i].string, i, idx);
            res = -1;
        }
        if (mechrow_from_string(mechtable_rows[i].string) == NULL) {
            fprintf(stderr, "Unable to get row reference for mechanism %s!\n",
                    mechtable_rows[i].string);
            res = -1;
        }
    }
    return res;
}

int checknumeric(void)
{
    unsigned int i;
    int idx, res = 0;

    for (i = 0; i < MECHTABLE_NUM_ELEMS; ++i) {
        idx = mechtable_idx_from_numeric(mechtable_rows[i].numeric);
        if (idx < 0) {
            fprintf(stderr, "Mechanism %lu not found in table!\n",
                    mechtable_rows[i].numeric);
            res = -1;
        } else if ((unsigned int) idx != i) {
            fprintf(stderr, "Expected mechanism %lu at index %u, but query returned %d!\n",
                    mechtable_rows[i].numeric, i, idx);
            res = -1;
        }
        if (mechrow_from_numeric(mechtable_rows[i].numeric) == NULL) {
            fprintf(stderr, "Unable to get row reference for mechanism %lu!\n",
                    mechtable_rows[i].numeric);
            res = -1;
        }
    }
    return res;
}

int checkalias(void)
{
    const struct mechrow *row = mechrow_from_string("CKM_ECDSA_KEY_PAIR_GEN");
    if (row)
        return strcmp(row->string, "CKM_EC_KEY_PAIR_GEN");
    return -1;
}

int checkfailure(void)
{
    int idxnum = mechtable_idx_from_numeric(0xffffffffu);
    int idxstr = mechtable_idx_from_string("CKM_DOES_NOT_EXIST");
    int res = 0;

    if (idxnum != -1) {
        fprintf(stderr, "Did find 0xffffffff which should not be a valid mechanism!\n");
        res = -1;
    }
    if (idxstr != -1) {
        fprintf(stderr, "Did find mechanism CKM_DOES_NOT_EXIST!\n");
        res = -1;
    }
    return res;
}

int main(void)
{
    if (checkstring() || checknumeric() || checkalias() || checkfailure())
        return TEST_FAIL;
    return TEST_PASS;
}