2 * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "test-platform.h"
32 struct test_platform_ctx
{
33 struct barectf_default_ctx ctx
;
39 static void write_packet(struct test_platform_ctx
* const platform_ctx
)
41 const size_t nmemb
= fwrite(barectf_packet_buf(&platform_ctx
->ctx
),
42 barectf_packet_buf_size(&platform_ctx
->ctx
), 1,
48 static int is_backend_full(void * const data
)
50 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
52 barectf_packet_set_buf(&platform_ctx
->ctx
, platform_ctx
->buf
,
53 platform_ctx
->buf_size
);
57 static void open_packet(void * const data
)
59 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
61 memset(barectf_packet_buf(&platform_ctx
->ctx
), 0,
62 barectf_packet_buf_size(&platform_ctx
->ctx
));
63 barectf_default_open_packet(&platform_ctx
->ctx
);
66 static void close_packet(void * const data
)
68 struct test_platform_ctx
* const platform_ctx
= (void *) data
;
70 barectf_default_close_packet(&platform_ctx
->ctx
);
71 write_packet(platform_ctx
);
72 barectf_packet_set_buf(&platform_ctx
->ctx
, NULL
, 512);
75 struct test_platform_ctx
*test_platform_init(const size_t buf_size
)
77 struct test_platform_ctx
*platform_ctx
;
78 struct barectf_platform_callbacks cbs
;
80 cbs
.is_backend_full
= is_backend_full
;
81 cbs
.open_packet
= open_packet
;
82 cbs
.close_packet
= close_packet
;
83 platform_ctx
= malloc(sizeof(*platform_ctx
));
85 platform_ctx
->buf_size
= buf_size
;
86 platform_ctx
->buf
= malloc(platform_ctx
->buf_size
);
87 assert(platform_ctx
->buf
);
88 platform_ctx
->fh
= fopen("stream", "wb");
89 assert(platform_ctx
->fh
);
90 barectf_init(&platform_ctx
->ctx
, platform_ctx
->buf
,
91 platform_ctx
->buf_size
, cbs
, platform_ctx
);
92 open_packet(platform_ctx
);
96 void test_platform_fini(struct test_platform_ctx
* const platform_ctx
)
98 if (barectf_packet_is_open(&platform_ctx
->ctx
) &&
99 !barectf_packet_is_empty(&platform_ctx
->ctx
)) {
100 close_packet(platform_ctx
);
103 fclose(platform_ctx
->fh
);
104 free(platform_ctx
->buf
);
108 struct barectf_default_ctx
*test_platform_barectf_ctx(
109 struct test_platform_ctx
* const platform_ctx
)
111 return &platform_ctx
->ctx
;