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
|
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
/* test_ctx.c
** libstrophe XMPP client library -- test routines for the library run-time
*context
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
** This software is provided AS-IS with no warranty, either express
** or implied.
**
** This program is dual licensed under the MIT or GPLv3 licenses.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "strophe.h"
#include "common.h"
static int log_called = 0;
static int mem_alloc_called = 0;
static int mem_free_called = 0;
static int mem_realloc_called = 0;
void *my_alloc(size_t size, void *userdata)
{
(void)userdata;
mem_alloc_called++;
return malloc(size);
}
void my_free(void *p, void *userdata)
{
(void)userdata;
mem_free_called++;
return free(p);
}
void *my_realloc(void *p, size_t size, void *userdata)
{
(void)userdata;
mem_realloc_called++;
return realloc(p, size);
}
void my_logger(void *userdata,
xmpp_log_level_t level,
const char *area,
const char *msg)
{
if (strcmp((char *)userdata, "asdf") == 0 && level == XMPP_LEVEL_DEBUG &&
strcmp(area, "test") == 0 && strcmp(msg, "hello") == 0)
log_called++;
}
int main()
{
xmpp_ctx_t *ctx;
xmpp_mem_t mymem;
xmpp_log_t mylog;
char my_str[5] = "asdf";
void *testptr1, *testptr2;
ctx = xmpp_ctx_new(NULL, NULL);
if (ctx == NULL)
return 1;
/* destroy context */
xmpp_ctx_free(ctx);
/* setup our memory handler */
mymem.alloc = my_alloc;
mymem.free = my_free;
mymem.realloc = my_realloc;
/* setup our logger */
mylog.handler = my_logger;
mylog.userdata = my_str;
ctx = xmpp_ctx_new(&mymem, &mylog);
strophe_debug(ctx, "test", "hello");
testptr1 = strophe_alloc(ctx, 1024);
if (testptr1 == NULL) {
xmpp_ctx_free(ctx);
return 1;
}
testptr2 = strophe_realloc(ctx, testptr1, 2048);
if (testptr2 == NULL) {
strophe_free(ctx, testptr1);
xmpp_ctx_free(ctx);
return 1;
}
strophe_free(ctx, testptr2);
xmpp_ctx_free(ctx);
/* check for test failure */
if (!(log_called && mem_alloc_called && mem_realloc_called &&
mem_free_called))
return 1;
if (mem_alloc_called != mem_free_called)
return 1;
return 0;
}
|