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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2011, 2015 Oracle and/or its affiliates. All rights reserved.
*/
#include <ctype.h>
#include <stdio.h>
#include <db.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#ifndef _WIN32
#include <pthread.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/uio.h>
#endif
#include "CuTest.h"
#include "test_util.h"
#define MAX_SEGS 10
#define MAX_MSGS 10
#undef LOCK_MUTEX
#undef UNLOCK_MUTEX
#ifdef _WIN32
#define sleep(s) Sleep(1000 * (s))
typedef HANDLE mutex_t;
#define mutex_init(m) \
(((*(m) = CreateMutex(NULL, FALSE, NULL)) != NULL) ? 0 : -1)
#define mutex_lock(m) \
((WaitForSingleObject(*(m), INFINITE) == WAIT_OBJECT_0) ? \
0 : GetLastError())
#define mutex_unlock(m) (ReleaseMutex(*(m)) ? 0 : GetLastError())
#define mutex_destroy(m) (CloseHandle(*(m)) ? 0 : GetLastError())
typedef HANDLE cond_t;
#define cond_init(c) ((*(c) = CreateEvent(NULL, \
TRUE, FALSE, NULL)) == NULL ? GetLastError() : 0)
#define cond_wait(c, m) (SignalObjectAndWait(*(m), *(c), INFINITE, FALSE) == WAIT_OBJECT_0 ? \
0 : GetLastError())
#define cond_wake(c) (SetEvent(*(c)) ? 0 : GetLastError())
#else
typedef pthread_mutex_t mutex_t;
#define mutex_init(m) pthread_mutex_init((m), NULL)
#define mutex_lock(m) pthread_mutex_lock(m)
#define mutex_unlock(m) pthread_mutex_unlock(m)
#define mutex_destroy(m) pthread_mutex_destroy(m)
typedef pthread_cond_t cond_t;
#define cond_init(c) pthread_cond_init((c), NULL)
#define cond_wait(c, m) pthread_cond_wait((c), (m))
#define cond_wake(c) pthread_cond_broadcast(c)
#endif
struct channel_test_globals {
CuTest *test;
mutex_t mtx;
cond_t cond;
u_int *ports;
int ports_cnt;
};
struct report {
int dbt_count;
DBT dbt[MAX_SEGS];
int msg_count;
char *msg[MAX_MSGS];
int done, ret;
};
struct reports {
mutex_t m;
int count;
struct report rpt[2];
};
struct env_info {
struct report *rpt;
struct reports *rpts;
struct channel_test_globals *g;
int startupdone;
};
struct msginfo {
DB_ENV *dbenv;
int count;
};
typedef int (*PRED) __P((void *));
static int await_condition __P((PRED, void *, long));
static int check_dbt_string __P((DBT *, const char *));
static void clear_rpt __P((DB_ENV *));
static void clear_rpt_int __P((struct report *));
static int env_done __P((void *));
static struct report *get_rpt __P((const DB_ENV *));
static int fortify __P((DB_ENV *, struct channel_test_globals *));
static int get_avail_ports __P((u_int *, int));
static int has_msgs __P((void *));
static void msg_disp __P((DB_ENV *, DB_CHANNEL *, DBT *, u_int32_t, u_int32_t));
static void msg_disp2 __P((DB_ENV *, DB_CHANNEL *, DBT *, u_int32_t, u_int32_t));
static void msg_disp3 __P((DB_ENV *, DB_CHANNEL *, DBT *, u_int32_t, u_int32_t));
static void msg_disp4 __P((DB_ENV *, DB_CHANNEL *, DBT *, u_int32_t, u_int32_t));
static void msg_disp5 __P((DB_ENV *, DB_CHANNEL *, DBT *, u_int32_t, u_int32_t));
static void notify __P((DB_ENV *, u_int32_t, void *));
static int is_started __P((void *));
static void td __P((DB_ENV *));
static void test_data_init __P((DBT *, char *));
static void test_zeroes __P((DB_CHANNEL *, DB_ENV *, CuTest *));
static int two_done __P((void *));
#define LOCK_MUTEX(m) do { \
int __ret; \
__ret = mutex_lock(m); \
assert(__ret == 0); \
} while (0)
#define UNLOCK_MUTEX(m) do { \
int __ret; \
__ret = mutex_unlock(m); \
assert(__ret == 0); \
} while (0)
int TestChannelSuiteSetup(CuSuite *suite) {
return (0);
}
int TestChannelSuiteTeardown(CuSuite *suite) {
return (0);
}
int TestChannelTestSetup(CuTest *test) {
struct channel_test_globals *g;
int ret;
if ((g = calloc(1, sizeof(*g))) == NULL)
return (ENOMEM);
if ((ret = mutex_init(&g->mtx)) != 0) {
free(g);
return (ret);
}
if ((ret = cond_init(&g->cond)) != 0) {
mutex_destroy(&g->mtx);
free(g);
return (ret);
}
g->test = test;
test->context = g;
return (0);
}
int TestChannelTestTeardown(CuTest *test) {
struct channel_test_globals *g;
int ret;
g = test->context;
assert(g != NULL);
ret = mutex_destroy(&g->mtx);
free(g);
test->context = NULL;
return (ret);
}
static void
myerrcall(const DB_ENV *dbenv, const char *errpfx, const char *msg) {
struct report *rpt = get_rpt(dbenv);
char *msgp;
assert(rpt->msg_count < MAX_MSGS);
msgp = strdup(msg);
assert(msgp != NULL);
rpt->msg[rpt->msg_count++] = msgp;
}
static int
fortify(dbenv, g)
DB_ENV *dbenv;
struct channel_test_globals *g;
{
struct report *rpt;
struct env_info *info;
if ((info = calloc(1, sizeof(*info))) == NULL)
return (ENOMEM);
if ((rpt = calloc(1, sizeof(*rpt))) == NULL) {
free(info);
return (ENOMEM);
}
info->rpt = rpt;
info->rpts = NULL;
info->g = g;
info->startupdone = 0;
dbenv->app_private = info;
return (0);
}
static int
setup(envp1, envp2, envp3, g)
DB_ENV **envp1, **envp2, **envp3;
struct channel_test_globals *g;
{
DB_ENV *dbenv1, *dbenv2, *dbenv3;
DB_SITE *dbsite;
u_int32_t flags;
int ret;
u_int *ports;
#define CHECK(call) \
do { \
if ((ret = (call)) != 0) { \
fprintf(stderr, "error %d from %s", ret, #call); \
goto err; \
} \
} while (0);
ports = g->ports;
dbenv1 = dbenv2 = dbenv3 = NULL;
CHECK(db_env_create(&dbenv1, 0));
CHECK(fortify(dbenv1, g));
dbenv1->set_errpfx(dbenv1, "ENV1");
dbenv1->set_errcall(dbenv1, myerrcall);
flags = DB_INIT_REP | DB_INIT_LOG | DB_INIT_LOCK | DB_INIT_MPOOL |
DB_INIT_TXN | DB_RECOVER | DB_THREAD | DB_CREATE;
setup_envdir("DIR1", 1);
CHECK(dbenv1->open(dbenv1, "DIR1", flags, 0));
CHECK(dbenv1->rep_set_config(dbenv1, DB_REPMGR_CONF_ELECTIONS, 0));
CHECK(dbenv1->repmgr_site(dbenv1, "localhost", ports[0], &dbsite, 0));
CHECK(dbsite->set_config(dbsite, DB_LOCAL_SITE, 1));
CHECK(dbsite->close(dbsite));
CHECK(dbenv1->set_event_notify(dbenv1, notify));
CHECK(dbenv1->repmgr_msg_dispatch(dbenv1, msg_disp, 0));
CHECK(dbenv1->repmgr_start(dbenv1, 2, DB_REP_MASTER));
CHECK(db_env_create(&dbenv2, 0));
CHECK(fortify(dbenv2, g));
dbenv2->set_errpfx(dbenv2, "ENV2");
dbenv2->set_errcall(dbenv2, myerrcall);
setup_envdir("DIR2", 1);
CHECK(dbenv2->open(dbenv2, "DIR2", flags, 0));
CHECK(dbenv2->rep_set_config(dbenv2, DB_REPMGR_CONF_ELECTIONS, 0));
CHECK(dbenv2->repmgr_site(dbenv2, "localhost", ports[1], &dbsite, 0));
CHECK(dbsite->set_config(dbsite, DB_LOCAL_SITE, 1));
CHECK(dbsite->close(dbsite));
CHECK(dbenv2->repmgr_site(dbenv2, "localhost", ports[0], &dbsite, 0));
CHECK(dbsite->set_config(dbsite, DB_BOOTSTRAP_HELPER, 1));
CHECK(dbsite->close(dbsite));
CHECK(dbenv2->set_event_notify(dbenv2, notify));
CHECK(dbenv2->repmgr_start(dbenv2, 2, DB_REP_CLIENT));
await_condition(is_started, dbenv2, 60);
if (!is_started(dbenv2)) {
dbenv2->errx(dbenv2, "startup done not achieved in 60 seconds");
ret = DB_TIMEOUT;
goto err;
}
CHECK(db_env_create(&dbenv3, 0));
CHECK(fortify(dbenv3, g));
dbenv3->set_errpfx(dbenv3, "ENV3");
dbenv3->set_errcall(dbenv3, myerrcall);
CHECK(dbenv3->repmgr_msg_dispatch(dbenv3, msg_disp2, 0));
setup_envdir("DIR3", 1);
CHECK(dbenv3->open(dbenv3, "DIR3", flags, 0));
CHECK(dbenv3->rep_set_config(dbenv3, DB_REPMGR_CONF_ELECTIONS, 0));
CHECK(dbenv3->repmgr_site(dbenv3, "localhost", ports[2], &dbsite, 0));
CHECK(dbsite->set_config(dbsite, DB_LOCAL_SITE, 1));
CHECK(dbsite->close(dbsite));
CHECK(dbenv3->repmgr_site(dbenv3, "localhost", ports[0], &dbsite, 0));
CHECK(dbsite->set_config(dbsite, DB_BOOTSTRAP_HELPER, 1));
CHECK(dbsite->close(dbsite));
CHECK(dbenv3->set_event_notify(dbenv3, notify));
CHECK(dbenv3->repmgr_start(dbenv3, 2, DB_REP_CLIENT));
await_condition(is_started, dbenv3, 60);
if (!is_started(dbenv3)) {
dbenv3->errx(dbenv3, "startup done not achieved in 60 seconds");
ret = DB_TIMEOUT;
goto err;
}
*envp1 = dbenv1;
*envp2 = dbenv2;
*envp3 = dbenv3;
return (0);
err:
if (dbenv3 != NULL)
td(dbenv3);
if (dbenv2 != NULL)
td(dbenv2);
if (dbenv1 != NULL)
td(dbenv1);
return (ret);
}
static void
td(dbenv)
DB_ENV *dbenv;
{
struct env_info *info;
dbenv->set_errcall(dbenv, NULL);
dbenv->set_event_notify(dbenv, NULL);
info = dbenv->app_private;
dbenv->close(dbenv, 0);
if (info != NULL) {
clear_rpt_int(info->rpt);
free(info->rpt);
free(info);
}
}
static void
clear_rpt_int(rpt)
struct report *rpt;
{
int i;
for (i = 0; i < rpt->dbt_count; i++)
free(rpt->dbt[i].data);
rpt->dbt_count = 0;
for (i = 0; i < rpt->msg_count; i++)
free(rpt->msg[i]);
rpt->msg_count = 0;
rpt->done = 0;
}
static void
clear_rpt(dbenv)
DB_ENV *dbenv;
{
struct env_info *info;
struct report *rpt;
info = dbenv->app_private;
rpt = info->rpt;
clear_rpt_int(rpt);
}
static int
env_done(ctx)
void *ctx;
{
DB_ENV *dbenv = ctx;
struct report *rpt = get_rpt(dbenv);
return (rpt->done);
}
static void
await_done(dbenv)
DB_ENV *dbenv;
{
await_condition(env_done, dbenv, 60);
assert(env_done(dbenv));
}
static int
has_msgs(ctx)
void *ctx;
{
struct msginfo *inf = ctx;
DB_ENV *dbenv = inf->dbenv;
struct report *rpt = get_rpt(dbenv);
return (rpt->msg_count == inf->count);
}
static struct report *
get_rpt(dbenv)
const DB_ENV *dbenv;
{
struct env_info *info;
if ((info = dbenv->app_private) == NULL)
return (NULL);
return (info->rpt);
}
int TestChannelFeature(CuTest *ct) {
/* Run this test only when replication is supported. */
#ifdef HAVE_REPLICATION
DB_ENV *dbenv1, *dbenv2, *dbenv3;
DB_CHANNEL *ch;
DB_REP_STAT *stats;
DB_SITE *dbsite;
DBT dbt, rdbts[10], resp;
struct channel_test_globals *g;
struct report *rpt;
struct reports rpts;
struct msginfo info;
char *p;
void *pointer, *vp, *buffer;
u_int8_t short_buf[4];
size_t sz;
int done, eid, ret;
u_int ports[3];
#ifdef _WIN32
setvbuf(stdout, NULL, _IONBF, 0);
#endif
printf("this is a test for repmgr channels feature\n");
g = ct->context;
ret = get_avail_ports(ports, 3);
CuAssertTrue(ct, (ret == 0));
g->ports = ports;
g->ports_cnt = 3;
printf("use ports: {%u, %u, %u}\n", ports[0], ports[1], ports[2]);
/*
* The ports[0] will be the local port for dbenv1, ports[1] for dbenv2,
* and ports[2] for dbenv3.
*/
CuAssertTrue(ct, (ret = setup(&dbenv1, &dbenv2, &dbenv3, g) == 0));
/*
* For this first section, we're sending to ENV2.
*/
CuAssertTrue(ct,
(ret = dbenv1->repmgr_site(dbenv1,
"localhost", ports[1], &dbsite, 0)) == 0);
CuAssertTrue(ct, (ret = dbsite->get_eid(dbsite, &eid)) == 0);
CuAssertTrue(ct, (ret = dbsite->close(dbsite)) == 0);
CuAssertTrue(ct, (ret = dbenv1->repmgr_channel(dbenv1, eid, &ch, 0)) == 0);
memset(&dbt, 0, sizeof(dbt));
p = "foobar";
dbt.data = p;
dbt.size = (u_int32_t)strlen(p) + 1;
memset(&resp, 0, sizeof(resp));
resp.flags = DB_DBT_MALLOC;
printf("1. send async msg with no msg dispatch in place\n");
clear_rpt(dbenv2);
CuAssertTrue(ct, (ret = ch->send_msg(ch, &dbt, 1, 0)) == 0);
/* Wait til dbenv2 has reported 1 msg. */
info.dbenv = dbenv2;
info.count = 1;
await_condition(has_msgs, &info, 90);
rpt = get_rpt(dbenv2);
CuAssertTrue(ct, rpt->msg_count == 1);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3670", strlen("BDB3670")) == 0);
printf("2. send request with no msg dispatch in place\n");
clear_rpt(dbenv2);
ret = ch->send_request(ch, &dbt, 1, &resp, 0, 0);
CuAssertTrue(ct, ret == DB_NOSERVER);
if (resp.data != NULL)
free(resp.data);
await_condition(has_msgs, &info, 90);
CuAssertTrue(ct, rpt->msg_count == 1);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3670", strlen("BDB3670")) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_msg_dispatch(dbenv2, msg_disp, 0)) == 0);
printf("3. send request where recip forgot resp\n");
clear_rpt(dbenv2);
ret = ch->send_request(ch, &dbt, 1, &resp, 0, 0);
CuAssertTrue(ct, ret == DB_KEYEMPTY);
if (resp.data != NULL)
free(resp.data);
await_done(dbenv2);
CuAssertTrue(ct, rpt->msg_count == 1);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3671", strlen("BDB3671")) == 0);
printf("4. now with dispatch fn installed, send a simple async msg\n");
clear_rpt(dbenv2);
test_data_init(&dbt, "Mr. Watson -- come here -- I want to see you.");
CuAssertTrue(ct, (ret = ch->send_msg(ch, &dbt, 1, 0)) == 0);
await_done(dbenv2);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0],
"Mr. Watson -- come here -- I want to see you.");
CuAssertTrue(ct, rpt->msg_count == 0);
printf("5. send a multi-seg request\n");
clear_rpt(dbenv2);
memset(&resp, 0, sizeof(resp));
resp.flags = DB_DBT_MALLOC;
test_data_init(&rdbts[0], "I wish I were a fish");
test_data_init(&rdbts[1], "I wish I were a bass");
test_data_init(&rdbts[2],
"I'd climb up on a slippery rock and slide down on my ... hands and knees");
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 3, &resp, 0, 0)) == 0);
check_dbt_string(&resp, "this is the answer to the request");
if (resp.data)
free(resp.data);
await_done(dbenv2);
CuAssertTrue(ct, rpt->dbt_count == 3);
check_dbt_string(&rpt->dbt[0], "I wish I were a fish");
check_dbt_string(&rpt->dbt[1], "I wish I were a bass");
check_dbt_string(&rpt->dbt[2],
"I'd climb up on a slippery rock and slide down on my ... hands and knees");
CuAssertTrue(ct, rpt->msg_count == 0);
test_zeroes(ch, dbenv2, ct);
printf("7. send request with too-small USERMEM buffer\n");
clear_rpt(dbenv2);
resp.data = short_buf;
resp.ulen = sizeof(short_buf);
resp.flags = DB_DBT_USERMEM;
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 3, &resp, 0, 0)) == DB_BUFFER_SMALL);
await_done(dbenv2);
CuAssertTrue(ct, rpt->msg_count == 1);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3659", strlen("BDB3659")) == 0);
CuAssertTrue(ct, rpt->ret == EINVAL);
#define BUFLEN 20000
buffer = malloc(BUFLEN);
if (buffer == NULL)
return (2);
resp.data = buffer;
resp.ulen = BUFLEN;
resp.flags = DB_DBT_USERMEM;
printf("8. send USERMEM request without necessary DB_MULTIPLE\n");
clear_rpt(dbenv2);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 2, &resp, 0, 0)) == DB_BUFFER_SMALL);
await_done(dbenv2);
CuAssertTrue(ct, rpt->msg_count == 1);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3658", strlen("BDB3658")) == 0);
CuAssertTrue(ct, rpt->ret == EINVAL);
printf("9. send USERMEM request with DB_MULTIPLE\n");
clear_rpt(dbenv2);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 2, &resp, 0, DB_MULTIPLE)) == 0);
DB_MULTIPLE_INIT(pointer, &resp);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, strcmp((char*)vp, "roses are red") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, strcmp((char*)vp, "violets are blue") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, pointer == NULL);
ch->close(ch, 0);
/* ------------------------------- */
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, DB_EID_MASTER, &ch, 0)) == 0);
CuAssertTrue(ct, (ret = dbenv1->repmgr_msg_dispatch(dbenv1, msg_disp2, 0)) == 0);
// do a request to master
// switch masters
// do a request to new master
printf("(now we try a couple of operations on a master channel)\n");
printf("10. send request to original master\n");
rpt = get_rpt(dbenv1);
clear_rpt(dbenv1);
resp.data = buffer;
resp.ulen = BUFLEN;
resp.flags = DB_DBT_USERMEM;
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 1, &resp, 0, 0)) == 0);
check_dbt_string(&resp, "ENV1");
await_done(dbenv1);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0], "I wish I were a fish");
printf("switch master and wait for our client to see the change\n");
((struct env_info *)dbenv2->app_private)->startupdone = 0;
CuAssertTrue(ct, (ret = dbenv1->repmgr_start(dbenv1, 0, DB_REP_CLIENT)) == 0);
sleep(1); /* workaround for 19329 */
for (done = 0; ; ) {
/*
* Become master, and then make sure it really happened.
* Occasionally a race develops, where we're still holding on to
* the msg lockout at env3 at this point, in which case the
* rep_start() call (underlying our repmgr_start() call here) is
* simply dropped on the floor.
*/
CuAssertTrue(ct, (ret = dbenv3->repmgr_start(dbenv3,
0, DB_REP_MASTER)) == 0);
CuAssertTrue(ct, (ret = dbenv3->rep_stat(dbenv3,
&stats, 0)) == 0);
done = stats->st_status == DB_REP_MASTER;
free(stats);
if (done)
break;
sleep(1);
};
/*
* !!!
* Workaround for 19297: wait until verify dance is complete at env2,
* because (just a little bit) later we're going to switch master again,
* to env2. If rep_start(MASTER) at env2 happens while processing
* VERIFY match record, core rep ignores the rep_start() (even though it
* returns 0).
*/
LOCK_MUTEX(&g->mtx);
while (!((struct env_info *)dbenv2->app_private)->startupdone) {
cond_wait(&g->cond, &g->mtx);
}
// TODO: fix these macros so that this ridiculous hack isn't necessary
#ifndef _WIN32
UNLOCK_MUTEX(&g->mtx);
#endif
printf("11. send request which should go to new master (only)\n");
clear_rpt(dbenv1);
clear_rpt(dbenv3);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 1, &resp, 0, 0)) == 0);
check_dbt_string(&resp, "ENV3");
rpt = get_rpt(dbenv3);
await_done(dbenv3);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0], "I wish I were a fish");
rpt = get_rpt(dbenv1);
CuAssertTrue(ct, !rpt->done); /* old master shouldn't have recvd anything */
printf("switch master again, to ``self''\n");
CuAssertTrue(ct, (ret = dbenv3->repmgr_start(dbenv3, 0, DB_REP_CLIENT)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_start(dbenv2, 0, DB_REP_MASTER)) == 0);
/* No need to wait for env2 to see that env2 has become master. */
clear_rpt(dbenv1);
clear_rpt(dbenv2);
clear_rpt(dbenv3);
printf("12. send to self, async\n");
CuAssertTrue(ct, (ret = ch->send_msg(ch, &dbt, 1, 0)) == 0);
await_done(dbenv2);
rpt = get_rpt(dbenv2);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0],
"Mr. Watson -- come here -- I want to see you.");
CuAssertTrue(ct, rpt->msg_count == 0);
printf(" (check that other two sites didn't receive it)\n");
sleep(1);
CuAssertTrue(ct, !get_rpt(dbenv1)->done);
CuAssertTrue(ct, !get_rpt(dbenv3)->done);
printf("13. send-to-self request\n");
clear_rpt(dbenv2);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 2, &resp, 0, DB_MULTIPLE)) == 0);
DB_MULTIPLE_INIT(pointer, &resp);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, strcmp((char*)vp, "roses are red") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, strcmp((char*)vp, "violets are blue") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, pointer == NULL);
/*
* re-test the 0-length cases, in the send-to-self context (the
* implementation has a bunch of separate code)
*/
test_zeroes(ch, dbenv2, ct);
ch->close(ch, 0);
/* ---------------------------------------- */
// If you go from env2 to env, we know that it's ports[0]
//
CuAssertTrue(ct, (ret = dbenv1->repmgr_msg_dispatch(dbenv1, msg_disp3, 0)) == 0);
CuAssertTrue(ct,
(ret = dbenv2->repmgr_site(dbenv2,
"localhost", ports[0], &dbsite, 0)) == 0);
CuAssertTrue(ct, (ret = dbsite->get_eid(dbsite, &eid)) == 0);
CuAssertTrue(ct, (ret = dbsite->close(dbsite)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, eid, &ch, 0)) == 0);
printf("14. send request to site that has been shut down\n");
td(dbenv1);
memset(&resp, 0, sizeof(resp));
resp.flags = DB_DBT_MALLOC;
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 2, &resp, 0, 0)) ==
DB_REP_UNAVAIL);
if (resp.data != NULL)
free(resp.data);
// TODO: a much more interesting case is to have the remote site shut
// down while waiting for the response, because that exercises some
// clean-up code. But I guess that requires running in a couple of
// threads.
ch->close(ch, 0);
printf("15. try to connect to a down site\n");
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, eid, &ch, 0)) == DB_REP_UNAVAIL);
printf("16. try to connect to a non-existent EID\n");
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, 1732, &ch, 0)) == EINVAL);
printf("17. connect master to self from the start\n");
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, DB_EID_MASTER, &ch, 0)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_msg_dispatch(dbenv2, msg_disp2, 0)) == 0);
rpt = get_rpt(dbenv2);
clear_rpt(dbenv2);
resp.data = buffer;
resp.ulen = BUFLEN;
resp.flags = DB_DBT_USERMEM;
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 1, &resp, 0, 0)) == 0);
check_dbt_string(&resp, "ENV2");
await_done(dbenv2);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0], "I wish I were a fish");
ch->close(ch, 0);
/*
* Send an async message from env2 to env3, at which point env3 will
* reply by returning two async messages back to env2.
*/
printf("18. test async replies to (async) messages\n");
CuAssertTrue(ct, (ret = dbenv3->repmgr_msg_dispatch(dbenv3, msg_disp3, 0)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_msg_dispatch(dbenv2, msg_disp4, 0)) == 0);
CuAssertTrue(ct,
(ret = dbenv2->repmgr_site(dbenv2,
"localhost", ports[2], &dbsite, 0)) == 0);
CuAssertTrue(ct, (ret = dbsite->get_eid(dbsite, &eid)) == 0);
CuAssertTrue(ct, (ret = dbsite->close(dbsite)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, eid, &ch, 0)) == 0);
rpt = get_rpt(dbenv3);
clear_rpt(dbenv3);
((struct env_info *)dbenv2->app_private)->rpts = &rpts;
memset(&rpts, 0, sizeof(rpts));
mutex_init(&rpts.m);
CuAssertTrue(ct, (ret = ch->send_msg(ch, rdbts, 1, 0)) == 0);
await_done(dbenv3);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, rpt->dbt_count == 1);
check_dbt_string(&rpt->dbt[0], "I wish I were a fish");
CuAssertTrue(ct, await_condition(two_done, dbenv2, 10));
CuAssertTrue(ct, rpts.rpt[0].done);
CuAssertTrue(ct, rpts.rpt[0].dbt_count == 1);
check_dbt_string(&rpts.rpt[0].dbt[0], "roses may be pink");
CuAssertTrue(ct, rpts.rpt[1].done);
CuAssertTrue(ct, rpts.rpt[1].dbt_count == 1);
check_dbt_string(&rpts.rpt[1].dbt[0], "I think");
clear_rpt_int(&rpts.rpt[0]);
clear_rpt_int(&rpts.rpt[1]);
ch->close(ch, 0);
sleep(1); /* wait for "EOF on connection" msg before cleaning, below */
// This kluge disappears when GM fixes that err msg to become an event
printf("19. test illegal calls from the msg disp function\n");
clear_rpt(dbenv3);
CuAssertTrue(ct, (ret = dbenv3->repmgr_msg_dispatch(dbenv3, msg_disp5, 0)) == 0);
CuAssertTrue(ct, (ret = dbenv2->repmgr_channel(dbenv2, eid, &ch, 0)) == 0);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 1, &resp, 0, 0)) == 0);
await_done(dbenv3);
rpt = get_rpt(dbenv3);
CuAssertTrue(ct, rpt->ret == EINVAL);
CuAssertTrue(ct, rpt->msg_count == 3);
CuAssertTrue(ct, strncmp(rpt->msg[0], "BDB3660", strlen("BDB3660")) == 0);
CuAssertTrue(ct, strncmp(rpt->msg[1], "BDB3660", strlen("BDB3660")) == 0);
CuAssertTrue(ct, strncmp(rpt->msg[2], "BDB3660", strlen("BDB3660")) == 0);
ch->close(ch, 0);
free(buffer);
td(dbenv2);
td(dbenv3);
#else
printf("TestChannelFeature is not supported by the build.\n");
#endif /* HAVE_REPLICATION */
return (0);
}
static int
two_done(ctx)
void *ctx;
{
DB_ENV *dbenv = ctx;
struct reports *rpts = ((struct env_info *)dbenv->app_private)->rpts;
return (rpts->count == 2 && rpts->rpt[0].done && rpts->rpt[1].done);
}
/* return 1 ("true") for a match, 0 ("false") otherwise */
static int
check_dbt_string(dbt, s)
DBT *dbt;
const char *s;
{
if (dbt->size != strlen(s))
return (0);
if (dbt->size == 0)
return (1);
return (strcmp((char*)dbt->data, s) == 0);
}
static int
is_started(ctx)
void *ctx;
{
DB_ENV *dbenv = ctx;
DB_REP_STAT *st;
u_int32_t ans;
int ret;
if ((ret = dbenv->rep_stat(dbenv, &st, 0)) != 0) {
dbenv->err(dbenv, ret, "rep_stat");
return (0);
}
ans = st->st_startup_complete;
free(st);
return (ans);
}
static int
await_condition(pred, ctx, limit)
PRED pred;
void *ctx;
long limit;
{
#ifndef _WIN32
struct timeval t;
#endif
time_t tim;
tim = time(NULL) + limit;
while (time(NULL) < tim) {
if ((*pred)(ctx))
return (1);
// sleep 1/10th of a second at a time
// (maybe Windows can use select() too, if include Winsock2.h)
#ifdef _WIN32
Sleep(100);
#else
t.tv_sec = 0;
t.tv_usec = 100000;
select(0, NULL, NULL, NULL, &t);
#endif
}
return (0);
}
static void
notify(dbenv, event, unused)
DB_ENV *dbenv;
u_int32_t event;
void *unused;
{
struct channel_test_globals *g;
struct env_info *info;
if (event == DB_EVENT_PANIC) {
fprintf(stderr, "BDB panic");
abort();
} else if (event == DB_EVENT_REP_STARTUPDONE) {
info = dbenv->app_private;
g = info->g;
LOCK_MUTEX(&g->mtx);
info->startupdone = 1;
cond_wake(&g->cond);
UNLOCK_MUTEX(&g->mtx);
}
}
static void
msg_disp(dbenv, ch, request, nseg, flags)
DB_ENV *dbenv;
DB_CHANNEL *ch;
DBT *request;
u_int32_t nseg;
u_int32_t flags;
{
CuTest *ct;
struct report *rpt = get_rpt(dbenv);
DBT answer, mult[3];
char *p;
size_t sz;
u_int32_t i;
int ret;
ct = ((struct env_info *)dbenv->app_private)->g->test;
CuAssertTrue(ct, nseg < MAX_SEGS);
for (i = 0; i < nseg; i++) {
if ((sz = (rpt->dbt[rpt->dbt_count].size = request[i].size)) > 0) {
CuAssertTrue(ct, (rpt->dbt[rpt->dbt_count].data = malloc(sz)) != NULL);
memcpy(rpt->dbt[rpt->dbt_count].data,
request[i].data, sz);
} else
rpt->dbt[rpt->dbt_count].data = NULL;
rpt->dbt_count++;
}
ret = 0;
if (flags & DB_REPMGR_NEED_RESPONSE) {
if (nseg == 2) {
/* Try a multi-segment response. */
memset(&mult, 0, sizeof(mult));
p = "roses are red";
mult[0].data = p;
mult[0].size = (u_int32_t)strlen(p) + 1;
p = "violets are blue";
mult[1].data = p;
mult[1].size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &mult[0], 2, 0);
} else if (nseg == 1) {
// pretend to ``forget'' to respond
} else if (nseg == 4) {
// send a response of zero segments
ret = ch->send_msg(ch, &answer, 0, 0);
} else if (nseg == 5) {
// send a response with a segment of zero length
memset(&answer, 0, sizeof(answer));
answer.size = 0;
ret = ch->send_msg(ch, &answer, 1, 0);
// TODO: we still need to try this with the DB_MULTIPLE approach too
} else if (nseg == 6) {
// patience, ...
/* Try a multi-segment response. */
memset(&mult, 0, sizeof(mult));
p = "roses are red";
mult[0].data = p;
mult[0].size = (u_int32_t)strlen(p) + 1;
p = "violets are blue";
mult[1].size = 0;
mult[2].data = p;
mult[2].size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &mult[0], 3, 0);
} else {
memset(&answer, 0, sizeof(answer));
p = "this is the answer to the request";
answer.data = p;
answer.size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &answer, 1, 0);
}
}
rpt->ret = ret;
rpt->done = 1;
}
static void
msg_disp2(dbenv, ch, request, nseg, flags)
DB_ENV *dbenv;
DB_CHANNEL *ch;
DBT *request;
u_int32_t nseg;
u_int32_t flags;
{
CuTest *ct;
struct report *rpt = get_rpt(dbenv);
DBT answer;
const char *p;
char buf[100];
size_t sz;
u_int32_t i;
int ret;
ct = ((struct env_info *)dbenv->app_private)->g->test;
CuAssertTrue(ct, nseg < MAX_SEGS);
for (i = 0; i < nseg; i++) {
if ((sz = (rpt->dbt[rpt->dbt_count].size = request[i].size)) > 0) {
CuAssertTrue(ct, (rpt->dbt[rpt->dbt_count].data = malloc(sz)) != NULL);
memcpy(rpt->dbt[rpt->dbt_count].data,
request[i].data, sz);
} else
rpt->dbt[rpt->dbt_count].data = NULL;
rpt->dbt_count++;
}
if (flags & DB_REPMGR_NEED_RESPONSE) {
memset(&answer, 0, sizeof(answer));
dbenv->get_errpfx(dbenv, &p);
strncpy(buf, p, sizeof(buf));
answer.data = buf;
answer.size = (u_int32_t)strlen(p) + 1;
if (answer.size > sizeof(buf))
answer.size = sizeof(buf);
ret = ch->send_msg(ch, &answer, 1, 0);
}
rpt->ret = ret;
rpt->done = 1;
}
/* Test async replies to (async) messages. */
static void
msg_disp3(dbenv, ch, request, nseg, flags)
DB_ENV *dbenv;
DB_CHANNEL *ch;
DBT *request;
u_int32_t nseg;
u_int32_t flags;
{
CuTest *ct;
struct report *rpt = get_rpt(dbenv);
DBT answer;
char *p;
size_t sz;
u_int32_t i;
int ret;
ct = ((struct env_info *)dbenv->app_private)->g->test;
CuAssertTrue(ct, nseg < MAX_SEGS);
for (i = 0; i < nseg; i++) {
if ((sz = (rpt->dbt[rpt->dbt_count].size = request[i].size)) > 0) {
CuAssertTrue(ct, (rpt->dbt[rpt->dbt_count].data = malloc(sz)) != NULL);
memcpy(rpt->dbt[rpt->dbt_count].data,
request[i].data, sz);
} else
rpt->dbt[rpt->dbt_count].data = NULL;
rpt->dbt_count++;
}
ret = 0;
// TODO: test that multiple calls to send_msg are not allowed on a request.
CuAssertTrue(ct, !(flags & DB_REPMGR_NEED_RESPONSE));
memset(&answer, 0, sizeof(answer));
p = "roses may be pink";
answer.data = p;
answer.size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &answer, 1, 0);
if (ret == 0) {
p = "I think";
answer.data = p;
answer.size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &answer, 1, 0);
}
rpt->ret = ret;
rpt->done = 1;
}
static void
msg_disp4(dbenv, ch, request, nseg, flags)
DB_ENV *dbenv;
DB_CHANNEL *ch;
DBT *request;
u_int32_t nseg;
u_int32_t flags;
{
CuTest *ct;
struct reports *rpts = ((struct env_info *)dbenv->app_private)->rpts;
struct report *rpt;
size_t sz;
u_int32_t i;
ct = ((struct env_info *)dbenv->app_private)->g->test;
LOCK_MUTEX(&rpts->m);
rpt = &rpts->rpt[rpts->count++];
UNLOCK_MUTEX(&rpts->m);
CuAssertTrue(ct, !(flags & DB_REPMGR_NEED_RESPONSE));
CuAssertTrue(ct, nseg < MAX_SEGS);
for (i = 0; i < nseg; i++) {
if ((sz = (rpt->dbt[rpt->dbt_count].size = request[i].size)) > 0) {
CuAssertTrue(ct, (rpt->dbt[rpt->dbt_count].data = malloc(sz)) != NULL);
memcpy(rpt->dbt[rpt->dbt_count].data,
request[i].data, sz);
} else
rpt->dbt[rpt->dbt_count].data = NULL;
rpt->dbt_count++;
}
rpt->done = 1;
}
static void
msg_disp5(dbenv, ch, request, nseg, flags)
DB_ENV *dbenv;
DB_CHANNEL *ch;
DBT *request;
u_int32_t nseg;
u_int32_t flags;
{
struct report *rpt = get_rpt(dbenv);
DBT answer;
u_int8_t buf[100];
char *p;
int ret;
memset(&answer, 0, sizeof(answer));
answer.flags = DB_DBT_USERMEM;
answer.ulen = sizeof(buf);
answer.data = buf;
if ((ret = ch->set_timeout(ch, 45000000)) != EINVAL ||
(ret = ch->close(ch, 0)) != EINVAL)
rpt->ret = ret;
else
rpt->ret = ch->send_request(ch, request, nseg, &answer, 0, 0);
memset(&answer, 0, sizeof(answer));
p = "roses may be pink";
answer.data = p;
answer.size = (u_int32_t)strlen(p) + 1;
ret = ch->send_msg(ch, &answer, 1, 0);
rpt->done = 1;
}
static void
test_data_init(dbt, data)
DBT *dbt;
char *data;
{
memset(dbt, 0, sizeof(*dbt));
dbt->data = data;
dbt->size = (u_int32_t)strlen(data) + 1;
}
static void
test_zeroes(ch, dest, ct)
DB_CHANNEL *ch;
DB_ENV *dest; /* destination env handle */
CuTest *ct;
{
DBT resp;
DBT rdbts[6];
struct report *rpt;
void *pointer, *vp;
size_t sz;
int i, ret;
memset(&resp, 0, sizeof(resp));
resp.flags = DB_DBT_MALLOC;
#define DATA0 "Dear kindly judge, your honor"
#define DATA1 "my parents treat me rough"
#define DATA2 "with all their marijuana"
#define DATA3 "they won't give me a puff"
#define DATA4 "The didn't wanna have me, but somehow I was had"
#define DATA5 "Leapin' lizards, that's why I'm so bad!"
#undef ADD_TEST_DATA
#define ADD_TEST_DATA(x) do { test_data_init(&rdbts[i++], (x)); } while (0);
i = 0;
ADD_TEST_DATA(DATA0);
ADD_TEST_DATA(DATA1);
ADD_TEST_DATA(DATA2);
ADD_TEST_DATA(DATA3);
ADD_TEST_DATA(DATA4);
ADD_TEST_DATA(DATA5);
rpt = get_rpt(dest);
printf("6. send zero-segment request\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 0, &resp, 0, 0)) == 0);
CuAssertTrue(ct, rpt->dbt_count == 0);
CuAssertTrue(ct, rpt->msg_count == 0);
check_dbt_string(&resp, "this is the answer to the request");
if (resp.data)
free(resp.data);
printf("6.a) send request with a zero-length segment (now why would anyone want to do that?)\n");
clear_rpt(dest);
rdbts[1].size = 0;
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 3, &resp, 0, 0)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->dbt_count == 3);
check_dbt_string(&rpt->dbt[0], DATA0);
check_dbt_string(&rpt->dbt[1], "");
check_dbt_string(&rpt->dbt[2], DATA2);
CuAssertTrue(ct, rpt->msg_count == 0);
check_dbt_string(&resp, "this is the answer to the request");
if (resp.data)
free(resp.data);
i = 1; ADD_TEST_DATA(DATA1); /* restore perturbed test data */
printf("6.b) get a zero-length response\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 4, &resp, 0, 0)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->dbt_count == 4);
CuAssertTrue(ct, rpt->msg_count == 0);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, resp.size == 0);
if (resp.data)
free(resp.data);
printf("6.c) get a zero-length response (alternate version)\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 5, &resp, 0, 0)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->dbt_count == 5);
CuAssertTrue(ct, rpt->msg_count == 0);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, resp.size == 0);
if (resp.data)
free(resp.data);
printf("6.d) get a zero-length response (DB_MULTIPLE, zero segments)\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 4, &resp, 0, DB_MULTIPLE)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->msg_count == 0);
CuAssertTrue(ct, rpt->ret == 0);
DB_MULTIPLE_INIT(pointer, &resp);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, pointer == NULL);
if (resp.data)
free(resp.data);
printf("6.e) get a zero-length response (DB_MULTIPLE, a zero-length segment)\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 5, &resp, 0, DB_MULTIPLE)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->msg_count == 0);
CuAssertTrue(ct, rpt->ret == 0);
DB_MULTIPLE_INIT(pointer, &resp);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, sz == 0);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, pointer == NULL);
if (resp.data)
free(resp.data);
printf("6.f) get a zero-length response (DB_MULTIPLE, a zero-length segment in the middle)\n");
clear_rpt(dest);
CuAssertTrue(ct, (ret = ch->send_request(ch, rdbts, 6, &resp, 0, DB_MULTIPLE)) == 0);
await_done(dest);
CuAssertTrue(ct, rpt->msg_count == 0);
CuAssertTrue(ct, rpt->ret == 0);
DB_MULTIPLE_INIT(pointer, &resp);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, rpt->ret == 0);
CuAssertTrue(ct, strcmp((char*)vp, "roses are red") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, sz == 0);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, strcmp((char*)vp, "violets are blue") == 0);
CuAssertTrue(ct, sz == strlen((char*)vp) + 1);
DB_MULTIPLE_NEXT(pointer, &resp, vp, sz);
CuAssertTrue(ct, pointer == NULL);
if (resp.data)
free(resp.data);
}
static int get_avail_ports(ports, count)
u_int *ports;
int count;
{
/* This function is used only when replication is supported. */
#ifdef HAVE_REPLICATION
u_int base, port, upper, curport;
int ret, t_ret, incr, i;
char buf[20], *rbuf, *str;
socket_t s;
ADDRINFO *orig_ai, *ai;
#ifdef _WIN32
#define in_port_t u_short
#ifndef EADDRINUSE
#define EADDRINUSE WSAEADDRINUSE
#endif
WSADATA wsaData;
if ((ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {
printf("WSAStartup failed with error: %d\n", ret);
return (ret);
}
#endif
base = 30100;
upper = 65535;
/*
* It is very convenient to have a very simple mapping between port
* numbers and sites. So usually, we search a sequence of ports
* starting from (10 * N + 1). To avoid redundant check on a port,
* we set the incr to be times of 10 and just bigger or equal to count.
*/
incr = 10 * ((count + 9) / 10);
/*
* The format for BDBPORTRANGE should be base:upper,
* either of base or upper can be empty, and if empty,
* we will use the default value for it.
* If no colon, the whole is considered to be base.
*/
rbuf = buf;
if ((ret = __os_getenv(NULL, "BDBPORTRANGE", &rbuf, sizeof(buf))) != 0)
goto end;
if (rbuf != NULL && rbuf[0] != '\0') {
if ((str = strsep(&rbuf, ":")) != NULL && str[0] != '\0')
base = (u_int)atoi(str);
if (rbuf != NULL && rbuf[0] != '\0')
upper = (u_int)atoi(rbuf);
}
for (port = base + 1; (port + incr) <= upper; port += incr) {
curport = port;
i = incr;
while (i-- > 0) {
if ((ret = __repmgr_getaddr(NULL, "localhost", curport,
AI_PASSIVE, &orig_ai)) != 0)
goto end;
for (ai = orig_ai; ai != NULL; ai = ai->ai_next) {
if ((s = socket(ai->ai_family, ai->ai_socktype,
ai->ai_protocol)) == INVALID_SOCKET)
continue;
if (bind(s, ai->ai_addr,
(socklen_t)ai->ai_addrlen) != 0) {
ret = net_errno;
(void)closesocket(s);
s = INVALID_SOCKET;
continue;
}
ret = 0;
goto clean;
}
if (ret == 0)
ret = net_errno;
clean:
if (s != INVALID_SOCKET)
(void)closesocket(s);
__os_freeaddrinfo(NULL, orig_ai);
if (ret != 0 && ret != EADDRINUSE)
goto end;
else if (ret != 0)
break;
curport++;
}
/* We've found the port sequence now */
if (ret == 0) {
for (i = 0; i < count; i++)
ports[i] = port + i;
break;
}
}
end:
#ifdef _WIN32
if (WSACleanup() == SOCKET_ERROR) {
t_ret = net_errno;
printf("WSACleanup failed with error: %d\n", t_ret);
if (ret == 0)
ret = t_ret;
}
#endif
return (ret);
#else
return (0);
#endif /* HAVE_REPLICATION */
}
|