Last-Modified: Tue, 27 Aug 2024 00:47:11 GMT Expires: Fri, 25 Aug 2034 00:47:11 GMT hdf5.git - Mirror from: https://github.com/HDFGroup/hdf5.git
summaryrefslogtreecommitdiffstats
path: root/test/testmeta.c
blob: c3044a6fc7e7475b121d23b22c0e0db882397e4b (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * 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://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * This program illustrates assertion errors when linked
 * to HDF5 1.4.1-post2 or 1.4.2-pre3 debug library.
 *
 * If the assertion errors are ignored, the program eventially causes
 * an error in H5Gcreate2 when writing object 83381.
 *
 * When writing in single file mode, the assertion errors still occur
 * but the H5Gcreate2 error does not.
 */

#include "h5test.h"

#define FILEN "testmeta.h5"

#define CHUNK_SIZE 512

#define NDATAARRAYS 3
/*#define NPOINTS         2048*/
#define NPOINTS      20
#define NEXTARRAYS   10
#define NDATAOBJECTS 100000

int
main(void)
{
    hid_t    file_id, prop_id, memspace_id, type_id;
    hid_t    group_id;
    hid_t    dataset_id, dataspace_id;
    herr_t   status;
    hsize_t  dims[1];
    hsize_t  maxdims[1];
    float    data[NPOINTS];
    float    floatval;
    unsigned numdataobj = 0;
    unsigned i, j;
    char     name[80];
    hsize_t  start[1]  = {0};
    hsize_t  stride[1] = {1};
    hsize_t  count[1]  = {1};

    /* Create a file */
    file_id = H5Fcreate(FILEN, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /* Create a dataset to hold the number of data objects */
    /* Create the data space */
    dataspace_id = H5Screate(H5S_SCALAR);

    /* Create dataset */
    dataset_id = H5Dcreate2(file_id, "/NumDataObj", H5T_NATIVE_UINT, dataspace_id, H5P_DEFAULT, H5P_DEFAULT,
                            H5P_DEFAULT);

    /* Write value to NumDataObj dataset */
    status = H5Dwrite(dataset_id, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &numdataobj);

    /* Close the identifiers */
    status = H5Dclose(dataset_id);
    status = H5Sclose(dataspace_id);

    /* Create extendible arrays */
    /* Set up for extendible dataset */
    prop_id = H5Pcreate(H5P_DATASET_CREATE);
    dims[0] = CHUNK_SIZE;
    status  = H5Pset_chunk(prop_id, 1, dims);

    /* Create dataspace */
    dims[0]      = 1;
    maxdims[0]   = H5S_UNLIMITED;
    dataspace_id = H5Screate_simple(1, dims, maxdims);

    for (i = 0; i < NEXTARRAYS; i++) {
        /* Create dataset */
        HDsprintf(name, "/ExtArray%06d", i);
        dataset_id =
            H5Dcreate2(file_id, name, H5T_NATIVE_FLOAT, dataspace_id, H5P_DEFAULT, prop_id, H5P_DEFAULT);

        /* Close the identifier */
        status = H5Dclose(dataset_id);
    }

    /* Close the identifiers */
    status = H5Sclose(dataspace_id);
    status = H5Pclose(prop_id);

    /* Create group to hold data object data arrays */
    group_id = H5Gcreate2(file_id, "/DataArray", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Gclose(group_id);

    for (j = 0; j < NDATAOBJECTS; j++) {
        /* Removed print statement as it would lock system resources on Windows */
        /*
         * HDprintf("\rWriting Object #%d of %d", j+1, NDATAOBJECTS);
         * HDfflush(stdout);
         */
        floatval = (float)j;

        /* Create group to hold data arrays for this object */
        HDsprintf(name, "/DataArray/%06d", j);
        group_id = H5Gcreate2(file_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
        if (group_id < 0) {
            HDfprintf(stderr, "Failed to create DataArray group.\n");
            status = H5Fclose(file_id);
            return -1;
        }

        /* Loop over data arrays */
        for (i = 0; i < NDATAARRAYS; i++) {
            /* Create dataspace */
            dims[0]      = NPOINTS;
            maxdims[0]   = NPOINTS;
            dataspace_id = H5Screate_simple(1, dims, maxdims);

            /* Create dataset */
            HDsprintf(name, "DataArray%06d", i);
            dataset_id = H5Dcreate2(group_id, name, H5T_NATIVE_FLOAT, dataspace_id, H5P_DEFAULT, H5P_DEFAULT,
                                    H5P_DEFAULT);
            if (dataset_id < 0) {
                HDfprintf(stderr, "Failed to create DataArray dataset.\n");
                status = H5Fclose(file_id);
                return -1;
            }

            /* Write the data array data */
            status = H5Dwrite(dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
            if (status < 0) {
                HDfprintf(stderr, "Failed to write DataArray dataset.\n");
                status = H5Fclose(file_id);
                return -1;
            }

            /* Close the identifiers */
            status = H5Dclose(dataset_id);
            status = H5Sclose(dataspace_id);
        }

        /* Open NumDataObj dataset */
        dataset_id = H5Dopen2(file_id, "/NumDataObj", H5P_DEFAULT);
        if (dataset_id < 0) {
            HDfprintf(stderr, "Failed to open NumDataObj dataset.\n");
            status = H5Fclose(file_id);
            return -1;
        }

        /* Write value to NumDataObj dataset */
        numdataobj = j + 1;
        status     = H5Dwrite(dataset_id, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &numdataobj);
        if (status < 0) {
            HDfprintf(stderr, "Failed to write NumDataObj dataset.\n");
            status = H5Fclose(file_id);
            return -1;
        }

        /* Close identifiers */
        status = H5Dclose(dataset_id);
        status = H5Gclose(group_id);

        /* Extend attribute arrays */
        for (i = 0; i < NEXTARRAYS; i++) {
            /* Open extendable dataset */
            HDsprintf(name, "/ExtArray%06d", i);
            dataset_id = H5Dopen2(file_id, name, H5P_DEFAULT);
            if (dataset_id < 0) {
                HDfprintf(stderr, "Failed to open ExtArray dataset.\n");
                status = H5Fclose(file_id);
                return -1;
            } /* end if */

            /* Extend attribute dataset */
            dims[0] = (hsize_t)j + 1;
            status  = H5Dset_extent(dataset_id, dims);
            if (status < 0) {
                HDfprintf(stderr, "Failed to extend DataArray dataset.\n");
                status = H5Fclose(file_id);
                return -1;
            } /* end if */

            /* Select element and write value to attribute dataset */
            dims[0]      = 1;
            memspace_id  = H5Screate_simple(1, dims, dims);
            dataspace_id = H5Dget_space(dataset_id);
            type_id      = H5Dget_type(dataset_id);

            start[0] = 0;
            status   = H5Sselect_hyperslab(memspace_id, H5S_SELECT_SET, start, stride, count, NULL);
            start[0] = (hssize_t)j;
            status   = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, start, stride, count, NULL);
            status   = H5Dwrite(dataset_id, type_id, memspace_id, dataspace_id, H5P_DEFAULT, &floatval);
            if (status < 0) {
                HDfprintf(stderr, "Failed to write DataArray dataset.\n");
                status = H5Fclose(file_id);
                return -1;
            }

            /* Close identifiers */
            status = H5Tclose(type_id);
            status = H5Sclose(dataspace_id);
            status = H5Sclose(memspace_id);
            status = H5Dclose(dataset_id);
        }
    }

    /* Close the file */
    status = H5Fclose(file_id);

    HDprintf("\n");

    return 0;
}
' href='#n998'>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 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Read-Only S3 Virtual File Driver (VFD)
 *
 * Purpose:    Unit tests for the S3 Communications (s3comms) module.
 *
 * Programmer: Jacob Smith
 *             2017-10-11
 */

#include "h5test.h"
#include "H5FDs3comms.h"
#include "H5MMprivate.h" /* memory management */

#ifdef H5_HAVE_ROS3_VFD

/*****************************************************************************
 *
 * FILE-LOCAL TESTING MACROS
 *
 * Purpose:
 *
 *     1) Upon test failure, goto-jump to single-location teardown in test
 *        function. E.g., `error:` (consistency with HDF corpus) or
 *        `failed:` (reflects purpose).
 *            >>> using "error", in part because `H5E_BEGIN_TRY` expects it.
 *     2) Increase clarity and reduce overhead found with `TEST_ERROR`.
 *        e.g., "if(somefunction(arg, arg2) < 0) TEST_ERROR:"
 *        requires reading of entire line to know whether this if/call is
 *        part of the test setup, test operation, or a test unto itself.
 *     3) Provide testing macros with optional user-supplied failure message;
 *        if not supplied (NULL), generate comparison output in the spirit of
 *        test-driven development. E.g., "expected 5 but was -3"
 *        User messages clarify test's purpose in code, encouraging description
 *        without relying on comments.
 *     4) Configurable expected-actual order in generated comparison strings.
 *        Some prefer `VERIFY(expected, actual)`, others
 *        `VERIFY(actual, expected)`. Provide preprocessor ifdef switch
 *        to satifsy both parties, assuming one paradigm per test file.
 *        (One could #undef and redefine the flag through the file as desired,
 *         but _why_.)
 *        Provided as courtesy, per consideration for inclusion in the library
 *        proper.
 *
 *     Macros:
 *
 *         JSVERIFY_EXP_ACT - ifdef flag, configures comparison order
 *         FAIL_IF()        - check condition
 *         FAIL_UNLESS()    - check _not_ condition
 *         JSVERIFY()       - long-int equality check; prints reason/comparison
 *         JSVERIFY_NOT()   - long-int inequality check; prints
 *         JSVERIFY_STR()   - string equality check; prints
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *****************************************************************************/

/*----------------------------------------------------------------------------
 *
 * ifdef flag: JSVERIFY_EXP_ACT
 *
 * JSVERIFY macros accept arguments as (EXPECTED, ACTUAL[, reason])
 *   default, if this is undefined, is (ACTUAL, EXPECTED[, reason])
 *
 *----------------------------------------------------------------------------
 */
#define JSVERIFY_EXP_ACT 1L

/*----------------------------------------------------------------------------
 *
 * Macro: JSFAILED_AT()
 *
 * Purpose:
 *
 *     Preface a test failure by printing "*FAILED*" and location to stdout
 *     Similar to `H5_FAILED(); AT();` from h5test.h
 *
 *     *FAILED* at somefile.c:12 in function_name()...
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
#define JSFAILED_AT()                                                                                        \
    {                                                                                                        \
        HDprintf("*FAILED* at %s:%d in %s()...\n", __FILE__, __LINE__, FUNC);                                \
    }

/*----------------------------------------------------------------------------
 *
 * Macro: FAIL_IF()
 *
 * Purpose:
 *
 *     Make tests more accessible and less cluttered than
 *         `if (thing == otherthing()) TEST_ERROR`
 *         paradigm.
 *
 *     The following lines are roughly equivalent:
 *
 *         `if (myfunc() < 0) TEST_ERROR;` (as seen elsewhere in HDF tests)
 *         `FAIL_IF(myfunc() < 0)`
 *
 *     Prints a generic "FAILED AT" line to stdout and jumps to `error`,
 *     similar to `TEST_ERROR` in h5test.h
 *
 * Programmer: Jacob Smith
 *             2017-10-23
 *
 *----------------------------------------------------------------------------
 */
#define FAIL_IF(condition)                                                                                   \
    if (condition) {                                                                                         \
        JSFAILED_AT()                                                                                        \
        goto error;                                                                                          \
    }

/*----------------------------------------------------------------------------
 *
 * Macro: FAIL_UNLESS()
 *
 * Purpose:
 *
 *     TEST_ERROR wrapper to reduce cognitive overhead from "negative tests",
 *     e.g., "a != b".
 *
 *     Opposite of FAIL_IF; fails if the given condition is _not_ true.
 *
 *     `FAIL_IF( 5 != my_op() )`
 *     is equivalent to
 *     `FAIL_UNLESS( 5 == my_op() )`
 *     However, `JSVERIFY(5, my_op(), "bad return")` may be even clearer.
 *         (see JSVERIFY)
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
#define FAIL_UNLESS(condition)                                                                               \
    if (!(condition)) {                                                                                      \
        JSFAILED_AT()                                                                                        \
        goto error;                                                                                          \
    }

/*----------------------------------------------------------------------------
 *
 * Macro: JSERR_LONG()
 *
 * Purpose:
 *
 *     Print an failure message for long-int arguments.
 *     ERROR-AT printed first.
 *     If `reason` is given, it is printed on own line and newlined after
 *     else, prints "expected/actual" aligned on own lines.
 *
 *     *FAILED* at myfile.c:488 in somefunc()...
 *     forest must be made of trees.
 *
 *     or
 *
 *     *FAILED* at myfile.c:488 in somefunc()...
 *       ! Expected 425
 *       ! Actual   3
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
static inline void
jserr_long(long expected, long actual, const char *reason)
{
    if (reason != NULL) {
        HDprintf("%s\n", reason);
    }
    else {
        HDprintf("  ! Expected %ld\n  ! Actual   %ld\n", expected, actual);
    }
}

#define JSERR_LONG(expected, actual, reason)                                                                 \
    {                                                                                                        \
        JSFAILED_AT()                                                                                        \
        jserr_long((long)(expected), (long)(actual), reason);                                                \
    }

/*----------------------------------------------------------------------------
 *
 * Macro: JSERR_STR()
 *
 * Purpose:
 *
 *     Print an failure message for string arguments.
 *     ERROR-AT printed first.
 *     If `reason` is given, it is printed on own line and newlined after
 *     else, prints "expected/actual" aligned on own lines.
 *
 *     *FAILED*  at myfile.c:421 in myfunc()...
 *     Blue and Red strings don't match!
 *
 *     or
 *
 *     *FAILED*  at myfile.c:421 in myfunc()...
 *     !!! Expected:
 *     this is my expected
 *     string
 *     !!! Actual:
 *     not what I expected at all
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
static inline void
jserr_str(const char *expected, const char *actual, const char *reason)
{
    if (reason != NULL) {
        HDprintf("%s\n", reason);
    }
    else {
        HDprintf("!!! Expected:\n%s\n!!!Actual:\n%s\n", expected, actual);
    }
}

#define JSERR_STR(expected, actual, reason)                                                                  \
    {                                                                                                        \
        JSFAILED_AT()                                                                                        \
        jserr_str((expected), (actual), (reason));                                                           \
    }

#ifdef JSVERIFY_EXP_ACT
/* VERIFY rountines with paramter order (<expected>, <actual> [, <msg> ])
 */

/*----------------------------------------------------------------------------
 *
 * Macro: JSVERIFY()
 *
 * Purpose:
 *
 *     Verify that two long integers are equal.
 *     If unequal, print failure message
 *     (with `reason`, if not NULL; expected/actual if NULL)
 *     and jump to `error` at end of function
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
#define JSVERIFY(expected, actual, reason)                                                                   \
    if ((long)(actual) != (long)(expected)) {                                                                \
        JSERR_LONG((expected), (actual), (reason))                                                           \
        goto error;                                                                                          \
    } /* JSVERIFY */

#if 0 /* UNUSED */

/*----------------------------------------------------------------------------
 *
 * Macro: JSVERIFY_NOT()
 *
 * Purpose:
 *
 *     Verify that two long integers are _not_ equal.
 *     If equal, print failure message
 *     (with `reason`, if not NULL; expected/actual if NULL)
 *     and jump to `error` at end of function
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
#define JSVERIFY_NOT(expected, actual, reason)                                                               \
    if ((long)(actual) == (long)(expected)) {                                                                \
        JSERR_LONG((expected), (actual), (reason))                                                           \
        goto error;                                                                                          \
    } /* JSVERIFY_NOT */
#endif /* JSVERIFY_NOT unused */

/*----------------------------------------------------------------------------
 *
 * Macro: JSVERIFY_STR()
 *
 * Purpose:
 *
 *     Verify that two strings are equal.
 *     If unequal, print failure message
 *     (with `reason`, if not NULL; expected/actual if NULL)
 *     and jump to `error` at end of function
 *
 * Programmer: Jacob Smith
 *             2017-10-24
 *
 *----------------------------------------------------------------------------
 */
#define JSVERIFY_STR(expected, actual, reason)                                                               \
    if (HDstrcmp((actual), (expected)) != 0) {                                                               \
        JSERR_STR((expected), (actual), (reason));                                                           \
        goto error;                                                                                          \
    } /* JSVERIFY_STR */

#else
/* JSVERIFY_EXP_ACT not defined
 *
 * Repeats macros above, but with actual/expected parameters reversed.
 */

/*----------------------------------------------------------------------------
 * Macro: JSVERIFY()
 * See: JSVERIFY documentation above.
 * Programmer: Jacob Smith
 *             2017-10-14
 *----------------------------------------------------------------------------
 */
#define JSVERIFY(actual, expected, reason)                                                                   \
    if ((long)(actual) != (long)(expected)) {                                                                \
        JSERR_LONG((expected), (actual), (reason));                                                          \
        goto error;                                                                                          \
    } /* JSVERIFY */

#if 0 /* UNUSED */

/*----------------------------------------------------------------------------
 * Macro: JSVERIFY_NOT()
 * See: JSVERIFY_NOT documentation above.
 * Programmer: Jacob Smith
 *             2017-10-14
 *----------------------------------------------------------------------------
 */
#define JSVERIFY_NOT(actual, expected, reason)                                                               \
    if ((long)(actual) == (long)(expected)) {                                                                \
        JSERR_LONG((expected), (actual), (reason))                                                           \
        goto error;                                                                                          \
    } /* JSVERIFY_NOT */
#endif /* JSVERIFY_NOT unused */

/*----------------------------------------------------------------------------
 * Macro: JSVERIFY_STR()
 * See: JSVERIFY_STR documentation above.
 * Programmer: Jacob Smith
 *             2017-10-14
 *----------------------------------------------------------------------------
 */
#define JSVERIFY_STR(actual, expected, reason)                                                               \
    if (HDstrcmp((actual), (expected)) != 0) {                                                               \
        JSERR_STR((expected), (actual), (reason));                                                           \
        goto error;                                                                                          \
    } /* JSVERIFY_STR */

#endif /* ifdef/else JSVERIFY_EXP_ACT */

#define S3_TEST_PROFILE_NAME "ros3_vfd_test"

#define S3_TEST_RESOURCE_TEXT_RESTRICTED "t8.shakespeare.txt"
#define S3_TEST_RESOURCE_TEXT_PUBLIC     "Poe_Raven.txt"
#define S3_TEST_RESOURCE_MISSING         "missing.csv"

#define S3_TEST_RUN_TIMEOUT  0   /* run tests that might hang */
#define S3_TEST_MAX_URL_SIZE 256 /* char array size */

/* Global variables for aws test profile.
 * An attempt is made to read ~/.aws/credentials and ~/.aws/config upon test
 * startup -- if unable to open either file or cannot load region, id, and key,
 * tests connecting with S3 will not be run
 */
static int     s3_test_credentials_loaded               = 0;
static char    s3_test_aws_region[16]                   = "";
static char    s3_test_aws_access_key_id[64]            = "";
static char    s3_test_aws_secret_access_key[128]       = "";
static char    s3_test_bucket_url[S3_TEST_MAX_URL_SIZE] = "";
static hbool_t s3_test_bucket_defined                   = FALSE;

/*---------------------------------------------------------------------------
 *
 * Function: test_macro_format_credential()
 *
 * Purpose:
 *
 *     Demonstrate that the macro `S3COMMS_FORMAT_CREDENTIAL`
 *     performs as expected.
 *
 * Programmer: Jacob Smith
 *             2017-09-19
 *
 *----------------------------------------------------------------------------
 */
static herr_t
test_macro_format_credential(void)
{
    /************************
     * test-local variables *
     ************************/

    char       dest[256];
    const char access[]   = "AKIAIOSFODNN7EXAMPLE";
    const char date[]     = "20130524";
    const char region[]   = "us-east-1";
    const char service[]  = "s3";
    const char expected[] = "AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request";

    TESTING("test_macro_format_credential");

    FAIL_IF(S3COMMS_MAX_CREDENTIAL_SIZE < S3COMMS_FORMAT_CREDENTIAL(dest, access, date, region, service))

    JSVERIFY_STR(expected, dest, NULL)

    PASSED();
    return 0;

error:
    return -1;

} /* end test_macro_format_credential() */

/*---------------------------------------------------------------------------
 *
 * Function: test_aws_canonical_request()
 *
 * Purpose:
 *
 *     Demonstrate the construction of a Canoncial Request (and Signed Headers)
 *
 *     Elided / not yet implemented:
 *         Query strings
 *         request "body"
 *
 * Programmer: Jacob Smith
 *             2017-10-04
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_aws_canonical_request(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct header {
        const char *name;
        const char *value;
    };

    struct testcase {
        const char *  exp_request;
        const char *  exp_headers;
        const char *  verb;
        const char *  resource;
        unsigned int  listsize;
        struct header list[5];
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            "GET\n/some/"
            "path.file\n\nhost:somebucket.someserver.somedomain\nrange:bytes=150-244\n\nhost;"
            "range\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "host;range",
            "GET",
            "/some/path.file",
            2,
            {
                {"Range", "bytes=150-244"},
                {"Host", "somebucket.someserver.somedomain"},
            },
        },
        {"HEAD\n/bucketpath/"
         "myfile.dat\n\nhost:place.domain\nx-amz-content-sha256:"
         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\nx-amz-date:"
         "19411207T150803Z\n\nhost;x-amz-content-sha256;x-amz-"
         "date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
         "host;x-amz-content-sha256;x-amz-date",
         "HEAD",
         "/bucketpath/myfile.dat",
         3,
         {
             {"x-amz-content-sha256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
             {"host", "place.domain"},
             {"x-amz-date", "19411207T150803Z"},
         }},
        {
            "PUT\n/\n\n\n\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "",
            "PUT",
            "/",
            0,
            {
                {"", ""},
            }, /* unused; satisfies compiler */
        },
    }; /* struct testcase cases[] */
    struct testcase *C = NULL;
    char             cr_dest[512];   /* canonical request */
    hrb_t *          hrb     = NULL; /* http request buffer object */
    unsigned int     i       = 0;    /* looping/indexing */
    unsigned int     j       = 0;    /* looping/indexing */
    hrb_node_t *     node    = NULL; /* http headers list pointer */
    unsigned int     n_cases = 3;
    char             sh_dest[64]; /* signed headers */

    TESTING("test_aws_canonical_request");

    for (i = 0; i < n_cases; i++) {
        /* pre-test bookkeeping
         */
        C = &cases[i];
        for (j = 0; j < 256; j++) {
            cr_dest[j] = 0;
        } /* zero request buffer */
        for (j = 0; j < 64; j++) {
            sh_dest[j] = 0;
        } /* zero headers buffer */

        /* create HTTP request object with given verb, resource/path
         */
        hrb = H5FD_s3comms_hrb_init_request(C->verb, C->resource, "HTTP/1.1");
        HDassert(hrb->body == NULL);

        /* Create headers list from test case input
         */
        for (j = 0; j < C->listsize; j++) {
            FAIL_IF(FAIL == H5FD_s3comms_hrb_node_set(&node, C->list[j].name, C->list[j].value));
        }

        hrb->first_header = node;

        /* test
         */
        JSVERIFY(SUCCEED, H5FD_s3comms_aws_canonical_request(cr_dest, 512, sh_dest, 64, hrb),
                 " unable to compose canonical request")
        JSVERIFY_STR(C->exp_headers, sh_dest, NULL)
        JSVERIFY_STR(C->exp_request, cr_dest, NULL)

        /* tear-down
         */
        while (node != NULL) {
            FAIL_IF(FAIL == H5FD_s3comms_hrb_node_set(&node, node->name, NULL));
        }
        HDassert(NULL == node);
        FAIL_IF(FAIL == H5FD_s3comms_hrb_destroy(&hrb));
        HDassert(NULL == hrb);

    } /* for each test case */

    /***************
     * ERROR CASES *
     ***************/

    /*  malformed hrb and/or node-list
     */
    JSVERIFY(FAIL, H5FD_s3comms_aws_canonical_request(cr_dest, 20, sh_dest, 20, NULL),
             "http request object cannot be null")

    hrb = H5FD_s3comms_hrb_init_request("GET", "/", "HTTP/1.1");
    JSVERIFY(FAIL, H5FD_s3comms_aws_canonical_request(NULL, 20, sh_dest, 20, hrb),
             "canonical request destination cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_aws_canonical_request(cr_dest, 20, NULL, 20, hrb),
             "signed headers destination cannot be null")

    FAIL_IF(FAIL == H5FD_s3comms_hrb_destroy(&hrb))
    HDassert(NULL == hrb);

    PASSED();
    return 0;

error:

    if (node != NULL) {
        while (node != NULL)
            (void)H5FD_s3comms_hrb_node_set(&node, node->name, NULL);
        HDassert(node == NULL);
    }
    if (hrb != NULL) {
        (void)H5FD_s3comms_hrb_destroy(&hrb);
    }

    return -1;

} /* end test_aws_canonical_request() */

/*---------------------------------------------------------------------------
 *
 * Function: test_bytes_to_hex
 *
 * Purpose:
 *
 *     Define and verify behavior of  `H5FD_s3comms_bytes_to_hex()`.
 *
 * Return:
 *
 *     Success:  0
 *     Failure: -1
 *
 * Programmer: Jacob Smith
 *             2017-09-14
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_bytes_to_hex(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char          exp[17]; /* in size * 2 + 1 for null terminator */
        const unsigned char in[8];
        size_t              size;
        hbool_t             lower;
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            "52F3000C9A",
            {82, 243, 0, 12, 154},
            5,
            FALSE,
        },
        {
            "009a0cf3005200", /* lowercase alphas */
            {0, 154, 12, 243, 0, 82, 0},
            7,
            TRUE,
        },
        {
            "", {17, 63, 26, 56}, 0, FALSE, /* irrelevant */
        },
    };
    int  i       = 0;
    int  n_cases = 3;
    char out[17];
    int  out_off = 0;

    TESTING("bytes-to-hex");

    for (i = 0; i < n_cases; i++) {
        for (out_off = 0; out_off < 17; out_off++) {
            out[out_off] = 0;
        }

        JSVERIFY(SUCCEED, H5FD_s3comms_bytes_to_hex(out, cases[i].in, cases[i].size, cases[i].lower), NULL)

        JSVERIFY_STR(cases[i].exp, out, NULL)
    }

    /* dest cannot be null
     */
    JSVERIFY(FAIL, H5FD_s3comms_bytes_to_hex(NULL, (const unsigned char *)"nada", 5, FALSE),
             "destination cannot be null")

    PASSED();
    return 0;

