1/*
2 * Copyright (c) 2019, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 *  * Redistributions of source code must retain the above copyright notice,
8 *    this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright notice,
10 *    this list of conditions and the following disclaimer in the documentation
11 *    and/or other materials provided with the distribution.
12 *  * Neither the name of Intel Corporation nor the names of its contributors
13 *    may be used to endorse or promote products derived from this software
14 *    without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "ptunit.h"
30
31#include "pt_packet_decoder.h"
32
33#include "intel-pt.h"
34
35
36/* A test fixture providing a decoder operating on a small buffer. */
37struct test_fixture {
38	/* The packet_decoder. */
39	struct pt_packet_decoder decoder;
40
41	/* The configuration. */
42	struct pt_config config;
43
44	/* The buffer it operates on. */
45	uint8_t buffer[24];
46
47	/* The test fixture initialization and finalization functions. */
48	struct ptunit_result (*init)(struct test_fixture *tfix);
49	struct ptunit_result (*fini)(struct test_fixture *tfix);
50};
51
52static struct ptunit_result tfix_init(struct test_fixture *tfix)
53{
54	struct pt_config *config;
55	uint8_t *buffer;
56	int errcode;
57
58	config = &tfix->config;
59	buffer = tfix->buffer;
60
61	memset(buffer, 0, sizeof(tfix->buffer));
62
63	pt_config_init(config);
64	config->begin = buffer;
65	config->end = buffer + sizeof(tfix->buffer);
66
67	errcode = pt_pkt_decoder_init(&tfix->decoder, config);
68	ptu_int_eq(errcode, 0);
69
70	return ptu_passed();
71}
72
73static struct ptunit_result decoder_init_null(void)
74{
75	struct pt_packet_decoder decoder;
76	struct pt_config config;
77	int errcode;
78
79	errcode = pt_pkt_decoder_init(NULL, &config);
80	ptu_int_eq(errcode, -pte_invalid);
81
82	errcode = pt_pkt_decoder_init(&decoder, NULL);
83	ptu_int_eq(errcode, -pte_invalid);
84
85	return ptu_passed();
86}
87
88static struct ptunit_result decoder_fini_null(void)
89{
90	pt_pkt_decoder_fini(NULL);
91
92	return ptu_passed();
93}
94
95static struct ptunit_result alloc_decoder_null(void)
96{
97	struct pt_packet_decoder *decoder;
98
99	decoder = pt_pkt_alloc_decoder(NULL);
100	ptu_null(decoder);
101
102	return ptu_passed();
103}
104
105static struct ptunit_result free_decoder_null(void)
106{
107	pt_pkt_free_decoder(NULL);
108
109	return ptu_passed();
110}
111
112static struct ptunit_result sync_forward_null(void)
113{
114	int errcode;
115
116	errcode = pt_pkt_sync_forward(NULL);
117	ptu_int_eq(errcode, -pte_invalid);
118
119	return ptu_passed();
120}
121
122static struct ptunit_result sync_backward_null(void)
123{
124	int errcode;
125
126	errcode = pt_pkt_sync_backward(NULL);
127	ptu_int_eq(errcode, -pte_invalid);
128
129	return ptu_passed();
130}
131
132static struct ptunit_result sync_set_null(void)
133{
134	int errcode;
135
136	errcode = pt_pkt_sync_set(NULL, 0ull);
137	ptu_int_eq(errcode, -pte_invalid);
138
139	return ptu_passed();
140}
141
142static struct ptunit_result sync_set_eos(struct test_fixture *tfix)
143{
144	int errcode;
145
146	errcode = pt_pkt_sync_set(&tfix->decoder, sizeof(tfix->buffer) + 1);
147	ptu_int_eq(errcode, -pte_eos);
148
149	return ptu_passed();
150}
151
152static struct ptunit_result get_offset_null(void)
153{
154	struct pt_packet_decoder decoder;
155	uint64_t offset;
156	int errcode;
157
158	errcode = pt_pkt_get_offset(NULL, &offset);
159	ptu_int_eq(errcode, -pte_invalid);
160
161	errcode = pt_pkt_get_offset(&decoder, NULL);
162	ptu_int_eq(errcode, -pte_invalid);
163
164	return ptu_passed();
165}
166
167static struct ptunit_result get_offset_init(struct test_fixture *tfix)
168{
169	uint64_t offset;
170	int errcode;
171
172	errcode = pt_pkt_get_offset(&tfix->decoder, &offset);
173	ptu_int_eq(errcode, -pte_nosync);
174
175	return ptu_passed();
176}
177
178static struct ptunit_result sync_set_get_offset(struct test_fixture *tfix)
179{
180	uint64_t offset;
181	int errcode;
182
183	errcode = pt_pkt_sync_set(&tfix->decoder, 1ull);
184	ptu_int_eq(errcode, 0);
185
186	errcode = pt_pkt_get_offset(&tfix->decoder, &offset);
187	ptu_int_eq(errcode, 0);
188	ptu_uint_eq(offset, 1ull);
189
190	return ptu_passed();
191}
192
193static struct ptunit_result get_sync_offset_null(void)
194{
195	struct pt_packet_decoder decoder;
196	uint64_t offset;
197	int errcode;
198
199	errcode = pt_pkt_get_sync_offset(NULL, &offset);
200	ptu_int_eq(errcode, -pte_invalid);
201
202	errcode = pt_pkt_get_sync_offset(&decoder, NULL);
203	ptu_int_eq(errcode, -pte_invalid);
204
205	return ptu_passed();
206}
207
208static struct ptunit_result get_config_null(void)
209{
210	const struct pt_config *config;
211
212	config = pt_pkt_get_config(NULL);
213	ptu_null(config);
214
215	return ptu_passed();
216}
217
218static struct ptunit_result get_config(struct test_fixture *tfix)
219{
220	const struct pt_config *config;
221
222	config = pt_pkt_get_config(&tfix->decoder);
223	ptu_ptr(config);
224
225	return ptu_passed();
226}
227
228static struct ptunit_result next_null(void)
229{
230	struct pt_packet_decoder decoder;
231	struct pt_packet packet;
232	int errcode;
233
234	errcode = pt_pkt_next(NULL, &packet, sizeof(packet));
235	ptu_int_eq(errcode, -pte_invalid);
236
237	errcode = pt_pkt_next(&decoder, NULL, sizeof(packet));
238	ptu_int_eq(errcode, -pte_invalid);
239
240	return ptu_passed();
241}
242
243int main(int argc, char **argv)
244{
245	struct test_fixture tfix;
246	struct ptunit_suite suite;
247
248	tfix.init = tfix_init;
249	tfix.fini = NULL;
250
251	suite = ptunit_mk_suite(argc, argv);
252
253	ptu_run(suite, decoder_init_null);
254	ptu_run(suite, decoder_fini_null);
255	ptu_run(suite, alloc_decoder_null);
256	ptu_run(suite, free_decoder_null);
257
258	ptu_run(suite, sync_forward_null);
259	ptu_run(suite, sync_backward_null);
260	ptu_run(suite, sync_set_null);
261	ptu_run_f(suite, sync_set_eos, tfix);
262
263	ptu_run(suite, get_offset_null);
264	ptu_run_f(suite, get_offset_init, tfix);
265	ptu_run_f(suite, sync_set_get_offset, tfix);
266	ptu_run(suite, get_sync_offset_null);
267
268	ptu_run(suite, get_config_null);
269	ptu_run_f(suite, get_config, tfix);
270
271	ptu_run(suite, next_null);
272
273	return ptunit_report(&suite);
274}
275