summaryrefslogtreecommitdiffstats
path: root/test/mirror_vfd.c
blob: f7f88deb1a9de5c03eb64f620eba8b6008655e4e (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
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
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose: Test the Mirror VFD functionality.
 */

/* WARNING: The use of realpath() is probably system-dependent, as are
 * other things here such as the socket calls.
 * Notable to realpath() in particular is the use of "PATH_MAX", which
 * apparently has some major potential issues if paths are abused.
 * http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
 * so BE CAREFUL about the paths we throw around?
 */

#include "h5test.h"
#include "cache_common.h"
#include "genall5.h"

#ifdef H5_HAVE_MIRROR_VFD

#include "H5FDmirror_priv.h" /* Private header for the mirror VFD */

/* For future consideration, IP address and port number might be
 * environment variables?
 */
#define SERVER_IP_ADDRESS "127.0.0.1"

/* Primary listening port on server. */
#define SERVER_HANDSHAKE_PORT 3000

#define DATABUFFER_SIZE 128
#define DSET_NAME_LEN   16

/* Parameters for the "large chunked dataset" writing */
#define MAX_DSET_COUNT 255
#define DSET_DIM       32
#define CHUNK_DIM      8

#define CONCURRENT_COUNT 3 /* Number of files in concurrent test */

/* Macro: LOGPRINT()
 * Prints logging and debugging messages to the output stream based
 * on the level of verbosity.
 *     0 : no logging
 *     1 : errors only
 *     2 : details
 *     3 : all
 */
#define DEFAULT_VERBOSITY 1
static unsigned int g_verbosity = DEFAULT_VERBOSITY;

/* Macro for selective debug printing / logging */
#define LOGPRINT(lvl, ...)                                                                                   \
    do {                                                                                                     \
        if ((lvl) <= g_verbosity) {                                                                          \
            fprintf(g_log_stream, __VA_ARGS__);                                                              \
            fflush(g_log_stream);                                                                            \
        }                                                                                                    \
    } while (0)

#define MIRROR_RW_DIR "mirror_rw/"
#define MIRROR_WO_DIR "mirror_wo/"

/* String buffer for error messages */
#define MIRR_MESG_SIZE 128
static char mesg[MIRR_MESG_SIZE + 1];

/* ----------------------------------------------------------------------------
 * Structure:   struct mt_opts
 *
 * Purpose:     Convenience structure to hold options as parsed from the
 *              command line.
 *
 * `portno` (int)
 *      Port number, as received from arguments.
 *
 * `ip` (char *)
 *      IP address string as received from arguments.
 *
 * ----------------------------------------------------------------------------
 */
struct mt_opts {
    int  portno;
    char ip[H5FD_MIRROR_MAX_IP_LEN + 1];
};

/* Convenience structure for passing file names via helper functions.
 */
struct mirrortest_filenames {
    char rw[H5FD_SPLITTER_PATH_MAX + 1];
    char wo[H5FD_SPLITTER_PATH_MAX + 1];
    char log[H5FD_SPLITTER_PATH_MAX + 1];
};

static FILE *g_log_stream = NULL; /* initialized at runtime */

static herr_t _verify_datasets(unsigned min_dset, unsigned max_dset, hid_t *filespace_id, hid_t *dataset_id,
                               hid_t memspace_id);

static herr_t _create_chunking_ids(hid_t file_id, unsigned min_dset, unsigned max_dset, hsize_t *chunk_dims,
                                   hsize_t *dset_dims, hid_t *dataspace_ids, hid_t *filespace_ids,
                                   hid_t *dataset_ids, hid_t *memspace_id);

static herr_t _close_chunking_ids(unsigned min_dset, unsigned max_dset, hid_t *dataspace_ids,
                                  hid_t *filespace_ids, hid_t *dataset_ids, hid_t *memspace_id);

static herr_t populate_filepath(const char *dirname, const char *_basename, hid_t fapl_id, char *path_out,
                                bool h5suffix);

static hid_t create_mirroring_split_fapl(const char *_basename, struct mirrortest_filenames *names,
                                         const struct mt_opts *opts);

/* ----------------------------------------------------------------------------
 * Function:   populate_filepath
 *
 * Purpose:    Given a directory name and a base name, concatenate the two and
 *             run h5fixname() to get the "actual" path to the intended target.
 *             `h5suffix' should be false to keep the base name unaltered;
 *             true will append the '.h5' h5suffix to the basename...
 *             false -> h5fixname_no_suffix(), true -> h5fixname()
 *             <h5fixname_prefix> / <dirname> / <basename> <h5prefix?>
 *
 * ----------------------------------------------------------------------------
 */
static herr_t
populate_filepath(const char *dirname, const char *basename, hid_t fapl_id, char *path_out, bool h5suffix)
{
    char *path = NULL;

    if ((basename == NULL) || (*basename == 0) || (dirname == NULL) || (*dirname == 0) || (path_out == NULL))
        TEST_ERROR;

    if (NULL == (path = calloc(H5FD_SPLITTER_PATH_MAX, sizeof(char))))
        TEST_ERROR;

    if (snprintf(path, H5FD_SPLITTER_PATH_MAX, "%s%s%s", dirname,
                 (dirname[strlen(dirname)] == '/') ? "" : "/", /* slash iff needed */
                 basename) > H5FD_SPLITTER_PATH_MAX)
        TEST_ERROR;

    if (h5suffix == true) {
        if (h5_fixname(path, fapl_id, path_out, H5FD_SPLITTER_PATH_MAX) == NULL)
            TEST_ERROR;
    }
    else {
        if (h5_fixname_no_suffix(path, fapl_id, path_out, H5FD_SPLITTER_PATH_MAX) == NULL)
            TEST_ERROR;
    }

    free(path);

    return SUCCEED;

error:
    free(path);
    return FAIL;
} /* end populate_filepath() */

/* ---------------------------------------------------------------------------
 * Function:    build_paths
 *
 * Purpose:     Convenience function to create the three file paths used in
 *              most mirror tests.
 *
 * Return:      SUCCEED/FAIL
 * ---------------------------------------------------------------------------
 */
static herr_t
build_paths(const char *basename, H5FD_splitter_vfd_config_t *splitter_config,
            struct mirrortest_filenames *names)
{
    char *baselogname = NULL;

    if (NULL == (baselogname = calloc(H5FD_SPLITTER_PATH_MAX, sizeof(char))))
        TEST_ERROR;

    if (populate_filepath(MIRROR_RW_DIR, basename, splitter_config->rw_fapl_id, names->rw, true) < 0)
        TEST_ERROR;

    if (populate_filepath(MIRROR_WO_DIR, basename, splitter_config->wo_fapl_id, names->wo, true) < 0)
        TEST_ERROR;

    if (basename == NULL || *basename == 0)
        TEST_ERROR;
    if (snprintf(baselogname, H5FD_SPLITTER_PATH_MAX, "%s_err.log", basename) > H5FD_SPLITTER_PATH_MAX)
        TEST_ERROR;

    if (populate_filepath(MIRROR_WO_DIR, baselogname, splitter_config->wo_fapl_id, names->log, false) < 0)
        TEST_ERROR;

    free(baselogname);

    return SUCCEED;

error:
    free(baselogname);
    return FAIL;
} /* end build_paths() */

/* ---------------------------------------------------------------------------
 * Function:    test_fapl_configuration
 *
 * Purpose:     Test FAPL configuration and examination.
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_fapl_configuration(void)
{
    hid_t              fapl_id     = H5I_INVALID_HID;
    H5FD_mirror_fapl_t mirror_conf = {
        H5FD_MIRROR_FAPL_MAGIC,          /* magic */
        H5FD_MIRROR_CURR_FAPL_T_VERSION, /* version */
        SERVER_HANDSHAKE_PORT,           /* handhake_port */
        SERVER_IP_ADDRESS,               /* remote_ip "IP address" */
    };
    H5FD_mirror_fapl_t fa_out = {0, 0, 0, ""};

    TESTING("Mirror fapl configuration (set/get)");

    if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;

    if (H5Pset_fapl_mirror(fapl_id, &mirror_conf) < 0)
        TEST_ERROR;

    if (H5Pget_fapl_mirror(fapl_id, &fa_out) < 0)
        TEST_ERROR;

    if (H5FD_MIRROR_FAPL_MAGIC != fa_out.magic)
        TEST_ERROR;

    if (H5FD_MIRROR_CURR_FAPL_T_VERSION != fa_out.version)
        TEST_ERROR;

    if (SERVER_HANDSHAKE_PORT != fa_out.handshake_port)
        TEST_ERROR;

    if (strncmp(SERVER_IP_ADDRESS, (const char *)fa_out.remote_ip, H5FD_MIRROR_MAX_IP_LEN))
        TEST_ERROR;

    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
    }
    H5E_END_TRY
    return -1;
} /* end test_fapl_configuration() */

#define PRINT_BUFFER_DIFF(act, exp, len)                                                                     \
    do {                                                                                                     \
        size_t _x = 0;                                                                                       \
        while ((act)[_x] == (exp)[_x]) {                                                                     \
            _x++;                                                                                            \
        }                                                                                                    \
        if (_x != (len)) {                                                                                   \
            size_t _y = 0;                                                                                   \
            printf("First bytes differ at %zu\n", _x);                                                       \
            printf("exp  ");                                                                                 \
            for (_y = _x; _y < (len); _y++) {                                                                \
                printf("%02X", (unsigned char)(exp)[_y]);                                                    \
            }                                                                                                \
            printf("\nact  ");                                                                               \
            for (_y = _x; _y < (len); _y++) {                                                                \
                printf("%02X", (unsigned char)(act)[_y]);                                                    \
            }                                                                                                \
            printf("\n");                                                                                    \
        }                                                                                                    \
    } while (0); /* end PRINT_BUFFER_DIFF */

