1 /*
2    Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; see the file COPYING. If not, write to the
15    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16    MA  02110-1301  USA.
17 */
18 
19 // testsuite.cpp
20 
21 #include "test.hpp"
22 #include "md5.hpp"
23 
24 
25 typedef unsigned char byte;
26 
27 void taocrypt_test(void*);
28 void file_test(const char*, byte*);
29 
30 void client_test(void*);
31 void echoclient_test(void*);
32 
33 THREAD_RETURN YASSL_API server_test(void*);
34 THREAD_RETURN YASSL_API echoserver_test(void*);
35 
36 void wait_tcp_ready(func_args&);
37 
38 
39 
main(int argc,char ** argv)40 int main(int argc, char** argv)
41 {
42     func_args args(argc, argv);
43     func_args server_args(argc, argv);
44 
45     // *** Crypto Test ***
46     taocrypt_test(&args);
47     assert(args.return_code == 0);
48 
49 
50     // *** Simple yaSSL client server test ***
51     tcp_ready ready;
52     server_args.SetSignal(&ready);
53 
54     THREAD_TYPE serverThread;
55     start_thread(server_test, &server_args, &serverThread);
56     wait_tcp_ready(server_args);
57 
58     client_test(&args);
59     assert(args.return_code == 0);
60     join_thread(serverThread);
61     assert(server_args.return_code == 0);
62 
63 
64     // *** Echo input yaSSL client server test ***
65     start_thread(echoserver_test, &server_args, &serverThread);
66     wait_tcp_ready(server_args);
67     func_args echo_args;
68 
69             // setup args
70     const int numArgs = 3;
71     echo_args.argc = numArgs;
72     char* myArgv[numArgs];
73 
74     char argc0[32];
75     char argc1[32];
76     char argc2[32];
77 
78     myArgv[0] = argc0;
79     myArgv[1] = argc1;
80     myArgv[2] = argc2;
81 
82     echo_args.argv = myArgv;
83 
84     strcpy(echo_args.argv[0], "echoclient");
85     strcpy(echo_args.argv[1], "input");
86     strcpy(echo_args.argv[2], "output");
87     remove("output");
88 
89             // make sure OK
90     echoclient_test(&echo_args);
91     assert(echo_args.return_code == 0);
92 
93 
94     // *** Echo quit yaSSL client server test ***
95     echo_args.argc = 2;
96     strcpy(echo_args.argv[1], "quit");
97 
98     echoclient_test(&echo_args);
99     assert(echo_args.return_code == 0);
100     join_thread(serverThread);
101     assert(server_args.return_code == 0);
102 
103 
104             // input output compare
105     byte input[TaoCrypt::MD5::DIGEST_SIZE];
106     byte output[TaoCrypt::MD5::DIGEST_SIZE];
107     file_test("input", input);
108     file_test("output", output);
109     assert(memcmp(input, output, sizeof(input)) == 0);
110 
111     printf("\nAll tests passed!\n");
112     yaSSL_CleanUp();
113 
114     return 0;
115 }
116 
117 
118 
start_thread(THREAD_FUNC fun,func_args * args,THREAD_TYPE * thread)119 void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
120 {
121 #ifndef _POSIX_THREADS
122     *thread = (HANDLE)_beginthreadex(0, 0, fun, args, 0, 0);
123 #else
124     pthread_create(thread, 0, fun, args);
125 #endif
126 }
127 
128 
join_thread(THREAD_TYPE thread)129 void join_thread(THREAD_TYPE thread)
130 {
131 #ifndef _POSIX_THREADS
132     int res = WaitForSingleObject(thread, INFINITE);
133     assert(res == WAIT_OBJECT_0);
134     res = CloseHandle(thread);
135     assert(res);
136 #else
137     pthread_join(thread, 0);
138 #endif
139 }
140 
141 
142 
wait_tcp_ready(func_args & args)143 void wait_tcp_ready(func_args& args)
144 {
145 #ifdef _POSIX_THREADS
146     pthread_mutex_lock(&args.signal_->mutex_);
147 
148     if (!args.signal_->ready_)
149         pthread_cond_wait(&args.signal_->cond_, &args.signal_->mutex_);
150     args.signal_->ready_ = false; // reset
151 
152     pthread_mutex_unlock(&args.signal_->mutex_);
153 #endif
154 }
155 
156 
test_openSSL_des()157 int test_openSSL_des()
158 {
159     /* test des encrypt/decrypt */
160     char data[] = "this is my data ";
161     int  dataSz = (int)strlen(data);
162     DES_key_schedule key[3];
163     byte iv[8];
164     EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_md5(), NULL, (byte*)data, dataSz, 1,
165                    (byte*)key, iv);
166 
167     byte cipher[16];
168     DES_ede3_cbc_encrypt((byte*)data, cipher, dataSz, &key[0], &key[1],
169                          &key[2], &iv, true);
170     byte plain[16];
171     DES_ede3_cbc_encrypt(cipher, plain, 16, &key[0], &key[1], &key[2],
172                          &iv, false);
173     return 0;
174 }
175