summaryrefslogtreecommitdiff
path: root/test/regression/nx_secure_test/nx_secure_x509_list_test.c
blob: 2497a1640a13250d5ce33c3b7c2fba6c9b780968 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation                                */
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* SPDX-License-Identifier: MIT                                            */
/***************************************************************************/

#include <stdio.h>


#include "tls_test_utility.h"
#include "nx_secure_x509.h"


/* Test basic X509 parsing with an example certificates. */
#include "google_cert.c"
#include "test_ca_cert.c"
#include "test_device_cert.c"

#define     DEMO_STACK_SIZE         2048

/* Define the ThreadX and NetX object control blocks...  */
static TX_THREAD               thread_0;

/* Define thread prototypes.  */
static void    thread_0_entry(ULONG thread_input);

void NX_SECURE_X509_ListTest();
void NX_SECURE_X509_ListTestNULLs();

#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void nx_secure_x509_list_test_application_define(void *first_unused_memory)
#endif
{

    /* Create the main thread.  */
    tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
                     first_unused_memory, DEMO_STACK_SIZE,
                     4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
}

static void    thread_0_entry(ULONG thread_input)
{

    /* Print out test information banner.  */
    printf("NetX Secure Test:   X509 list Test  ...................................");

    NX_SECURE_X509_ListTest();
    
    NX_SECURE_X509_ListTestNULLs();

    printf("SUCCESS!\n");
    test_control_return(0);

}

#define NUM_TEST_CERTS 3
NX_SECURE_X509_CERT certificate_array[NUM_TEST_CERTS];

TEST(NX_SECURE_X509, ListTest)
{
UINT status;
NX_SECURE_X509_CERT *list_head;
UINT i;
NX_SECURE_X509_DISTINGUISHED_NAME cert_name;
NX_SECURE_X509_DISTINGUISHED_NAME cert_name_g;
NX_SECURE_X509_DISTINGUISHED_NAME cert_name_ca;
NX_SECURE_X509_CERT *certificate;


    /* Initialize our certificate structures with some data. */
    for(i = 0; i < NUM_TEST_CERTS; ++i)
    {
        memset(&certificate_array[i], 0, sizeof(NX_SECURE_X509_CERT));
    }

    /* Initialize the list head. */
    list_head = NX_NULL;

    status = nx_secure_x509_certificate_initialize(&certificate_array[0], google_cert_der, google_cert_der_len, NX_NULL, 0, NX_NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    EXPECT_EQ(0, status);

    status = nx_secure_x509_certificate_initialize(&certificate_array[1], test_ca_cert_der, test_ca_cert_der_len, NX_NULL, 0, NX_NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    EXPECT_EQ(0, status);

    status = nx_secure_x509_certificate_initialize(&certificate_array[2], test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, NX_NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    EXPECT_EQ(0, status);

    /* Add our certificates to the list. */
    for(i = 0; i < NUM_TEST_CERTS; ++i)
    {
        status = _nx_secure_x509_certificate_list_add(&list_head, &certificate_array[i], 0);

        EXPECT_EQ(0, status);
    }

    /* Try adding a certificate twice. */
    status = _nx_secure_x509_certificate_list_add(&list_head, &certificate_array[0], 0);
    EXPECT_EQ(NX_INVALID_PARAMETERS, status);

    /* Setup an example certificate name to search for - use strict name comparison.*/
    cert_name.nx_secure_x509_common_name = (const UCHAR*)"www.example.com";
    cert_name.nx_secure_x509_common_name_length = strlen("www.example.com");

    cert_name.nx_secure_x509_organization = (const UCHAR*)"Express Logic";
    cert_name.nx_secure_x509_organization_length = strlen("Express Logic");

    cert_name.nx_secure_x509_org_unit = (const UCHAR*)"NetX Secure";
    cert_name.nx_secure_x509_org_unit_length= strlen("NetX Secure");

    cert_name.nx_secure_x509_state = (const UCHAR*)"CA";
    cert_name.nx_secure_x509_state_length = strlen("CA");

    cert_name.nx_secure_x509_country = (const UCHAR*)"US";
    cert_name.nx_secure_x509_country_length = strlen("US");

    cert_name.nx_secure_x509_serial_number = NX_NULL;
    cert_name.nx_secure_x509_serial_number_length = 0;

    cert_name.nx_secure_x509_distinguished_name_qualifier = NX_NULL;
    cert_name.nx_secure_x509_distinguished_name_qualifier_length = 0;

    /* Search for certificate in the list. */
    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name, 0, &certificate);

    EXPECT_EQ(0, status);

    /* Remove our certificate from the list. */
    status = _nx_secure_x509_certificate_list_remove(&list_head, &cert_name, NX_NULL);
    EXPECT_EQ(0, status);

    /* Search for certificate in the list after removing it. */
    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name, 0, &certificate);
    EXPECT_EQ(NX_SECURE_X509_CERTIFICATE_NOT_FOUND, status);

    /* Setup an example certificate name to search for - use common name comparison.*/
    cert_name_g.nx_secure_x509_common_name = (const UCHAR*)"www.google.com";
    cert_name_g.nx_secure_x509_common_name_length = strlen("www.google.com");
    cert_name_ca.nx_secure_x509_common_name = (const UCHAR*)"NetX Secure Test CA";
    cert_name_ca.nx_secure_x509_common_name_length = strlen("NetX Secure Test CA");

    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name_g, 0, &certificate);
    EXPECT_EQ(0, status);

    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name_ca, 0, &certificate);
    EXPECT_EQ(0, status);

    /* Remove google certificate from the list. */
    status = _nx_secure_x509_certificate_list_remove(&list_head, &cert_name_g, NX_NULL);
    EXPECT_EQ(0, status);

    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name_g, 0, &certificate);
    EXPECT_EQ(NX_SECURE_X509_CERTIFICATE_NOT_FOUND, status);

    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name_ca, 0, &certificate);
    EXPECT_EQ(0, status);

    status = _nx_secure_x509_certificate_list_remove(&list_head, &cert_name_g, NX_NULL);
    EXPECT_EQ(NX_SECURE_X509_CERTIFICATE_NOT_FOUND, status);

    /* Remove CA certificate from the list. */
    status = _nx_secure_x509_certificate_list_remove(&list_head, &cert_name_ca, NX_NULL);
    EXPECT_EQ(0, status);

    status = _nx_secure_x509_certificate_list_find(&list_head, &cert_name_ca, 0, &certificate);
    EXPECT_EQ(NX_SECURE_X509_CERTIFICATE_NOT_FOUND, status);

}



TEST(NX_SECURE_X509, ListTestNULLs)
{
UINT status;
NX_SECURE_X509_DISTINGUISHED_NAME cert_name;
NX_SECURE_X509_CERT *certificate;

    /* Test NULLS. */
    status = _nx_secure_x509_certificate_list_find(NX_NULL, &cert_name, 0, &certificate);

    EXPECT_EQ(NX_PTR_ERROR, status);
    EXPECT_TRUE((certificate == NX_NULL));

    status = _nx_secure_x509_certificate_list_remove(NX_NULL, &cert_name, NX_NULL);

    EXPECT_EQ(NX_PTR_ERROR, status);

}