Debian Codegraph @

libsframe/testsuite/libsframe.decode/be-flipping.c

raw
1 /* be-flipping.c -- Test for handling different endianness in libsframe.
2
3 Copyright (C) 2022-2025 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #include "sframe-api.h"
25
26 /* DejaGnu should not use gnulib's vsnprintf replacement here. */
27 #undef vsnprintf
28 #include <dejagnu.h>
29
30 /* SFrame info from the following source (1 fde 5 fres):
31 static int cnt;
32 extern void foo (void);
33
34 int bar()
35 {
36 cnt++;
37 if (cnt == 3)
38 foo();
39 return (cnt);
40 }
41 gcc -mbig-endian -Wa,--gsframe -c -O3 t.c
42 objcopy --dump-section .sframe=DATA-BE t.o
43 */
44 #define DATA "DATA-BE"
45
46 int
47 main (void)
48 {
49 sframe_decoder_ctx *dctx = NULL;
50 uint32_t nfres, fsize;
51 int32_t fstart;
52 unsigned char finfo;
53 int err = 0;
54 FILE *fp;
55 struct stat st;
56 char *sf_buf;
57 size_t sf_size;
58 uint8_t rep_block_size;
59
60 #define TEST(name, cond) \
61 do \
62 { \
63 if (cond) \
64 pass (name); \
65 else \
66 fail (name); \
67 } \
68 while (0)
69
70 /* Test setup. */
71 fp = fopen (DATA, "r");
72 if (fp == NULL)
73 goto setup_fail;
74 if (fstat (fileno (fp), &st) < 0)
75 {
76 perror ("fstat");
77 fclose (fp);
78 goto setup_fail;
79 }
80 sf_buf = malloc (st.st_size);
81 if (sf_buf == NULL)
82 {
83 perror ("malloc");
84 goto setup_fail;
85 }
86 sf_size = fread (sf_buf, 1, st.st_size, fp);
87 fclose (fp);
88 if (sf_size == 0)
89 {
90 fprintf (stderr, "Decode: Read buffer failed\n");
91 goto setup_fail;
92 }
93
94 /* Execute tests. */
95
96 /* Call to sframe_decode will endian flip the input buffer (big-endian) if
97 the host running the test is a little-endian system. This endian-flipped
98 copy of the buffer is kept internally in dctx. */
99 dctx = sframe_decode (sf_buf, sf_size, &err);
100 TEST ("be-flipping: Decoder setup", dctx != NULL);
101
102 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
103 TEST ("be-flipping: Decoder FDE count", fde_cnt == 1);
104
105 err = sframe_decoder_get_funcdesc_v2 (dctx, 0, &nfres, &fsize, &fstart,
106 &finfo, &rep_block_size);
107 TEST ("be-flipping: Decoder get FDE", err == 0);
108 TEST ("be-flipping: Decoder FRE count", nfres == 5);
109
110 free (sf_buf);
111 sf_buf = NULL;
112
113 sframe_decoder_free (&dctx);
114 return 0;
115
116 setup_fail:
117 sframe_decoder_free (&dctx);
118 fail ("be-flipping: Test setup");
119 return 1;
120 }