summaryrefslogtreecommitdiffstats
path: root/test/vfd_plugin.c
blob: 8bfc09f07aa277f764474398067c7da20ca90669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:     Tests basic VFD plugin operations. Uses a basic testing VFD
 *              which is loaded as a dynamic plugin.
 */

#include "h5test.h"

#include "null_vfd_plugin.h"

#define DEFAULT_DRIVER_NAME "sec2"

/*-------------------------------------------------------------------------
 * Function:    test_set_by_name()
 *
 * Purpose:     Tests if we can load and register a VFD plugin by name.
 *
 * Return:      SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_set_by_name(void)
{
    htri_t is_registered = FAIL;
    hid_t  driver_id     = H5I_INVALID_HID;
    hid_t  fapl_id       = H5I_INVALID_HID;

    TESTING("VFD plugin registration by name");

    /* The null VFD should not be registered at the start of the test */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    /* Register the null VFD by name */
    if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if (H5Pset_driver_by_name(fapl_id, NULL_VFD_NAME, NULL) < 0)
        TEST_ERROR;

    /* The null VFD should be registered now */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (false == is_registered)
        FAIL_PUTS_ERROR("NULL VFD was not registered");

    /* Unregister the null VFD */
    if ((driver_id = H5Pget_driver(fapl_id)) < 0)
        TEST_ERROR;
    if (H5FDunregister(driver_id) < 0)
        TEST_ERROR;

    /* Close FAPL, which holds last reference to null VFD */
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* The null VFD should not be registered now */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    PASSED();

    return SUCCEED;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
    }
    H5E_END_TRY

    return FAIL;
}

/*-------------------------------------------------------------------------
 * Function:    test_set_by_value()
 *
 * Purpose:     Tests if we can load and register a VFD plugin by value
 *              (ID).
 *
 * Return:      SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_set_by_value(void)
{
    htri_t is_registered = FAIL;
    hid_t  driver_id     = H5I_INVALID_HID;
    hid_t  fapl_id       = H5I_INVALID_HID;

    TESTING("VFD plugin registration by value (ID)");

    /* The null VFD should not be registered at the start of the test */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    /* Register the null VFD by value */
    if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if (H5Pset_driver_by_value(fapl_id, NULL_VFD_VALUE, NULL) < 0)
        TEST_ERROR;

    /* The null VFD should be registered now */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (false == is_registered)
        FAIL_PUTS_ERROR("NULL VFD was not registered");

    /* Unregister the null VFD */
    if ((driver_id = H5Pget_driver(fapl_id)) < 0)
        TEST_ERROR;
    if (H5FDunregister(driver_id) < 0)
        TEST_ERROR;

    /* Close FAPL, which holds last reference to null VFD */
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* The null VFD should not be registered now */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    PASSED();

    return SUCCEED;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
    }
    H5E_END_TRY

    return FAIL;
}

/*-------------------------------------------------------------------------
 * Function:    test_set_multi()
 *
 * Purpose:     Tests if we can register a VFD plugin multiple times.
 *
 * Return:      SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
#define N_REGISTRATIONS 10
static herr_t
test_set_multi(void)
{
    htri_t is_registered = FAIL;
    hid_t  driver_id     = H5I_INVALID_HID;
    hid_t  fapl_id       = H5I_INVALID_HID;
    int    i;

    TESTING("registering a VFD plugin multiple times");

    /* The null VFD should not be registered at the start of the test */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    /* Register the VFD multiple times */
    if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    for (i = 0; i < N_REGISTRATIONS; i++) {
        if (H5Pset_driver_by_name(fapl_id, NULL_VFD_NAME, NULL) < 0)
            TEST_ERROR;
    }

    /* The null VFD should be registered now */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (false == is_registered)
        FAIL_PUTS_ERROR("NULL VFD was not registered");

    /* Unregister the null VFD */
    if ((driver_id = H5Pget_driver(fapl_id)) < 0)
        TEST_ERROR;
    for (i = 0; i < N_REGISTRATIONS; i++) {
        if (H5FDunregister(driver_id) < 0)
            TEST_ERROR;
    }

    /* Close FAPL, which holds last reference to null VFD */
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* The null VFD should not be registered now */
    if ((is_registered = H5FDis_driver_registered_by_name(NULL_VFD_NAME)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    /* Repeat testing with the _by_value routines */

    /* The null VFD should not be registered at the start of the test */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    /* Register the VFD multiple times */
    if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    for (i = 0; i < N_REGISTRATIONS; i++) {
        if (H5Pset_driver_by_value(fapl_id, NULL_VFD_VALUE, NULL) < 0)
            TEST_ERROR;
    }

    /* The null VFD should be registered now */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (false == is_registered)
        FAIL_PUTS_ERROR("NULL VFD was not registered");

    /* Unregister the null VFD */
    if ((driver_id = H5Pget_driver(fapl_id)) < 0)
        TEST_ERROR;
    for (i = 0; i < N_REGISTRATIONS; i++) {
        if (H5FDunregister(driver_id) < 0)
            TEST_ERROR;
    }

    /* Close FAPL, which holds last reference to null VFD */
    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    /* The null VFD should not be registered now */
    if ((is_registered = H5FDis_driver_registered_by_value(NULL_VFD_VALUE)) < 0)
        TEST_ERROR;
    if (true == is_registered)
        FAIL_PUTS_ERROR("NULL VFD is inappropriately registered");

    PASSED();

    return SUCCEED;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
    }
    H5E_END_TRY

    return FAIL;
}
#undef N_REGISTRATIONS

