mirror of
https://gitlab.xiph.org/xiph/icecast-server.git
synced 2025-02-02 15:07:36 -05:00
Cleanup: Removed unused buffer.[ch]
This commit is contained in:
parent
e4787e228d
commit
eb4d037842
@ -34,7 +34,6 @@ noinst_HEADERS = \
|
|||||||
matchfile.h \
|
matchfile.h \
|
||||||
tls.h \
|
tls.h \
|
||||||
refobject.h \
|
refobject.h \
|
||||||
buffer.h \
|
|
||||||
module.h \
|
module.h \
|
||||||
reportxml.h \
|
reportxml.h \
|
||||||
reportxml_helper.h \
|
reportxml_helper.h \
|
||||||
@ -89,7 +88,6 @@ icecast_SOURCES = \
|
|||||||
matchfile.c \
|
matchfile.c \
|
||||||
tls.c \
|
tls.c \
|
||||||
refobject.c \
|
refobject.c \
|
||||||
buffer.c \
|
|
||||||
module.c \
|
module.c \
|
||||||
reportxml.c \
|
reportxml.c \
|
||||||
reportxml_helper.c \
|
reportxml_helper.c \
|
||||||
|
311
src/buffer.c
311
src/buffer.c
@ -1,311 +0,0 @@
|
|||||||
/* Icecast
|
|
||||||
*
|
|
||||||
* This program is distributed under the GNU General Public License, version 2.
|
|
||||||
* A copy of this license is included with this source.
|
|
||||||
*
|
|
||||||
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
#include "buffer.h"
|
|
||||||
#include "refobject.h"
|
|
||||||
|
|
||||||
struct buffer_tag {
|
|
||||||
refobject_base_t __base;
|
|
||||||
/* Buffer itself */
|
|
||||||
void *buffer;
|
|
||||||
/* Length in bytes of buffer */
|
|
||||||
size_t length;
|
|
||||||
/* Amount of bytes in use of the buffer. This includes offset bytes */
|
|
||||||
size_t fill;
|
|
||||||
/* Bytes of offset at the start of the buffer */
|
|
||||||
size_t offset;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void __free(refobject_t self, void **userdata)
|
|
||||||
{
|
|
||||||
buffer_t *buffer = REFOBJECT_TO_TYPE(self, buffer_t*);
|
|
||||||
|
|
||||||
free(buffer->buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
REFOBJECT_DEFINE_TYPE(buffer_t,
|
|
||||||
REFOBJECT_DEFINE_TYPE_FREE(__free),
|
|
||||||
REFOBJECT_DEFINE_TYPE_NEW_NOOP()
|
|
||||||
);
|
|
||||||
|
|
||||||
buffer_t * buffer_new(ssize_t preallocation, void *userdata, const char *name, refobject_t associated)
|
|
||||||
{
|
|
||||||
buffer_t *buffer = refobject_new_ext(buffer_t, userdata, name, associated);
|
|
||||||
|
|
||||||
if (!buffer)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (preallocation > 0)
|
|
||||||
buffer_preallocate(buffer, preallocation);
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer_t * buffer_new_simple(void)
|
|
||||||
{
|
|
||||||
return refobject_new(buffer_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
void buffer_preallocate(buffer_t *buffer, size_t request)
|
|
||||||
{
|
|
||||||
void *n;
|
|
||||||
size_t newlen;
|
|
||||||
|
|
||||||
if (!buffer)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* Remove the offset if it makes sense to do so. */
|
|
||||||
if (buffer->offset == buffer->fill) {
|
|
||||||
buffer->offset = 0;
|
|
||||||
buffer->fill = 0;
|
|
||||||
} else if ((2*buffer->offset) < buffer->fill || buffer->offset >= 512 || (buffer->offset > 128 && buffer->offset >= request)) {
|
|
||||||
buffer->fill -= buffer->offset;
|
|
||||||
memmove(buffer->buffer, buffer->buffer + buffer->offset, buffer->fill);
|
|
||||||
buffer->offset = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!request)
|
|
||||||
return;
|
|
||||||
|
|
||||||
newlen = buffer->fill + request;
|
|
||||||
|
|
||||||
if (buffer->length >= newlen)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* Make sure we at least add 64 bytes and are 64 byte aligned */
|
|
||||||
newlen = newlen + 64 - (newlen % 64);
|
|
||||||
|
|
||||||
n = realloc(buffer->buffer, newlen);
|
|
||||||
|
|
||||||
/* Just return if this failed */
|
|
||||||
if (!n)
|
|
||||||
return;
|
|
||||||
|
|
||||||
buffer->buffer = n;
|
|
||||||
buffer->length = newlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_get_data(buffer_t *buffer, const void **data, size_t *length)
|
|
||||||
{
|
|
||||||
if (!buffer)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
*data = buffer->buffer + buffer->offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length) {
|
|
||||||
*length = buffer->fill - buffer->offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_get_string(buffer_t *buffer, const char **string)
|
|
||||||
{
|
|
||||||
char *ret;
|
|
||||||
|
|
||||||
if (!buffer || !string)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* Ensure we have space for one additional byte ('\0'-termination). */
|
|
||||||
if (buffer->length == buffer->fill) {
|
|
||||||
buffer_preallocate(buffer, 1);
|
|
||||||
if (buffer->length == buffer->fill)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Actually add a '\0'-termination. */
|
|
||||||
ret = buffer->buffer;
|
|
||||||
ret[buffer->fill] = 0;
|
|
||||||
*string = ret + buffer->offset;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_set_length(buffer_t *buffer, size_t length)
|
|
||||||
{
|
|
||||||
if (!buffer)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (length > (buffer->fill - buffer->offset))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
buffer->fill = length + buffer->offset;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_shift(buffer_t *buffer, size_t amount)
|
|
||||||
{
|
|
||||||
if (!buffer)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (amount > (buffer->fill - buffer->offset))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
buffer->offset += amount;
|
|
||||||
|
|
||||||
/* run cleanup */
|
|
||||||
buffer_preallocate(buffer, 0);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_push_data(buffer_t *buffer, const void *data, size_t length)
|
|
||||||
{
|
|
||||||
void *buf;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!buffer)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!length)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (!data)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
ret = buffer_zerocopy_push_request(buffer, &buf, length);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
memcpy(buf, data, length);
|
|
||||||
|
|
||||||
ret = buffer_zerocopy_push_complete(buffer, length);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_push_string(buffer_t *buffer, const char *string)
|
|
||||||
{
|
|
||||||
if (!buffer || !string)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return buffer_push_data(buffer, string, strlen(string));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int buffer_push_printf(buffer_t *buffer, const char *format, ...)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
if (!buffer || !format)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!*format)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
va_start(ap, format);
|
|
||||||
ret = buffer_push_vprintf(buffer, format, ap);
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_push_vprintf(buffer_t *buffer, const char *format, va_list ap)
|
|
||||||
{
|
|
||||||
void *buf;
|
|
||||||
int ret;
|
|
||||||
size_t length = 1024;
|
|
||||||
|
|
||||||
if (!buffer || !format)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!*format)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
ret = buffer_zerocopy_push_request(buffer, &buf, length);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = vsnprintf(buf, length, format, ap);
|
|
||||||
if (ret >= 0 && (size_t)ret < length) {
|
|
||||||
return buffer_zerocopy_push_complete(buffer, ret);
|
|
||||||
} else if (ret < 0) {
|
|
||||||
/* This vsnprintf() likely does not follow POSIX.
|
|
||||||
* We don't know what length we need to asume. So asume a big one and hope for the best. */
|
|
||||||
length = 8192;
|
|
||||||
} else {
|
|
||||||
/* Reallocate the buffer to the size reported plus one for '\0'-termination */
|
|
||||||
length = ret + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We have not written any data yet. */
|
|
||||||
ret = buffer_zerocopy_push_complete(buffer, 0);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
/* Now let's try again. */
|
|
||||||
ret = buffer_zerocopy_push_request(buffer, &buf, length);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = vsnprintf(buf, length, format, ap);
|
|
||||||
if (ret < 0 || (size_t)ret >= length) {
|
|
||||||
/* This still didn't work. Giving up. */
|
|
||||||
buffer_zerocopy_push_complete(buffer, 0);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return buffer_zerocopy_push_complete(buffer, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_push_buffer(buffer_t *buffer, buffer_t *source)
|
|
||||||
{
|
|
||||||
const void *data;
|
|
||||||
size_t length;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!buffer || !source)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
ret = buffer_get_data(source, &data, &length);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
return buffer_push_data(buffer, data, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_zerocopy_push_request(buffer_t *buffer, void **data, size_t request)
|
|
||||||
{
|
|
||||||
if (!buffer || !data)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
buffer_preallocate(buffer, request);
|
|
||||||
|
|
||||||
if (request > (buffer->length - buffer->fill))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
*data = buffer->buffer + buffer->fill;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int buffer_zerocopy_push_complete(buffer_t *buffer, size_t done)
|
|
||||||
{
|
|
||||||
if (!buffer)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (done > (buffer->length - buffer->fill))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
buffer->fill += done;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
193
src/buffer.h
193
src/buffer.h
@ -1,193 +0,0 @@
|
|||||||
/* Icecast
|
|
||||||
*
|
|
||||||
* This program is distributed under the GNU General Public License, version 2.
|
|
||||||
* A copy of this license is included with this source.
|
|
||||||
*
|
|
||||||
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file contains the API for a refobject based buffer object.
|
|
||||||
* It can be used to store data and allows on the fly re-allocation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __BUFFER_H__
|
|
||||||
#define __BUFFER_H__
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdarg.h>
|
|
||||||
|
|
||||||
#include "icecasttypes.h"
|
|
||||||
#include "compat.h"
|
|
||||||
#include "refobject.h"
|
|
||||||
|
|
||||||
/* About thread safety:
|
|
||||||
* This set of functions is intentinally not thread safe.
|
|
||||||
*/
|
|
||||||
|
|
||||||
REFOBJECT_FORWARD_TYPE(buffer_t);
|
|
||||||
|
|
||||||
/* This creates a new buffer object.
|
|
||||||
* Parameters:
|
|
||||||
* preallocation
|
|
||||||
* The number of bytes to allocate for use later on. See buffer_preallocate() for details.
|
|
||||||
* userdata, name, associated
|
|
||||||
* See refobject_new().
|
|
||||||
*/
|
|
||||||
buffer_t * buffer_new(ssize_t preallocation, void *userdata, const char *name, refobject_t associated);
|
|
||||||
|
|
||||||
/* Depreciated: This creates a new buffer with defaults.
|
|
||||||
* Do NOT use this. Use refobject_new(buffer_t)
|
|
||||||
*
|
|
||||||
* This is the same as:
|
|
||||||
* buffer_new(-1, NULL, NULL, REFOBJECT_NULL)
|
|
||||||
*/
|
|
||||||
buffer_t * buffer_new_simple(void);
|
|
||||||
|
|
||||||
/* This function preallocates space for later use.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* request
|
|
||||||
* Number of bytes to additionally allocate.
|
|
||||||
* Notes:
|
|
||||||
* This function is very usedful when adding a large number of smaller buffers to avoid
|
|
||||||
* internal reallocation calls happening to often. However it is not required to call
|
|
||||||
* this function before adding data to the buffer.
|
|
||||||
*/
|
|
||||||
void buffer_preallocate(buffer_t *buffer, size_t request);
|
|
||||||
|
|
||||||
/* Gets data and length of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* data
|
|
||||||
* Pointer to the stored data. If NULL the pointer is not returned.
|
|
||||||
* length
|
|
||||||
* Pointer to the length of how many bytes are in the buffer. If NULL
|
|
||||||
* length is not returned.
|
|
||||||
*/
|
|
||||||
int buffer_get_data(buffer_t *buffer, const void **data, size_t *length);
|
|
||||||
|
|
||||||
/* Gets data as a string. The string is '\0'-terminated.
|
|
||||||
* Parameters:
|
|
||||||
* buffery
|
|
||||||
* The buffer to operate on.
|
|
||||||
* string
|
|
||||||
* The string representing the data hold by the buffer.
|
|
||||||
*/
|
|
||||||
int buffer_get_string(buffer_t *buffer, const char **string);
|
|
||||||
|
|
||||||
/* Sets the length of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* length
|
|
||||||
* New length of the buffer.
|
|
||||||
* Notes:
|
|
||||||
* This can only be used to reduce the size of the buffer. To add data to
|
|
||||||
* the buffer use buffer_push_*().
|
|
||||||
*
|
|
||||||
* Calling this with length set to 0 clears the buffer but does not deallocate it.
|
|
||||||
*/
|
|
||||||
int buffer_set_length(buffer_t *buffer, size_t length);
|
|
||||||
|
|
||||||
/* Shifts data out of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* amount
|
|
||||||
* The amount of bytes to be removed from the begin of the buffer.
|
|
||||||
* Notes:
|
|
||||||
* This function can be useful for skipping some small header. However this
|
|
||||||
* must not be used to implement a kind of ring buffer as it will result in
|
|
||||||
* poor performance caused by massive reallocations and memory copies.
|
|
||||||
*/
|
|
||||||
int buffer_shift(buffer_t *buffer, size_t amount);
|
|
||||||
|
|
||||||
/* This pushes data to the end of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* data
|
|
||||||
* The data to push.
|
|
||||||
* length
|
|
||||||
* The length of the data to push in byte.
|
|
||||||
* Notes:
|
|
||||||
* Consider using buffer_zerocopy_*().
|
|
||||||
*/
|
|
||||||
int buffer_push_data(buffer_t *buffer, const void *data, size_t length);
|
|
||||||
|
|
||||||
/* This pushes a string to the end of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* string
|
|
||||||
* The string to be pushed. The tailing '\0'-termination will not be
|
|
||||||
* part of the buffer.
|
|
||||||
* Notes:
|
|
||||||
* Consider using buffer_zerocopy_*().
|
|
||||||
*/
|
|
||||||
int buffer_push_string(buffer_t *buffer, const char *string);
|
|
||||||
|
|
||||||
/* This pushes a formatted string to the end of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* format
|
|
||||||
* The format string as for printf() family functions.
|
|
||||||
* ...
|
|
||||||
* The parameters according to the format string.
|
|
||||||
*/
|
|
||||||
int buffer_push_printf(buffer_t *buffer, const char *format, ...);
|
|
||||||
|
|
||||||
/* This pushes a formatted string to the end of the buffer using a va_list.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* format
|
|
||||||
* The format string as for printf() family functions.
|
|
||||||
* ap
|
|
||||||
* The parameters according to the format string as va_list.
|
|
||||||
* See also:
|
|
||||||
* vprintf(3).
|
|
||||||
*/
|
|
||||||
int buffer_push_vprintf(buffer_t *buffer, const char *format, va_list ap);
|
|
||||||
|
|
||||||
/* This pushes the content of another buffer to the end of the buffer.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* source
|
|
||||||
* The buffer which's content is to be copied.
|
|
||||||
*/
|
|
||||||
int buffer_push_buffer(buffer_t *buffer, buffer_t *source);
|
|
||||||
|
|
||||||
/* This requests for a memory buffer that can be pushed to without the need for copy.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* data
|
|
||||||
* Pointer to memory that can be written and will become part of the buffer object.
|
|
||||||
* request
|
|
||||||
* Size of the memory area that is returned by data in bytes.
|
|
||||||
* Notes:
|
|
||||||
* This is the first step of the zero copy push. After the memory returned by data has been
|
|
||||||
* written (e.g. used in a call to read(2)) buffer_zerocopy_push_complete() must be called.
|
|
||||||
*/
|
|
||||||
int buffer_zerocopy_push_request(buffer_t *buffer, void **data, size_t request);
|
|
||||||
|
|
||||||
/* This is the final step of a zero copy push.
|
|
||||||
* Parameters:
|
|
||||||
* buffer
|
|
||||||
* The buffer to operate on.
|
|
||||||
* done
|
|
||||||
* Amount of data in bytes that has actually been written into the memory area.
|
|
||||||
* May be zero to what has been requested with request.
|
|
||||||
*/
|
|
||||||
int buffer_zerocopy_push_complete(buffer_t *buffer, size_t done);
|
|
||||||
|
|
||||||
#endif
|
|
@ -101,10 +101,6 @@ typedef enum {
|
|||||||
|
|
||||||
typedef struct relay_tag relay_t;
|
typedef struct relay_tag relay_t;
|
||||||
|
|
||||||
/* ---[ buffer.[ch] ]--- */
|
|
||||||
|
|
||||||
typedef struct buffer_tag buffer_t;
|
|
||||||
|
|
||||||
/* ---[ module.[ch] ]--- */
|
/* ---[ module.[ch] ]--- */
|
||||||
|
|
||||||
typedef struct module_tag module_t;
|
typedef struct module_tag module_t;
|
||||||
@ -138,7 +134,6 @@ typedef struct refobject_base_tag refobject_base_t;
|
|||||||
#ifdef HAVE_TYPE_ATTRIBUTE_TRANSPARENT_UNION
|
#ifdef HAVE_TYPE_ATTRIBUTE_TRANSPARENT_UNION
|
||||||
typedef union __attribute__ ((__transparent_union__)) {
|
typedef union __attribute__ ((__transparent_union__)) {
|
||||||
refobject_base_t *refobject_base;
|
refobject_base_t *refobject_base;
|
||||||
buffer_t *buffer;
|
|
||||||
reportxml_t *reportxml;
|
reportxml_t *reportxml;
|
||||||
reportxml_node_t *reportxml_node;
|
reportxml_node_t *reportxml_node;
|
||||||
reportxml_database_t *reportxml_database;
|
reportxml_database_t *reportxml_database;
|
||||||
|
@ -27,13 +27,5 @@ ctest_refobject_test_LDADD = \
|
|||||||
icecast-refobject.o
|
icecast-refobject.o
|
||||||
check_PROGRAMS += ctest_refobject.test
|
check_PROGRAMS += ctest_refobject.test
|
||||||
|
|
||||||
ctest_buffer_test_SOURCES = tests/ctest_buffer.c
|
|
||||||
ctest_buffer_test_LDADD = \
|
|
||||||
common/thread/libicethread.la \
|
|
||||||
common/avl/libiceavl.la \
|
|
||||||
icecast-refobject.o \
|
|
||||||
icecast-buffer.o
|
|
||||||
check_PROGRAMS += ctest_buffer.test
|
|
||||||
|
|
||||||
# Add all programs to TESTS
|
# Add all programs to TESTS
|
||||||
TESTS = $(check_PROGRAMS)
|
TESTS = $(check_PROGRAMS)
|
||||||
|
@ -1,371 +0,0 @@
|
|||||||
/* Icecast
|
|
||||||
*
|
|
||||||
* This program is distributed under the GNU General Public License, version 2.
|
|
||||||
* A copy of this license is included with this source.
|
|
||||||
*
|
|
||||||
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h> /* for EXIT_FAILURE */
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <igloo/tap.h>
|
|
||||||
|
|
||||||
#include "../src/buffer.h"
|
|
||||||
#include "../src/refobject.h"
|
|
||||||
|
|
||||||
static void ctest_diagnostic_printf(const char *format, ...)
|
|
||||||
{
|
|
||||||
char buf[1024];
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, format);
|
|
||||||
|
|
||||||
vsnprintf(buf, sizeof(buf), format, ap);
|
|
||||||
|
|
||||||
va_end(ap);
|
|
||||||
|
|
||||||
igloo_tap_diagnostic(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_create_ref_unref(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
|
|
||||||
a = buffer_new(-1, NULL, NULL, REFOBJECT_NULL);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_name(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const char *name = "test object name";
|
|
||||||
const char *ret;
|
|
||||||
|
|
||||||
a = buffer_new(-1, NULL, name, REFOBJECT_NULL);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
ret = refobject_get_name(a);
|
|
||||||
igloo_tap_test("get name", ret != NULL);
|
|
||||||
igloo_tap_test("name match", strcmp(name, ret) == 0);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_userdata(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
int tmp = 0;
|
|
||||||
void *userdata = &tmp;
|
|
||||||
void *ret;
|
|
||||||
|
|
||||||
a = buffer_new(-1, NULL, NULL, REFOBJECT_NULL);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
ret = refobject_get_userdata(a);
|
|
||||||
igloo_tap_test("get userdata", ret == NULL);
|
|
||||||
igloo_tap_test("set userdata", refobject_set_userdata(a, userdata) == 0);
|
|
||||||
ret = refobject_get_userdata(a);
|
|
||||||
igloo_tap_test("get userdata", ret == userdata);
|
|
||||||
igloo_tap_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
|
|
||||||
ret = refobject_get_userdata(a);
|
|
||||||
igloo_tap_test("get userdata", ret == NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
|
|
||||||
a = buffer_new(-1, userdata, NULL, REFOBJECT_NULL);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
igloo_tap_test("refobject created", !REFOBJECT_IS_NULL(a));
|
|
||||||
ret = refobject_get_userdata(a);
|
|
||||||
igloo_tap_test("get userdata", ret == userdata);
|
|
||||||
igloo_tap_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
|
|
||||||
ret = refobject_get_userdata(a);
|
|
||||||
igloo_tap_test("get userdata", ret == NULL);
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_associated(void)
|
|
||||||
{
|
|
||||||
refobject_base_t *a;
|
|
||||||
buffer_t *b;
|
|
||||||
|
|
||||||
a = refobject_new(refobject_base_t);
|
|
||||||
igloo_tap_test("refobject created", !REFOBJECT_IS_NULL(a));
|
|
||||||
|
|
||||||
|
|
||||||
b = buffer_new(-1, NULL, NULL, a);
|
|
||||||
igloo_tap_test("buffer created with associated", !REFOBJECT_IS_NULL(b));
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced (1 of 2)", refobject_unref(b) == 0);
|
|
||||||
igloo_tap_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_empty(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const void *data = &data;
|
|
||||||
size_t length = 5;
|
|
||||||
const char *string;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
ret = buffer_get_data(a, &data, &length);
|
|
||||||
igloo_tap_test("got data and length from buffer", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("data is updated", data != &data);
|
|
||||||
igloo_tap_test("length is zero", length == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
data = &data;
|
|
||||||
ret = buffer_get_data(a, &data, NULL);
|
|
||||||
igloo_tap_test("got data from buffer", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("data is updated", data != &data);
|
|
||||||
}
|
|
||||||
|
|
||||||
length = 5;
|
|
||||||
ret = buffer_get_data(a, NULL, &length);
|
|
||||||
igloo_tap_test("got length from buffer", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("length is zero", length == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = buffer_get_string(a, &string);
|
|
||||||
igloo_tap_test("got string from buffer", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("string is non-NULL", string != NULL);
|
|
||||||
if (string != NULL) {
|
|
||||||
igloo_tap_test("string is empty", *string == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_string(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const char *hw = "Hello World!";
|
|
||||||
const char *count = "0 1 2 3 4";
|
|
||||||
const char *combined = "Hello World!" "0 1 2 3 4";
|
|
||||||
const char *string = NULL;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
igloo_tap_test("pushed string", buffer_push_string(a, hw) == 0);
|
|
||||||
ret = buffer_get_string(a, &string);
|
|
||||||
igloo_tap_test("got strong", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("string is non-NULL", string != NULL);
|
|
||||||
if (string != NULL) {
|
|
||||||
igloo_tap_test("string matches input", strcmp(string, hw) == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
igloo_tap_test("pushed string", buffer_push_string(a, count) == 0);
|
|
||||||
string = NULL;
|
|
||||||
ret = buffer_get_string(a, &string);
|
|
||||||
igloo_tap_test("got strong", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("string is non-NULL", string != NULL);
|
|
||||||
if (string != NULL) {
|
|
||||||
igloo_tap_test("string matches combined input", strcmp(string, combined) == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_binary(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
char pattern_a[8] = {0x01, 0x10, 0x80, 0xFF, 0x00, 0x55, 0xEE, 0xAA};
|
|
||||||
char pattern_b[9] = {0x02, 0x03, 0xF0, 0x80, 0x0F, 0x04, 0x1A, 0x7F, 0x33};
|
|
||||||
int ret;
|
|
||||||
size_t length;
|
|
||||||
const void *data;
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed data pattern a", buffer_push_data(a, pattern_a, sizeof(pattern_a)) == 0);
|
|
||||||
length = sizeof(pattern_a) + 42;
|
|
||||||
data = &data;
|
|
||||||
ret = buffer_get_data(a, &data, &length);
|
|
||||||
igloo_tap_test("got data", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("correct length was returned", length == sizeof(pattern_a));
|
|
||||||
igloo_tap_test("data is non-NULL", data != NULL);
|
|
||||||
igloo_tap_test("data has been set", data != &data);
|
|
||||||
if (length == sizeof(pattern_a) && data != NULL && data != &data) {
|
|
||||||
igloo_tap_test("data matches pattern", memcmp(data, pattern_a, sizeof(pattern_a)) == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
igloo_tap_test("pushed data pattern b", buffer_push_data(a, pattern_b, sizeof(pattern_b)) == 0);
|
|
||||||
length = sizeof(pattern_a) + sizeof(pattern_b) + 42;
|
|
||||||
data = &data;
|
|
||||||
ret = buffer_get_data(a, &data, &length);
|
|
||||||
igloo_tap_test("got data", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("correct length was returned", length == (sizeof(pattern_a) + sizeof(pattern_b)));
|
|
||||||
igloo_tap_test("data is non-NULL", data != NULL);
|
|
||||||
igloo_tap_test("data has been set", data != &data);
|
|
||||||
if (length == (sizeof(pattern_a) + sizeof(pattern_b)) && data != NULL && data != &data) {
|
|
||||||
igloo_tap_test("data matches combined pattern", memcmp(data, pattern_a, sizeof(pattern_a)) == 0 && memcmp(data + sizeof(pattern_a), pattern_b, sizeof(pattern_b)) == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test__compare_to_string(buffer_t *a, const char *testname, const char *pattern)
|
|
||||||
{
|
|
||||||
const char *string = NULL;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = buffer_get_string(a, &string);
|
|
||||||
igloo_tap_test("got strong", ret == 0);
|
|
||||||
if (ret == 0) {
|
|
||||||
igloo_tap_test("string is non-NULL", string != NULL);
|
|
||||||
if (string != NULL) {
|
|
||||||
igloo_tap_test(testname, strcmp(string, pattern) == 0);
|
|
||||||
ctest_diagnostic_printf("string=\"%s\", pattern=\"%s\"", string, pattern);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_shift(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const char *pattern = "AABBBCC";
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed string", buffer_push_string(a, pattern) == 0);
|
|
||||||
test__compare_to_string(a, "string matches input", pattern);
|
|
||||||
igloo_tap_test("shifted data by 0 bytes", buffer_shift(a, 0) == 0);
|
|
||||||
test__compare_to_string(a, "string matches input (no shift happened)", pattern);
|
|
||||||
igloo_tap_test("shifted data by 2 bytes", buffer_shift(a, 2) == 0);
|
|
||||||
test__compare_to_string(a, "string matches shifted input", pattern + 2);
|
|
||||||
igloo_tap_test("shifted data by 3 bytes", buffer_shift(a, 3) == 0);
|
|
||||||
test__compare_to_string(a, "string matches shifted input", pattern + 2 + 3);
|
|
||||||
igloo_tap_test("shifted data by 3 bytes", buffer_shift(a, 2) == 0);
|
|
||||||
test__compare_to_string(a, "string matches shifted input", pattern + 2 + 3 + 2);
|
|
||||||
igloo_tap_test("shifted data beyond end (42 bytes)", buffer_shift(a, 42) != 0);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_length(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const char *pattern = "AABBBCC";
|
|
||||||
const char *match_a = "AABBB";
|
|
||||||
const char *match_b = "AABB";
|
|
||||||
const char *match_c = "";
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed string", buffer_push_string(a, pattern) == 0);
|
|
||||||
test__compare_to_string(a, "string matches input", pattern);
|
|
||||||
igloo_tap_test("Set length to match pattern a", buffer_set_length(a, strlen(match_a)) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern a", match_a);
|
|
||||||
igloo_tap_test("Set length to match pattern b", buffer_set_length(a, strlen(match_b)) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern a", match_b);
|
|
||||||
igloo_tap_test("Set length to match pattern c", buffer_set_length(a, strlen(match_c)) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern a", match_c);
|
|
||||||
igloo_tap_test("Set length to match pattern a (again)", buffer_set_length(a, strlen(match_a)) != 0);
|
|
||||||
test__compare_to_string(a, "string still matches pattern c", match_c);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_printf(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
const char *str = "Hello World!";
|
|
||||||
const int num = -127;
|
|
||||||
const char *match_a = ":Hello World!:";
|
|
||||||
const char *match_b = ":Hello World!:<-127 >";
|
|
||||||
const char *match_c = ":Hello World!:<-127 >? +127?";
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer created", a != NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("Set length to match pattern a", buffer_push_printf(a, ":%s:", str) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern a", match_a);
|
|
||||||
igloo_tap_test("Set length to match pattern a", buffer_push_printf(a, "<%-5i>", num) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern b", match_b);
|
|
||||||
igloo_tap_test("Set length to match pattern a", buffer_push_printf(a, "?%+5i?", -num) == 0);
|
|
||||||
test__compare_to_string(a, "string matches pattern c", match_c);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_push_buffer(void)
|
|
||||||
{
|
|
||||||
buffer_t *a;
|
|
||||||
buffer_t *b;
|
|
||||||
const char *pattern = "AABBBCC";
|
|
||||||
const char *match_a = "AABBBCCAABBBCC";
|
|
||||||
|
|
||||||
a = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer a created", a != NULL);
|
|
||||||
b = refobject_new(buffer_t);
|
|
||||||
igloo_tap_test("buffer b created", b != NULL);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed string", buffer_push_string(a, pattern) == 0);
|
|
||||||
test__compare_to_string(a, "string matches input", pattern);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed buffer a to b", buffer_push_buffer(b, a) == 0);
|
|
||||||
test__compare_to_string(b, "string matches input", pattern);
|
|
||||||
|
|
||||||
igloo_tap_test("pushed buffer a to b", buffer_push_buffer(b, a) == 0);
|
|
||||||
test__compare_to_string(b, "string matches pattern a", match_a);
|
|
||||||
|
|
||||||
igloo_tap_test("un-referenced b", refobject_unref(b) == 0);
|
|
||||||
igloo_tap_test("un-referenced a", refobject_unref(a) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main (void)
|
|
||||||
{
|
|
||||||
igloo_tap_init();
|
|
||||||
igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
|
|
||||||
|
|
||||||
igloo_tap_group_run("create-ref-unref", test_create_ref_unref);
|
|
||||||
|
|
||||||
igloo_tap_group_run("name", test_name);
|
|
||||||
igloo_tap_group_run("userdata", test_userdata);
|
|
||||||
igloo_tap_group_run("associated", test_associated);
|
|
||||||
|
|
||||||
igloo_tap_group_run("empty", test_empty);
|
|
||||||
igloo_tap_group_run("string", test_string);
|
|
||||||
igloo_tap_group_run("binary", test_binary);
|
|
||||||
|
|
||||||
igloo_tap_group_run("shift", test_shift);
|
|
||||||
igloo_tap_group_run("length", test_length);
|
|
||||||
|
|
||||||
igloo_tap_group_run("printf", test_printf);
|
|
||||||
igloo_tap_group_run("push_buffer", test_push_buffer);
|
|
||||||
|
|
||||||
igloo_tap_fin();
|
|
||||||
|
|
||||||
return EXIT_FAILURE; // return failure as we should never reach this point!
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user