error:
    return -1;

} /* end test_bytes_to_hex() */

/*---------------------------------------------------------------------------
 *
 * Function: test_hrb_init_request()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_hrb_init_request()`
 *
 * Programmer: Jacob Smith
 *             2017-09-20
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_hrb_init_request(void)
{
    /*********************
     * test-local macros *
     *********************/

    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char  msg[64];
        const char *verb;
        const char *resource;
        const char *exp_res;
        const char *version;
        hbool_t     ret_null;
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            "get HTTP request just as we provided",
            "GET",
            "/path/to/some/file",
            "/path/to/some/file",
            "HTTP/1.1",
            FALSE,
        },
        {
            "null verb substitues to GET",
            NULL,
            "/MYPATH/MYFILE.tiff",
            "/MYPATH/MYFILE.tiff",
            "HTTP/1.1",
            FALSE,
        },
        {
            "demonstrate non-GET verb",
            "HEAD",
            "/MYPATH/MYFILE.tiff",
            "/MYPATH/MYFILE.tiff",
            "HTTP/1.1",
            FALSE,
        },
        {
            "slash prepended to resource path, if necessary",
            NULL,
            "MYPATH/MYFILE.tiff",
            "/MYPATH/MYFILE.tiff",
            NULL,
            FALSE,
        },
        {
            "null resource path causes problem",
            "GET",
            NULL,
            NULL,
            NULL,
            TRUE,
        },
    };
    struct testcase *C      = NULL;
    unsigned int     i      = 0;
    unsigned int     ncases = 5;
    hrb_t *          req    = NULL;

    TESTING("hrb_init_request");

    for (i = 0; i < ncases; i++) {
        C   = &cases[i];
        req = H5FD_s3comms_hrb_init_request(C->verb, C->resource, C->version);
        if (cases[i].ret_null == TRUE) {
            FAIL_IF(req != NULL);
        }
        else {
            FAIL_IF(req == NULL);
            JSVERIFY(S3COMMS_HRB_MAGIC, req->magic, NULL)
            if (C->verb == NULL) {
                JSVERIFY_STR("GET", req->verb, NULL)
            }
            else {
                JSVERIFY_STR(req->verb, C->verb, NULL)
            }
            JSVERIFY_STR("HTTP/1.1", req->version, NULL)
            JSVERIFY_STR(C->exp_res, req->resource, NULL)
            FAIL_IF(req->first_header != NULL);
            FAIL_IF(req->body != NULL);
            JSVERIFY(0, req->body_len, NULL)
            JSVERIFY(SUCCEED, H5FD_s3comms_hrb_destroy(&req), "unable to destroy hrb_t")
            FAIL_IF(NULL != req); /* should annull pointer as well as free */
        }

    } /* end for each testcase */

    PASSED();
    return 0;

