summaryrefslogtreecommitdiffstats
path: root/tests/basic/open-behind/tester.h
blob: 64e940c78fc264262199931f83b8ef8d0bf7c255 (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
/*
  Copyright (c) 2020 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#ifndef __TESTER_H__
#define __TESTER_H__

#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>

enum _obj_type;
typedef enum _obj_type obj_type_t;

enum _arg_type;
typedef enum _arg_type arg_type_t;

struct _buffer;
typedef struct _buffer buffer_t;

struct _obj;
typedef struct _obj obj_t;

struct _context;
typedef struct _context context_t;

struct _arg;
typedef struct _arg arg_t;

struct _command;
typedef struct _command command_t;

enum _obj_type { OBJ_TYPE_NONE, OBJ_TYPE_FD };

enum _arg_type { ARG_TYPE_NONE, ARG_TYPE_OBJ, ARG_TYPE_NUM, ARG_TYPE_STR };

struct _buffer {
    char *base;
    uint32_t size;
    uint32_t len;
    uint32_t pos;
};

struct _obj {
    obj_type_t type;
    union {
        int32_t fd;
    };
};

struct _context {
    obj_t *objs;
    buffer_t buffer;
    uint32_t obj_count;
    bool active;
};

struct _arg {
    arg_type_t type;
    union {
        struct {
            obj_type_t type;
            obj_t *ref;
        } obj;
        struct {
            uint64_t value;
            uint64_t min;
            uint64_t max;
        } num;
        struct {
            uint32_t size;
            char *data;
        } str;
    };
};

struct _command {
    const char *name;
    int32_t (*handler)(context_t *ctx, command_t *cmd);
    union {
        arg_t *args;
        command_t *cmds;
    };
};

#define msg(_stream, _fmt, _args...)                                           \
    do {                                                                       \
        fprintf(_stream, _fmt "\n", ##_args);                                  \
        fflush(_stream);                                                       \
    } while (0)

#define msg_out(_fmt, _args...) msg(stdout, _fmt, ##_args)
#define msg_err(_err, _fmt, _args...)                                          \
    ({                                                                         \
        int32_t __msg_err = (_err);                                            \
        msg(stderr, "[%4u:%-15s] " _fmt, __LINE__, __FUNCTION__, __msg_err,    \
            ##_args);                                                          \
        -__msg_err;                                                            \
    })

#define error(_err, _fmt, _args...) msg_err(_err, "E(%4d) " _fmt, ##_args)
#define warn(_err, _fmt, _args...) msg_err(_err, "W(%4d) " _fmt, ##_args)
#define info(_err, _fmt, _args...) msg_err(_err, "I(%4d) " _fmt, ##_args)

#define out_ok(_args...) msg_out("OK " _args)
#define out_err(_err) msg_out("ERR %d", _err)

#define ARG_END                                                                \
    {                                                                          \
        ARG_TYPE_NONE                                                          \
    }

#define CMD_ARGS1(_x, _args...)                                                \
    .args = (arg_t[]) { _args }
#define CMD_ARGS(_args...) CMD_ARGS1(, ##_args, ARG_END)

#define CMD_SUB(_cmds) .cmds = _cmds

#define CMD_END                                                                \
    {                                                                          \
        NULL, NULL, CMD_SUB(NULL)                                              \
    }

#define ARG_VAL(_type)                                                         \
    {                                                                          \
        ARG_TYPE_OBJ, .obj = {.type = _type }                                  \
    }
#define ARG_NUM(_min, _max)                                                    \
    {                                                                          \
        ARG_TYPE_NUM, .num = {.min = _min, .max = _max }                       \
    }
#define ARG_STR(_size)                                                         \
    {                                                                          \
        ARG_TYPE_STR, .str = {.size = _size }                                  \
    }

extern command_t fd_commands[];

#endif /* __TESTER_H__ */