Merge branch 'upstream' into feature/bullseye/1.0.55

This commit is contained in:
r3vn 2021-06-07 15:11:49 +02:00
commit 097d65f66d
No known key found for this signature in database
GPG Key ID: B66A4E32FBF108BB
10 changed files with 400 additions and 62 deletions

View File

@ -20,7 +20,7 @@ all: debug release pkgconfig
VERSION_MAJOR = 1
VERSION_MINOR = 0
VERSION_RELEASE = 52
VERSION_RELEASE = 55
# Version for pkg-config
PCVERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_RELEASE)

23
debian/changelog vendored
View File

@ -1,3 +1,26 @@
libglibutil (1.0.55) unstable; urgency=low
* Added gutil_log_dump()
* Added gutil_range_init_with_bytes()
* Added gutil_range_has_prefix()
* Added gutil_range_skip_prefix()
-- Slava Monich <slava.monich@jolla.com> Sun, 23 May 2021 03:33:58 +0300
libglibutil (1.0.54) unstable; urgency=low
* Added GUtilRange type
* Tweaked interpretation of environment variables
-- Slava Monich <slava.monich@jolla.com> Mon, 17 May 2021 16:06:25 +0300
libglibutil (1.0.53) unstable; urgency=low
* Optimized gutil_parse_int()
* Added gutil_parse_uint()
-- Slava Monich <slava.monich@jolla.com> Wed, 05 May 2021 15:30:57 +0300
libglibutil (1.0.52) unstable; urgency=low
* Added gutil_memdup()

View File