error:
    (void)H5FD_s3comms_hrb_destroy(&req);

    return -1;

} /* end test_hrb_init_request() */

/*---------------------------------------------------------------------------
 *
 * Function: test_hrb_node_set()
 *
 * Purpose:
 *
 *     Test operations on hrb_node_t structure
 *
 * Programmer: Jacob Smith
 *             2017-09-22
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_hrb_node_set(void)
{
    /*************************
     * test-local structures *
     *************************/

    /* bundle of name/value representing an hrb_node_t
     */
    typedef struct node_mock_t {
        const char *name;
        const char *value;
    } node_mock_t;

    /* bundle for a testcase
     *
     * `message`
     *     purpose of the testcase
     *
     * `delta`
     *     container for name and value strings to pass into node-set function
     *     to to modify the list.
     *
     * `returned`
     *     expected return value of node-set function
     *
     * `given`
     * `expected`
     *     string arrays representing the state of the list before and after
     *     modification. The number of strings must be even, with each name
     *     paired to a value. `NULL` terminates the list, with `{NULL}`
     *     representing the empty list.
     */
    typedef struct testcase {
        const char *message;
        node_mock_t delta;
        herr_t      returned;
        const char *given[11]; /* name/value pairs in array; NULL sentinel */
        const char *expected[11];
    } testcase;

    /************************
     * test-local variables *
     ************************/

    testcase cases[] = {
        {
            "cannot remove node from null list",
            {"Host", NULL},
            FAIL,
            {NULL},
            {NULL},
        },
        {
            "cannot create list with NULL field name",
            {NULL, "somevalue"},
            FAIL,
            {NULL},
            {NULL},
        },
        {
            "create a new list",
            {"Host", "somevalue"},
            SUCCEED,
            {NULL},
            {
                "Host",
                "somevalue",
                NULL,
            },
        },
        {
            "insert new node at head list",
            {"Host", "somevalue"},
            SUCCEED,
            {
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Host",
                "somevalue",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
        {
            "append new node at list end",
            {"x-amz-date", "somevalue"},
            SUCCEED,
            {
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Range",
                "bytes=20-40",
                "x-amz-date",
                "somevalue",
                NULL,
            },
        },
        {
            "insert new node inside list",
            {"Intermediary", "somevalue"},
            SUCCEED,
            {
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Host",
                "somehost",
                "Intermediary",
                "somevalue",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
        {
            "modify node",
            {"Range", "bytes=40-80"},
            SUCCEED,
            {
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Host",
                "somehost",
                "Range",
                "bytes=40-80",
                NULL,
            },
        },
        {
            "modify node with new case",
            {"RANGE", "bytes=40-80"},
            SUCCEED,
            {
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Host",
                "somehost",
                "RANGE",
                "bytes=40-80",
                NULL,
            },
        },
        {
            "cannot add node with no name",
            {NULL, "bytes=40-80"},
            FAIL,
            {
                "Host",
                "somehost",
                NULL,
            },
            {
                "Host",
                "somehost",
                NULL,
            },
        },
        {
            "add node with 'empty' name",
            {"", "bytes=40-80"},
            SUCCEED,
            {
                "Host",
                "somehost",
                NULL,
            },
            {
                "",
                "bytes=40-80",
                "Host",
                "somehost",
                NULL,
            },
        },
        {
            "remove node from end of list",
            {"Host", NULL},
            SUCCEED,
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Host",
                "somehost",
                NULL,
            },
            {
                "Date",
                "Thr, 25 Jan 2018",
                NULL,
            },
        },
        {
            "remove node from middle of list",
            {"Host", NULL},
            SUCCEED,
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
        {
            "remove node from start of list",
            {"Date", NULL},
            SUCCEED,
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
        {
            "remove only node in list",
            {"Date", NULL},
            SUCCEED,
            {
                "Date",
                "Thr, 25 Jan 2018",
                NULL,
            },
            {
                NULL,
            },
        },
        {
            "attempt to remove absent node fails",
            {"Host", NULL},
            FAIL,
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
        {
            "removal is case-insensitive",
            {"hOsT", NULL},
            SUCCEED,
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Host",
                "somehost",
                "Range",
                "bytes=20-40",
                NULL,
            },
            {
                "Date",
                "Thr, 25 Jan 2018",
                "Range",
                "bytes=20-40",
                NULL,
            },
        },
    };
    unsigned    testcases_count = 16;
    unsigned    test_i          = 0;
    hrb_node_t *list            = NULL;

    TESTING("hrb_node_t (test_hrb_node_set)");

    for (test_i = 0; test_i < testcases_count; test_i++) {
        const hrb_node_t *node   = NULL;
        const testcase *  test   = &(cases[test_i]);
        unsigned          mock_i = 0;

        /*********
         * SETUP *
         *********/

        for (mock_i = 0; test->given[mock_i] != NULL; mock_i += 2) {
            const char *name = test->given[mock_i];
            const char *valu = test->given[mock_i + 1];

            FAIL_IF(SUCCEED != H5FD_s3comms_hrb_node_set(&list, name, valu))
        }
        /********
         * TEST *
         ********/

        /* perform modification on list
         */
        JSVERIFY(test->returned, H5FD_s3comms_hrb_node_set(&list, test->delta.name, test->delta.value),
                 test->message)

        /* verify resulting list
         */
        node   = list;
        mock_i = 0;
        while (test->expected[mock_i] != NULL && node != NULL) {
            const char *name = test->expected[mock_i];
            const char *valu = test->expected[mock_i + 1];

            JSVERIFY_STR(name, node->name, NULL)
            JSVERIFY_STR(valu, node->value, NULL)

            mock_i += 2;
            node = node->next;
        }
        FAIL_IF(test->expected[mock_i] != NULL)
        FAIL_IF(node != NULL)

        /************
         * TEARDOWN *
         ************/

        while (list != NULL) {
            FAIL_IF(SUCCEED != H5FD_s3comms_hrb_node_set(&list, list->name, NULL))
        }
    } /* end for each testcase */

    PASSED();
    return 0;

error:
    while (list != NULL) {
        (void)H5FD_s3comms_hrb_node_set(&list, list->name, NULL);
    }

    return -1;

} /* end test_hrb_node_t() */

/*---------------------------------------------------------------------------
 *
 * Function: test_HMAC_SHA256()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_HMAC_SHA256()`
 *
 * Programmer: Jacob Smith
 *             2017-09-19
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_HMAC_SHA256(void)
{

    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        herr_t              ret; /* SUCCEED/FAIL expected from call */
        const unsigned char key[SHA256_DIGEST_LENGTH];
        size_t              key_len;
        const char *        msg;
        size_t              msg_len;
        const char *        exp;       /* not used if ret == FAIL */
        size_t              dest_size; /* if 0, `dest` is not malloc'd */
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            SUCCEED,
            {
                0xdb, 0xb8, 0x93, 0xac, 0xc0, 0x10, 0x96, 0x49, 0x18, 0xf1, 0xfd,
                0x43, 0x3a, 0xdd, 0x87, 0xc7, 0x0e, 0x8b, 0x0d, 0xb6, 0xbe, 0x30,
                0xc1, 0xfb, 0xea, 0xfe, 0xfa, 0x5e, 0xc6, 0xba, 0x83, 0x78,
            },
            SHA256_DIGEST_LENGTH,
            "AWS4-HMAC-SHA256\n20130524T000000Z\n20130524/us-east-1/s3/"
            "aws4_request\n7344ae5b7ee6c3e7e6b0fe0640412a37625d1fbfff95c48bbb2dc43964946972",
            HDstrlen("AWS4-HMAC-SHA256\n20130524T000000Z\n20130524/us-east-1/s3/"
                     "aws4_request\n7344ae5b7ee6c3e7e6b0fe0640412a37625d1fbfff95c48bbb2dc43964946972"),
            "f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41",
            SHA256_DIGEST_LENGTH * 2 + 1, /* +1 for null terminator */
        },
        {
            SUCCEED,
            {'J', 'e', 'f', 'e'},
            4,
            "what do ya want for nothing?",
            28,
            "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
            SHA256_DIGEST_LENGTH * 2 + 1,
        },
        {
            FAIL, "DOESN'T MATTER", 14, "ALSO IRRELEVANT", 15, NULL,
            0, /* dest -> null, resulting in immediate error */
        },
    };
    char *dest    = NULL;
    int   i       = 0;
    int   n_cases = 3;

    TESTING("HMAC_SHA256");

    for (i = 0; i < n_cases; i++) {
        if (cases[i].dest_size == 0) {
            dest = NULL;
        }
        else {
            dest = (char *)HDmalloc(sizeof(char) * cases[i].dest_size);
            HDassert(dest != NULL);
        }

        JSVERIFY(
            cases[i].ret,
            H5FD_s3comms_HMAC_SHA256(cases[i].key, cases[i].key_len, cases[i].msg, cases[i].msg_len, dest),
            cases[i].msg);
        if (cases[i].ret == SUCCEED) {
#ifdef VERBOSE
            if (0 != HDstrncmp(cases[i].exp, dest, HDstrlen(cases[i].exp))) {
                /* print out how wrong things are, and then fail
                 */
                dest = (char *)HDrealloc(dest, cases[i].dest_size + 1);
                HDassert(dest != NULL);
                dest[cases[i].dest_size] = 0;
                HDfprintf(stdout, "ERROR:\n!!! \"%s\"\n != \"%s\"\n", cases[i].exp, dest);
                TEST_ERROR;
            }
#else /* VERBOSE not defined */
            /* simple pass/fail test
             */
            JSVERIFY(0, HDstrncmp(cases[i].exp, dest, HDstrlen(cases[i].exp)), NULL);
#endif /* VERBOSE */
        }
        HDfree(dest);
    }

    PASSED();
    return 0;

error:
    HDfree(dest);
    return -1;

} /* end test_HMAC_SHA256() */

/*----------------------------------------------------------------------------
 *
 * Function: test_nlowercase()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_nlowercase()`
 *
 * Programmer: Jacob Smith
 *             2017-19-18
 *
 *----------------------------------------------------------------------------
 */
static herr_t
test_nlowercase(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char *in;
        size_t      len;
        const char *exp;
    };

    /************************
     * test-local variables *
     ************************/

    /* any character after in exp on or after exp[len] is undefined.
     * in this test, kept as the null character for simplicity.
     */
    struct testcase cases[] = {
        {
            "HALlEluJAh",
            6,
            "hallel",
        },
        {
            "all\0 lower",
            10,
            "all\0 lower",
        },
        {
            "to meeeeeee",
            0,
            "",
        },
    };
    char *dest    = NULL;
    int   i       = 0;
    int   n_cases = 3;

    TESTING("nlowercase");

    for (i = 0; i < n_cases; i++) {
        dest = (char *)HDmalloc(sizeof(char) * 16);

        JSVERIFY(SUCCEED, H5FD_s3comms_nlowercase(dest, cases[i].in, cases[i].len), cases[i].in)
        if (cases[i].len > 0) {
            JSVERIFY(0, HDstrncmp(dest, cases[i].exp, cases[i].len), NULL)
        }
        HDfree(dest);
    } /* end for each testcase */

    JSVERIFY(FAIL, H5FD_s3comms_nlowercase(NULL, cases[0].in, cases[0].len), "null distination should fail")

    PASSED();
    return 0;

error:
    HDfree(dest);
    return -1;

} /* end test_nlowercase() */

