summaryrefslogtreecommitdiff
path: root/release/src/router/cyassl/testsuite/testsuite.c
blob: 09e445372bb26ff5aed4928b0026d31162a29603 (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
197
198
199
200
/* testsuite.c */

#include "ssl.h"
#include "cyassl_test.h"
#include "ctc_md5.h"

#ifdef SINGLE_THREADED
    #error testsuite needs threads to run, please run ctaocrypt/test, \
           and the examples/ individually
#endif

void wait_tcp_ready(func_args*);
void ctaocrypt_test(void*);

void client_test(void*);
void echoclient_test(void*);

THREAD_RETURN CYASSL_API server_test(void*);
THREAD_RETURN CYASSL_API echoserver_test(void*);

void file_test(char* file, byte* hash);

enum {
    NUMARGS = 3
};


int main(int argc, char** argv)
{
    func_args args;
    func_args server_args;

    tcp_ready ready;
    THREAD_TYPE serverThread;

    StartTCP();

    args.argc = server_args.argc = argc;
    args.argv = server_args.argv = argv;
   
    /* CTaoCrypt test */
    ctaocrypt_test(&args);
    if (args.return_code != 0) return args.return_code;
 
    /* Simple CyaSSL client server test */
    InitCyaSSL();
    InitTcpReady(&ready);
    server_args.signal = &ready;
    start_thread(server_test, &server_args, &serverThread);
    wait_tcp_ready(&server_args);

    client_test(&args);
    if (args.return_code != 0) return args.return_code;
    join_thread(serverThread);
    if (server_args.return_code != 0) return server_args.return_code;

    /* Echo input yaSSL client server test */
    start_thread(echoserver_test, &server_args, &serverThread);
    wait_tcp_ready(&server_args);
    {
        func_args echo_args;
        char* myArgv[NUMARGS];

        char argc0[32];
        char argc1[32];
        char argc2[32];

        myArgv[0] = argc0;
        myArgv[1] = argc1;
        myArgv[2] = argc2;

        echo_args.argc = NUMARGS;
        echo_args.argv = myArgv;
   
        strcpy(echo_args.argv[0], "echoclient");
        strcpy(echo_args.argv[1], "input");
        strcpy(echo_args.argv[2], "output");
        remove("output");

        /* make sure OK */
        echoclient_test(&echo_args);
        if (echo_args.return_code != 0) return echo_args.return_code;  

#ifdef CYASSL_DTLS
        wait_tcp_ready(&server_args);
#endif
        /* send quit to echoserver */
        echo_args.argc = 2;
        strcpy(echo_args.argv[1], "quit");

        echoclient_test(&echo_args);
        if (echo_args.return_code != 0) return echo_args.return_code;
        join_thread(serverThread);
        if (server_args.return_code != 0) return server_args.return_code;
    }

    /* validate output equals input */
    {
        byte input[MD5_DIGEST_SIZE];
        byte output[MD5_DIGEST_SIZE];

        file_test("input",  input);
        file_test("output", output);
        if (memcmp(input, output, sizeof(input)) != 0)
            return -1;
    }

    FreeCyaSSL();
    FreeTcpReady(&ready);

    printf("\nAll tests passed!\n");
    return 0;
}



void wait_tcp_ready(func_args* args)
{
#ifdef _POSIX_THREADS
    pthread_mutex_lock(&args->signal->mutex);
    
    if (!args->signal->ready)
        pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
    args->signal->ready = 0; /* reset */

    pthread_mutex_unlock(&args->signal->mutex);
#endif
}


void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
{
#ifndef _POSIX_THREADS
    *thread = (HANDLE)_beginthreadex(0, 0, fun, args, 0, 0);
#else
    pthread_create(thread, 0, fun, args);
#endif
}


void join_thread(THREAD_TYPE thread)
{
#ifndef _POSIX_THREADS
    int res = WaitForSingleObject(thread, INFINITE);
    assert(res == WAIT_OBJECT_0);
    res = CloseHandle(thread);
    assert(res);
#else
    pthread_join(thread, 0);
#endif
}


void InitTcpReady(tcp_ready* ready)
{
    ready->ready = 0;
#ifdef _POSIX_THREADS
    pthread_mutex_init(&ready->mutex, 0);
    pthread_cond_init(&ready->cond, 0);
#endif
}


void FreeTcpReady(tcp_ready* ready)
{
#ifdef _POSIX_THREADS
    pthread_mutex_destroy(&ready->mutex);
    pthread_cond_destroy(&ready->cond);
#endif
}


void file_test(char* file, byte* check)
{
    FILE* f;
    int   i = 0, j;
    Md5   md5;
    byte  buf[1024];
    byte  md5sum[MD5_DIGEST_SIZE];
   
    InitMd5(&md5); 
    if( !( f = fopen( file, "rb" ) )) {
        printf("Can't open %s\n", file);
        return;
    }
    while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
        Md5Update(&md5, buf, i);
    
    Md5Final(&md5, md5sum);
    memcpy(check, md5sum, sizeof(md5sum));

    for(j = 0; j < MD5_DIGEST_SIZE; ++j ) 
        printf( "%02x", md5sum[j] );
   
    printf("  %s\n", file);

    fclose(f);
}