pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__compress_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2022-2024 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
13
14#include "mock_private.h"
15
16#define SIMPLE_DATA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
17
18const char *SIMPLE_COMPRESSED = "BZh41AY&SYO\x1ai";
19
20static void
21simple_compress(void **state)
22{
23 char *result = pcmk__assert_alloc(1024, sizeof(char));
24 unsigned int len;
25
26 assert_int_equal(pcmk__compress(SIMPLE_DATA, 40, 0, &result, &len), pcmk_rc_ok);
27 assert_memory_equal(result, SIMPLE_COMPRESSED, 13);
28}
29
30static void
31max_too_small(void **state)
32{
33 char *result = pcmk__assert_alloc(1024, sizeof(char));
34 unsigned int len;
35
36 assert_int_equal(pcmk__compress(SIMPLE_DATA, 40, 10, &result, &len), EFBIG);
37}
38
39static void
40calloc_fails(void **state) {
41 char *result = pcmk__assert_alloc(1024, sizeof(char));
42 unsigned int len;
43
46 {
47 pcmk__mock_calloc = true; // calloc() will return NULL
48 expect_value(__wrap_calloc, nmemb, (size_t) ((40 * 1.01) + 601));
49 expect_value(__wrap_calloc, size, sizeof(char));
50 pcmk__compress(SIMPLE_DATA, 40, 0, &result, &len);
51 pcmk__mock_calloc = false; // Use the real calloc()
52 }
53 );
54}
55
56PCMK__UNIT_TEST(NULL, NULL,
57 cmocka_unit_test(simple_compress),
58 cmocka_unit_test(max_too_small),
59 cmocka_unit_test(calloc_fails))
#define pcmk__assert_alloc(nmemb, size)
Definition internal.h:246
uint32_t size
Definition cpg.c:4
bool pcmk__mock_calloc
Definition mock.c:89
void * __wrap_calloc(size_t nmemb, size_t size)
Definition mock.c:92
const char * SIMPLE_COMPRESSED
#define SIMPLE_DATA
pcmk__action_result_t result
Definition pcmk_fence.c:37
@ CRM_EX_OSERR
External (OS/environmental) problem.
Definition results.h:254
@ pcmk_rc_ok
Definition results.h:159
int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len)
Definition strings.c:839
#define pcmk__assert_exits(rc, stmt)
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)