/*---------------------------------------------------------------------------
 *
 * Function: test_parse_url()
 *
 * Programmer: Jacob Smith
 *             2017-11-??
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_parse_url(void)
{
    /*********************
     * test-local macros *
     *********************/

    /*************************
     * test-local structures *
     *************************/

    typedef struct {
        const char *scheme;
        const char *host;
        const char *port;
        const char *path;
        const char *query;
    } const_purl_t;

    struct testcase {
        const char *url;
        herr_t      exp_ret; /* expected return;              */
                             /* if FAIL, `expected` is unused */
        const_purl_t expected;
        const char * msg;
    };

    /************************
     * test-local variables *
     ************************/

    parsed_url_t *  purl    = NULL;
    unsigned int    i       = 0;
    unsigned int    ncases  = 15;
    struct testcase cases[] = {
        {
            NULL,
            FAIL,
            {NULL, NULL, NULL, NULL, NULL},
            "null url",
        },
        {
            "",
            FAIL,
            {NULL, NULL, NULL, NULL, NULL},
            "empty url",
        },
        {
            "ftp://[1000:4000:0002:2010]",
            SUCCEED,
            {
                "ftp",
                "[1000:4000:0002:2010]",
                NULL,
                NULL,
                NULL,
            },
            "IPv6 ftp and empty path (root)",
        },
        {
            "ftp://[1000:4000:0002:2010]:2040",
            SUCCEED,
            {
                "ftp",
                "[1000:4000:0002:2010]",
                "2040",
                NULL,
                NULL,
            },
            "root IPv6 ftp with port",
        },
        {
            "http://some.domain.org:9000/path/to/resource.txt",
            SUCCEED,
            {
                "http",
                "some.domain.org",
                "9000",
                "path/to/resource.txt",
                NULL,
            },
            "without query",
        },
        {
            "https://domain.me:00/file.txt?some_params unchecked",
            SUCCEED,
            {
                "https",
                "domain.me",
                "00",
                "file.txt",
                "some_params unchecked",
            },
            "with query",
        },
        {
            "ftp://domain.com/",
            SUCCEED,
            {
                "ftp",
                "domain.com",
                NULL,
                NULL,
                NULL,
            },
            "explicit root w/out port",
        },
        {
            "ftp://domain.com:1234/",
            SUCCEED,
            {
                "ftp",
                "domain.com",
                "1234",
                NULL,
                NULL,
            },
            "explicit root with port",
        },
        {
            "ftp://domain.com:1234/file?",
            FAIL,
            {
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
            },
            "empty query is invalid",
        },
        {
            "ftp://:1234/file",
            FAIL,
            {
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
            },
            "no host",
        },
        {
            "h&r block",
            FAIL,
            {
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
            },
            "no scheme (bad URL)",
        },
        {
            "http://domain.com?a=b&d=b",
            SUCCEED,
            {
                "http",
                "domain.com",
                NULL,
                NULL,
                "a=b&d=b",
            },
            "QUERY with implict PATH",
        },
        {
            "http://[5]/path?a=b&d=b",
            SUCCEED,
            {
                "http",
                "[5]",
                NULL,
                "path",
                "a=b&d=b",
            },
            "IPv6 extraction is really dumb",
        },
        {
            "http://[1234:5678:0910:1112]:port/path",
            FAIL,
            {
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
            },
            "non-decimal PORT (port)",
        },
        {
            "http://mydomain.com:01a3/path",
            FAIL,
            {
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
            },
            "non-decimal PORT (01a3)",
        },
    };

    TESTING("url-parsing functionality");

    /*********
     * TESTS *
     *********/

    for (i = 0; i < ncases; i++) {
        HDassert(purl == NULL);

        JSVERIFY(cases[i].exp_ret, H5FD_s3comms_parse_url(cases[i].url, &purl), cases[i].msg)

        if (cases[i].exp_ret == FAIL) {
            /* on FAIL, `purl` should be untouched--remains NULL */
            FAIL_UNLESS(purl == NULL)
        }
        else {
            /* on SUCCEED, `purl` should be set */
            FAIL_IF(purl == NULL)

            if (cases[i].expected.scheme != NULL) {
                FAIL_IF(NULL == purl->scheme)
                JSVERIFY_STR(cases[i].expected.scheme, purl->scheme, cases[i].msg)
            }
            else {
                FAIL_UNLESS(NULL == purl->scheme)
            }

            if (cases[i].expected.host != NULL) {
                FAIL_IF(NULL == purl->host)
                JSVERIFY_STR(cases[i].expected.host, purl->host, cases[i].msg)
            }
            else {
                FAIL_UNLESS(NULL == purl->host)
            }

            if (cases[i].expected.port != NULL) {
                FAIL_IF(NULL == purl->port)
                JSVERIFY_STR(cases[i].expected.port, purl->port, cases[i].msg)
            }
            else {
                FAIL_UNLESS(NULL == purl->port)
            }

            if (cases[i].expected.path != NULL) {
                FAIL_IF(NULL == purl->path)
                JSVERIFY_STR(cases[i].expected.path, purl->path, cases[i].msg)
            }
            else {
                FAIL_UNLESS(NULL == purl->path)
            }

            if (cases[i].expected.query != NULL) {
                FAIL_IF(NULL == purl->query)
                JSVERIFY_STR(cases[i].expected.query, purl->query, cases[i].msg)
            }
            else {
                FAIL_UNLESS(NULL == purl->query)
            }
        } /* end if parse-url return SUCCEED/FAIL */

        /* per-test cleanup
         * well-behaved, even if `purl` is NULL
         */
        FAIL_IF(FAIL == H5FD_s3comms_free_purl(purl))
        purl = NULL;

    } /* end for each testcase */

    PASSED();
    return 0;

