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
|
/* Utility functions for unit tests.
Copyright (C) 2016 Petr Tesarik <ptesarik@suse.cz>
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
libkdumpfile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TESTUTIL_H
#define _TESTUTIL_H 1
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <endian.h>
#include <libkdumpfile/addrxlat.h>
#define TEST_OK 0
#define TEST_FAIL 1
#define TEST_SKIP 77
#define TEST_ERR 99
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
/* Endianity conversions */
typedef enum endian {
data_le, /**< Least significant byte first */
data_be /**< Most significant byte first */
} endian_t;
static inline uint16_t
htodump16(endian_t endian, uint16_t x)
{
return endian != data_le
? htobe16(x)
: htole16(x);
}
static inline uint32_t
htodump32(endian_t endian, uint32_t x)
{
return endian != data_le
? htobe32(x)
: htole32(x);
}
static inline uint64_t
htodump64(endian_t endian, uint64_t x)
{
return endian != data_le
? htobe64(x)
: htole64(x);
}
/* Hex/oct */
static inline signed char
unhex(char digit)
{
if (digit >= '0' && digit <= '9')
return digit - '0';
if (digit >= 'A' && digit <= 'F')
return digit - 'A' + 10;
if (digit >= 'a' && digit <= 'f')
return digit - 'a' + 10;
return -1;
}
static inline signed char
unoct(char digit)
{
if (digit >= '0' && digit <= '7')
return digit - '0';
return -1;
}
/* Generic types */
struct blob {
size_t length;
unsigned char data[];
};
/* Parameter files */
struct number_array {
unsigned n;
unsigned long long *val;
};
struct param {
const char *key;
enum {
param_string,
param_yesno,
param_number,
param_number_array,
param_blob,
param_fulladdr,
} type;
union {
char **string;
bool *yesno;
unsigned long long *number;
struct number_array *number_array;
struct blob **blob;
addrxlat_fulladdr_t *fulladdr;
};
};
struct params {
unsigned n;
const struct param *params;
};
#define PARAM_STRING(key, val) \
{ (key), param_string, { .string = &(val) } }
#define PARAM_YESNO(key, val) \
{ (key), param_yesno, { .yesno = &(val) } }
#define PARAM_NUMBER(key, val) \
{ (key), param_number, { .number = &(val) } }
#define PARAM_NUMBER_ARRAY(key, val) \
{ (key), param_number_array, { .number_array = &(val) } }
#define PARAM_FULLADDR(key, val) \
{ (key), param_fulladdr, { .fulladdr = &(val) } }
int parse_key_val(char *line, char **key, char **val);
int set_param(const struct param *p, const char *val);
int parse_params_file(const struct params *params, FILE *f);
int parse_params(const struct params *params, const char *fname);
/* Data files */
struct page_data;
typedef int data_parse_hdr_fn(struct page_data *pg, char *p);
typedef int data_write_page_fn(struct page_data *pg);
struct page_data {
size_t alloc; /**< Allocated bytes */
size_t len; /**< Current buffer length */
unsigned char *buf; /**< Page buffer */
endian_t endian; /**< Data endianity */
void *priv; /**< To be used by callbacks */
data_parse_hdr_fn *parse_hdr; /**< Parse header */
data_write_page_fn *write_page; /**< Write full page */
};
int process_data(struct page_data *pg, const char *fname);
int process_data_file(struct page_data *pg, FILE *f);
/* RLE encoding */
int compress_rle(unsigned char *dst, size_t *pdstlen,
const unsigned char *src, size_t srclen);
/* File content slurping */
struct blob *slurp(const char *fname);
struct blob *slurp_file(FILE *f);
#endif /* testutil.h */
|