/*******************************************/
/* Encode/decode tests for various C types */
/*******************************************/

/* Test uint8_t encode/decode */
static int
test_encdec_uint8_t(void)
{
    unsigned char buf[8];
    unsigned char expected[8];
    const uint8_t v   = 200;
    unsigned char out = 0;

    TESTING("Mirror encode/decode of uint8_t data");

    /* Start of buffer uint8_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[0] = 200;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint8(buf, v) != 1)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint8(&out, buf) != 1)
        TEST_ERROR;
    if (v != out)
        TEST_ERROR;

    /* Middle of buffer uint8_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[3] = v;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint8((buf + 3), v) != 1)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint8(&out, (buf + 3)) != 1)
        TEST_ERROR;
    if (v != out)
        TEST_ERROR;

    /* End of buffer uint8_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[7] = v;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint8((buf + 7), v) != 1)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint8(&out, (buf + 7)) != 1)
        TEST_ERROR;
    if (v != out)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test uint16_t encode/decode */
static int
test_encdec_uint16_t(void)
{
    unsigned char  buf[8];
    unsigned char  expected[8];
    const uint16_t v   = 0x8F02;
    uint16_t       out = 0;

    TESTING("Mirror encode/decode of uint16_t data");

    /* Start of buffer uint16_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[0] = 0x8F;
    expected[1] = 0x02;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint16(buf, v) != 2)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint16(&out, buf) != 2)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    /* Middle of buffer uint16_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[3] = 0x8F;
    expected[4] = 0x02;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint16((buf + 3), v) != 2)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint16(&out, (buf + 3)) != 2)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    /* slice */
    if (H5FD__mirror_xmit_decode_uint16(&out, (buf + 4)) != 2)
        TEST_ERROR;
    if (out != 0x0200)
        TEST_ERROR;

    /* End of buffer uint16_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[6] = 0x8F;
    expected[7] = 0x02;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint16((buf + 6), v) != 2)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint16(&out, (buf + 6)) != 2)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test uint32_t encode/decode */
static int
test_encdec_uint32_t(void)
{
    unsigned char  buf[8];
    unsigned char  expected[8];
    const uint32_t v   = 0x8F020048;
    uint32_t       out = 0;

    TESTING("Mirror encode/decode of uint32_t data");

    /* Start of buffer uint32_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[0] = 0x8F;
    expected[1] = 0x02;
    expected[2] = 0x00;
    expected[3] = 0x48;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint32(buf, v) != 4)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint32(&out, buf) != 4)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    /* Middle of buffer uint32_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[3] = 0x8F;
    expected[4] = 0x02;
    expected[5] = 0x00;
    expected[6] = 0x48;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint32((buf + 3), v) != 4)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint32(&out, (buf + 3)) != 4)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;
    /* slice */
    if (H5FD__mirror_xmit_decode_uint32(&out, (buf + 4)) != 4)
        TEST_ERROR;
    if (out != 0x02004800)
        TEST_ERROR;

    /* End of buffer uint32_t */
    memset(buf, 0, 8);
    memset(expected, 0, 8);
    expected[4] = 0x8F;
    expected[5] = 0x02;
    expected[6] = 0x00;
    expected[7] = 0x48;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint32((buf + 4), v) != 4)
        TEST_ERROR;
    if (memcmp(buf, expected, 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 8);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint32(&out, (buf + 4)) != 4)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test uint64_t encode/decode */
static int
test_encdec_uint64_t(void)
{
    unsigned char  buf[16];
    unsigned char  expected[16];
    const uint64_t v   = 0x90DCBE17939CE4BB;
    uint64_t       out = 0;

    TESTING("Mirror encode/decode of uint64_t data");

    /* Start of buffer uint64_t */
    memset(buf, 0, 16);
    memset(expected, 0, 16);
    expected[0] = 0x90;
    expected[1] = 0xDC;
    expected[2] = 0xBE;
    expected[3] = 0x17;
    expected[4] = 0x93;
    expected[5] = 0x9C;
    expected[6] = 0xE4;
    expected[7] = 0xBB;
    out         = 0;
    if (H5FD__mirror_xmit_encode_uint64(buf, v) != 8)
        TEST_ERROR;
    if (memcmp(buf, expected, 16) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 16);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint64(&out, buf) != 8)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    /* Middle of buffer uint64_t */
    memset(buf, 0, 16);
    memset(expected, 0, 16);
    expected[3]  = 0x90;
    expected[4]  = 0xDC;
    expected[5]  = 0xBE;
    expected[6]  = 0x17;
    expected[7]  = 0x93;
    expected[8]  = 0x9C;
    expected[9]  = 0xE4;
    expected[10] = 0xBB;
    out          = 0;
    if (H5FD__mirror_xmit_encode_uint64((buf + 3), v) != 8)
        TEST_ERROR;
    if (memcmp(buf, expected, 16) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 16);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint64(&out, (buf + 3)) != 8)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;
    /* Slice */
    if (H5FD__mirror_xmit_decode_uint64(&out, (buf + 6)) != 8)
        TEST_ERROR;
    if (out != 0x17939CE4BB000000)
        TEST_ERROR;

    /* End of buffer uint64_t */
    memset(buf, 0, 16);
    memset(expected, 0, 16);
    expected[8]  = 0x90;
    expected[9]  = 0xDC;
    expected[10] = 0xBE;
    expected[11] = 0x17;
    expected[12] = 0x93;
    expected[13] = 0x9C;
    expected[14] = 0xE4;
    expected[15] = 0xBB;
    out          = 0;
    if (H5FD__mirror_xmit_encode_uint64((buf + 8), v) != 8)
        TEST_ERROR;
    if (memcmp(buf, expected, 16) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, 16);
        TEST_ERROR;
    }
    if (H5FD__mirror_xmit_decode_uint64(&out, (buf + 8)) != 8)
        TEST_ERROR;
    if (out != v)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/*****************************/
/* Other Encode/decode tests */
/*****************************/

/* Test xmit header structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 */
static int
test_encdec_header(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char      buf[H5FD_MIRROR_XMIT_HEADER_SIZE + 8];
    unsigned char      expected[H5FD_MIRROR_XMIT_HEADER_SIZE + 8];
    H5FD_mirror_xmit_t xmit_out;
    size_t             i = 0;

    TESTING("Mirror encode/decode of xmit header");

    /* Sanity check */
    if (14 != H5FD_MIRROR_XMIT_HEADER_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_HEADER_SIZE + 8);
    for (i = 0; i < H5FD_MIRROR_XMIT_HEADER_SIZE; i++) {
        expected[i + 2] = (unsigned char)i;
    }

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_HEADER_SIZE + 8);
    if (H5FD_mirror_xmit_encode_header((buf + 2), &xmit_mock) != H5FD_MIRROR_XMIT_HEADER_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_HEADER_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_HEADER_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_header(&xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_HEADER_SIZE)
        TEST_ERROR;
    if (xmit_out.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out.op != xmit_mock.op)
        TEST_ERROR;

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_header(&xmit_out, buf) != H5FD_MIRROR_XMIT_HEADER_SIZE)
        TEST_ERROR;
    if (xmit_out.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out.version != 0x02)
        TEST_ERROR;
    if (xmit_out.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out.op != 0x0B)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test xmit set-eoa structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 */
static int
test_encdec_set_eoa(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char          buf[H5FD_MIRROR_XMIT_EOA_SIZE + 8];
    unsigned char          expected[H5FD_MIRROR_XMIT_EOA_SIZE + 8];
    H5FD_mirror_xmit_eoa_t xmit_in;
    H5FD_mirror_xmit_eoa_t xmit_out;
    size_t                 i = 0;

    TESTING("Mirror encode/decode of xmit set-eoa");

    /* Sanity check */
    if ((14 + 9) != H5FD_MIRROR_XMIT_EOA_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");
    if (xmit_mock.op != 0x0D)
        FAIL_PUTS_ERROR("shared header structure is not in expected state");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_EOA_SIZE + 8);
    for (i = 0; i < H5FD_MIRROR_XMIT_EOA_SIZE; i++)
        expected[i + 2] = (unsigned char)i;

    /* Set xmit_in */
    xmit_in.pub      = xmit_mock; /* shared/common */
    xmit_in.type     = 0x0E;
    xmit_in.eoa_addr = 0x0F10111213141516;

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_EOA_SIZE + 8);
    if (H5FD_mirror_xmit_encode_set_eoa((buf + 2), &xmit_in) != H5FD_MIRROR_XMIT_EOA_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_EOA_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_EOA_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_set_eoa(&xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_EOA_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out.pub.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out.pub.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out.pub.op != xmit_mock.op)
        TEST_ERROR;
    if (xmit_out.type != 0x0E)
        TEST_ERROR;
    if (xmit_out.eoa_addr != 0x0F10111213141516)
        TEST_ERROR;

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_set_eoa(&xmit_out, buf) != H5FD_MIRROR_XMIT_EOA_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out.pub.version != 0x02)
        TEST_ERROR;
    if (xmit_out.pub.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out.pub.op != 0x0B)
        TEST_ERROR;
    if (xmit_out.type != 0x0C)
        TEST_ERROR;
    if (xmit_out.eoa_addr != 0x0D0E0F1011121314)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test xmit lock structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 */