error:
    /***********
     * cleanup *
     ***********/
    (void)H5FD_s3comms_free_purl(purl);

    return -1;

} /* end test_parse_url() */

/*---------------------------------------------------------------------------
 *
 * Function: test_percent_encode_char()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_percent_encode_char()`
 *
 * Return:
 *
 *     Success:  0
 *     Failure: -1
 *
 * Programmer: Jacob Smith
 *             2017-09-14
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_percent_encode_char(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char  c;
        const char *exp;
        size_t      exp_len;
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {'$', "%24", 3},        /* u+0024 dollar sign */
        {' ', "%20", 3},        /* u+0020 space */
        {'^', "%5E", 3},        /* u+0094 carat */
        {'/', "%2F", 3},        /* u+002f solidus (forward slash) */
        /* {??, "%C5%8C", 6},*/ /* u+014c Latin Capital Letter O with Macron */
        /* Not included because it is multibyte "wide" character that poses  */
        /* issues both in the underlying function and in being written in    */
        /* this file.                                                        */
        /* {'¢', "%C2%A2", 6}, */ /* u+00a2 cent sign */
        /* above works, but complains about wide character overflow      */
        /* Elide for now, until it is determined (a) unnecessary or      */
        /* (b) requiring signature change to accommodate wide characters */
        {'\0', "%00", 3}, /* u+0000 null */
    };
    char   dest[13];
    size_t dest_len = 0;
    int    i        = 0;
    int    n_cases  = 5;

    TESTING("percent encode characters");

    for (i = 0; i < n_cases; i++) {
        JSVERIFY(SUCCEED, H5FD_s3comms_percent_encode_char(dest, (const unsigned char)cases[i].c, &dest_len),
                 NULL)
        JSVERIFY(cases[i].exp_len, dest_len, NULL)
        JSVERIFY(0, HDstrncmp(dest, cases[i].exp, dest_len), NULL)
        JSVERIFY_STR(cases[i].exp, dest, NULL)
    }

    JSVERIFY(FAIL, H5FD_s3comms_percent_encode_char(NULL, (const unsigned char)'^', &dest_len), NULL)

    PASSED();
    return 0;

