~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/testsuite/testsuite.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// testsuite.cpp
 
2
 
 
3
#include "test.hpp"
 
4
#include "md5.hpp"
 
5
 
 
6
 
 
7
typedef unsigned char byte;
 
8
 
 
9
void taocrypt_test(void*);
 
10
void file_test(const char*, byte*);
 
11
 
 
12
void client_test(void*);
 
13
void echoclient_test(void*);
 
14
 
 
15
THREAD_RETURN YASSL_API server_test(void*);
 
16
THREAD_RETURN YASSL_API echoserver_test(void*);
 
17
 
 
18
void wait_tcp_ready(func_args&);
 
19
 
 
20
 
 
21
 
 
22
int main(int argc, char** argv)
 
23
{
 
24
    func_args args(argc, argv);
 
25
    func_args server_args(argc, argv);
 
26
 
 
27
    // *** Crypto Test ***
 
28
    taocrypt_test(&args);
 
29
    assert(args.return_code == 0);
 
30
    
 
31
    
 
32
    // *** Simple yaSSL client server test ***
 
33
    tcp_ready ready;
 
34
    server_args.SetSignal(&ready);
 
35
 
 
36
    THREAD_TYPE serverThread;
 
37
    start_thread(server_test, &server_args, &serverThread);
 
38
    wait_tcp_ready(server_args);
 
39
 
 
40
    client_test(&args);
 
41
    assert(args.return_code == 0);
 
42
    join_thread(serverThread);
 
43
    assert(server_args.return_code == 0);
 
44
    
 
45
 
 
46
    // *** Echo input yaSSL client server test ***
 
47
    start_thread(echoserver_test, &server_args, &serverThread);
 
48
    wait_tcp_ready(server_args);
 
49
    func_args echo_args;
 
50
 
 
51
            // setup args
 
52
    const int numArgs = 3;
 
53
    echo_args.argc = numArgs;
 
54
    char* myArgv[numArgs];
 
55
 
 
56
    char argc0[32];
 
57
    char argc1[32];
 
58
    char argc2[32];
 
59
 
 
60
    myArgv[0] = argc0;
 
61
    myArgv[1] = argc1;
 
62
    myArgv[2] = argc2;
 
63
 
 
64
    echo_args.argv = myArgv;
 
65
   
 
66
    strcpy(echo_args.argv[0], "echoclient");
 
67
    strcpy(echo_args.argv[1], "input");
 
68
    strcpy(echo_args.argv[2], "output");
 
69
    remove("output");
 
70
 
 
71
            // make sure OK
 
72
    echoclient_test(&echo_args);
 
73
    assert(echo_args.return_code == 0);
 
74
 
 
75
 
 
76
    // *** Echo quit yaSSL client server test ***
 
77
    echo_args.argc = 2;
 
78
    strcpy(echo_args.argv[1], "quit");
 
79
 
 
80
    echoclient_test(&echo_args);
 
81
    assert(echo_args.return_code == 0);
 
82
    join_thread(serverThread);
 
83
    assert(server_args.return_code == 0);
 
84
 
 
85
 
 
86
            // input output compare
 
87
    byte input[TaoCrypt::MD5::DIGEST_SIZE];
 
88
    byte output[TaoCrypt::MD5::DIGEST_SIZE];
 
89
    file_test("input", input);
 
90
    file_test("output", output);
 
91
    assert(memcmp(input, output, sizeof(input)) == 0);
 
92
 
 
93
    printf("\nAll tests passed!\n");
 
94
    yaSSL_CleanUp();
 
95
 
 
96
    return 0;
 
97
}
 
98
 
 
99
 
 
100
 
 
101
void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
 
102
{
 
103
#ifndef _POSIX_THREADS
 
104
    *thread = _beginthreadex(0, 0, fun, args, 0, 0);
 
105
#else
 
106
    pthread_create(thread, 0, fun, args);
 
107
#endif
 
108
}
 
109
 
 
110
 
 
111
void join_thread(THREAD_TYPE thread)
 
112
{
 
113
#ifndef _POSIX_THREADS
 
114
    int res = WaitForSingleObject(reinterpret_cast<HANDLE>(thread), INFINITE);
 
115
    assert(res == WAIT_OBJECT_0);
 
116
    res = CloseHandle(reinterpret_cast<HANDLE>(thread));
 
117
    assert(res);
 
118
#else
 
119
    pthread_join(thread, 0);
 
120
#endif
 
121
}
 
122
 
 
123
 
 
124
 
 
125
void wait_tcp_ready(func_args& args)
 
126
{
 
127
#ifdef _POSIX_THREADS
 
128
    pthread_mutex_lock(&args.signal_->mutex_);
 
129
    
 
130
    if (!args.signal_->ready_)
 
131
        pthread_cond_wait(&args.signal_->cond_, &args.signal_->mutex_);
 
132
    args.signal_->ready_ = false; // reset
 
133
 
 
134
    pthread_mutex_unlock(&args.signal_->mutex_);
 
135
#endif
 
136
}
 
137
 
 
138
 
 
139
int test_openSSL_des()
 
140
{
 
141
    /* test des encrypt/decrypt */
 
142
    char data[] = "this is my data ";
 
143
    int  dataSz = strlen(data);
 
144
    DES_key_schedule key[3];
 
145
    byte iv[8];
 
146
    EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_md5(), NULL, (byte*)data, dataSz, 1,
 
147
                   (byte*)key, iv);
 
148
 
 
149
    byte cipher[16];
 
150
    DES_ede3_cbc_encrypt((byte*)data, cipher, dataSz, &key[0], &key[1],
 
151
                         &key[2], &iv, true);
 
152
    byte plain[16];
 
153
    DES_ede3_cbc_encrypt(cipher, plain, 16, &key[0], &key[1], &key[2],
 
154
                         &iv, false);
 
155
    return 0;
 
156
}