static int
test_encdec_lock(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char           buf[H5FD_MIRROR_XMIT_LOCK_SIZE + 8];
    unsigned char           expected[H5FD_MIRROR_XMIT_LOCK_SIZE + 8];
    H5FD_mirror_xmit_lock_t xmit_in;
    H5FD_mirror_xmit_lock_t xmit_out;
    size_t                  i = 0;

    TESTING("Mirror encode/decode of xmit lock");

    /* Sanity check */
    if ((14 + 8) != H5FD_MIRROR_XMIT_LOCK_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");
    if (xmit_mock.op != 0x0D)
        FAIL_PUTS_ERROR("shared header structure is not in expected state");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_LOCK_SIZE + 8);
    for (i = 0; i < H5FD_MIRROR_XMIT_LOCK_SIZE; i++)
        expected[i + 2] = (unsigned char)i;

    /* Set xmit_in */
    xmit_in.pub = xmit_mock; /* shared/common */
    xmit_in.rw  = 0x0E0F101112131415;

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_LOCK_SIZE + 8);
    if (H5FD_mirror_xmit_encode_lock((buf + 2), &xmit_in) != H5FD_MIRROR_XMIT_LOCK_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_LOCK_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_LOCK_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_lock(&xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_LOCK_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out.pub.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out.pub.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out.pub.op != xmit_mock.op)
        TEST_ERROR;
    if (xmit_out.rw != 0x0E0F101112131415)
        TEST_ERROR;

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_lock(&xmit_out, buf) != H5FD_MIRROR_XMIT_LOCK_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out.pub.version != 0x02)
        TEST_ERROR;
    if (xmit_out.pub.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out.pub.op != 0x0B)
        TEST_ERROR;
    if (xmit_out.rw != 0x0C0D0E0F10111213)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* Test xmit open structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 *
 * Verifies that the first zero character in the filepath will end the
 * string, with all following bytes in the encoded buffer being zeroed.
 */
static int
test_encdec_open(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char           *buf      = NULL;
    unsigned char           *expected = NULL;
    H5FD_mirror_xmit_open_t *xmit_in  = NULL;
    H5FD_mirror_xmit_open_t *xmit_out = NULL;

    TESTING("Mirror encode/decode of xmit open");

    /* Sanity check */
    if ((14 + 20 + 4097) != H5FD_MIRROR_XMIT_OPEN_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");
    if (xmit_mock.op != 0x0D)
        FAIL_PUTS_ERROR("shared header structure is not in expected state");

    /* Allocate memory */
    if (NULL == (buf = malloc((H5FD_MIRROR_XMIT_OPEN_SIZE + 8) * sizeof(unsigned char))))
        FAIL_PUTS_ERROR("Unable to allocate memory for buf");
    if (NULL == (expected = malloc((H5FD_MIRROR_XMIT_OPEN_SIZE + 8) * sizeof(unsigned char))))
        FAIL_PUTS_ERROR("Unable to allocate memory for expected");
    if (NULL == (xmit_in = malloc(sizeof(H5FD_mirror_xmit_open_t))))
        FAIL_PUTS_ERROR("Unable to allocate memory for xmit_in");
    if (NULL == (xmit_out = malloc(sizeof(H5FD_mirror_xmit_open_t))))
        FAIL_PUTS_ERROR("Unable to allocate memory for xmit_out");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_OPEN_SIZE + 8);
    for (size_t i = 0; i < H5FD_MIRROR_XMIT_OPEN_SIZE; i++) {
        /* 0x100 is "zero" in a byte, so encode will treat it as a NULL-
         * terminator in the filepath string. Expect all zeroes following.
         */
        expected[i + 2] = (i > 0xFF) ? 0 : (unsigned char)i;
    }

    /* Set xmit_in */
    xmit_in->pub         = xmit_mock; /* shared/common */
    xmit_in->flags       = 0x0E0F1011;
    xmit_in->maxaddr     = 0x1213141516171819;
    xmit_in->size_t_blob = 0x1A1B1C1D1E1F2021;
    for (size_t i = 0x22; i < H5FD_MIRROR_XMIT_FILEPATH_MAX + 0x22; i++) {
        /* Non-zero values repeat after 0x100, but will not be encoded */
        xmit_in->filename[i - 0x22] = (char)(i % 0x100);
    }
    xmit_in->filename[H5FD_MIRROR_XMIT_FILEPATH_MAX - 1] = 0;

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_OPEN_SIZE + 8);
    if (H5FD_mirror_xmit_encode_open((buf + 2), xmit_in) != H5FD_MIRROR_XMIT_OPEN_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_OPEN_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_OPEN_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_open(xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_OPEN_SIZE)
        TEST_ERROR;
    if (xmit_out->pub.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out->pub.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out->pub.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out->pub.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out->pub.op != xmit_mock.op)
        TEST_ERROR;
    if (xmit_out->flags != xmit_in->flags)
        TEST_ERROR;
    if (xmit_out->maxaddr != xmit_in->maxaddr)
        TEST_ERROR;
    if (xmit_out->size_t_blob != xmit_in->size_t_blob)
        TEST_ERROR;
    if (strncmp(xmit_out->filename, xmit_in->filename, H5FD_MIRROR_XMIT_FILEPATH_MAX) != 0) {
        PRINT_BUFFER_DIFF(xmit_out->filename, xmit_in->filename, H5FD_MIRROR_XMIT_FILEPATH_MAX);
        TEST_ERROR;
    }

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_open(xmit_out, buf) != H5FD_MIRROR_XMIT_OPEN_SIZE)
        TEST_ERROR;
    if (xmit_out->pub.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out->pub.version != 0x02)
        TEST_ERROR;
    if (xmit_out->pub.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out->pub.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out->pub.op != 0x0B)
        TEST_ERROR;
    if (xmit_out->flags != 0x0C0D0E0F)
        TEST_ERROR;
    if (xmit_out->maxaddr != 0x1011121314151617)
        TEST_ERROR;
    if (xmit_out->size_t_blob != 0x18191A1B1C1D1E1F)
        TEST_ERROR;
    /* Update expected "filepath" in structure */
    for (size_t i = 0x20; i < H5FD_MIRROR_XMIT_FILEPATH_MAX + 0x20; i++)
        xmit_in->filename[i - 0x20] = (i > 0xFF) ? 0 : (char)i;
    if (strncmp(xmit_out->filename, xmit_in->filename, H5FD_MIRROR_XMIT_FILEPATH_MAX) != 0) {
        PRINT_BUFFER_DIFF(xmit_out->filename, xmit_in->filename, H5FD_MIRROR_XMIT_FILEPATH_MAX);
        TEST_ERROR;
    }

    free(buf);
    free(expected);
    free(xmit_in);
    free(xmit_out);

    PASSED();
    return 0;

error:
    free(buf);
    free(expected);
    free(xmit_in);
    free(xmit_out);

    return -1;
}

/* Test xmit reply structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 *
 * Verifies that the first zero character in the filepath will end the
 * string, with all following bytes in the encoded buffer being zeroed.
 */
static int
test_encdec_reply(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char            buf[H5FD_MIRROR_XMIT_REPLY_SIZE + 8];
    unsigned char            expected[H5FD_MIRROR_XMIT_REPLY_SIZE + 8];
    H5FD_mirror_xmit_reply_t xmit_in;
    H5FD_mirror_xmit_reply_t xmit_out;
    size_t                   i = 0;

    TESTING("Mirror encode/decode of xmit reply");

    /* Sanity check */
    if ((14 + 4 + 256) != H5FD_MIRROR_XMIT_REPLY_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");
    if (xmit_mock.op != 0x0D)
        FAIL_PUTS_ERROR("shared header structure is not in expected state");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_REPLY_SIZE + 8);
    for (i = 0; i < H5FD_MIRROR_XMIT_REPLY_SIZE; i++) {
        /* 0x100 is "zero" in a byte, so encode will treat it as a NULL-
         * terminator in the filepath string. Expect all zeroes following.
         */
        expected[i + 2] = (i > 0xFF) ? 0 : (unsigned char)i;
    }

    /* Set xmit_in */
    xmit_in.pub    = xmit_mock; /* shared/common */
    xmit_in.status = 0x0E0F1011;
    for (i = 0x12; i < H5FD_MIRROR_STATUS_MESSAGE_MAX + 0x12; i++) {
        /* Non-zero values repeat after 0x100, but will not be encoded */
        xmit_in.message[i - 0x12] = (char)(i % 0x100);
    }
    xmit_in.message[H5FD_MIRROR_STATUS_MESSAGE_MAX - 1] = 0;

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_REPLY_SIZE + 8);
    if (H5FD_mirror_xmit_encode_reply((buf + 2), &xmit_in) != H5FD_MIRROR_XMIT_REPLY_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_REPLY_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_REPLY_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_reply(&xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_REPLY_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out.pub.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out.pub.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out.pub.op != xmit_mock.op)
        TEST_ERROR;
    if (xmit_out.status != xmit_in.status)
        TEST_ERROR;
    if (strncmp(xmit_out.message, xmit_in.message, H5FD_MIRROR_STATUS_MESSAGE_MAX) != 0) {
        PRINT_BUFFER_DIFF(xmit_out.message, xmit_in.message, H5FD_MIRROR_STATUS_MESSAGE_MAX);
        TEST_ERROR;
    }

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_reply(&xmit_out, buf) != H5FD_MIRROR_XMIT_REPLY_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out.pub.version != 0x02)
        TEST_ERROR;
    if (xmit_out.pub.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out.pub.op != 0x0B)
        TEST_ERROR;
    if (xmit_out.status != 0x0C0D0E0F)
        TEST_ERROR;
    /* Update expected "message" in structure */
    for (i = 0x10; i < H5FD_MIRROR_STATUS_MESSAGE_MAX + 0x10; i++)
        xmit_in.message[i - 0x10] = (i > 0xFF) ? 0 : (char)i;
    if (strncmp(xmit_out.message, xmit_in.message, H5FD_MIRROR_STATUS_MESSAGE_MAX) != 0) {
        PRINT_BUFFER_DIFF(xmit_out.message, xmit_in.message, H5FD_MIRROR_STATUS_MESSAGE_MAX);
        TEST_ERROR;
    }

    PASSED();
    return 0;