error:
    return -1;
} /* end test_percent_encode_char() */

/*---------------------------------------------------------------------------
 * Function: test_s3r_open()
 *
 * Programmer: Jacob Smith 2018-01-24
 *
 * Changes: None
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_s3r_get_filesize(void)
{

    /************************
     * test-local variables *
     ************************/

    char   url_raven[S3_TEST_MAX_URL_SIZE];
    s3r_t *handle = NULL;

    TESTING("s3r_get_filesize");

    /* setup -- compose url to target resource
     */
    if (FALSE == s3_test_bucket_defined) {
        SKIPPED();
        HDputs("    environment variable HDF5_ROS3_TEST_BUCKET_URL not defined");
        HDfflush(stdout);
        return 0;
    }

    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_raven, S3_TEST_MAX_URL_SIZE, "%s/%s", s3_test_bucket_url,
                                              S3_TEST_RESOURCE_TEXT_PUBLIC));

    JSVERIFY(0, H5FD_s3comms_s3r_get_filesize(NULL), "filesize of the null handle should be 0")

    handle = H5FD_s3comms_s3r_open(url_raven, NULL, NULL, NULL);
    FAIL_IF(handle == NULL)

    JSVERIFY(6464, H5FD_s3comms_s3r_get_filesize(handle), NULL)

    FAIL_IF(SUCCEED != H5FD_s3comms_s3r_close(handle))

    PASSED();
    return 0;

error:
    if (handle != NULL)
        (void)H5FD_s3comms_s3r_close(handle);

    return -1;

} /* end test_s3r_get_filesize() */

/*---------------------------------------------------------------------------
 * Function: test_s3r_open()
 *
 * Programmer: Jacob Smith 2018-01-??
 *
 * Changes: None
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_s3r_open(void)
{

    /************************
     * test-local variables *
     ************************/

    char          url_missing[S3_TEST_MAX_URL_SIZE];
    char          url_raven[S3_TEST_MAX_URL_SIZE];
    char          url_raven_badport[S3_TEST_MAX_URL_SIZE];
    char          url_shakespeare[S3_TEST_MAX_URL_SIZE];
    unsigned char signing_key[SHA256_DIGEST_LENGTH];
    struct tm *   now = NULL;
    char          iso8601now[ISO8601_SIZE];
    s3r_t *       handle     = NULL;
    hbool_t       curl_ready = FALSE;
    parsed_url_t *purl       = NULL;

    TESTING("s3r_open");

    if (s3_test_credentials_loaded == 0) {
        SKIPPED();
        HDputs("    s3 credentials are not loaded");
        HDfflush(stdout);
        return 0;
    }
    if (FALSE == s3_test_bucket_defined) {
        SKIPPED();
        HDputs("    environment variable HDF5_ROS3_TEST_BUCKET_URL not defined");
        HDfflush(stdout);
        return 0;
    }

    /******************
     * PRE-TEST SETUP *
     ******************/

    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_shakespeare, S3_TEST_MAX_URL_SIZE, "%s/%s",
                                              s3_test_bucket_url, S3_TEST_RESOURCE_TEXT_RESTRICTED));

    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_missing, S3_TEST_MAX_URL_SIZE, "%s/%s", s3_test_bucket_url,
                                              S3_TEST_RESOURCE_MISSING));

    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_raven, S3_TEST_MAX_URL_SIZE, "%s/%s", s3_test_bucket_url,
                                              S3_TEST_RESOURCE_TEXT_PUBLIC));

    /* Set given bucket url with invalid/inactive port number for badport.
     * Note, this sort of micro-management of parsed_url_t is not advised
     */
    FAIL_IF(FAIL == H5FD_s3comms_parse_url(s3_test_bucket_url, &purl))
    if (purl->port == NULL) {
        purl->port = (char *)H5MM_malloc(sizeof(char) * 5);
        FAIL_IF(purl->port == NULL);
        FAIL_IF(5 < HDsnprintf(purl->port, 5, "9000"))
    }
    else if (HDstrcmp(purl->port, "9000") != 0) {
        FAIL_IF(5 < HDsnprintf(purl->port, 5, "9000"))
    }
    else {
        FAIL_IF(5 < HDsnprintf(purl->port, 5, "1234"))
    }
    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_raven_badport, S3_TEST_MAX_URL_SIZE, "%s://%s:%s/%s",
                                              purl->scheme, purl->host, purl->port,
                                              S3_TEST_RESOURCE_TEXT_PUBLIC));

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl_ready = TRUE;

    now = gmnow();
    FAIL_IF(now == NULL)
    FAIL_IF(ISO8601NOW(iso8601now, now) != (ISO8601_SIZE - 1));

    /* It is desired to have means available to verify that signing_key
     * was set successfully and to an expected value.
     */
    FAIL_IF(FAIL == H5FD_s3comms_signing_key(signing_key, (const char *)s3_test_aws_secret_access_key,
                                             (const char *)s3_test_aws_region, (const char *)iso8601now));

    /*************************
     * OPEN NONEXISTENT FILE *
     *************************/

    /* attempt anonymously
     */
    handle = H5FD_s3comms_s3r_open(url_missing, NULL, NULL, NULL);
    FAIL_IF(handle != NULL);

    /* attempt with authentication
     */
    handle =
        H5FD_s3comms_s3r_open(url_missing, (const char *)s3_test_aws_region,
                              (const char *)s3_test_aws_access_key_id, (const unsigned char *)signing_key);
    FAIL_IF(handle != NULL);

    /*************************
     * INACTIVE PORT ON HOST *
     *************************/

#if S3_TEST_RUN_TIMEOUT
    HDprintf("Opening on inactive port may hang for a minute; waiting for timeout\n");
    handle = H5FD_s3comms_s3r_open(url_raven_badport, NULL, NULL, NULL);
    FAIL_IF(handle != NULL);
#endif

    /*******************************
     * INVALID AUTHENTICATION INFO *
     *******************************/

    /* anonymous access on restricted file
     */
    handle = H5FD_s3comms_s3r_open(url_shakespeare, NULL, NULL, NULL);
    FAIL_IF(handle != NULL);

    /* passed in a bad ID
     */
    handle = H5FD_s3comms_s3r_open(url_shakespeare, (const char *)s3_test_aws_region, "I_MADE_UP_MY_ID",
                                   (const unsigned char *)signing_key);
    FAIL_IF(handle != NULL);

    /* using an invalid signing key
     */
    handle =
        H5FD_s3comms_s3r_open(url_shakespeare, (const char *)s3_test_aws_region,
                              (const char *)s3_test_aws_access_key_id, (const unsigned char *)EMPTY_SHA256);
    FAIL_IF(handle != NULL);

    /*******************************
     * SUCCESSFUL OPEN (AND CLOSE) *
     *******************************/

    /* anonymous
     */
    handle = H5FD_s3comms_s3r_open(url_raven, NULL, NULL, NULL);
    FAIL_IF(handle == NULL);
    JSVERIFY(6464, H5FD_s3comms_s3r_get_filesize(handle), "did not get expected filesize")
    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_close(handle), "unable to close file")
    handle = NULL;

    /* using authentication on anonymously-accessible file?
     */
    handle =
        H5FD_s3comms_s3r_open(url_raven, (const char *)s3_test_aws_region,
                              (const char *)s3_test_aws_access_key_id, (const unsigned char *)signing_key);
    FAIL_IF(handle == NULL);
    JSVERIFY(6464, H5FD_s3comms_s3r_get_filesize(handle), NULL)
    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_close(handle), "unable to close file")
    handle = NULL;

    /* authenticating
     */
    handle =
        H5FD_s3comms_s3r_open(url_shakespeare, (const char *)s3_test_aws_region,
                              (const char *)s3_test_aws_access_key_id, (const unsigned char *)signing_key);
    FAIL_IF(handle == NULL);
    JSVERIFY(5458199, H5FD_s3comms_s3r_get_filesize(handle), NULL)
    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_close(handle), "unable to close file")
    handle = NULL;

    curl_global_cleanup();
    curl_ready = FALSE;

    FAIL_IF(FAIL == H5FD_s3comms_free_purl(purl))
    purl = NULL;

    PASSED();
    return 0;
