Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_utils.h
1/* Copyright (c) 2020 tevador <tevador@gmail.com> */
2/* See LICENSE for licensing information */
3
4#ifndef TEST_UTILS_H
5#define TEST_UTILS_H
6
7#include <stdio.h>
8#include <stdbool.h>
9#include <string.h>
10#include <stdlib.h>
11#include <hashx.h>
12
13static inline void read_option(const char* option, int argc, char** argv, bool* out) {
14 for (int i = 0; i < argc; ++i) {
15 if (strcmp(argv[i], option) == 0) {
16 *out = true;
17 return;
18 }
19 }
20 *out = false;
21}
22
23static inline void read_int_option(const char* option, int argc, char** argv, int* out, int default_val) {
24 for (int i = 0; i < argc - 1; ++i) {
25 if (strcmp(argv[i], option) == 0 && (*out = atoi(argv[i + 1])) > 0) {
26 return;
27 }
28 }
29 *out = default_val;
30}
31
32static inline char parse_nibble(char hex) {
33 hex &= ~0x20;
34 return (hex & 0x40) ? hex - ('A' - 10) : hex & 0xf;
35}
36
37static inline void hex2bin(const char* in, int length, char* out) {
38 for (int i = 0; i < length; i += 2) {
39 char nibble1 = parse_nibble(*in++);
40 char nibble2 = parse_nibble(*in++);
41 *out++ = nibble1 << 4 | nibble2;
42 }
43}
44
45static inline void output_hex(const char* data, int length) {
46 for (unsigned i = 0; i < length; ++i)
47 printf("%02x", data[i] & 0xff);
48}
49
50static inline bool hashes_equal(char* a, char* b) {
51 return memcmp(a, b, HASHX_SIZE) == 0;
52}
53
54static inline bool equals_hex(const void* hash, const char* hex) {
55 char reference[HASHX_SIZE];
56 hex2bin(hex, 2 * HASHX_SIZE, reference);
57 return memcmp(hash, reference, sizeof(reference)) == 0;
58}
59
60#endif