error:
    return -1;
}

/* Test xmit write structure encode/decode
 * Write bogus but easily verifiable data to inside a buffer, and compare.
 * Then decode the buffer and compare the structure contents.
 * Then repeat from a different offset in the buffer and compare.
 */
static int
test_encdec_write(H5FD_mirror_xmit_t xmit_mock)
{
    unsigned char            buf[H5FD_MIRROR_XMIT_WRITE_SIZE + 8];
    unsigned char            expected[H5FD_MIRROR_XMIT_WRITE_SIZE + 8];
    H5FD_mirror_xmit_write_t xmit_in;
    H5FD_mirror_xmit_write_t xmit_out;
    size_t                   i = 0;

    TESTING("Mirror encode/decode of xmit write");

    /* Sanity check */
    if ((14 + 17) != H5FD_MIRROR_XMIT_WRITE_SIZE)
        FAIL_PUTS_ERROR("Header size definition does not match test\n");
    if (xmit_mock.op != 0x0D)
        FAIL_PUTS_ERROR("shared header structure is not in expected state");

    /* Populate the expected buffer; expect end padding of 0xFF */
    memset(expected, 0xFF, H5FD_MIRROR_XMIT_WRITE_SIZE + 8);
    for (i = 0; i < H5FD_MIRROR_XMIT_WRITE_SIZE; i++)
        expected[i + 2] = (unsigned char)i;

    /* Set xmit_in */
    xmit_in.pub    = xmit_mock; /* shared/common */
    xmit_in.type   = 0x0E;
    xmit_in.offset = 0x0F10111213141516;
    xmit_in.size   = 0x1718191A1B1C1D1E;

    /* Encode, and compare buffer contents
     * Initial buffer is filled with 0xFF to match expected padding
     */
    memset(buf, 0xFF, H5FD_MIRROR_XMIT_WRITE_SIZE + 8);
    if (H5FD_mirror_xmit_encode_write((buf + 2), &xmit_in) != H5FD_MIRROR_XMIT_WRITE_SIZE)
        TEST_ERROR;
    if (memcmp(buf, expected, H5FD_MIRROR_XMIT_WRITE_SIZE + 8) != 0) {
        PRINT_BUFFER_DIFF(buf, expected, H5FD_MIRROR_XMIT_WRITE_SIZE + 8);
        TEST_ERROR;
    }

    /* Decode from buffer */
    if (H5FD_mirror_xmit_decode_write(&xmit_out, (buf + 2)) != H5FD_MIRROR_XMIT_WRITE_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != xmit_mock.magic)
        TEST_ERROR;
    if (xmit_out.pub.version != xmit_mock.version)
        TEST_ERROR;
    if (xmit_out.pub.session_token != xmit_mock.session_token)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != xmit_mock.xmit_count)
        TEST_ERROR;
    if (xmit_out.pub.op != xmit_mock.op)
        TEST_ERROR;
    if (xmit_out.type != 0x0E)
        TEST_ERROR;
    if (xmit_out.offset != 0x0F10111213141516)
        TEST_ERROR;
    if (xmit_out.size != 0x1718191A1B1C1D1E)
        TEST_ERROR;

    /* Decode from different offset in buffer
     * Observe changes when ingesting the padding
     */
    if (H5FD_mirror_xmit_decode_write(&xmit_out, buf) != H5FD_MIRROR_XMIT_WRITE_SIZE)
        TEST_ERROR;
    if (xmit_out.pub.magic != 0xFFFF0001)
        TEST_ERROR;
    if (xmit_out.pub.version != 0x02)
        TEST_ERROR;
    if (xmit_out.pub.session_token != 0x03040506)
        TEST_ERROR;
    if (xmit_out.pub.xmit_count != 0x0708090A)
        TEST_ERROR;
    if (xmit_out.pub.op != 0x0B)
        TEST_ERROR;
    if (xmit_out.type != 0x0C)
        TEST_ERROR;
    if (xmit_out.offset != 0x0D0E0F1011121314)
        TEST_ERROR;
    if (xmit_out.size != 0x15161718191A1B1C)
        TEST_ERROR;

    PASSED();
    return 0;

error:
    return -1;
}

/* ---------------------------------------------------------------------------
 * Function:    create_mirroring_split_fapl
 *
 * Purpose:     Create and populate a mirroring FAPL ID.
 *              Creates target files with the given base name -- ideally the
 *              test name -- and creates mirroring/split FAPL set to use the
 *              global mirroring info and a sec2 R/W channel driver.
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: HID of the top-level (splitter) FAPL, a non-negative
 *                       value.
 *              Failure: H5I_INVALID_HID, a negative value.
 * ---------------------------------------------------------------------------
 */