error:
    /***********
     * cleanup *
     ***********/

    if (handle != NULL)
        H5FD_s3comms_s3r_close(handle);
    if (purl != NULL)
        H5FD_s3comms_free_purl(purl);
    if (curl_ready == TRUE)
        curl_global_cleanup();

    return -1;

} /* end test_s3r_open() */

/*---------------------------------------------------------------------------
 *
 * Function: test_s3r_read()
 *
 * Purpose:
 *
 *     Specify and demonstrate the use and life cycle of an S3 Request handle
 *     `s3r_t`, through its related functions.
 *
 *     H5FD_s3comms_s3r_open
 *     H5FD_s3comms_s3r_getsize << called by open() _only_
 *     H5FD_s3comms_s3r_read    << called by getsize(), multiple times working
 *     H5FD_s3comms_s3r_close
 *
 *     Shows most basic curl interation.
 *
 * Programmer: Jacob Smith
 *             2017-10-06
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_s3r_read(void)
{

#define S3COMMS_TEST_BUFFER_SIZE 256

    /************************
     * test-local variables *
     ************************/

    char         url_raven[S3_TEST_MAX_URL_SIZE];
    char         buffer[S3COMMS_TEST_BUFFER_SIZE];
    s3r_t *      handle     = NULL;
    hbool_t      curl_ready = FALSE;
    unsigned int i          = 0;

    TESTING("test_s3r_read");

    /*
     * initial setup
     */
    if (FALSE == s3_test_bucket_defined) {
        SKIPPED();
        HDputs("    environment variable HDF5_ROS3_TEST_BUCKET_URL not defined");
        HDfflush(stdout);
        return 0;
    }

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl_ready = TRUE;
    FAIL_IF(S3_TEST_MAX_URL_SIZE < HDsnprintf(url_raven, S3_TEST_MAX_URL_SIZE, "%s/%s", s3_test_bucket_url,
                                              S3_TEST_RESOURCE_TEXT_PUBLIC));

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /* open file
     */
    handle = H5FD_s3comms_s3r_open(url_raven, NULL, NULL, NULL);
    FAIL_IF(handle == NULL)
    JSVERIFY(6464, H5FD_s3comms_s3r_get_filesize(handle), NULL)

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /**********************
     * read start of file *
     **********************/

    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_read(handle, (haddr_t)0, (size_t)118, buffer), NULL)
    JSVERIFY_STR("Once upon a midnight dreary, while I pondered, weak and weary,\n"
                 "Over many a quaint and curious volume of forgotten lore",
                 buffer, NULL)

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /************************
     * read arbitrary range *
     ************************/

    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_read(handle, (haddr_t)2540, (size_t)54, buffer), NULL)
    JSVERIFY_STR("the grave and stern decorum of the countenance it wore", buffer, NULL)

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /**********************
     * read one character *
     **********************/

    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_read(handle, (haddr_t)2540, (size_t)1, buffer), NULL)
    JSVERIFY_STR("t", buffer, NULL)

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /***************
     * read to EoF *
     ***************/

    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_read(handle, (haddr_t)6370, (size_t)0, buffer), NULL)
    JSVERIFY(
        0,
        HDstrncmp(
            buffer,
            "And my soul from out that shadow that lies floating on the floor\nShall be lifted—nevermore!\n",
            94),
        buffer)

    for (i = 0; i < S3COMMS_TEST_BUFFER_SIZE; i++)
        buffer[i] = '\0';

    /*****************
     * read past eof *
     *****************/

    JSVERIFY(FAIL,
             H5FD_s3comms_s3r_read(handle, (haddr_t)6400, (size_t)100, /* 6400+100 > 6464 */
                                   buffer),
             NULL)
    JSVERIFY(0, HDstrcmp("", buffer), NULL)

    /************************
     * read starts past eof *
     ************************/

    JSVERIFY(FAIL,
             H5FD_s3comms_s3r_read(handle, (haddr_t)1200699, /* 1200699 > 6464 */
                                   (size_t)100, buffer),
             NULL)
    JSVERIFY(0, HDstrcmp("", buffer), NULL)

    /**********************
     * read starts on eof *
     **********************/

    JSVERIFY(FAIL, H5FD_s3comms_s3r_read(handle, (haddr_t)6464, (size_t)0, buffer), NULL)
    JSVERIFY(0, HDstrcmp("", buffer), NULL)

    /*************
     * TEAR DOWN *
     *************/

    JSVERIFY(SUCCEED, H5FD_s3comms_s3r_close(handle), "unable to close file")
    handle = NULL;

    curl_global_cleanup();
    curl_ready = FALSE;

    PASSED();
    return 0;

error:
    /***********
     * cleanup *
     ***********/

    if (handle != NULL)
        H5FD_s3comms_s3r_close(handle);

    if (curl_ready == TRUE)
        curl_global_cleanup();

    return -1;

#undef S3COMMS_TEST_BUFFER_SIZE

} /* end test_s3r_read() */

/*---------------------------------------------------------------------------
 *
 * Function: test_signing_key()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_signing_key()`
 *
 *     More test cases would be a very good idea.
 *
 * Programmer: Jacob Smith
 *             2017-09-18
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_signing_key(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char *  region;
        const char *  secret_key;
        const char *  when;
        unsigned char exp[SHA256_DIGEST_LENGTH];
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            "us-east-1",
            "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
            "20130524T000000Z",
            {
                0xdb, 0xb8, 0x93, 0xac, 0xc0, 0x10, 0x96, 0x49, 0x18, 0xf1, 0xfd,
                0x43, 0x3a, 0xdd, 0x87, 0xc7, 0x0e, 0x8b, 0x0d, 0xb6, 0xbe, 0x30,
                0xc1, 0xfb, 0xea, 0xfe, 0xfa, 0x5e, 0xc6, 0xba, 0x83, 0x78,
            },
        },
    };
    int            i      = 0;
    unsigned char *key    = NULL;
    int            ncases = 1;

    TESTING("signing_key");

    for (i = 0; i < ncases; i++) {
        key = (unsigned char *)HDmalloc(sizeof(unsigned char) * SHA256_DIGEST_LENGTH);
        HDassert(key != NULL);

        JSVERIFY(SUCCEED, H5FD_s3comms_signing_key(key, cases[i].secret_key, cases[i].region, cases[i].when),
                 NULL)

        JSVERIFY(0, HDstrncmp((const char *)cases[i].exp, (const char *)key, SHA256_DIGEST_LENGTH),
                 (const char *)cases[i].exp)

        HDfree(key);
        key = NULL;
    }

    /***************
     * ERROR CASES *
     ***************/

    key = (unsigned char *)HDmalloc(sizeof(unsigned char) * SHA256_DIGEST_LENGTH);
    HDassert(key != NULL);

    JSVERIFY(FAIL, H5FD_s3comms_signing_key(NULL, cases[0].secret_key, cases[0].region, cases[0].when),
             "destination cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_signing_key(key, NULL, cases[0].region, cases[0].when),
             "secret key cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_signing_key(key, cases[0].secret_key, NULL, cases[0].when),
             "aws region cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_signing_key(key, cases[0].secret_key, cases[0].region, NULL),
             "time string cannot be NULL")

    HDfree(key);
    key = NULL;

    PASSED();
    return 0;

error:
    if (key != NULL) {
        HDfree(key);
    }

    return -1;

} /* end test_signing_key() */

/*---------------------------------------------------------------------------
 *
 * Function: test_tostringtosign()
 *
 * Purpose:
 *
 *     Verify that we can get the "string to sign" from a Canonical Request and
 *     related information.
 *
 *     Demonstrate failure cases.
 *
 * Return:
 *
 *     Success:  0
 *     Failure: -1
 *
 * Programmer: Jacob Smith
 *             2017-09-13
 *
 *---------------------------------------------------------------------------
 */
