summaryrefslogtreecommitdiffstats
path: root/test/test-lib.c
blob: a9e970634939007cf47d975e63addd3e4a1400fd (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

#include <stdio.h>
#include <config.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <regex.h>
#include <string.h>
#include <strings.h>

#include <test/test-lib.h>

static struct lt_config_shared *get_config(char *config_dir)
{
	struct lt_config_shared* sh;
	int page = sysconf(_SC_PAGE_SIZE);
	int fd, len;
	char config_file[LT_MAXFILE];

	snprintf(config_file, LT_MAXFILE, "%s/config", config_dir);

	if (-1 == (fd = open(config_file, O_RDWR))) {
		perror("open failed");
		return NULL;
	}

	/* align the shared config length */
	len = sizeof(struct lt_config_shared);
	len = (len + page) & ~(page - 1);

	sh = mmap(NULL, len,
		PROT_READ | PROT_WRITE,
		MAP_SHARED, fd, 0);

	if ((void *) -1 == sh) {
		perror("mmap failed");
		return NULL;
	}

	sh->sh = sh;
	return sh;
}

static int fout_init(struct lt_config_shared *sh, char *config_dir)
{
	char output_file[LT_MAXFILE];
	FILE *file;

	snprintf(output_file, LT_MAXFILE, "%s/test-output", config_dir);

	file = fopen(output_file, "w+");
	if (!file) {
		perror("fopen failed");
		return -1;
	}

	sh->fout = file;
	return 0;
}

int fout_read(struct lt_config_shared *sh, char *buf, int size)
{
	FILE *file = sh->fout;

	/* XXX ugly, but I could not get work pipe/fifo with
	 * streams..  maybe to get rid of streams completely
	 * would be the solution */
	bzero(buf, size);
	rewind(file);
	fread(buf, size, 1, file);
	rewind(file);
	ftruncate(fileno(file), 0);
	return strlen(buf);
}

struct lt_config_shared *config_init(void)
{
	struct lt_config_shared *sh;
	char *config_dir;

	config_dir = getenv("LT_DIR");
	if (!config_dir) {
		printf("failed: could not get config dir\n");
		return NULL;
	}

	sh = get_config(config_dir);
	if (!sh)
		return NULL;

	if (fout_init(sh, config_dir))
		return NULL;

	sh->pipe = 0;
	return sh;
}

void config_clear(struct lt_config_shared *sh)
{
	sh->args_enabled = 0;
	sh->args_detailed = 0;
	sh->disabled = 1;
	sh->verbose = 0;
	sh->timestamp = 0;
	sh->debug = 0;
	sh->indent_sym = 0;
	sh->indent_size = 0;
	sh->braces = 0;
	sh->demangle = 0;
	sh->counts = 0;
	sh->pipe = 0;
	sh->hide_tid = 0;
	sh->not_follow_exec = 0;
	sh->not_follow_fork = 0;
	sh->framesize_check = 0;
	sh->framesize = 1000;
	sh->pid = 0;
}

static void re_err(regex_t *re, int errcode)
{
	char ebuf[BUFSIZE];
	int ret;

	ret = regerror(errcode, re, ebuf, BUFSIZE);
	if (!ret)
		return;

	printf("regex failed: %s\n", ebuf);
}

int re_test(char *line, struct re_test_data *data, int cnt)
{
	int i;

	for(i = 0; i < cnt; i++) {
		int ret;
		struct re_test_data *d = &data[i];
		regmatch_t m[1];
		regex_t re;
		unsigned long val;

		memset(&re, 0x0, sizeof(re));

		ret = regcomp(&re,
			d->pat,
			REG_EXTENDED);
		if (ret) {
			re_err(&re, ret);
			return i;
		}

		ret = regexec(&re, line, 1, m, 0);
		if (ret == REG_NOMATCH) {
			printf("failed: did not match\n");
			return i;
		}

		switch(d->type) {
		case RE_TEST_TYPE_STR:
			/* start offset check */
			if (d->so != RE_TEST_OFF_UNDEF) {
				if (d->so != m[0].rm_so) {
					printf("failed: so mismatch %d <> %d\n",
						d->so, m[0].rm_so);
					return i;
				}
			}

			/* end offset check */
			if (d->eo != RE_TEST_OFF_UNDEF) {
				if (d->eo != m[0].rm_eo) {
					printf("failed: eo mismatch %d <> %d\n",
						d->eo, m[0].rm_eo);
					return i;
				}
			}
			break;
		case RE_TEST_TYPE_INT:
		case RE_TEST_TYPE_PID:
			val = strtoul(&line[m[0].rm_so], NULL, 10);
			if (val != getpid())
				return i;
			break;
		}
	}

	return RE_TEST_OK;
}