static hid_t
create_mirroring_split_fapl(const char *basename, struct mirrortest_filenames *names,
                            const struct mt_opts *opts)
{
    H5FD_splitter_vfd_config_t *splitter_config = NULL;
    H5FD_mirror_fapl_t          mirror_conf;
    hid_t                       ret_value = H5I_INVALID_HID;

    if (NULL == (splitter_config = malloc(sizeof(H5FD_splitter_vfd_config_t))))
        TEST_ERROR;

    /* Initialize the fapls, too, so the library doesn't try to
     * close non-existing IDs on errors
     */
    splitter_config->magic          = H5FD_SPLITTER_MAGIC;
    splitter_config->version        = H5FD_CURR_SPLITTER_VFD_CONFIG_VERSION;
    splitter_config->ignore_wo_errs = false;
    splitter_config->rw_fapl_id     = H5I_INVALID_HID;
    splitter_config->wo_fapl_id     = H5I_INVALID_HID;

    if (basename == NULL || *basename == '\0')
        TEST_ERROR;

    /* Create Splitter R/W channel driver (sec2) */
    if ((splitter_config->rw_fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if (H5Pset_fapl_sec2(splitter_config->rw_fapl_id) < 0)
        TEST_ERROR;

    /* Create Splitter W/O channel driver (mirror) */
    mirror_conf.magic          = H5FD_MIRROR_FAPL_MAGIC;
    mirror_conf.version        = H5FD_MIRROR_CURR_FAPL_T_VERSION;
    mirror_conf.handshake_port = opts->portno;
    if (strncpy(mirror_conf.remote_ip, opts->ip, H5FD_MIRROR_MAX_IP_LEN) == NULL)
        TEST_ERROR;
    if ((splitter_config->wo_fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if (H5Pset_fapl_mirror(splitter_config->wo_fapl_id, &mirror_conf) < 0)
        TEST_ERROR;

    /* Build r/w, w/o, and log file paths */
    if (build_paths(basename, splitter_config, names) < 0)
        TEST_ERROR;

    /* Set file paths for w/o and logfile */
    if (strncpy(splitter_config->wo_path, (const char *)names->wo, H5FD_SPLITTER_PATH_MAX) == NULL)
        TEST_ERROR;
    if (strncpy(splitter_config->log_file_path, (const char *)names->log, H5FD_SPLITTER_PATH_MAX) == NULL)
        TEST_ERROR;

    /* Create Splitter FAPL */
    if ((ret_value = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if (H5Pset_fapl_splitter(ret_value, splitter_config) < 0)
        TEST_ERROR;

    /* Close FAPLs created for child channels */
    if (H5Pclose(splitter_config->rw_fapl_id) < 0)
        TEST_ERROR;
    if (H5Pclose(splitter_config->wo_fapl_id) < 0)
        TEST_ERROR;

    free(splitter_config);

    return ret_value;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(splitter_config->wo_fapl_id);
        H5Pclose(splitter_config->rw_fapl_id);
        H5Pclose(ret_value);
    }
    H5E_END_TRY
    free(splitter_config);

    return H5I_INVALID_HID;
} /* end create_mirroring_split_fapl() */

/* ---------------------------------------------------------------------------
 * Function:    test_create_and_close
 *
 * Purpose:     Test/demonstrate a do-nothing file open and close.
 *
 *              Verifying file existence and contents is part of other tests.
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_create_and_close(const struct mt_opts *opts)
{
    struct mirrortest_filenames *names   = NULL;
    hid_t                        file_id = H5I_INVALID_HID;
    hid_t                        fapl_id = H5I_INVALID_HID;

    TESTING("File creation and immediate close");

    if (NULL == (names = malloc(sizeof(struct mirrortest_filenames))))
        TEST_ERROR;

    /* Create FAPL for splitter[sec2|mirror] */
    if ((fapl_id = create_mirroring_split_fapl("basic_create", names, opts)) < 0)
        TEST_ERROR;

    if ((file_id = H5Fcreate(names->rw, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
        TEST_ERROR;

    if (H5Fclose(file_id) < 0)
        TEST_ERROR;
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    free(names);

    PASSED();
    return 0;

error:
    free(names);
    H5E_BEGIN_TRY
    {
        H5Fclose(file_id);
        H5Pclose(fapl_id);
    }
    H5E_END_TRY
    return -1;
} /* end test_create_and_close() */

/* ----------------------------------------------------------------------------
 * Function:    create_datasets
 *
 * Purpose:     Given a file ID and least and greateset dataset indices, create
 *              populated chunked datasets in the target file from min_dset to
 *              (and including) max_dset.
 *              Uses #defined constants to determine chunk and dataset sizes
 *              and values.
 *
 * Return:      SUCCEED/FAIL
 * ----------------------------------------------------------------------------
 */
static herr_t
create_datasets(hid_t file_id, unsigned min_dset, unsigned max_dset)
{
    hid_t        dataspace_ids[MAX_DSET_COUNT + 1];
    hid_t        dataset_ids[MAX_DSET_COUNT + 1];
    hid_t        filespace_ids[MAX_DSET_COUNT + 1];
    int          data_chunk[CHUNK_DIM][CHUNK_DIM];
    unsigned int i, j, k, l, m;
    hsize_t      offset[2];
    hid_t        memspace_id   = H5I_INVALID_HID;
    hsize_t      a_size[2]     = {CHUNK_DIM, CHUNK_DIM};
    hsize_t      chunk_dims[2] = {CHUNK_DIM, CHUNK_DIM};
    hsize_t      dset_dims[2]  = {DSET_DIM, DSET_DIM};

    assert(file_id >= 0);
    assert(min_dset <= max_dset);
    assert(max_dset <= MAX_DSET_COUNT);

    LOGPRINT(2, "create_dataset()\n");

    /* Initialize ID arrays */
    for (i = 0; i < MAX_DSET_COUNT; i++) {
        LOGPRINT(3, "clearing IDs [%d]\n", i);
        dataspace_ids[i] = H5I_INVALID_HID;
        dataset_ids[i]   = H5I_INVALID_HID;
        filespace_ids[i] = H5I_INVALID_HID;
    }

    /* Generate dataspace, dataset, and 'filespace' IDs */
    if (_create_chunking_ids(file_id, min_dset, max_dset, chunk_dims, dset_dims, dataspace_ids, filespace_ids,
                             dataset_ids, &memspace_id) < 0)
        TEST_ERROR;

    /* Initialize (write) all datasets in a "round robin"...
     * for a given chunk 'location', write chunk data to each dataset.
     */
    for (i = 0; i < DSET_DIM; i += CHUNK_DIM) {
        LOGPRINT(3, "i: %d\n", i);
        for (j = 0; j < DSET_DIM; j += CHUNK_DIM) {
            LOGPRINT(3, "  j: %d\n", j);
            for (m = min_dset; m <= max_dset; m++) {
                LOGPRINT(3, "    m: %d\n", m);
                for (k = 0; k < CHUNK_DIM; k++) {
                    for (l = 0; l < CHUNK_DIM; l++) {
                        data_chunk[k][l] = (int)((DSET_DIM * DSET_DIM * m) + (DSET_DIM * (i + k)) + j + l);
                        LOGPRINT(3, "      data_chunk[%d][%d]: %d\n", k, l, data_chunk[k][l]);
                    }
                }

                /* Select on disk hyperslab */
                offset[0] = (hsize_t)i;
                offset[1] = (hsize_t)j;
                LOGPRINT(3, "    H5Sselect_hyperslab()\n");
                if (H5Sselect_hyperslab(filespace_ids[m], H5S_SELECT_SET, offset, NULL, a_size, NULL) < 0)
                    TEST_ERROR;

                LOGPRINT(3, "    H5Dwrite()\n");
                if (H5Dwrite(dataset_ids[m], H5T_NATIVE_INT, memspace_id, filespace_ids[m], H5P_DEFAULT,
                             data_chunk) < 0)
                    TEST_ERROR;
            }
        }
    }

    /* Read and verify data from datasets */
    if (_verify_datasets(min_dset, max_dset, filespace_ids, dataset_ids, memspace_id) < 0)
        TEST_ERROR;

    /* Cleanup */
    if (_close_chunking_ids(min_dset, max_dset, dataspace_ids, filespace_ids, dataset_ids, &memspace_id) < 0)
        TEST_ERROR;

    return SUCCEED;

error:
    (void)_close_chunking_ids(min_dset, max_dset, dataspace_ids, filespace_ids, dataset_ids, &memspace_id);
    LOGPRINT(1, "create_datasets() FAILED\n");
    return FAIL;
} /* end create_datasets() */

/* ----------------------------------------------------------------------------
 * Function:   _create_chunking_ids
 *
 * Purpose:    Create new IDs to be used with the associated file.
 *
 * Return:     SUCCEED/FAIL
 * ----------------------------------------------------------------------------
 */
static herr_t
_create_chunking_ids(hid_t file_id, unsigned min_dset, unsigned max_dset, hsize_t *chunk_dims,
                     hsize_t *dset_dims, hid_t *dataspace_ids, hid_t *filespace_ids, hid_t *dataset_ids,
                     hid_t *memspace_id)
{
    char  dset_name[DSET_NAME_LEN + 1];
    hid_t dcpl_id = H5I_INVALID_HID;

    LOGPRINT(2, "_create_chunking_ids()\n");

    /* Create chunking DCPL */
    if ((dcpl_id = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        TEST_ERROR;
    if (H5Pset_chunk(dcpl_id, 2, chunk_dims) < 0)
        TEST_ERROR;

    /* Create dataspace IDs */
    for (unsigned m = min_dset; m <= max_dset; m++) {
        if ((dataspace_ids[m] = H5Screate_simple(2, dset_dims, NULL)) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to create dataspace ID %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    /* Create dataset IDs */
    for (unsigned m = min_dset; m <= max_dset; m++) {
        if (snprintf(dset_name, DSET_NAME_LEN, "/dset%03d", m) > DSET_NAME_LEN) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to compose dset name %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }

        if ((dataset_ids[m] = H5Dcreate2(file_id, dset_name, H5T_STD_I32BE, dataspace_ids[m], H5P_DEFAULT,
                                         dcpl_id, H5P_DEFAULT)) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to create dset ID %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    /* Get file space IDs */
    for (unsigned m = min_dset; m <= max_dset; m++) {
        if ((filespace_ids[m] = H5Dget_space(dataset_ids[m])) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to create filespace ID %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    /* Create mem space to be used to read and write chunks */
    if ((*memspace_id = H5Screate_simple(2, chunk_dims, NULL)) < 0)
        TEST_ERROR;

    /* Clean up the DCPL  */
    if (H5Pclose(dcpl_id) < 0)
        TEST_ERROR;

    return SUCCEED;

error:
    H5E_BEGIN_TRY
    {
        /* Note that it's the caller's responsibility to clean up any IDs
         * passed back via out parameters
         */
        H5Pclose(dcpl_id);
    }
    H5E_END_TRY

    LOGPRINT(1, "_create_chunking_ids() FAILED\n");

    return FAIL;
} /* end _create_chunking_ids() */

/* ----------------------------------------------------------------------------
 * Function:    _open_chunking_ids
 *
 * Purpose:     Open/access IDs from the given file.
 *
 * Return:      SUCCEED/FAIL
 * ----------------------------------------------------------------------------
 */
static herr_t
_open_chunking_ids(hid_t file_id, unsigned min_dset, unsigned max_dset, hsize_t *chunk_dims,
                   hid_t *filespace_ids, hid_t *dataset_ids, hid_t *memspace_id)
{
    char dset_name[DSET_NAME_LEN + 1];

    LOGPRINT(2, "_open_chunking_ids()\n");

    /* Open dataset IDs */
    for (unsigned m = min_dset; m <= max_dset; m++) {
        if (snprintf(dset_name, DSET_NAME_LEN, "/dset%03d", m) > DSET_NAME_LEN) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to compose dset name %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }

        if ((dataset_ids[m] = H5Dopen2(file_id, dset_name, H5P_DEFAULT)) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to open dset ID %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    /* Open filespace IDs */
    for (unsigned m = min_dset; m <= max_dset; m++) {
        if ((filespace_ids[m] = H5Dget_space(dataset_ids[m])) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to get filespace ID %d\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    /* Create mem space to be used to read and write chunks */
    if ((*memspace_id = H5Screate_simple(2, chunk_dims, NULL)) < 0)
        TEST_ERROR;

    return SUCCEED;

error:
    /* Note that the caller is responsible for cleaning up IDs returned
     * as out parameters
     */
    LOGPRINT(1, "_open_chunking_ids() FAILED\n");
    return FAIL;
} /* end _open_chunking_ids() */

/* ---------------------------------------------------------------------------
 * Function:    _close_chunking_ids
 *
 * Purpose:     Close IDs that were created or opened.
 *              Pass NULL into `dataspace_ids` when closing items opened with
 *              _open_chunking_ids(). (as opposed to created IDs)
 *
 * Return:      SUCCEED/FAIL
 * ---------------------------------------------------------------------------
 */
static herr_t
_close_chunking_ids(unsigned min_dset, unsigned max_dset, hid_t *dataspace_ids, hid_t *filespace_ids,
                    hid_t *dataset_ids, hid_t *memspace_id)
{
    LOGPRINT(2, "_close_chunking_ids()\n");

    for (unsigned m = min_dset; m <= max_dset; m++) {
        LOGPRINT(3, "closing ids[%d]\n", m);
        if (dataspace_ids) {
            if (H5Sclose(dataspace_ids[m]) < 0) {
                snprintf(mesg, MIRR_MESG_SIZE, "unable to close dataspace_id[%d]\n", m);
                FAIL_PUTS_ERROR(mesg);
            }
        }
        if (H5Dclose(dataset_ids[m]) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to close dataset_id[%d]\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
        if (H5Sclose(filespace_ids[m]) < 0) {
            snprintf(mesg, MIRR_MESG_SIZE, "unable to close filespace_id[%d]\n", m);
            FAIL_PUTS_ERROR(mesg);
        }
    }

    if (*memspace_id != H5I_INVALID_HID)
        if (H5Sclose(*memspace_id) < 0)
            TEST_ERROR;

    return SUCCEED;

error:
    LOGPRINT(1, "_close_chunking_ids() FAILED\n");
    return FAIL;
} /* end _close_chunking_ids() */

/* ---------------------------------------------------------------------------
 * Function:    _verify_datasets
 *
 * Purpose:     Check that each chunk's contents are as expected, as pertaining
 *              to create_datasets().
 *
 * Return:      SUCCEED/FAIL
 * ---------------------------------------------------------------------------
 */
static herr_t
_verify_datasets(unsigned min_dset, unsigned max_dset, hid_t *filespace_ids, hid_t *dataset_ids,
                 hid_t memspace_id)
{
    unsigned i, j, k, l, m;
    int      data_chunk[CHUNK_DIM][CHUNK_DIM];
    hsize_t  offset[2];
    hsize_t  a_size[2] = {CHUNK_DIM, CHUNK_DIM};

    LOGPRINT(2, "_verify_datasets()\n");

    for (i = 0; i < DSET_DIM; i += CHUNK_DIM) {
        LOGPRINT(3, "i: %d\n", i);
        for (j = 0; j < DSET_DIM; j += CHUNK_DIM) {
            LOGPRINT(3, "  j: %d\n", j);
            for (m = min_dset; m <= max_dset; m++) {
                LOGPRINT(3, "    m: %d\n", m);

                /* select on disk hyperslab */
                offset[0] = (hsize_t)i;
                offset[1] = (hsize_t)j;
                if (H5Sselect_hyperslab(filespace_ids[m], H5S_SELECT_SET, offset, NULL, a_size, NULL) < 0)
                    TEST_ERROR;

                if (H5Dread(dataset_ids[m], H5T_NATIVE_INT, memspace_id, filespace_ids[m], H5P_DEFAULT,
                            data_chunk) < 0) {
                    snprintf(mesg, MIRR_MESG_SIZE, "      H5Dread() [%d][%d][%d]\n", i, j, m);
                    FAIL_PUTS_ERROR(mesg);
                }

                for (k = 0; k < CHUNK_DIM; k++) {
                    for (l = 0; l < CHUNK_DIM; l++) {
                        if ((unsigned)data_chunk[k][l] !=
                            ((DSET_DIM * DSET_DIM * m) + (DSET_DIM * (i + k)) + j + l)) {
                            snprintf(mesg, MIRR_MESG_SIZE, "      MISMATCH [%d][%d][%d][%d][%d]\n", i, j, m,
                                     k, l);
                            FAIL_PUTS_ERROR(mesg);
                        }
                    }
                }
            }
        }
    }

    return SUCCEED;

error:
    LOGPRINT(1, "_verify_datasets() FAILED\n");
    return FAIL;
} /* end _verify_datasets() */

/* ---------------------------------------------------------------------------
 * Function:    verify_datasets
 *
 * Purpose:     Inspect the datasets in the file created by create_datasets().
 *              Wrapper for _verify_datasets() -- this function sets up and
 *              tears down accessor information.
 *
 * Return:      SUCCEED/FAIL
 * ---------------------------------------------------------------------------
 */
static herr_t
verify_datasets(hid_t file_id, unsigned min_dset, unsigned max_dset)
{
    hid_t   dataset_ids[MAX_DSET_COUNT + 1];
    hid_t   filespace_ids[MAX_DSET_COUNT + 1];
    hid_t   memspace_id   = H5I_INVALID_HID;
    hsize_t chunk_dims[2] = {CHUNK_DIM, CHUNK_DIM};

    assert(file_id >= 0);
    assert(min_dset <= max_dset);
    assert(max_dset <= MAX_DSET_COUNT);

    LOGPRINT(2, "verify_datasets()\n");

    /* Initialize ID arrays */
    for (unsigned i = 0; i < MAX_DSET_COUNT; i++) {
        LOGPRINT(3, "clearing IDs [%d]\n", i);
        dataset_ids[i]   = H5I_INVALID_HID;
        filespace_ids[i] = H5I_INVALID_HID;
    }

    /* Generate dataspace, dataset, and 'filespace' IDs */
    if (_open_chunking_ids(file_id, min_dset, max_dset, chunk_dims, filespace_ids, dataset_ids,
                           &memspace_id) < 0)
        TEST_ERROR;

    /* Read and verify data from datasets */
    if (_verify_datasets(min_dset, max_dset, filespace_ids, dataset_ids, memspace_id) < 0)
        TEST_ERROR;

    /* Cleanup */
    if (_close_chunking_ids(min_dset, max_dset, NULL, filespace_ids, dataset_ids, &memspace_id) < 0)
        TEST_ERROR;

    return SUCCEED;

error:
    LOGPRINT(1, "verify_datasets() FAILED\n");
    (void)_close_chunking_ids(min_dset, max_dset, NULL, filespace_ids, dataset_ids, &memspace_id);
    return FAIL;

} /* end verify_datasets() */

/* ---------------------------------------------------------------------------
 * Function:    test_basic_dataset_write
 *
 * Purpose:     Create and close files; repoen files and write a dataset,
 *              close; compare files.
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_basic_dataset_write(const struct mt_opts *opts)
{
    struct mirrortest_filenames *names     = NULL;
    hid_t                        file_id   = H5I_INVALID_HID;
    hid_t                        fapl_id   = H5I_INVALID_HID;
    hid_t                        dset_id   = H5I_INVALID_HID;
    hid_t                        dspace_id = H5I_INVALID_HID;
    hid_t                        dtype_id  = H5T_NATIVE_INT;
    hsize_t                      dims[2]   = {DATABUFFER_SIZE, DATABUFFER_SIZE};
    int                         *buf       = NULL;
    int                          i         = 0;
    int                          j         = 0;

    TESTING("Mirror open and dataset writing");

    if (NULL == (names = malloc(sizeof(struct mirrortest_filenames))))
        TEST_ERROR;

    /* Create FAPL for Splitter[sec2|mirror] */
    if ((fapl_id = create_mirroring_split_fapl("basic_write", names, opts)) < 0)
        TEST_ERROR;

    /* Prepare data to be written */
    if (NULL == (buf = malloc(DATABUFFER_SIZE * DATABUFFER_SIZE * sizeof(int))))
        TEST_ERROR;
    for (i = 0; i < DATABUFFER_SIZE; i++) {
        for (j = 0; j < DATABUFFER_SIZE; j++) {
            int k  = i * DATABUFFER_SIZE + j;
            buf[k] = k;
        }
    }

    /* -------------------- */
    /* TEST: Create and Close */

    if ((file_id = H5Fcreate(names->rw, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
        TEST_ERROR;
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Repoen and Write */

    if ((file_id = H5Fopen(names->rw, H5F_ACC_RDWR, fapl_id)) < 0)
        TEST_ERROR;

    if ((dspace_id = H5Screate_simple(2, dims, NULL)) < 0)
        TEST_ERROR;
    if ((dset_id =
             H5Dcreate2(file_id, "dataset", dtype_id, dspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if (H5Dwrite(dset_id, dtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
        TEST_ERROR;

    /* Cleanup */

    if (H5Dclose(dset_id) < 0)
        TEST_ERROR;
    if (H5Sclose(dspace_id) < 0)
        TEST_ERROR;
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Verify that the R/W and W/O files are identical */

    if (h5_compare_file_bytes(names->rw, names->wo) < 0)
        TEST_ERROR;

    free(buf);
    free(names);

    PASSED();
    return 0;

error:
    free(buf);
    free(names);

    H5E_BEGIN_TRY
    {
        H5Fclose(file_id);
        H5Dclose(dset_id);
        H5Sclose(dspace_id);
        H5Pclose(fapl_id);
    }
    H5E_END_TRY
    return -1;
} /* end test_basic_dataset_write() */

/* ---------------------------------------------------------------------------
 * Function:    test_chunked_dataset_write
 *
 * Purpose:     Create and close files; repoen files and write a dataset,
 *              close; compare files.
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_chunked_dataset_write(const struct mt_opts *opts)
{
    struct mirrortest_filenames *names = NULL;

    hid_t file_id = H5I_INVALID_HID;
    hid_t fapl_id = H5P_DEFAULT;

    TESTING("Mirror open and dataset writing (chunked)");

    if (NULL == (names = malloc(sizeof(struct mirrortest_filenames))))
        TEST_ERROR;

    /* Create FAPL for Splitter[sec2|mirror] */
    if ((fapl_id = create_mirroring_split_fapl("chunked_write", names, opts)) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Create and Close */

    if ((file_id = H5Fcreate(names->rw, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
        TEST_ERROR;
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Reopen and Write */

    if ((file_id = H5Fopen(names->rw, H5F_ACC_RDWR, fapl_id)) < 0)
        TEST_ERROR;

    /* Write datasets to file */
    if (create_datasets(file_id, 0, MAX_DSET_COUNT) < 0)
        TEST_ERROR;

    /* Close to 'flush to disk', and reopen file */
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    /* Reopen file */
    if ((file_id = H5Fopen(names->rw, H5F_ACC_RDWR, fapl_id)) < 0)
        TEST_ERROR;

    /* Verify written data integrity */
    if (verify_datasets(file_id, 0, MAX_DSET_COUNT) < 0)
        TEST_ERROR;

    /* Cleanup */
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Verify that the R/W and W/O files are identical */

    if (h5_compare_file_bytes(names->rw, names->wo) < 0) {
        TEST_ERROR;
    }

    free(names);

    PASSED();
    return 0;

error:
    free(names);
    H5E_BEGIN_TRY
    {
        H5Fclose(file_id);
        H5Pclose(fapl_id);
    }
    H5E_END_TRY
    return -1;
} /* end test_chunked_dataset_write() */

/* ---------------------------------------------------------------------------
 * Function:    test_on_disk_zoo
 *
 * Purpose:     Verify that the mirror can handle the passing of all the
 *              various on-disk data structures over the wire, as implemented
 *              in genall5.c:create_zoo().
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_on_disk_zoo(const struct mt_opts *opts)
{
    const char                   grp_name[] = "/only";
    struct mirrortest_filenames *names      = NULL;
    hid_t                        file_id    = H5I_INVALID_HID;
    hid_t                        grp_id     = H5I_INVALID_HID;
    hid_t                        fapl_id    = H5I_INVALID_HID;

    TESTING("'Zoo' of on-disk structures");

    if (NULL == (names = malloc(sizeof(struct mirrortest_filenames))))
        TEST_ERROR;

    /* Create FAPL for Splitter[sec2|mirror] */
    if ((fapl_id = create_mirroring_split_fapl("zoo", names, opts)) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Create file    */
    if ((file_id = H5Fcreate(names->rw, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
        TEST_ERROR;

    if ((grp_id = H5Gcreate2(file_id, grp_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create datasets in file, close (flush) and reopen, validate.
     * Use of ( pass ) a conceit required for using create_ and validate_zoo()
     * from cache_common and/or genall5.
     */

    if (pass)
        create_zoo(file_id, grp_name, 0);
    if (pass) {
        if (H5Fclose(file_id) < 0)
            TEST_ERROR;
        if ((file_id = H5Fopen(names->rw, H5F_ACC_RDWR, fapl_id)) < 0)
            TEST_ERROR;
    }
    if (pass)
        validate_zoo(file_id, grp_name, 0); /* sanity-check */

    if (!pass) {
        printf("%s", failure_mssg);
        TEST_ERROR;
    }

    /* Cleanup */

    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;
    if (H5Gclose(grp_id) < 0)
        TEST_ERROR;
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Verify that the R/W and W/O files are identical */

    if (h5_compare_file_bytes(names->rw, names->wo) < 0)
        TEST_ERROR;

    free(names);

    PASSED();
    return 0;

error:
    free(names);
    H5E_BEGIN_TRY
    {
        H5Fclose(file_id);
        H5Gclose(grp_id);
        H5Pclose(fapl_id);
    }
    H5E_END_TRY
    return -1;
} /* end test_on_disk_zoo() */

/* ---------------------------------------------------------------------------
 * Function:    test_vanishing_datasets
 *
 * Purpose:     Verify behavior when writing to a file where data is deleted.
 *
 *              Each dataset is populated with the value of its suffix
 *              (dset5 is all fives).
 *
 *              Opens 0..15 create one new dataset each, '/dset[i]'.
 *              Opens 3..18 delete '/dset[1-3]'
 *
 *              Should end with no data in file.
 *
 * Return:      Success:  0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_vanishing_datasets(const struct mt_opts *opts)
{
    struct mirrortest_filenames *names     = NULL;
    hid_t                        file_id   = H5I_INVALID_HID;
    hid_t                        fapl_id   = H5I_INVALID_HID;
    hid_t                        dset_id   = H5I_INVALID_HID;
    hid_t                        dspace_id = H5I_INVALID_HID;
    hsize_t                      dims[2]   = {DATABUFFER_SIZE, DATABUFFER_SIZE};
    H5G_info_t                   group_info;
    unsigned int                 i, j, k;
    const unsigned int           max_loops       = 20;
    const unsigned int           max_at_one_time = 3;
    struct {
        uint32_t arr[DATABUFFER_SIZE][DATABUFFER_SIZE];
    } *buf = NULL;

    TESTING("Vanishing Datasets");

    if (NULL == (names = malloc(sizeof(struct mirrortest_filenames))))
        TEST_ERROR;
    if (NULL == (buf = calloc(1, sizeof(*buf))))
        TEST_ERROR;

    /* -------------------- */
    /* Set up recurrent data (FAPL, dataspace) */

    /* Create FAPL for Splitter[sec2|mirror] */
    if ((fapl_id = create_mirroring_split_fapl("vanishing", names, opts)) < 0)
        TEST_ERROR;

    if ((dspace_id = H5Screate_simple(2, dims, NULL)) < 0)
        TEST_ERROR;

    /* Create file */
    if ((file_id = H5Fcreate(names->rw, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) < 0)
        TEST_ERROR;

    for (i = 0; i < max_loops; i++) {
        char namebuf[DSET_NAME_LEN + 1];

        /* Delete datasets */
        if (i >= max_at_one_time) {
            if (snprintf(namebuf, DSET_NAME_LEN, "/dset%02d", (i - max_at_one_time)) > DSET_NAME_LEN)
                TEST_ERROR;
            if (H5Ldelete(file_id, namebuf, H5P_DEFAULT) < 0)
                TEST_ERROR;
        }

        /* Write to datasets */
        if (i < (max_loops - max_at_one_time)) {
            if (snprintf(namebuf, DSET_NAME_LEN, "/dset%02d", i) > DSET_NAME_LEN)
                TEST_ERROR;
            if ((dset_id = H5Dcreate2(file_id, namebuf, H5T_STD_U32LE, dspace_id, H5P_DEFAULT, H5P_DEFAULT,
                                      H5P_DEFAULT)) < 0)
                TEST_ERROR;

            for (j = 0; j < DATABUFFER_SIZE; j++)
                for (k = 0; k < DATABUFFER_SIZE; k++)
                    buf->arr[j][k] = (uint32_t)i;

            if (H5Dwrite(dset_id, H5T_STD_U32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
                TEST_ERROR;

            if (H5Dclose(dset_id) < 0)
                TEST_ERROR;
        }

    } /* end for dataset create-destroy cycles */

    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    /* verify there are no datasets in file */
    if ((file_id = H5Fopen(names->rw, H5F_ACC_RDONLY, fapl_id)) < 0)
        TEST_ERROR;
    if (H5Gget_info(file_id, &group_info) < 0)
        TEST_ERROR;
    if (group_info.nlinks > 0) {
        fprintf(stderr, "links in rw file: %" PRIuHSIZE "\n", group_info.nlinks);
        TEST_ERROR;
    }
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;
    if ((file_id = H5Fopen(names->wo, H5F_ACC_RDONLY, fapl_id)) < 0)
        TEST_ERROR;
    if (H5Gget_info(file_id, &group_info) < 0)
        TEST_ERROR;
    if (group_info.nlinks > 0) {
        fprintf(stderr, "links in wo file: %" PRIuHSIZE "\n", group_info.nlinks);
        TEST_ERROR;
    }
    if (H5Fclose(file_id) < 0)
        TEST_ERROR;

    if (h5_compare_file_bytes(names->rw, names->wo) < 0)
        TEST_ERROR;

    /* Teardown */

    if (H5Sclose(dspace_id) < 0)
        TEST_ERROR;
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    free(names);
    free(buf);

    PASSED();
    return 0;

error:
    free(names);
    free(buf);
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
        H5Fclose(file_id);
        H5Dclose(dset_id);
        H5Sclose(dspace_id);
    }
    H5E_END_TRY
    return -1;
} /* test_vanishing_datasets() */

/* ---------------------------------------------------------------------------
 * Function:    test_concurrent_access
 *
 * Purpose:     Verify that more than one file may be opened at a time.
 *
 *              TODO: receive target IP from caller?
 *
 * Return:      Success: 0
 *              Failure: -1
 * ---------------------------------------------------------------------------
 */
static int
test_concurrent_access(const struct mt_opts *opts)
{
    struct file_bundle {
        struct mirrortest_filenames names;
        hid_t                       dset_id;
        hid_t                       fapl_id;
        hid_t                       file_id;
    };

    struct file_bundle *bundle = NULL;

    hid_t   dspace_id = H5I_INVALID_HID;
    hid_t   dtype_id  = H5T_NATIVE_INT;
    hsize_t dims[2]   = {DATABUFFER_SIZE, DATABUFFER_SIZE};
    int    *buf       = NULL;
    int     i         = 0;
    int     j         = 0;

    TESTING("Concurrent opened mirrored files");

    if (NULL == (bundle = malloc(sizeof(struct file_bundle) * CONCURRENT_COUNT)))
        TEST_ERROR;

    /* Initialize bundle */
    for (i = 0; i < CONCURRENT_COUNT; i++) {
        bundle[i].dset_id    = H5I_INVALID_HID;
        bundle[i].fapl_id    = H5I_INVALID_HID;
        bundle[i].file_id    = H5I_INVALID_HID;
        *bundle[i].names.rw  = '\0';
        *bundle[i].names.wo  = '\0';
        *bundle[i].names.log = '\0';
    }

    /* Create FAPL for Splitter[sec2|mirror] */
    for (i = 0; i < CONCURRENT_COUNT; i++) {
        char  name[16] = "";
        hid_t fapl_id  = H5I_INVALID_HID;

        snprintf(name, 15, "concurrent%d", i);
        if ((fapl_id = create_mirroring_split_fapl(name, &bundle[i].names, opts)) < 0)
            TEST_ERROR;
        bundle[i].fapl_id = fapl_id;
    }

    /* Prepare data to be written */
    if (NULL == (buf = malloc(DATABUFFER_SIZE * DATABUFFER_SIZE * sizeof(int))))
        TEST_ERROR;
    for (i = 0; i < DATABUFFER_SIZE; i++) {
        for (j = 0; j < DATABUFFER_SIZE; j++) {
            int k  = i * DATABUFFER_SIZE + j;
            buf[k] = k;
        }
    }

    /* Prepare generic dataspace */
    if ((dspace_id = H5Screate_simple(2, dims, NULL)) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Create file and open elements */

    for (i = 0; i < CONCURRENT_COUNT; i++) {
        hid_t file_id = H5I_INVALID_HID;
        hid_t dset_id = H5I_INVALID_HID;

        if ((file_id = H5Fcreate(bundle[i].names.rw, H5F_ACC_TRUNC, H5P_DEFAULT, bundle[i].fapl_id)) < 0)
            TEST_ERROR;

        bundle[i].file_id = file_id;

        if ((dset_id = H5Dcreate2(file_id, "dataset", dtype_id, dspace_id, H5P_DEFAULT, H5P_DEFAULT,
                                  H5P_DEFAULT)) < 0)
            TEST_ERROR;
        bundle[i].dset_id = dset_id;
    }

    /* -------------------- */
    /* TEST: Write to files */

    for (i = 0; i < CONCURRENT_COUNT; i++)
        if (H5Dwrite(bundle[i].dset_id, dtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
            TEST_ERROR;

    /* -------------------- */
    /* TEST: Close elements  */

    for (i = 0; i < CONCURRENT_COUNT; i++) {
        if (H5Dclose(bundle[i].dset_id) < 0)
            TEST_ERROR;
        if (H5Fclose(bundle[i].file_id) < 0)
            TEST_ERROR;
        if (H5Pclose(bundle[i].fapl_id) < 0)
            TEST_ERROR;
    }

    if (H5Sclose(dspace_id) < 0)
        TEST_ERROR;

    /* -------------------- */
    /* TEST: Verify that the R/W and W/O files are identical */

    for (i = 0; i < CONCURRENT_COUNT; i++)
        if (h5_compare_file_bytes(bundle[i].names.rw, bundle[i].names.wo) < 0)
            TEST_ERROR;

    free(bundle);
    free(buf);

    PASSED();
    return 0;

error:
    H5E_BEGIN_TRY
    {
        H5Sclose(dspace_id);
        for (i = 0; i < CONCURRENT_COUNT; i++) {
            H5Dclose(bundle[i].dset_id);
            H5Fclose(bundle[i].file_id);
            H5Pclose(bundle[i].fapl_id);
        }
    }
    H5E_END_TRY
    free(bundle);
    free(buf);
    return -1;
} /* end test_concurrent_access() */

/* ----------------------------------------------------------------------------
 * Function:    parse_args
 *
 * Purpose:     Parse command-line arguments, populating the options struct
 *              pointer as appropriate.
 *              Default values will be set for unspecified options.
 *
 * Return:      0 on success, negative (-1) if error.
 * ----------------------------------------------------------------------------
 */
static int
parse_args(int argc, char **argv, struct mt_opts *opts)
{
    int i = 0;

    opts->portno = SERVER_HANDSHAKE_PORT;
    strncpy(opts->ip, SERVER_IP_ADDRESS, H5FD_MIRROR_MAX_IP_LEN);

    for (i = 1; i < argc; i++) { /* start with first possible option argument */
        if (!strncmp(argv[i], "--ip=", 5))
            strncpy(opts->ip, argv[i] + 5, H5FD_MIRROR_MAX_IP_LEN);
        else if (!strncmp(argv[i], "--port=", 7))
            opts->portno = atoi(argv[i] + 7);
        else {
            printf("Unrecognized option: '%s'\n", argv[i]);
            return -1;
        }
    } /* end for each argument from command line */

    /* Auto-replace 'localhost' with numeric IP */
    if (!strncmp(opts->ip, "localhost", 10)) /* include NUL terminator */
        strncpy(opts->ip, "127.0.0.1", H5FD_MIRROR_MAX_IP_LEN);

    return 0;
} /* end parse_args() */

/* ----------------------------------------------------------------------------
 * Function:    confirm_server
 *
 * Purpose:     Create socket and confirm remote server is available.
 *
 * Return:      0 on success, negative (-1) if error.
 * ----------------------------------------------------------------------------
 */
static int
confirm_server(struct mt_opts *opts)
{
    char               mybuf[16];
    int                live_socket = -1;
    struct sockaddr_in target_addr;
    unsigned           attempt = 0;

    live_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (live_socket < 0) {
        printf("ERROR socket()\n");
        return -1;
    }

    target_addr.sin_family      = AF_INET;
    target_addr.sin_port        = htons((uint16_t)opts->portno);
    target_addr.sin_addr.s_addr = inet_addr(opts->ip);
    memset(target_addr.sin_zero, '\0', sizeof(target_addr.sin_zero));

    while (1) {
        if (connect(live_socket, (struct sockaddr *)&target_addr, (socklen_t)sizeof(target_addr)) < 0) {
            if (attempt > 10) {
                printf("ERROR connect() (%d)\n%s\n", errno, strerror(errno));
                return -1;
            }

            /* Close socket during sleep() */
            if (HDclose(live_socket) < 0) {
                printf("ERROR close() can't close socket\n");
                return -1;
            }
            live_socket = -1;

            attempt++;
            HDsleep(1);
            printf("attempt #%u: ERROR connect() (%d)\n%s\n", attempt, errno, strerror(errno));

            /* Re-open socket for retry */
            live_socket = socket(AF_INET, SOCK_STREAM, 0);
            if (live_socket < 0) {
                printf("ERROR socket()\n");
                return -1;
            }
        }
        else
            break;
    }

    /* Request confirmation from the server */
    if (HDwrite(live_socket, "CONFIRM", 8) == -1) {
        printf("ERROR write() (%d)\n%s\n", errno, strerror(errno));
        return -1;
    }

    /* Read & verify response from port connection.  */
    if (HDread(live_socket, &mybuf, sizeof(mybuf)) == -1) {
        printf("ERROR read() can't receive data\n");
        return -1;
    }
    if (strncmp("ALIVE", mybuf, 6)) {
        printf("ERROR read() didn't receive data from server\n");
        return -1;
    }

    if (HDclose(live_socket) < 0) {
        printf("ERROR close() can't close socket\n");
        return -1;
    }

    return 0;
} /* end confirm_server() */

/* ---------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     Run tests
 *
 * Return:      Success: 0
 *              Failure: 1
 * ---------------------------------------------------------------------------
 */
int
main(int argc, char **argv)
{
    struct mt_opts opts;
    int            nerrors = 0;

    h5_reset();

    g_log_stream = stdout; /* default debug/logging output stream */

    printf("Testing Mirror VFD functionality.\n");

    /* SETUP */

    /* Create directories for test-generated .h5 files */
    if (nerrors == 0) {
        if ((HDmkdir(MIRROR_RW_DIR, (mode_t)0755) < 0) && (errno != EEXIST))
            nerrors++;
    }
    if (nerrors == 0) {
        if ((HDmkdir(MIRROR_WO_DIR, (mode_t)0755) < 0) && (errno != EEXIST))
            nerrors++;
    }

    if (parse_args(argc, argv, &opts) < 0) {
        printf("Unable to parse arguments\n");
        exit(EXIT_FAILURE);
    }

    if (confirm_server(&opts) < 0) {
        printf("Unable to confirm server is running\n");
        exit(EXIT_FAILURE);
    }

    /* TESTS */

    if (nerrors == 0) {
        H5FD_mirror_xmit_t xmit_mock; /* Re-used header in various xmit tests */

        /* Set bogus values matching expected; encoding doesn't care
         * Use sequential values to easily generate the expected buffer with a
         * for loop.
         */
        xmit_mock.magic         = 0x00010203;
        xmit_mock.version       = 0x04;
        xmit_mock.session_token = 0x05060708;
        xmit_mock.xmit_count    = 0x090A0B0C;
        xmit_mock.op            = 0x0D;

        nerrors += test_fapl_configuration() < 0 ? 1 : 0;

        nerrors += test_encdec_uint8_t() < 0 ? 1 : 0;
        nerrors += test_encdec_uint16_t() < 0 ? 1 : 0;
        nerrors += test_encdec_uint32_t() < 0 ? 1 : 0;
        nerrors += test_encdec_uint64_t() < 0 ? 1 : 0;

        nerrors += test_encdec_header(xmit_mock) < 0 ? 1 : 0;
        nerrors += test_encdec_set_eoa(xmit_mock) < 0 ? 1 : 0;
        nerrors += test_encdec_lock(xmit_mock) < 0 ? 1 : 0;
        nerrors += test_encdec_open(xmit_mock) < 0 ? 1 : 0;
        nerrors += test_encdec_reply(xmit_mock) < 0 ? 1 : 0;
        nerrors += test_encdec_write(xmit_mock) < 0 ? 1 : 0;

        nerrors += test_create_and_close(&opts) < 0 ? 1 : 0;
        nerrors += test_basic_dataset_write(&opts) < 0 ? 1 : 0;
        nerrors += test_chunked_dataset_write(&opts) < 0 ? 1 : 0;
        nerrors += test_on_disk_zoo(&opts) < 0 ? 1 : 0;
        nerrors += test_vanishing_datasets(&opts) < 0 ? 1 : 0;
        nerrors += test_concurrent_access(&opts) < 0 ? 1 : 0;
    }

    if (nerrors) {
        printf("***** %d Mirror VFD TEST%s FAILED! *****\n", nerrors, nerrors > 1 ? "S" : "");
        return EXIT_FAILURE;
    }

    printf("All Mirror Virtual File Driver tests passed.\n");
    return EXIT_SUCCESS;
} /* end main() */

#else /* H5_HAVE_MIRROR_VFD */

int
main(void)
{
    printf("Testing Mirror VFD functionality.\n");
    printf("SKIPPED - Mirror VFD not built.\n");
    return EXIT_SUCCESS;
}

#endif /* H5_HAVE_MIRROR_VFD */