-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.cpp
837 lines (750 loc) · 24.1 KB
/
tests.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#include <gtest/gtest.h>
#include "def.h"
#include <iostream>
#include <thread>
#include <future>
#include <poll.h>
#include "cases/tests.hpp"
#include <errno.h>
#include "lib.hpp"
#include "tls.hpp"
#include "server.hpp"
extern pthread_cond_t server_cond;
extern pthread_mutex_t server_lock;
extern int server_up;
/* Set timeout for tests that can potentially block */
#define GTEST_TIMEOUT_BEGIN auto asyncFuture = \
std::async(std::launch::async, [this]()->void {
#define GTEST_TIMEOUT_END(X) return; }); \
EXPECT_TRUE(asyncFuture.wait_for(std::chrono::milliseconds(X)) \
!= std::future_status::timeout);
std::vector<std::future<void>> pending_futures;
using namespace std;
/* Sends a short message using send(), and checks its return value */
void test_send_small_encrypt(int opfd, void *unused) {
char const*test_str = "test_send";
int to_send = strlen(test_str) + 1;
EXPECT_EQ(send(opfd, test_str, to_send, 0), to_send);
}
/* Sends a short file using sendfile(), and checks its return */
void test_sendfile_small_encrypt(int opfd, void *unused) {
int filefd = open("small.txt", O_RDONLY);
EXPECT_NE(filefd, -1);
struct stat st;
fstat(filefd, &st);
EXPECT_GE(sendfile(opfd, filefd, 0, st.st_size), 0);
}
void test_send_max(int opfd, void *unused) {
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char buf[send_len];
gen_random(buf, send_len);
EXPECT_GE(send(opfd, buf, send_len, 0), 0);
}
void test_recv_max(int opfd, void *unused) {
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char buf[send_len];
gen_random(buf, send_len);
EXPECT_GE(send(opfd, buf, send_len, 0), 0);
char recv_mem[send_len];
EXPECT_NE(recv(opfd, recv_mem, send_len, 0), -1);
EXPECT_STREQ(recv_mem, buf);
}
/* Sends a short message and read the reply, which should echo the send message
* Checks that the message was sent and received correctly
*/
void test_recv_small_decrypt(int opfd, void *unused) {
char const *test_str = "test_read";
int send_len = strlen(test_str) + 1;
char buf[send_len];
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
EXPECT_NE(recv(opfd, buf, send_len, 0), -1);
EXPECT_STREQ(test_str, buf);
}
void test_send_overflow(int opfd, void *unused) {
/* This test does not pass in reference server */
unsigned int send_len = TLS_PAYLOAD_MAX_LEN + 1;
char buf[send_len];
gen_random(buf, send_len);
EXPECT_EQ(send(opfd, buf, send_len, 0), -1);
EXPECT_EQ(errno, E2BIG);
}
void test_sendmsg_single(int opfd, void *unused) {
struct msghdr msg;
char *buffer = prepare_msghdr(&msg);
//Load up the send data
char const *test_str = "test_sendmsg";
size_t send_len = strlen(test_str) + 1;
struct iovec vec = { (void *) test_str, send_len };
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
EXPECT_EQ(sendmsg(opfd, &msg, 0), send_len);
char buf[send_len];
EXPECT_NE(recv(opfd, buf, send_len, 0), -1);
EXPECT_STREQ(test_str, buf);
free(buffer);
}
void test_sendmsg_multiple(int opfd, void *unused) {
struct msghdr msg;
char *buffer = prepare_msghdr(&msg);
//Load up the send data
int iov_len = 5;
char *test_strs[iov_len];
struct iovec vec[iov_len];
int total_len = 0;
char const *test_str = "test_sendmsg_multiple";
for (int i = 0; i < iov_len; i++) {
test_strs[i] = (char *) malloc(strlen(test_str) + 1);
snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
vec[i].iov_base = (void *) test_strs[i];
vec[i].iov_len = strlen(test_strs[i]) + 1;
total_len += vec[i].iov_len;
}
msg.msg_iov = vec;
msg.msg_iovlen = iov_len;
EXPECT_EQ(sendmsg(opfd, &msg, 0), total_len);
char buf[total_len];
EXPECT_NE(recv(opfd, buf, total_len, 0), -1);
int len_cmp = 0;
for (int i = 0; i < iov_len; i++) {
EXPECT_STREQ(test_strs[i], buf + len_cmp);
len_cmp += strlen(buf + len_cmp) + 1;
}
free(buffer);
for(int i=0;i<iov_len;i++)
free(test_strs[i]);
}
/* Test sendmsg where iovecs point to memory scattered across
* physical memory
*/
void test_sendmsg_multiple_scattered(int opfd, void *unused) {
struct msghdr msg;
char *buffer = prepare_msghdr(&msg);
//Load up the send data
int iov_len = 3;
struct iovec vec[iov_len];
int total_len = 0;
char test_stack[] = "test_sendmsg_stack";
char const *test_data = "test_sendmsg_data";
char const *test_heap = "test_sendmsg_heap";
char *heap = (char *) malloc(strlen(test_heap) + 1);
snprintf(heap, strlen(test_heap) + 1, "%s", test_heap);
vec[0].iov_base = (void *) test_stack;
vec[0].iov_len = strlen(test_stack) + 1;
total_len += vec[0].iov_len;
vec[1].iov_base = (void *) test_data;
vec[1].iov_len = strlen(test_data) + 1;
total_len += vec[1].iov_len;
vec[2].iov_base = (void *) test_heap;
vec[2].iov_len = strlen(test_heap) + 1;
total_len += vec[2].iov_len;
msg.msg_iov = vec;
msg.msg_iovlen = iov_len;
EXPECT_EQ(sendmsg(opfd, &msg, 0), total_len);
char buf[total_len];
EXPECT_NE(recv(opfd, buf, total_len, 0), -1);
int len_cmp = 0;
EXPECT_STREQ(test_stack, buf + len_cmp);
len_cmp += vec[0].iov_len;
EXPECT_STREQ(test_data, buf + len_cmp);
len_cmp += vec[1].iov_len;
EXPECT_STREQ(test_heap, buf + len_cmp);
free(buffer);
free(heap);
}
/* Send 1<<14 amount of data using 1024 (max) iovecs */
void test_sendmsg_multiple_stress(int opfd, void *unused) {
struct msghdr msg;
char *buffer = prepare_msghdr(&msg);
//Load up the send data
int iov_len = 1024;
char *test_strs[iov_len];
struct iovec vec[iov_len];
int total_len = 0;
char const *test_str = "abcdefghijklmno";
for (int i = 0; i < iov_len; i++) {
test_strs[i] = (char *) malloc(strlen(test_str) + 1);
snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
vec[i].iov_base = (void *) test_strs[i];
vec[i].iov_len = strlen(test_strs[i]) + 1;
total_len += vec[i].iov_len;
}
msg.msg_iov = vec;
msg.msg_iovlen = iov_len;
EXPECT_EQ(sendmsg(opfd, &msg, 0), total_len);
char buf[1<<14];
EXPECT_NE(recv(opfd, buf, total_len, 0), -1);
int len_cmp = 0;
for (int i = 0; i < iov_len; i++) {
EXPECT_STREQ(test_strs[i], buf + len_cmp);
len_cmp += strlen(buf + len_cmp) + 1;
}
free(buffer);
for(int i=0;i<iov_len;i++)
free(test_strs[i]);
}
void test_splice_from_pipe(int opfd, void *unused) {
int p[2];
ASSERT_GE(pipe(p), 0);
int send_len = TLS_PAYLOAD_MAX_LEN;
char mem_send[TLS_PAYLOAD_MAX_LEN];
gen_random(mem_send, send_len);
EXPECT_GE(write(p[1], mem_send, send_len),0);
EXPECT_GE(splice(p[0], nullptr, opfd, nullptr, send_len, 0), 0);
char mem_recv[TLS_PAYLOAD_MAX_LEN];
EXPECT_GE(recv(opfd, mem_recv, send_len, 0), 0);
EXPECT_STREQ(mem_send, mem_recv);
}
void test_splice_to_pipe(int opfd, void *unused) {
int p[2];
ASSERT_GE(pipe(p), 0);
int send_len = TLS_PAYLOAD_MAX_LEN;
char mem_send[TLS_PAYLOAD_MAX_LEN];
gen_random(mem_send, send_len);
EXPECT_GE(send(opfd, mem_send, send_len, 0),0);
EXPECT_GE(splice(opfd, nullptr, p[1], nullptr, send_len, 0), 0);
char mem_recv[TLS_PAYLOAD_MAX_LEN];
EXPECT_GE(read(p[0], mem_recv, send_len), 0);
EXPECT_STREQ(mem_send, mem_recv);
}
void test_recvmsg_single(int opfd, void *unused) {
char const *test_str = "test_recvmsg_single";
int send_len = strlen(test_str) + 1;
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
char buf[send_len];
struct iovec vec;
vec.iov_base = (char *)buf;
vec.iov_len = send_len;
struct msghdr hdr;
hdr.msg_iovlen = 1;
hdr.msg_iov = &vec;
EXPECT_NE(recvmsg(opfd, &hdr, 0), -1);
EXPECT_STREQ(test_str, buf);
}
void test_recvmsg_single_max(int opfd, void *unused) {
int send_len = TLS_PAYLOAD_MAX_LEN;
char send_mem[send_len];
gen_random(send_mem, send_len);
EXPECT_EQ(send(opfd, send_mem, send_len, 0), send_len);
char recv_mem[TLS_PAYLOAD_MAX_LEN];
struct iovec vec;
vec.iov_base = (char *)recv_mem;
vec.iov_len = TLS_PAYLOAD_MAX_LEN;
struct msghdr hdr;
hdr.msg_iovlen = 1;
hdr.msg_iov = &vec;
EXPECT_NE(recvmsg(opfd, &hdr, 0), -1);
EXPECT_STREQ(send_mem, recv_mem);
}
void test_recvmsg_multiple(int opfd, void *unused) {
char buf[1<<14];
int send_len = 1<<14;
gen_random(buf, send_len);
EXPECT_EQ(send(opfd, buf, send_len, 0), send_len);
unsigned int msg_iovlen = 1024;
unsigned int iov_len = 16;
struct iovec vec[msg_iovlen];
char *iov_base[msg_iovlen];
for(int i=0;i<msg_iovlen;i++)
{
iov_base[i] = (char *)malloc(iov_len);
vec[i].iov_base = iov_base[i];
vec[i].iov_len = iov_len;
}
struct msghdr hdr;
hdr.msg_iovlen = msg_iovlen;
hdr.msg_iov = vec;
EXPECT_NE(recvmsg(opfd, &hdr, 0), -1);
unsigned int len_compared = 0;
for(int i=0;i<msg_iovlen;i++) {
EXPECT_EQ(memcmp(buf + len_compared, iov_base[i], iov_len), 0);
len_compared += iov_len;
}
for(int i=0;i<msg_iovlen;i++)
free(iov_base[i]);
}
/* Tests recvmsg_multiple under the case that decryption is
* guaranteed to be done by the async worker
*/
void test_recvmsg_multiple_async(int opfd, void *unused) {
char buf[1<<14];
int send_len = 1<<14;
gen_random(buf, send_len);
EXPECT_EQ(send(opfd, buf, send_len, 0), send_len);
unsigned int msg_iovlen = 1024;
unsigned int iov_len = 16;
struct iovec vec[msg_iovlen];
char *iov_base[msg_iovlen];
for(int i=0;i<msg_iovlen;i++)
{
iov_base[i] = (char *)malloc(iov_len);
vec[i].iov_base = iov_base[i];
vec[i].iov_len = iov_len;
}
struct msghdr hdr;
hdr.msg_iovlen = msg_iovlen;
hdr.msg_iov = vec;
/* Sleep for a while to give async worker a chance to run */
sleep(2);
EXPECT_NE(recvmsg(opfd, &hdr, 0), -1);
unsigned int len_compared = 0;
for(int i=0;i<msg_iovlen;i++) {
EXPECT_EQ(memcmp(buf + len_compared, iov_base[i], iov_len), 0);
len_compared += iov_len;
}
for(int i=0;i<msg_iovlen;i++)
free(iov_base[i]);
}
void test_single_send_multiple_recv(int opfd, void *unused) {
unsigned int num_messages = 2;
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
#define total_len send_len * num_messages
char send_mem[total_len];
gen_random(send_mem, send_len);
EXPECT_GE(send(opfd, send_mem, send_len, 0), 0);
char recv_mem[send_len];
memset(recv_mem, 0, send_len);
/* Give async worker time to run */
sleep(2);
EXPECT_NE(recv(opfd, recv_mem, send_len, 0), -1);
EXPECT_STREQ(recv_mem, send_mem);
EXPECT_NE(recv(opfd, recv_mem, send_len, 0), -1);
EXPECT_STREQ(recv_mem, send_mem);
}
/* Sends n messages of size TLS_PAYLOAD_MAX_LEN and checks that
* a single recv can receive them all.
* This test forces decryption to happen in both the cache and outside
*/
void test_multiple_send_single_recv(int opfd, void *unused) {
/* Client must be called with type == serve_send_twice */
unsigned int num_messages = 2;
unsigned int send_len = 10;
#define total_len send_len * num_messages
char send_mem[send_len];
gen_random(send_mem, send_len);
EXPECT_GE(send(opfd, send_mem, send_len, 0), 0);
char recv_mem[total_len];
memset(recv_mem, 0, total_len);
sleep(4);
EXPECT_EQ(recv(opfd, recv_mem, total_len, 0), total_len);
/* Must use memcmp since all recv are placed in single buffer */
EXPECT_STREQ(recv_mem, send_mem);
EXPECT_STREQ(recv_mem+send_len, send_mem);
}
void test_recv_partial(int opfd, void *unused) {
char const *test_str = "test_read_partial";
char const *test_str_first = "test_read";
char const *test_str_second = "_partial";
int send_len = strlen(test_str) + 1;
char recv_mem[send_len];
memset(recv_mem, 0, sizeof(recv_mem));
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
EXPECT_NE(recv(opfd, recv_mem, strlen(test_str_first), 0), -1);
EXPECT_STREQ(test_str_first, recv_mem);
memset(recv_mem, 0, sizeof(recv_mem));
EXPECT_NE(recv(opfd, recv_mem, strlen(test_str_second), 0), -1);
EXPECT_STREQ(test_str_second, recv_mem);
}
void test_recv_nonblock(int opfd, void *unused) {
char buf[4096];
EXPECT_EQ(recv(opfd, buf, sizeof(buf), MSG_DONTWAIT), -1);
EXPECT_TRUE(errno == EAGAIN || errno == EWOULDBLOCK);
}
void test_recv_peek(int opfd, void *unused) {
char const *test_str = "test_read_peek";
int send_len = strlen(test_str) + 1;
char buf[send_len];
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
EXPECT_NE(recv(opfd, buf, send_len, MSG_PEEK), -1);
EXPECT_STREQ(test_str, buf);
memset(buf, 0, sizeof(buf));
EXPECT_STREQ("", buf);
EXPECT_NE(recv(opfd, buf, send_len, 0), -1);
EXPECT_STREQ(test_str, buf);
}
void test_recv_peek_multiple(int opfd, void *unused) {
unsigned int num_peeks = 100;
char const *test_str = "test_read_peek";
int send_len = strlen(test_str) + 1;
char buf[send_len];
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
for(int i=0;i<num_peeks;i++) {
EXPECT_NE(recv(opfd, buf, send_len, MSG_PEEK), -1);
EXPECT_STREQ(test_str, buf);
memset(buf, 0, sizeof(buf));
EXPECT_STREQ("", buf);
}
EXPECT_NE(recv(opfd, buf, send_len, 0), -1);
EXPECT_STREQ(test_str, buf);
}
void test_poll_POLLIN(int opfd, void *unused) {
/* Test waiting for some descriptor */
char const *test_str = "test_poll";
int send_len = strlen(test_str) + 1;
char buf[send_len];
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
struct pollfd fd = {0,0,0};
fd.fd = opfd;
fd.events = POLLIN;
/* Set timeout to 2 secs */
EXPECT_EQ(poll(&fd, 1, 2000), 1);
EXPECT_NE(fd.revents & POLLIN, 0);
EXPECT_EQ(recv(opfd, buf, send_len, 0), send_len);
/* Test timing out */
EXPECT_EQ(poll(&fd, 1, 2000), 0);
}
/* Test waiting for some desscriptor, where
* the thread calling poll is guaranteed
* to need to be awoken, rather than returning
* instantly
*/
void test_poll_POLLIN_wait(int opfd, void *unused) {
char const *test_str = "test_poll_wait";
int send_len = strlen(test_str) + 1;
struct pollfd fd = {0,0,0};
fd.fd = opfd;
fd.events = POLLIN;
EXPECT_EQ(send(opfd, test_str, send_len, 0), send_len);
/* Set timeout to inf. secs */
EXPECT_EQ(poll(&fd, 1, -1), 1);
EXPECT_NE(fd.revents & POLLIN, 0);
char recv_mem[send_len];
EXPECT_EQ(recv(opfd, recv_mem, send_len, 0), send_len);
}
void test_poll_POLLOUT(int opfd, void *unused) {
struct pollfd fd = {0,0,0};
fd.fd = opfd;
fd.events = POLLOUT;
/* Set timeout to 2 secs */
EXPECT_EQ(poll(&fd, 1, 2000), 1);
EXPECT_NE(fd.revents & POLLOUT, 0);
}
void test_recv_wait(int opfd, void *unused) {
//Run with server_delay.
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char buf[send_len];
gen_random(buf, send_len);
EXPECT_GE(send(opfd, buf, send_len, 0), 0);
char recv_mem[send_len];
EXPECT_NE(recv(opfd, recv_mem, send_len, 0), -1);
EXPECT_STREQ(recv_mem, buf);
}
void test_recv_async(int opfd, void *unused) {
//Run with server_delay.
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char buf[send_len];
gen_random(buf, send_len);
EXPECT_GE(send(opfd, buf, send_len, 0), 0);
sleep(2);
char recv_mem[send_len];
EXPECT_NE(recv(opfd, recv_mem, send_len, 0), -1);
EXPECT_STREQ(recv_mem, buf);
}
//These doom tests will raise a SIGSEGV which will kill the
//program. Future work can include somehow catching and
//verifying that the signal was received, but for now its
//done manually
void test_recv_doom_noasync(int opfd, void *unused) {
//Run with server delay
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char send_mem[send_len];
gen_random(send_mem, send_len);
EXPECT_GE(send(opfd, send_mem, send_len, 0), 0);
char recv_mem[10];
EXPECT_EQ(recv(opfd, recv_mem, send_len, 0), -1);
}
void test_recv_doom_async(int opfd, void *unused) {
unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
char send_mem[send_len];
gen_random(send_mem, send_len);
EXPECT_GE(send(opfd, send_mem, send_len, 0), 0);
sleep(2);
char recv_mem[10];
EXPECT_EQ(recv(opfd, recv_mem, send_len, 0), -1);
}
void test_recvmsg_doom_noasync(int opfd, void *unused) {
char buf[1<<14];
int send_len = 1<<14;
gen_random(buf, send_len);
EXPECT_EQ(send(opfd, buf, send_len, 0), send_len);
unsigned int msg_iovlen = 1024;
unsigned int iov_len = 16;
struct iovec vec[msg_iovlen];
char *iov_base[msg_iovlen];
for(int i=0;i<msg_iovlen-1;i++)
{
iov_base[i] = (char *)malloc(iov_len);
vec[i].iov_base = iov_base[i];
vec[i].iov_len = iov_len;
}
//Set one of the iovecs to read-only data
iov_base[msg_iovlen-1] = (char *)test_recvmsg_doom_noasync;
vec[msg_iovlen-1].iov_base = iov_base[msg_iovlen-1];
vec[msg_iovlen-1].iov_len = iov_len;
struct msghdr hdr;
hdr.msg_iovlen = msg_iovlen;
hdr.msg_iov = vec;
EXPECT_EQ(recvmsg(opfd, &hdr, 0), -1);
for(int i=0;i<msg_iovlen-1;i++)
free(iov_base[i]);
}
void test_recvmsg_doom_async(int opfd, void *unused) {
char buf[1<<14];
int send_len = 1<<14;
gen_random(buf, send_len);
EXPECT_EQ(send(opfd, buf, send_len, 0), send_len);
unsigned int msg_iovlen = 1024;
unsigned int iov_len = 16;
struct iovec vec[msg_iovlen];
char *iov_base[msg_iovlen];
for(int i=0;i<msg_iovlen-1;i++)
{
iov_base[i] = (char *)malloc(iov_len);
vec[i].iov_base = iov_base[i];
vec[i].iov_len = iov_len;
}
//Set one of the iovecs to read-only data
iov_base[msg_iovlen-1] = (char *)test_recvmsg_doom_noasync;
vec[msg_iovlen-1].iov_base = iov_base[msg_iovlen-1];
vec[msg_iovlen-1].iov_len = iov_len;
struct msghdr hdr;
hdr.msg_iovlen = msg_iovlen;
hdr.msg_iov = vec;
sleep(2);
EXPECT_EQ(recvmsg(opfd, &hdr, 0), -1);
for(int i=0;i<msg_iovlen-1;i++)
free(iov_base[i]);
}
pthread_t server_thread;
class MyTestSuite: public testing::Test {
protected:
static void SetUpTestCase() {
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, nullptr);
SSL_library_init();
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
SSL_load_error_strings();/* load all error messages */
server_up = -2 * (server_max - server_min + 1);
pthread_cond_init(&server_cond, nullptr);
pthread_mutex_init(&server_lock, nullptr);
for(int i=(int)server_min; i<= (int)server_max;i++) {
thread t1(main_server, i);
t1.detach();
thread t2(ref_server, i);
t2.detach();
}
pthread_mutex_lock(&server_lock);
while (server_up < 0)
pthread_cond_wait(&server_cond, &server_lock);
pthread_mutex_unlock(&server_lock);
}
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(MyTestSuite, send_small_encrypt)
{
main_test_client(test_send_small_encrypt);
}
TEST_F(MyTestSuite, sendfile_small_encrypt)
{
main_test_client(test_sendfile_small_encrypt);
}
TEST_F(MyTestSuite, send_overflow)
{
main_test_client(test_send_overflow);
}
TEST_F(MyTestSuite, read_small_decrypt)
{
main_test_client(test_recv_small_decrypt);
}
TEST_F(MyTestSuite, DISABLED_socketpair)
{
EXPECT_EQ(1, 0)
;
}
TEST_F(MyTestSuite, unbinded)
{
main_test_client(test_unbinded);
}
TEST_F(MyTestSuite, DISABLED_sendto)
{
EXPECT_EQ(1, 0)
;
}
TEST_F(MyTestSuite, DISABLED_recvfrom)
{
EXPECT_EQ(1, 0)
;
}
TEST_F(MyTestSuite, sendmsg)
{
main_test_client(test_sendmsg_single);
}
TEST_F(MyTestSuite, DISABLED_sendmsg_multiple_iovecs)
{
/* Worked with iovec patch */
main_test_client(test_sendmsg_multiple);
}
TEST_F(MyTestSuite, DISABLED_sendmsg_multiple_iovecs_scattered)
{
/* Worked with iovec patch */
main_test_client(test_sendmsg_multiple_scattered);
}
TEST_F(MyTestSuite, sendmsg_multiple_iovecs_stress)
{
/* Worked with iovec patch */
main_test_client(test_sendmsg_multiple_stress);
}
TEST_F(MyTestSuite, splice_from_pipe)
{
/* Tests sendpage implementation */
main_test_client(test_splice_from_pipe);
}
TEST_F(MyTestSuite, splice_to_pipe)
{
/* Test splice_read implementation */
main_test_client(test_splice_to_pipe);
}
TEST_F(MyTestSuite, DISABLED_sendmmsg)
{
EXPECT_EQ(1, 0)
;
}
TEST_F(MyTestSuite, recvmsg_single)
{
main_test_client(test_recvmsg_single);
}
TEST_F(MyTestSuite, DISABLED_recvmsg_multiple)
{
/* Works with iovec patch */
main_test_client(test_recvmsg_multiple);
}
TEST_F(MyTestSuite, DISABLED_recvmsg_multiple_async)
{
/* Works with iovec patch */
main_test_client(test_recvmsg_multiple_async);
}
TEST_F(MyTestSuite, single_send_multiple_recv)
{
/* Works with iovec patch */
main_test_client(test_single_send_multiple_recv, server_send_twice);
}
TEST_F(MyTestSuite, multiple_send_single_recv)
{
/* Works with iovec patch */
main_test_client(test_multiple_send_single_recv, server_send_twice);
}
TEST_F(MyTestSuite, DISABLED_recv_partial)
{
main_test_client(test_recv_partial);
}
TEST_F(MyTestSuite, sockopt)
{
main_test_client(test_sockopt);
}
TEST_F(MyTestSuite, DISABLED_recv_nonblock)
{
GTEST_TIMEOUT_BEGIN
main_test_client(test_recv_nonblock);
GTEST_TIMEOUT_END(5000);
pending_futures.push_back(std::move(asyncFuture));
}
TEST_F(MyTestSuite, DISABLED_recv_peek)
{
GTEST_TIMEOUT_BEGIN
main_test_client(test_recv_peek);
GTEST_TIMEOUT_END(5000);
pending_futures.push_back(std::move(asyncFuture));
}
TEST_F(MyTestSuite, DISABLED_recv_peek_multiple)
{
GTEST_TIMEOUT_BEGIN
main_test_client(test_recv_peek_multiple);
GTEST_TIMEOUT_END(5000);
pending_futures.push_back(std::move(asyncFuture));
}
TEST_F(MyTestSuite, poll_POLLIN)
{
/* Worked with tls_poll patch */
main_test_client(test_poll_POLLIN);
}
TEST_F(MyTestSuite, poll_POLLIN_wait)
{
main_test_client(test_poll_POLLIN_wait, server_delay);
}
TEST_F(MyTestSuite, poll_POLLOUT)
{
main_test_client(test_poll_POLLOUT);
}
TEST_F(MyTestSuite, DISABLED_poll_POLLOUT_fail)
{
EXPECT_EQ(1, 0);
}
TEST_F(MyTestSuite, send_max)
{
main_test_client(test_send_max);
}
TEST_F(MyTestSuite, recv_max)
{
main_test_client(test_recv_max);
}
TEST_F(MyTestSuite, recvmsg_single_max)
{
main_test_client(test_recvmsg_single_max);
}
TEST_F(MyTestSuite, recv_wait)
{
main_test_client(test_recv_wait, server_delay);
}
TEST_F(MyTestSuite, recv_async)
{
main_test_client(test_recv_async);
}
TEST_F(MyTestSuite, recv_doom_noasync)
{
main_test_client(test_recv_doom_noasync, server_delay);
}
TEST_F(MyTestSuite, recv_doom_async)
{
main_test_client(test_recv_doom_async);
}
TEST_F(MyTestSuite, recvmsg_doom_noasync)
{
main_test_client(test_recvmsg_doom_noasync, server_delay);
}
TEST_F(MyTestSuite, recvmsg_doom_async)
{
main_test_client(test_recvmsg_doom_async);
}
/* These tests run on a plaintext server */
TEST_F(MyTestSuite, ref)
{
ref_test_client(test_send_small_encrypt);
ref_test_client(test_sendfile_small_encrypt);
ref_test_client(test_recv_small_decrypt);
ref_test_client(test_sendmsg_single);
ref_test_client(test_sendmsg_multiple);
ref_test_client(test_sendmsg_multiple_scattered);
ref_test_client(test_sendmsg_multiple_stress);
ref_test_client(test_recvmsg_single);
ref_test_client(test_recvmsg_multiple);
ref_test_client(test_recv_partial);
ref_test_client(test_recv_nonblock);
ref_test_client(test_recv_peek);
ref_test_client(test_recv_peek_multiple);
ref_test_client(test_poll_POLLIN);
ref_test_client(test_unbinded);
ref_test_client(test_send_max);
ref_test_client(test_recv_max);
ref_test_client(test_recvmsg_single_max);
ref_test_client(test_poll_POLLIN_wait, server_delay);
ref_test_client(test_recvmsg_multiple_async);
ref_test_client(test_multiple_send_single_recv, server_send_twice);
ref_test_client(test_single_send_multiple_recv, server_send_twice);
ref_test_client(test_poll_POLLOUT);
ref_test_client(test_recv_wait, server_delay);
}