static herr_t
test_tostringtosign(void)
{
    /************************
     * test-local variables *
     ************************/

    const char canonreq[] = "GET\n/"
                            "test.txt\n\nhost:examplebucket.s3.amazonaws.com\nrange:bytes=0-9\nx-amz-content-"
                            "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\nx-amz-"
                            "date:20130524T000000Z\n\nhost;range;x-amz-content-sha256;x-amz-"
                            "date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
    const char iso8601now[] = "20130524T000000Z";
    const char region[]     = "us-east-1";
    char       s2s[512];

    TESTING("s3comms tostringtosign");

    JSVERIFY(SUCCEED, H5FD_s3comms_tostringtosign(s2s, canonreq, iso8601now, region),
             "unable to create string to sign")

    JSVERIFY_STR("AWS4-HMAC-SHA256\n20130524T000000Z\n20130524/us-east-1/s3/"
                 "aws4_request\n7344ae5b7ee6c3e7e6b0fe0640412a37625d1fbfff95c48bbb2dc43964946972",
                 s2s, NULL)

    JSVERIFY(FAIL, H5FD_s3comms_tostringtosign(s2s, NULL, iso8601now, region),
             "canonical request string cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_tostringtosign(s2s, canonreq, NULL, region), "time string cannot be NULL")

    JSVERIFY(FAIL, H5FD_s3comms_tostringtosign(s2s, canonreq, iso8601now, NULL), "aws region cannot be NULL")

    PASSED();
    return 0;

error:
    return -1;

} /* end test_tostringtosign() */

/*----------------------------------------------------------------------------
 *
 * Function: test_trim()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_trim()`.
 *
 * Programmer: Jacob Smith
 *             2017-09-14
 *
 *----------------------------------------------------------------------------
 */
static herr_t
test_trim(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char *in;
        size_t      in_len;
        const char *exp;
        size_t      exp_len;
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[] = {
        {
            "block string",
            12,
            "block string",
            12,
        },
        {
            " \n\r  \t",
            6,
            "",
            0,
        },
        {
            " \twhite b4",
            10,
            "white b4",
            8,
        },
        {
            "white after\r\n  ",
            15,
            "white after",
            11,
        },
        {
            " on\nends\t",
            9,
            "on\nends",
            7,
        },
    };
    char   dest[32];
    size_t dest_len = 0;
    int    i        = 0;
    int    n_cases  = 5;
    char * str      = NULL;

    TESTING("s3comms trim");

    for (i = 0; i < n_cases; i++) {
        HDassert(str == NULL);
        str = (char *)HDmalloc(sizeof(char) * cases[i].in_len);
        HDassert(str != NULL);
        HDstrncpy(str, cases[i].in, cases[i].in_len);

        JSVERIFY(SUCCEED, H5FD_s3comms_trim(dest, str, cases[i].in_len, &dest_len), NULL)
        JSVERIFY(cases[i].exp_len, dest_len, cases[i].in)
        if (dest_len > 0) {
            JSVERIFY(0, HDstrncmp(cases[i].exp, dest, dest_len), cases[i].exp)
        }
        HDfree(str);
        str = NULL;
    } /* end for each testcase */

    JSVERIFY(SUCCEED, H5FD_s3comms_trim(dest, NULL, 3, &dest_len),
             "should not fail when trimming a null string");
    JSVERIFY(0, dest_len, "trimming NULL string writes 0 characters")

    HDassert(str == NULL);
    str = (char *)HDmalloc(sizeof(char *) * 11);
    HDassert(str != NULL);
    HDmemcpy(str, "some text ", 11); /* string with null terminator */
    JSVERIFY(FAIL, H5FD_s3comms_trim(NULL, str, 10, &dest_len), "destination for trim cannot be NULL");
    HDfree(str);
    str = NULL;

    PASSED();
    return 0;

error:
    if (str != NULL) {
        HDfree(str);
    }
    return -1;

} /* end test_trim() */

/*----------------------------------------------------------------------------
 *
 * Function: test_uriencode()
 *
 * Purpose:
 *
 *     Define and verify behavior of `H5FD_s3comms_uriencode()`.
 *
 * Programmer: Jacob Smith
 *             2017-09-14
 *
 *----------------------------------------------------------------------------
 */
static herr_t
test_uriencode(void)
{
    /*************************
     * test-local structures *
     *************************/

    struct testcase {
        const char *str;
        size_t      s_len;
        hbool_t     encode_slash;
        const char *expected;
    };

    /************************
     * test-local variables *
     ************************/

    struct testcase cases[]      = {{
                                   "/path/to/resource.jpg",
                                   21,
                                   FALSE,
                                   "/path/to/resource.jpg",
                               },
                               {
                                   "/path/to/resource.jpg",
                                   21,
                                   TRUE,
                                   "%2Fpath%2Fto%2Fresource.jpg",
                               },
                               {
                                   "string got_spaa  ces",
                                   20,
                                   TRUE,
                                   "string%20got_spaa%20%20ces",
                               },
                               {
                                   "sp ac~es/and-sl ash.encoded",
                                   27,
                                   TRUE,
                                   "sp%20ac~es%2Fand-sl%20ash.encoded",
                               },
                               {
                                   "sp ac~es/and-sl ash.unencoded",
                                   29,
                                   FALSE,
                                   "sp%20ac~es/and-sl%20ash.unencoded",
                               },
                               {
                                   "/path/to/resource.txt",
                                   0,
                                   FALSE,
                                   "",

                               }};
    char *          dest         = NULL;
    size_t          dest_written = 0;
    int             i            = 0;
    int             ncases       = 6;
    size_t          str_len      = 0;

    TESTING("s3comms uriencode")

    for (i = 0; i < ncases; i++) {
        str_len = cases[i].s_len;
        dest    = (char *)HDmalloc(sizeof(char) * str_len * 3 + 1);
        FAIL_IF(dest == NULL)

        JSVERIFY(SUCCEED,
                 H5FD_s3comms_uriencode(dest, cases[i].str, str_len, cases[i].encode_slash, &dest_written),
                 NULL);
        JSVERIFY(HDstrlen(cases[i].expected), dest_written, NULL)
        JSVERIFY(0, HDstrncmp(dest, cases[i].expected, dest_written), cases[i].expected);

        HDfree(dest);
        dest = NULL;
    } /* end for each testcase */

    /***************
     * ERROR CASES *
     ***************/

    dest = (char *)HDmalloc(sizeof(char) * 15);
    HDassert(dest != NULL);

    JSVERIFY(FAIL, H5FD_s3comms_uriencode(NULL, "word$", 5, false, &dest_written),
             "destination cannot be NULL");
    JSVERIFY(FAIL, H5FD_s3comms_uriencode(dest, NULL, 5, false, &dest_written),
             "source string cannot be NULL");

    HDfree(dest);
    dest = NULL;

    PASSED();
    return 0;

error:
    if (dest != NULL) {
        HDfree(dest);
    }
    return -1;

} /* end test_uriencode() */

#endif /* H5_HAVE_ROS3_VFD */

/*-------------------------------------------------------------------------
 * Function: main()
 *
 * Purpose:
 *
 *     Run unit tests for S3 Communications (s3comms).
 *
 * Return:
 *
 *     Success: 0
 *     Failure: 1
 *
 * Programmer:  Jacob Smith
 *              2017-10-12
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
#ifdef H5_HAVE_ROS3_VFD
    int         nerrors        = 0;
    const char *bucket_url_env = NULL;

    h5_reset();

#endif /* H5_HAVE_ROS3_VFD */

    HDprintf("Testing S3Communications functionality.\n");

#ifdef H5_HAVE_ROS3_VFD

    /* "clear" profile data strings */
    s3_test_aws_access_key_id[0]     = '\0';
    s3_test_aws_secret_access_key[0] = '\0';
    s3_test_aws_region[0]            = '\0';
    s3_test_bucket_url[0]            = '\0';

    /* TODO: unit/regression test for H5FD_s3comms_load_aws_profile()
     * requires a few test files and/or manipulation of default path
     */
    /* attempt to load test credentials
     * if unable, certain tests will be skipped
     */
    if (SUCCEED == H5FD_s3comms_load_aws_profile(S3_TEST_PROFILE_NAME, s3_test_aws_access_key_id,
                                                 s3_test_aws_secret_access_key, s3_test_aws_region)) {
        s3_test_credentials_loaded = 1;
    }

    bucket_url_env = HDgetenv("HDF5_ROS3_TEST_BUCKET_URL");
    if (bucket_url_env == NULL || bucket_url_env[0] == '\0') {
        HDprintf("WARNING: S3 bucket url is not defined in enviornment "
                 "variable 'HDF5_ROS3_TEST_BUCKET_URL'!\n");
    }
    else {
        HDstrncpy(s3_test_bucket_url, bucket_url_env, S3_TEST_MAX_URL_SIZE);
        s3_test_bucket_defined = TRUE;
    }

    /* tests ordered rougly by dependence */
    nerrors += test_macro_format_credential() < 0 ? 1 : 0;
    nerrors += test_trim() < 0 ? 1 : 0;
    nerrors += test_nlowercase() < 0 ? 1 : 0;
    nerrors += test_uriencode() < 0 ? 1 : 0;
    nerrors += test_percent_encode_char() < 0 ? 1 : 0;
    nerrors += test_bytes_to_hex() < 0 ? 1 : 0;
    nerrors += test_HMAC_SHA256() < 0 ? 1 : 0;
    nerrors += test_signing_key() < 0 ? 1 : 0;
    nerrors += test_hrb_node_set() < 0 ? 1 : 0;
    nerrors += test_hrb_init_request() < 0 ? 1 : 0;
    nerrors += test_parse_url() < 0 ? 1 : 0;
    nerrors += test_aws_canonical_request() < 0 ? 1 : 0;
    nerrors += test_tostringtosign() < 0 ? 1 : 0;
    nerrors += test_s3r_open() < 0 ? 1 : 0;
    nerrors += test_s3r_get_filesize() < 0 ? 1 : 0;
    nerrors += test_s3r_read() < 0 ? 1 : 0;

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

    HDprintf("All S3comms tests passed.\n");

    return 0;

#else

    HDprintf("SKIPPED - read-only S3 VFD not built\n");
    return EXIT_SUCCESS;

#endif /* H5_HAVE_ROS3_VFD */

} /* end main() */