summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_xasprintf.c
blob: 4b5d0935c4111c4e0798a3d61abda87c40445e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
 * SPDX-License-Identifier: BSD-3-Clause
 */


#include <setjmp.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <stdarg.h>  // Required by <cmocka.h>
#include <stddef.h>  // Required by <cmocka.h>
#include <setjmp.h>  // Required by <cmocka.h>
#include <stdint.h>  // Required by <cmocka.h>
#include <cmocka.h>

#include "string/sprintf.h"


#define assert_unreachable()  assert_true(0)

#define XASPRINTF_CALLED  (-36)
#define EXIT_CALLED       (42)
#define TEST_OK           (-6)


static jmp_buf  jmpb;


/**********************
 * WRAPPERS
 **********************/
int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
void __wrap_exit(int status);


int
__wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap)
{
	return mock() == -1 ? -1 : __real_vasprintf(p, fmt, ap);
}


void
__wrap_exit(int status)
{
	longjmp(jmpb, EXIT_CALLED);
}


/**********************
 * TEST
 **********************/
static void test_xasprintf_exit(void **state);
static void test_xasprintf_ok(void **state);


static void
test_xasprintf_exit(void **state)
{
	volatile int    len;
	char *volatile  p;

	will_return(__wrap_vasprintf, -1);

	len = 0;

	switch (setjmp(jmpb)) {
	case 0:
		len = XASPRINTF_CALLED;
		len = xasprintf(&p, "foo%s", "bar");
		assert_unreachable();
		break;
	case EXIT_CALLED:
		assert_int_equal(len, XASPRINTF_CALLED);
		len = TEST_OK;
		break;
	default:
		assert_unreachable();
		break;
	}

	assert_int_equal(len, TEST_OK);
}


static void
test_xasprintf_ok(void **state)
{
	int   len;
	char  *p;

	// Trick: it will actually return the length, not 0.
	will_return(__wrap_vasprintf, 0);

	len = xasprintf(&p, "foo%d%s", 1, "bar");
	assert_int_equal(len, strlen("foo1bar"));
	assert_string_equal(p, "foo1bar");
	free(p);
}


int
main(void)
{
    const struct CMUnitTest  tests[] = {
        cmocka_unit_test(test_xasprintf_exit),
        cmocka_unit_test(test_xasprintf_ok),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}