@ -14,8 +14,8 @@
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -139,6 +139,14 @@ gutil_logv(
const char* format,
va_list va);
void
gutil_log_dump(
const GLogModule* module,
int level,
const char* prefix,
const void* data,
gsize size); /* Since 1.0.55 */
/* Check if logging is enabled for the specified log level */
gboolean
gutil_log_enabled(
@ -318,9 +326,12 @@ gutil_log_assert(
GLOG_LEVEL_DEBUG, f, ##args)
# define GDEBUG_(f,args...) gutil_log(GLOG_MODULE_CURRENT, \
GLOG_LEVEL_DEBUG, "%s() " f, __FUNCTION__, ##args)
# define GDEBUG_DUMP(buf,n) gutil_log_dump(GLOG_MODULE_CURRENT, \
GLOG_LEVEL_DEBUG, NULL, buf, n) /* Since 1.0.55 */
# else
# define GDEBUG(f,args...) GLOG_NOTHING
# define GDEBUG_(f,args...) GLOG_NOTHING
# define GDEBUG_DUMP(buf,n) GLOG_NOTHING /* Since 1.0.55 */
# endif /* GUTIL_LOG_DEBUG */
#else
# define GDEBUG_ GDEBUG

View File

@ -69,6 +69,12 @@ gutil_parse_int(
int base,
int* value); /* Since 1.0.30 */
gboolean
gutil_parse_uint(
const char* str,
int base,
unsigned int* value); /* Since 1.0.53 */
gboolean
gutil_data_equal(
const GUtilData* data1,
@ -130,6 +136,21 @@ gutil_memdup(
const void* ptr,
gsize size); /* Since 1.0.52 */
gsize
gutil_range_init_with_bytes(
GUtilRange* range,
GBytes* bytes); /* Since 1.0.55 */
gboolean
gutil_range_has_prefix(
const GUtilRange* range,
const GUtilData* prefix); /* Since 1.0.55 */
gboolean
gutil_range_skip_prefix(
GUtilRange* range,
const GUtilData* prefix); /* Since 1.0.55 */
G_END_DECLS
#endif /* GUTIL_MISC_H */

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2018 Jolla Ltd.
* Copyright (C) 2014-2018 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2014-2021 Jolla Ltd.
* Copyright (C) 2014-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
@ -14,8 +14,8 @@
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -54,6 +54,11 @@ typedef struct gutil_data {
gsize size;
} GUtilData;
typedef struct gutil_range {
const guint8* ptr;
const guint8* end;
} GUtilRange; /* Since 1.0.54 */
#define GLOG_MODULE_DECL(m) extern GLogModule m;
typedef struct glog_module GLogModule;

View File

@ -1,10 +1,12 @@
Name: libglibutil
Version: 1.0.52
Version: 1.0.55
Release: 0
Summary: Library of glib utilities
License: BSD
URL: https://git.sailfishos.org/mer-core/libglibutil
Source: %{name}-%{version}.tar.bz2
BuildRequires: pkgconfig(glib-2.0)
Requires(post): /sbin/ldconfig
Requires(postun): /sbin/ldconfig

View File

@ -490,6 +490,30 @@ gutil_log_enabled(
return FALSE;
}
void
gutil_log_dump(
const GLogModule* module,
int level,
const char* prefix,
const void* data,
gsize size) /* Since 1.0.55 */
{
if (gutil_log_enabled(module, level)) {
const guint8* ptr = data;
guint off = 0;
if (!prefix) prefix = "";
while (size > 0) {
char buf[GUTIL_HEXDUMP_BUFSIZE];
const guint consumed = gutil_hexdump(buf, ptr + off, size);
gutil_log(module, level, "%s%04X: %s", prefix, off, buf);
size -= consumed;
off += consumed;
}
}
}
/* gutil_log_parse_option helper */
static
int
@ -657,12 +681,12 @@ gutil_log_init()
GDEBUG("Default log level %d", val);
}
if (gutil_parse_int(getenv("GUTIL_LOG_TIMESTAMP"), 0, &val) && val > 0) {
if (gutil_parse_int(getenv("GUTIL_LOG_TIMESTAMP"), 0, &val) && val >= 0) {
gutil_log_timestamp = (val > 0);
GDEBUG("Timestamps %s", (val > 0) ? "enabled" : "disabled");
}
if (gutil_parse_int(getenv("GUTIL_LOG_TID"), 0, &val) && val > 0) {
if (gutil_parse_int(getenv("GUTIL_LOG_TID"), 0, &val) && val >= 0) {
gutil_log_tid = (val > 0);
GDEBUG("Thread id prefix %s", (val > 0) ? "enabled" : "disabled");
}

View File

@ -36,7 +36,6 @@
#include <ctype.h>
#include <limits.h>
#include <errno.h>
void
gutil_disconnect_handlers(
@ -46,6 +45,7 @@ gutil_disconnect_handlers(
{
if (G_LIKELY(instance) && G_LIKELY(ids)) {
int i;
for (i=0; i<count; i++) {
if (ids[i]) {
g_signal_handler_disconnect(instance, ids[i]);
@ -64,6 +64,7 @@ gutil_hex2bin(
if (str && data && len > 0 && !(len & 1)) {
gssize i;
guint8* ptr = data;
for (i=0; i<len; i+=2) {
static const guint8 hex[] = {
0, 1, 2, 3, 4, 5, 6, 7, /* 0x30..0x37 */
@ -76,7 +77,8 @@ gutil_hex2bin(
};
const char x1 = str[i];
const char x2 = str[i+1];
if (isxdigit(x1) && isxdigit(x2)) {
if (g_ascii_isxdigit(x1) && g_ascii_isxdigit(x2)) {
*ptr++ = (hex[x1-0x30] << 4) + hex[x2-0x30];
} else {
return NULL;
@ -95,9 +97,11 @@ gutil_hex2bytes(
if (str) {
if (len < 0) len = strlen(str);
if (len > 0 && !(len & 1)) {
void* data = g_malloc(len/2);
const gsize n = len/2;
void* data = g_malloc(n);
if (gutil_hex2bin(str, len, data)) {
return g_bytes_new_take(data, len/2);
return g_bytes_new_take(data, n);
}
g_free(data);
}
@ -153,36 +157,74 @@ gutil_hexdump(
return bytes_dumped;
}
/* Since 1.0.30 */
static
const char*
gutil_strstrip(
const char* str,
char** tmp)
{
/* Caller makes sure that str isn't NULL */
const gsize len = strlen(str);
if (g_ascii_isspace(str[0]) || g_ascii_isspace(str[len - 1])) {
/* Need to modify the original string */
return (*tmp = g_strstrip(gutil_memdup(str, len + 1)));
} else {
/* The original string is fine as is */
return str;
}
}
gboolean
gutil_parse_int(
const char* str,
int base,
int* value)
int* value) /* Since 1.0.30 */
{
gboolean ok = FALSE;
if (str && str[0]) {
char* str2 = g_strstrip(g_strdup(str));
char* end = str2;
gint64 l;
char* tmp = NULL;
char* end = NULL;
const char* stripped = gutil_strstrip(str, &tmp);
const gint64 ll = g_ascii_strtoll(stripped, &end, base);
errno = 0;
l = g_ascii_strtoll(str2, &end, base);
ok = !*end && errno != ERANGE && l >= INT_MIN && l <= INT_MAX;
ok = !*end && ll >= INT_MIN && ll <= INT_MAX;
if (ok && value) {
*value = (int)l;
*value = (int)ll;
}
g_free(str2);
g_free(tmp);
}
return ok;
}
gboolean
gutil_parse_uint(
const char* str,
int base,
unsigned int* value) /* Since 1.0.53 */
{
gboolean ok = FALSE;
if (str && str[0]) {
char* tmp = NULL;
char* end = NULL;
const char* stripped = gutil_strstrip(str, &tmp);
const guint64 ull = g_ascii_strtoull(stripped, &end, base);
ok = !*end && ull <= UINT_MAX;
if (ok && value) {
*value = (unsigned int)ull;
}
g_free(tmp);
}
return ok;
}
/* since 1.0.31 */
gboolean
gutil_data_equal(
const GUtilData* data1,
const GUtilData* data2)
const GUtilData* data2) /* Since 1.0.31 */
{
if (data1 == data2) {
return TRUE;
@ -460,6 +502,52 @@ gutil_memdup(
}
}
gsize
gutil_range_init_with_bytes(
GUtilRange* range,
GBytes* bytes) /* Since 1.0.55 */
{
gsize size = 0;
if (G_LIKELY(range)) {
if (G_LIKELY(bytes)) {
range->ptr = (const guint8*) g_bytes_get_data(bytes, &size);
range->end = range->ptr + size;
} else {
memset(range, 0, sizeof(*range));
}
}
return size;
}
gboolean
gutil_range_has_prefix(
const GUtilRange* range,
const GUtilData* prefix) /* Since 1.0.55 */
{
if (G_LIKELY(range) && G_LIKELY(range->ptr) && G_LIKELY(prefix)) {
if (range->end > range->ptr) {
return (gsize)(range->end - range->ptr) >= prefix->size &&
!memcmp(range->ptr, prefix->bytes, prefix->size);
} else {
return !prefix->size;
}
}
return FALSE;
}
gboolean
gutil_range_skip_prefix(
GUtilRange* range,
const GUtilData* prefix) /* Since 1.0.55 */
{
if (gutil_range_has_prefix(range, prefix)) {
range->ptr += prefix->size;
return TRUE;
}
return FALSE;
}
/*
* Local Variables:
* mode: C

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2017-2019 Jolla Ltd.
* Copyright (C) 2017-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2017-2021 Jolla Ltd.
* Copyright (C) 2017-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
@ -14,8 +14,8 @@
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -41,24 +41,24 @@
#include "gutil_log.h"
static TestOpt test_opt;
/*==========================================================================*
* Basic
*==========================================================================*/
static GString* test_log_basic_buf;
static GString* test_log_buf;
static
void
test_log_basic_fn(
test_log_fn(
const char* name,
int level,
const char* format,
va_list va)
{
g_string_append_vprintf(test_log_basic_buf, format, va);
g_string_append_vprintf(test_log_buf, format, va);
g_string_append_c(test_log_buf, '\n');
}
/*==========================================================================*
* Basic
*==========================================================================*/
static
void
test_log_basic(
@ -68,8 +68,8 @@ test_log_basic(
const GLogProc2 fn2 = gutil_log_func2;
const int level = gutil_log_default.level;
GLOG_MODULE_DEFINE2_(module, "test", gutil_log_default);
test_log_basic_buf = g_string_new(NULL);
gutil_log_func = test_log_basic_fn;
test_log_buf = g_string_new(NULL);
gutil_log_func = test_log_fn;
module.level = GLOG_LEVEL_INHERIT;
gutil_log_default.level = GLOG_LEVEL_ERR;
@ -77,10 +77,10 @@ test_log_basic(
gutil_log(NULL, GLOG_LEVEL_DEBUG, "Debug!");
gutil_log(&module, GLOG_LEVEL_DEBUG, "Debug!");
gutil_log_assert(NULL, GLOG_LEVEL_WARN, "Test!", __FILE__, __LINE__);
g_assert(!test_log_basic_buf->len);
g_assert(!test_log_buf->len);
gutil_log(&module, GLOG_LEVEL_ERR, "Err!");
g_assert(test_log_basic_buf->len);
g_string_set_size(test_log_basic_buf, 0);
g_assert(test_log_buf->len);
g_string_set_size(test_log_buf, 0);
/* With NULL parent, still gutil_log_default is going to be checked */
module.parent = NULL;
@ -88,19 +88,19 @@ test_log_basic(
gutil_log(NULL, GLOG_LEVEL_DEBUG, "Debug!");
gutil_log(&module, GLOG_LEVEL_DEBUG, "Debug!");
gutil_log_assert(NULL, GLOG_LEVEL_WARN, "Test!", __FILE__, __LINE__);
g_assert(!test_log_basic_buf->len);
g_assert(!test_log_buf->len);
gutil_log(&module, GLOG_LEVEL_ERR, "Err!");
g_assert(test_log_basic_buf->len);
g_string_set_size(test_log_basic_buf, 0);
g_assert(test_log_buf->len);
g_string_set_size(test_log_buf, 0);
gutil_log(&module, GLOG_LEVEL_ALWAYS, "Always!");
g_assert(test_log_basic_buf->len);
g_string_set_size(test_log_basic_buf, 0);
g_assert(test_log_buf->len);
g_string_set_size(test_log_buf, 0);
/* Test GLOG_FLAG_DISABLE */
module.flags |= GLOG_FLAG_DISABLE;
gutil_log(&module, GLOG_LEVEL_ALWAYS, "Always!");
g_assert(!test_log_basic_buf->len);
g_assert(!test_log_buf->len);
module.flags &= ~GLOG_FLAG_DISABLE;
/* Without log functions these calls have no effect */
@ -109,8 +109,8 @@ test_log_basic(
gutil_log_func2 = NULL;
gutil_log(NULL, GLOG_LEVEL_ALWAYS, "Always!");
g_string_free(test_log_basic_buf, TRUE);
test_log_basic_buf = NULL;
g_string_free(test_log_buf, TRUE);
test_log_buf = NULL;
gutil_log_default.level = level;
gutil_log_func = fn;
gutil_log_func2 = fn2;
@ -169,7 +169,7 @@ test_log_file(
stdout = default_stdout;
g_assert(fflush(out) == 0);
GDEBUG("%s", buf->str);
g_assert(!g_strcmp0(buf->str, "WARNING: Test\n"));
g_assert_cmpstr(buf->str, == ,"WARNING: Test\n");
g_string_set_size(buf, 0);
/* Error prefix */
@ -178,7 +178,7 @@ test_log_file(
stdout = default_stdout;
g_assert(fflush(out) == 0);
GDEBUG("%s", buf->str);
g_assert(!g_strcmp0(buf->str, "ERROR: Test\n"));
g_assert_cmpstr(buf->str, == ,"ERROR: Test\n");
g_string_set_size(buf, 0);
/* Empty name (dropped) */
@ -188,7 +188,7 @@ test_log_file(
stdout = default_stdout;
g_assert(fflush(out) == 0);
GDEBUG("%s", buf->str);
g_assert(!g_strcmp0(buf->str, "Test\n"));
g_assert_cmpstr(buf->str, == ,"Test\n");
g_string_set_size(buf, 0);
/* Non-empty name */
@ -198,7 +198,7 @@ test_log_file(
stdout = default_stdout;
g_assert(fflush(out) == 0);
GDEBUG("%s", buf->str);
g_assert(!g_strcmp0(buf->str, "[test] Test\n"));
g_assert_cmpstr(buf->str, == ,"[test] Test\n");
g_string_set_size(buf, 0);
/* Hide the name */
@ -208,7 +208,7 @@ test_log_file(
stdout = default_stdout;
g_assert(fflush(out) == 0);
GDEBUG("%s", buf->str);
g_assert(!g_strcmp0(buf->str, "Test\n"));
g_assert_cmpstr(buf->str, == ,"Test\n");
g_string_set_size(buf, 0);
/* Forward output to test_log_drop */
@ -284,13 +284,56 @@ test_log_misc(
const GLogProc fn = gutil_log_func;
g_assert(gutil_log_set_type(GLOG_TYPE_STDOUT, "test"));
g_assert(gutil_log_func == gutil_log_stdout);
g_assert(!g_strcmp0(gutil_log_get_type(), GLOG_TYPE_STDOUT));
g_assert_cmpstr(gutil_log_get_type(), == ,GLOG_TYPE_STDOUT);
g_assert(gutil_log_set_type(GLOG_TYPE_STDERR, "test"));
g_assert(gutil_log_func == gutil_log_stderr);
g_assert(!g_strcmp0(gutil_log_get_type(), GLOG_TYPE_STDERR));
g_assert_cmpstr(gutil_log_get_type(), == ,GLOG_TYPE_STDERR);
g_assert(!gutil_log_set_type("whatever", "test"));
gutil_log_func = NULL;
g_assert(!g_strcmp0(gutil_log_get_type(), GLOG_TYPE_CUSTOM));
g_assert_cmpstr(gutil_log_get_type(), == ,GLOG_TYPE_CUSTOM);
gutil_log_func = fn;
}
/*==========================================================================*
* Dump
*==========================================================================*/
static
void
test_log_dump(
void)
{
static const guint8 short_data[] = { 0x01, 0x02, 0x03, 0x04 };
static const guint8 long_data[] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x00
};
const GLogProc fn = gutil_log_func;
test_log_buf = g_string_new(NULL);
gutil_log_func = test_log_fn;
gutil_log_dump(&gutil_log_default, GLOG_LEVEL_NONE, NULL,
short_data, sizeof(short_data));
g_assert_cmpuint(test_log_buf->len, == ,0);
gutil_log_dump(&gutil_log_default, GLOG_LEVEL_ALWAYS, " ",
short_data, sizeof(short_data));
g_assert_cmpstr(test_log_buf->str, == ,
" 0000: 01 02 03 04 ....\n");
g_string_set_size(test_log_buf, 0);
gutil_log_dump(&gutil_log_default, GLOG_LEVEL_ALWAYS, NULL,
long_data, sizeof(long_data));
g_assert_cmpstr(test_log_buf->str, == ,
"0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f "
"01234567 89:;<=>?\n"
"0010: 00 "
".\n");
g_string_free(test_log_buf, TRUE);
test_log_buf = NULL;
gutil_log_func = fn;
}
@ -309,6 +352,7 @@ int main(int argc, char* argv[])
#endif
g_test_add_func(TEST_PREFIX "enabled", test_log_enabled);
g_test_add_func(TEST_PREFIX "misc", test_log_misc);
g_test_add_func(TEST_PREFIX "dump", test_log_dump);
test_init(&test_opt, argc, argv);
return g_test_run();
}

View File

@ -175,17 +175,58 @@ test_parse_int(
g_assert(!gutil_parse_int("0 trailing garbage", 0, NULL));
g_assert(gutil_parse_int("0", 0, NULL));
g_assert(gutil_parse_int("0", 0, &value));
g_assert(value == 0);
g_assert_cmpint(value, == ,0);
g_assert(gutil_parse_int("-1", 0, &value));
g_assert_cmpint(value, == ,-1);
g_assert(gutil_parse_int("42", 0, &value));
g_assert_cmpint(value, == ,42);
g_assert(!gutil_parse_int("0x10000000000000000", 0, &value));
g_assert(!gutil_parse_int("-2147483649", 0, &value));
g_assert(!gutil_parse_int("4294967295", 0, &value));
g_assert(gutil_parse_int(" 0x7fffffff ", 0, &value));
g_assert(value == 0x7fffffff);
g_assert_cmpint(value, == ,0x7fffffff);
g_assert(gutil_parse_int(" 7fffffff ", 16, &value));
g_assert(value == 0x7fffffff);
g_assert_cmpint(value, == ,0x7fffffff);
g_assert(gutil_parse_int("7ffffffe ", 16, &value));
g_assert_cmpint(value, == ,0x7ffffffe);
g_assert(!gutil_parse_int("0xffffffff", 0, &value));
}
/*==========================================================================*
* parse_uint
*==========================================================================*/
static
void
test_parse_uint(
void)
{
unsigned int value;
g_assert(!gutil_parse_uint(NULL, 0, NULL));
g_assert(!gutil_parse_uint("", 0, NULL));
g_assert(!gutil_parse_uint("garbage", 0, NULL));
g_assert(!gutil_parse_uint("0 trailing garbage", 0, NULL));
g_assert(gutil_parse_uint("0", 0, NULL));
g_assert(gutil_parse_uint("0", 0, &value));
g_assert_cmpuint(value, == ,0);
g_assert(gutil_parse_uint("42", 0, &value));
g_assert_cmpuint(value, == ,42);
g_assert(!gutil_parse_uint("0x10000000000000000", 0, &value));
g_assert(!gutil_parse_uint("-2147483649", 0, &value));
g_assert(!gutil_parse_uint("-1", 0, &value));
g_assert(gutil_parse_uint("4294967295", 0, &value));
g_assert_cmpuint(value, == ,4294967295);
g_assert(gutil_parse_uint(" 0x7fffffff ", 0, &value));
g_assert_cmpuint(value, == ,0x7fffffff);
g_assert(gutil_parse_uint(" 7fffffff ", 16, &value));
g_assert_cmpuint(value, == ,0x7fffffff);
g_assert(gutil_parse_uint("7ffffffe ", 16, &value));
g_assert_cmpuint(value, == ,0x7ffffffe);
g_assert(gutil_parse_uint("0xffffffff", 0, &value));
g_assert_cmpuint(value, == ,0xffffffff);
}
/*==========================================================================*
* data_equal
*==========================================================================*/
@ -540,6 +581,82 @@ test_memdup(
g_assert(!gutil_memdup(NULL, 1));
}
/*==========================================================================*
* range_init
*==========================================================================*/
static
void
test_range_init(
void)
{
static const guint8 data[] = { 0x01, 0x02, 0x03 };
GBytes* bytes = g_bytes_new_static(data, sizeof(data));
GUtilRange range;
g_assert(!gutil_range_init_with_bytes(NULL, NULL));
g_assert(!gutil_range_init_with_bytes(&range, NULL));
g_assert(!range.ptr);
g_assert(!range.end);
g_assert(gutil_range_init_with_bytes(&range, bytes) == sizeof(data));
g_assert(range.ptr == data);
g_assert(range.end == (data + sizeof(data)));
g_bytes_unref(bytes);
}
/*==========================================================================*
* range_prefix
*==========================================================================*/
static
void
test_range_prefix(
void)
{
static const guint8 data[] = { 0x01, 0x02, 0x03, 0x04 };
static const guint8 prefix[] = { 0x01, 0x02 };
static const guint8 not_prefix[] = { 0x03, 0x04 };
static const guint8 too_long[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
GUtilData prefix_data, not_prefix_data, too_long_data;
GUtilRange range;
memset(&range, 0, sizeof(range));
g_assert(!gutil_range_has_prefix(NULL, NULL));
g_assert(!gutil_range_has_prefix(&range, NULL));
not_prefix_data.bytes = not_prefix;
not_prefix_data.size = sizeof(not_prefix);
too_long_data.bytes = too_long;
too_long_data.size = sizeof(too_long);
range.end = range.ptr = data; /* Empty range */
memset(&prefix_data, 0, sizeof(prefix_data)); /* Empty prefix */
/* Empty range doesn't have NULL prefix */
g_assert(!gutil_range_has_prefix(&range, NULL));
/* But does have empty prefix */
g_assert(gutil_range_has_prefix(&range, &prefix_data));
/* And doesn't have non-empty prefix */
g_assert(!gutil_range_has_prefix(&range, &not_prefix_data));
prefix_data.bytes = prefix;
prefix_data.size = sizeof(prefix);
g_assert(!gutil_range_has_prefix(&range, &prefix_data));
range.end = range.ptr + sizeof(data);
g_assert(gutil_range_has_prefix(&range, &prefix_data));
g_assert(!gutil_range_has_prefix(&range, &not_prefix_data));
g_assert(!gutil_range_has_prefix(&range, &too_long_data));
/* Test skipping */
g_assert(!gutil_range_skip_prefix(&range, &not_prefix_data));
g_assert(range.ptr == data);
g_assert(gutil_range_skip_prefix(&range, &prefix_data));
g_assert(range.ptr == (data + prefix_data.size));
}
/*==========================================================================*
* Common
*==========================================================================*/
@ -561,6 +678,7 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("hex2bin"), test_hex2bin);
g_test_add_func(TEST_("hexdump"), test_hexdump);
g_test_add_func(TEST_("parse_int"), test_parse_int);
g_test_add_func(TEST_("parse_uint"), test_parse_uint);
g_test_add_func(TEST_("data_equal"), test_data_equal);
g_test_add_func(TEST_("data_prefix"), test_data_prefix);
g_test_add_func(TEST_("data_suffix"), test_data_suffix);
@ -572,6 +690,8 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("ptrv_lenght"), test_ptrv_length);
g_test_add_func(TEST_("ptrv_free"), test_ptrv_free);
g_test_add_func(TEST_("memdup"), test_memdup);
g_test_add_func(TEST_("range_init"), test_range_init);
g_test_add_func(TEST_("range_prefix"), test_range_prefix);
test_init(&test_opt, argc, argv);
return g_test_run();
}