Commit | Line | Data |
---|---|---|
88004776 PP |
1 | /* |
2 | * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
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 | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <stdint.h> | |
26 | #include <string.h> | |
27 | #include <assert.h> | |
28 | ||
29 | #include "barectf.h" | |
30 | #include "test-platform.h" | |
31 | ||
32 | struct test_platform_ctx { | |
33 | struct barectf_default_ctx ctx; | |
34 | FILE *fh; | |
35 | }; | |
36 | ||
37 | static void write_packet(struct test_platform_ctx * const platform_ctx) | |
38 | { | |
39 | size_t nmemb = fwrite(barectf_packet_buf(&platform_ctx->ctx), | |
40 | barectf_packet_buf_size(&platform_ctx->ctx), 1, | |
41 | platform_ctx->fh); | |
42 | assert(nmemb == 1); | |
43 | } | |
44 | ||
45 | static int is_backend_full(void * const data) | |
46 | { | |
47 | return 0; | |
48 | } | |
49 | ||
50 | static void open_packet(void * const data) | |
51 | { | |
52 | struct test_platform_ctx * const platform_ctx = (void *) data; | |
53 | ||
54 | barectf_default_open_packet(&platform_ctx->ctx); | |
55 | } | |
56 | ||
57 | static void close_packet(void * const data) | |
58 | { | |
59 | struct test_platform_ctx * const platform_ctx = (void *) data; | |
60 | ||
61 | barectf_default_close_packet(&platform_ctx->ctx); | |
62 | write_packet(platform_ctx); | |
63 | } | |
64 | ||
65 | struct test_platform_ctx *test_platform_init(const size_t buf_size) | |
66 | { | |
67 | uint8_t *buf; | |
68 | struct test_platform_ctx *platform_ctx; | |
69 | struct barectf_platform_callbacks cbs; | |
70 | ||
71 | cbs.is_backend_full = is_backend_full; | |
72 | cbs.open_packet = open_packet; | |
73 | cbs.close_packet = close_packet; | |
74 | platform_ctx = malloc(sizeof(*platform_ctx)); | |
75 | assert(platform_ctx); | |
76 | buf = malloc(buf_size); | |
77 | assert(buf); | |
78 | memset(buf, 0, buf_size); | |
79 | platform_ctx->fh = fopen("stream", "wb"); | |
80 | assert(platform_ctx->fh); | |
81 | barectf_init(&platform_ctx->ctx, buf, buf_size, cbs, platform_ctx); | |
82 | open_packet(platform_ctx); | |
83 | return platform_ctx; | |
84 | } | |
85 | ||
86 | void test_platform_fini(struct test_platform_ctx * const platform_ctx) | |
87 | { | |
88 | if (barectf_packet_is_open(&platform_ctx->ctx) && | |
89 | !barectf_packet_is_empty(&platform_ctx->ctx)) { | |
90 | close_packet(platform_ctx); | |
91 | } | |
92 | ||
93 | fclose(platform_ctx->fh); | |
94 | free(barectf_packet_buf(&platform_ctx->ctx)); | |
95 | free(platform_ctx); | |
96 | } | |
97 | ||
98 | struct barectf_default_ctx *test_platform_barectf_ctx( | |
99 | struct test_platform_ctx * const platform_ctx) | |
100 | { | |
101 | return &platform_ctx->ctx; | |
102 | } |