xref: /freebsd/contrib/processor-trace/libipt/test/src/ptunit-encoder.c (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
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_encoder.h"
32 
33 #include "intel-pt.h"
34 
35 
36 /* A test fixture providing a decoder operating on a small buffer. */
37 struct test_fixture {
38 	/* The encoder. */
39 	struct pt_encoder encoder;
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 
52 static 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_encoder_init(&tfix->encoder, config);
68 	ptu_int_eq(errcode, 0);
69 
70 	return ptu_passed();
71 }
72 
73 static struct ptunit_result encoder_init_null(void)
74 {
75 	struct pt_encoder encoder;
76 	struct pt_config config;
77 	int errcode;
78 
79 	errcode = pt_encoder_init(NULL, &config);
80 	ptu_int_eq(errcode, -pte_invalid);
81 
82 	errcode = pt_encoder_init(&encoder, NULL);
83 	ptu_int_eq(errcode, -pte_invalid);
84 
85 	return ptu_passed();
86 }
87 
88 static struct ptunit_result encoder_fini_null(void)
89 {
90 	pt_encoder_fini(NULL);
91 
92 	return ptu_passed();
93 }
94 
95 static struct ptunit_result alloc_encoder_null(void)
96 {
97 	struct pt_encoder *encoder;
98 
99 	encoder = pt_alloc_encoder(NULL);
100 	ptu_null(encoder);
101 
102 	return ptu_passed();
103 }
104 
105 static struct ptunit_result free_encoder_null(void)
106 {
107 	pt_free_encoder(NULL);
108 
109 	return ptu_passed();
110 }
111 
112 static struct ptunit_result sync_set_null(void)
113 {
114 	int errcode;
115 
116 	errcode = pt_enc_sync_set(NULL, 0ull);
117 	ptu_int_eq(errcode, -pte_invalid);
118 
119 	return ptu_passed();
120 }
121 
122 static struct ptunit_result sync_set_eos(struct test_fixture *tfix)
123 {
124 	int errcode;
125 
126 	errcode = pt_enc_sync_set(&tfix->encoder, sizeof(tfix->buffer) + 1);
127 	ptu_int_eq(errcode, -pte_eos);
128 
129 	return ptu_passed();
130 }
131 
132 static struct ptunit_result get_offset_null(void)
133 {
134 	struct pt_encoder encoder;
135 	uint64_t offset;
136 	int errcode;
137 
138 	errcode = pt_enc_get_offset(NULL, &offset);
139 	ptu_int_eq(errcode, -pte_invalid);
140 
141 	errcode = pt_enc_get_offset(&encoder, NULL);
142 	ptu_int_eq(errcode, -pte_invalid);
143 
144 	return ptu_passed();
145 }
146 
147 static struct ptunit_result get_offset_init(struct test_fixture *tfix)
148 {
149 	uint64_t offset;
150 	int errcode;
151 
152 	errcode = pt_enc_get_offset(&tfix->encoder, &offset);
153 	ptu_int_eq(errcode, 0);
154 	ptu_uint_eq(offset, 0ull);
155 
156 	return ptu_passed();
157 }
158 
159 static struct ptunit_result sync_set_get_offset(struct test_fixture *tfix)
160 {
161 	uint64_t offset;
162 	int errcode;
163 
164 	errcode = pt_enc_sync_set(&tfix->encoder, 1ull);
165 	ptu_int_eq(errcode, 0);
166 
167 	errcode = pt_enc_get_offset(&tfix->encoder, &offset);
168 	ptu_int_eq(errcode, 0);
169 	ptu_uint_eq(offset, 1ull);
170 
171 	return ptu_passed();
172 }
173 
174 static struct ptunit_result get_config_null(void)
175 {
176 	const struct pt_config *config;
177 
178 	config = pt_enc_get_config(NULL);
179 	ptu_null(config);
180 
181 	return ptu_passed();
182 }
183 
184 static struct ptunit_result get_config(struct test_fixture *tfix)
185 {
186 	const struct pt_config *config;
187 
188 	config = pt_enc_get_config(&tfix->encoder);
189 	ptu_ptr(config);
190 
191 	return ptu_passed();
192 }
193 
194 static struct ptunit_result next_null(void)
195 {
196 	struct pt_encoder encoder;
197 	struct pt_packet packet;
198 	int errcode;
199 
200 	errcode = pt_enc_next(NULL, &packet);
201 	ptu_int_eq(errcode, -pte_invalid);
202 
203 	errcode = pt_enc_next(&encoder, NULL);
204 	ptu_int_eq(errcode, -pte_invalid);
205 
206 	return ptu_passed();
207 }
208 
209 int main(int argc, char **argv)
210 {
211 	struct test_fixture tfix;
212 	struct ptunit_suite suite;
213 
214 	tfix.init = tfix_init;
215 	tfix.fini = NULL;
216 
217 	suite = ptunit_mk_suite(argc, argv);
218 
219 	ptu_run(suite, encoder_init_null);
220 	ptu_run(suite, encoder_fini_null);
221 	ptu_run(suite, alloc_encoder_null);
222 	ptu_run(suite, free_encoder_null);
223 
224 	ptu_run(suite, sync_set_null);
225 	ptu_run_f(suite, sync_set_eos, tfix);
226 
227 	ptu_run(suite, get_offset_null);
228 	ptu_run_f(suite, get_offset_init, tfix);
229 	ptu_run_f(suite, sync_set_get_offset, tfix);
230 
231 	ptu_run(suite, get_config_null);
232 	ptu_run_f(suite, get_config, tfix);
233 
234 	ptu_run(suite, next_null);
235 
236 	return ptunit_report(&suite);
237 }
238