Skip to content

Commit

Permalink
msg utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Pabón committed Feb 3, 2016
1 parent 02a1af1 commit c69b22d
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 68 deletions.
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
lib_LTLIBRARIES = libpblc.la
libpblc_la_SOURCES = pblc.c msg.c
libpblc_la_SOURCES = pblc.c \
msg.c \
msg_utils.c
libpblc_la_LDFLAGS = $(MSGPACK_LIBS)
libpblc_la_CFLAGS = $(MSGPACK_CFLAGS)

Expand All @@ -22,7 +24,7 @@ TESTS += pblc_test
# msg tests
msg_test_CFLAGS = $(UNITTEST_CFLAGS) $(MSGPACK_CFLAGS)
msg_test_LDFLAGS = $(UNITTEST_LIBS) $(MSGPACK_LIBS)
msg_test_SOURCES = msg_test.c msg.c
msg_test_SOURCES = msg_test.c msg.c msg_utils.c
TESTS += msg_test

# --- End UNIT TEST
Expand Down
70 changes: 4 additions & 66 deletions src/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
#include <cmockery/pbc.h>

#include "msg.h"
#include "msg_utils.h"

#ifdef UNIT_TESTING
#include <cmockery/cmockery_override.h>
#ifdef strndup
#undef strndup
#endif
#define strndup test_strndup
char * test_strndup(const char *s, size_t n);
#endif

int
Expand Down Expand Up @@ -122,55 +118,6 @@ msg_put_marshal(const msg_t *m) {
return sbuf;
}

int
msg_unpack_next_u64(uint64_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off) {

REQUIRE(value != NULL);
REQUIRE(upk != NULL);
REQUIRE(data != NULL);
REQUIRE(len > 0);

msgpack_unpack_return ret;

ret = msgpack_unpack_next(upk, data, len, off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
return -1;
}
if (MSGPACK_OBJECT_POSITIVE_INTEGER != upk->data.type) {
return -1;
}
*value = upk->data.via.u64;

ENSURE(*value == upk->data.via.u64);

return 0;
}

int
msg_unpack_next_u32(uint32_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off) {

uint64_t value64;
int ret;

ret = msg_unpack_next_u64(&value64, upk, data, len, off);
if (0 > ret) {
return ret;
}
*value = (uint32_t)value64;

ENSURE(*value == (uint32_t)value64);

return 0;
}

int
msg_put_unmarshal(msg_t *m,
const char *data,
Expand All @@ -181,7 +128,6 @@ msg_put_unmarshal(msg_t *m,

int retval;
size_t offset = 0;
msgpack_unpack_return ret;
msgpack_unpacked upk;

msgpack_unpacked_init(&upk);
Expand All @@ -198,17 +144,9 @@ msg_put_unmarshal(msg_t *m,
if (0 > retval) {
return retval;
}

ret = msgpack_unpack_next(&upk, data, len, &offset);
if (ret != MSGPACK_UNPACK_SUCCESS) {
return -1;
}
if (MSGPACK_OBJECT_STR != upk.data.type) {
return -1;
}
m->path = strndup(upk.data.via.str.ptr, upk.data.via.str.size);
if (m->path == NULL) {
return -1;
retval = msg_unpack_next_string(&m->path, &upk, data, len, &offset);
if (0 > retval) {
return retval;
}

return 0;
Expand Down
111 changes: 111 additions & 0 deletions src/msg_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2016 The libpblc authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdint.h>
#include <msgpack.h>
#include <cmockery/pbc.h>

#include "msg.h"

#ifdef UNIT_TESTING
#include <cmockery/cmockery_override.h>
#ifdef strndup
#undef strndup
#endif
#define strndup test_strndup
char * test_strndup(const char *s, size_t n);
#endif

int
msg_unpack_next_u32(uint32_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off) {

uint64_t value64;
int ret;

ret = msg_unpack_next_u64(&value64, upk, data, len, off);
if (0 > ret) {
return -1;
}
*value = (uint32_t)value64;

ENSURE(*value == (uint32_t)value64);

return 0;
}

int
msg_unpack_next_u64(uint64_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off) {

REQUIRE(value != NULL);
REQUIRE(upk != NULL);
REQUIRE(data != NULL);
REQUIRE(len > 0);

msgpack_unpack_return ret;

ret = msgpack_unpack_next(upk, data, len, off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
return -1;
}
if (MSGPACK_OBJECT_POSITIVE_INTEGER != upk->data.type) {
return -1;
}
*value = upk->data.via.u64;

ENSURE(*value == upk->data.via.u64);

return 0;
}

int
msg_unpack_next_string(char **str,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off) {

REQUIRE(str != NULL);
REQUIRE(upk != NULL);
REQUIRE(data != NULL);
REQUIRE(len > 0);

msgpack_unpack_return ret;

ret = msgpack_unpack_next(upk, data, len, off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
return -1;
}
if (MSGPACK_OBJECT_STR != upk->data.type) {
return -1;
}
*str = strndup(upk->data.via.str.ptr, upk->data.via.str.size);
if (*str == NULL) {
return -1;
}

ENSURE(strlen(*str) == upk->data.via.str.size);
ENSURE(strncmp(*str, upk->data.via.str.ptr, upk->data.via.str.size) == 0);

return 0;
}
40 changes: 40 additions & 0 deletions src/msg_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 The libpblc authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LIBPBLC_MSG_UTILS_H_
#define LIBPBLC_MSG_UTILS_H_

int
msg_unpack_next_u32(uint32_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off);
int
msg_unpack_next_u64(uint64_t *value,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off);

int
msg_unpack_next_string(char **str,
msgpack_unpacked *upk,
const char *data,
size_t len,
size_t *off);

#endif /* MSG_UTILS_H_ */

0 comments on commit c69b22d

Please sign in to comment.