/*-------------------------------------------------------------------------
 * Function:    test_get_config_str()
 *
 * Purpose:     Tests if we can retrieve a configuration string set for a
 *              VFL driver.
 *
 * Return:      SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_get_config_str(void)
{
    const char *const config_str     = "{name: sec2}";
    ssize_t           config_str_len = 0;
    hid_t             fapl_id        = H5I_INVALID_HID;
    char              config_str_buf[128];

    TESTING("Retrieval of VFD configuration string");

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

    /* Try to retrieve length of default configuration string - should be 0 */
    memset(config_str_buf, 0, 128);

    if ((config_str_len = H5Pget_driver_config_str(fapl_id, config_str_buf, 128)) < 0)
        TEST_ERROR;
    if (0 != config_str_len)
        TEST_ERROR;
    if (strlen(config_str_buf) > 0)
        TEST_ERROR;

    /* Set a new configuration string on the FAPL and retrieve it */
    if (H5Pset_driver_by_name(fapl_id, DEFAULT_DRIVER_NAME, config_str) < 0)
        TEST_ERROR;
    if ((config_str_len = H5Pget_driver_config_str(fapl_id, config_str_buf, 128)) < 0)
        TEST_ERROR;
    if (strlen(config_str) != config_str_len)
        TEST_ERROR;
    if (strncmp(config_str_buf, config_str, 128))
        TEST_ERROR;

    if (H5Pclose(fapl_id) < 0)
        TEST_ERROR;

    PASSED();

    return SUCCEED;

error:
    H5E_BEGIN_TRY
    {
        H5Pclose(fapl_id);
    }
    H5E_END_TRY

    return FAIL;
}

/*-------------------------------------------------------------------------
 * Function:    test_env_var
 *
 * Purpose:     Tests loading of NULL VFD plugin with HDF5_DRIVER
 *              environment variable and setting of VFD configuration
 *              string with HDF5_DRIVER_CONFIG environment variable
 *
 * Return:      EXIT_SUCCESS/EXIT_FAILURE
 *
 *-------------------------------------------------------------------------
 */
static int
test_env_var(void)
{
    const char *const config_str     = "{name: null}";
    ssize_t           config_str_len = 0;
    htri_t            driver_is_registered;
    char              config_str_buf[128];

    TESTING("Loading of VFD plugin with HDF5_DRIVER environment variable");

    /* Try to retrieve length of default configuration string - should be 0 */
    memset(config_str_buf, 0, 128);

    if ((config_str_len = H5Pget_driver_config_str(H5P_FILE_ACCESS_DEFAULT, config_str_buf, 128)) < 0)
        TEST_ERROR;
    if (0 != config_str_len)
        TEST_ERROR;
    if (strlen(config_str_buf) > 0)
        TEST_ERROR;

    /* Set default driver and driver configuration using environment variables */
    if (HDsetenv(HDF5_DRIVER, "null_vfd_plugin", 1) < 0)
        TEST_ERROR;
    if (HDsetenv(HDF5_DRIVER_CONFIG, config_str, 1) < 0)
        TEST_ERROR;

    /* Close and re-open HDF5 to have it parse the environment variables */
    if (H5close() < 0)
        TEST_ERROR;
    if (H5open() < 0)
        TEST_ERROR;

    /* Check driver */
    if ((driver_is_registered = H5FDis_driver_registered_by_name("null_vfd_plugin")) < 0)
        TEST_ERROR;
    if (!driver_is_registered)
        TEST_ERROR;
    if (H5Pget_driver(H5P_FILE_ACCESS_DEFAULT) == H5_DEFAULT_VFD)
        TEST_ERROR;

    /* Check driver configuration string */
    memset(config_str_buf, 0, 128);
    if ((config_str_len = H5Pget_driver_config_str(H5P_FILE_ACCESS_DEFAULT, config_str_buf, 128)) < 0)
        TEST_ERROR;
    if (strlen(config_str) != config_str_len)
        TEST_ERROR;
    if (strncmp(config_str_buf, config_str, 128))
        TEST_ERROR;

    /* Unset environment variables */
    if (HDsetenv(HDF5_DRIVER, "", 1) < 0)
        TEST_ERROR;
    if (HDsetenv(HDF5_DRIVER_CONFIG, "", 1) < 0)
        TEST_ERROR;

    PASSED();

    return SUCCEED;

error:
    HDsetenv(HDF5_DRIVER, "", 1);
    HDsetenv(HDF5_DRIVER_CONFIG, "", 1);

    return FAIL;
}

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     Tests VFD plugin operations
 *
 * Return:      EXIT_SUCCESS/EXIT_FAILURE
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    int nerrors = 0;

    h5_reset();

    puts("Testing VFD plugin functionality.");

    nerrors += (test_set_by_name() < 0) ? 1 : 0;
    nerrors += (test_set_by_value() < 0) ? 1 : 0;
    nerrors += (test_set_multi() < 0) ? 1 : 0;
    nerrors += (test_get_config_str() < 0) ? 1 : 0;
    nerrors += (test_env_var() < 0) ? 1 : 0;

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

    puts("All VFD plugin tests passed.");

    exit(EXIT_SUCCESS);
}