File: block-limits.c

package info (click to toggle)
libblkio 1.5.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,040 kB
  • sloc: ansic: 3,443; sh: 460; makefile: 35; python: 16
file content (124 lines) | stat: -rw-r--r-- 3,139 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: (MIT OR Apache-2.0)
#include "util.h"

enum {
    TEST_FILE_SIZE = 2 * 1024 * 1024,
};

static char filename[] = "block-limits-XXXXXX";

static void expect_get_int_err(struct blkio *b,
                               int neg_errno,
                               const char *props[])
{
    for (int i = 0; props[i]; i++) {
        int value;

        err(blkio_get_int(b, props[i], &value), neg_errno);
    }
}

static void expect_get_int_ok(struct blkio *b, const char *props[])
{
    for (int i = 0; props[i]; i++) {
        int value;

        ok(blkio_get_int(b, props[i], &value));
    }
}

static void expect_set_int_err(struct blkio *b,
                               int neg_errno,
                               const char *props[])
{
    for (int i = 0; props[i]; i++) {
        err(blkio_set_int(b, props[i], 123456789), neg_errno);
    }
}

static void expect_get_uint64_err(struct blkio *b,
                                  int neg_errno,
                                  const char *props[])
{
    for (int i = 0; props[i]; i++) {
        uint64_t value;

        err(blkio_get_uint64(b, props[i], &value), neg_errno);
    }
}

static void expect_get_uint64_ok(struct blkio *b, const char *props[])
{
    for (int i = 0; props[i]; i++) {
        uint64_t value;

        ok(blkio_get_uint64(b, props[i], &value));
    }
}

static void expect_set_uint64_err(struct blkio *b,
                                  int neg_errno,
                                  const char *props[])
{
    for (int i = 0; props[i]; i++) {
        err(blkio_set_uint64(b, props[i], 123456789), neg_errno);
    }
}

/*
 * Get and set block limits properties, checking for expected error codes and
 * ignoring valid values returned.
 *
 * The specific values of these properties depend on the driver and environment
 * so it is difficult to validate them. It is still useful to exercise the code
 * just for code coverage (e.g. to prove there are no crashes).
 */
int main(int argc, char **argv)
{
    static const char *int_props[] = {
        "buf-alignment",
        "discard-alignment",
        "discard-alignment-offset",
        "max-segment-len",
        "max-segments",
        "max-transfer",
        "optimal-buf-alignment",
        "optimal-io-alignment",
        "optimal-io-size",
        "request-alignment",
        NULL
    };

    static const char *uint64_props[] = {
        "max-discard-len",
        "max-write-zeroes-len",
        NULL
    };

    struct test_opts opts;
    struct blkio *b;

    parse_generic_opts(&opts, argc, argv);

    create(&b, &opts, filename, TEST_FILE_SIZE);

    expect_get_int_err(b, -ENODEV, int_props);
    expect_get_uint64_err(b, -ENODEV, uint64_props);

    expect_set_int_err(b, -EACCES, int_props);
    expect_set_uint64_err(b, -EACCES, uint64_props);

    ok(blkio_connect(b));

    /* The properties are still read-only */
    expect_set_int_err(b, -EACCES, int_props);
    expect_set_uint64_err(b, -EACCES, uint64_props);

    expect_get_int_ok(b, int_props);
    expect_get_uint64_ok(b, uint64_props);

    blkio_destroy(&b);
    assert(!b);

    return 0;
}