|
Packit Service |
3749ba |
/*
|
|
Packit Service |
3749ba |
* Copyright (c) 2012 Red Hat Inc.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
3749ba |
* modification, are permitted provided that the following conditions
|
|
Packit Service |
3749ba |
* are met:
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* * Redistributions of source code must retain the above
|
|
Packit Service |
3749ba |
* copyright notice, this list of conditions and the
|
|
Packit Service |
3749ba |
* following disclaimer.
|
|
Packit Service |
3749ba |
* * Redistributions in binary form must reproduce the
|
|
Packit Service |
3749ba |
* above copyright notice, this list of conditions and
|
|
Packit Service |
3749ba |
* the following disclaimer in the documentation and/or
|
|
Packit Service |
3749ba |
* other materials provided with the distribution.
|
|
Packit Service |
3749ba |
* * The names of contributors to this software may not be
|
|
Packit Service |
3749ba |
* used to endorse or promote products derived from this
|
|
Packit Service |
3749ba |
* software without specific prior written permission.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
3749ba |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
3749ba |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit Service |
3749ba |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit Service |
3749ba |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
Packit Service |
3749ba |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
Packit Service |
3749ba |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
Packit Service |
3749ba |
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
Packit Service |
3749ba |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
Packit Service |
3749ba |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
Packit Service |
3749ba |
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
Packit Service |
3749ba |
* DAMAGE.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Author: Stef Walter <stef@thewalter.net>
|
|
Packit Service |
3749ba |
*/
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "config.h"
|
|
Packit Service |
3749ba |
#include "test.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include <stdlib.h>
|
|
Packit Service |
3749ba |
#include <stdio.h>
|
|
Packit Service |
3749ba |
#include <string.h>
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "debug.h"
|
|
Packit Service |
3749ba |
#include "buffer.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_init_uninit (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init (&buffer, 10);
|
|
Packit Service |
3749ba |
assert_ptr_not_null (buffer.data);
|
|
Packit Service |
3749ba |
assert_num_eq (0, buffer.len);
|
|
Packit Service |
3749ba |
assert_num_eq (0, buffer.flags);
|
|
Packit Service |
3749ba |
assert (buffer.size >= 10);
|
|
Packit Service |
3749ba |
assert_ptr_not_null (buffer.ffree);
|
|
Packit Service |
3749ba |
assert_ptr_not_null (buffer.frealloc);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_append (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init (&buffer, 10);
|
|
Packit Service |
3749ba |
buffer.len = 5;
|
|
Packit Service |
3749ba |
p11_buffer_append (&buffer, 35);
|
|
Packit Service |
3749ba |
assert_num_eq (5 + 35, buffer.len);
|
|
Packit Service |
3749ba |
assert (buffer.size >= 35 + 5);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_append (&buffer, 15);
|
|
Packit Service |
3749ba |
assert_num_eq (5 + 35 + 15, buffer.len);
|
|
Packit Service |
3749ba |
assert (buffer.size >= 5 + 35 + 15);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_null (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init_null (&buffer, 10);
|
|
Packit Service |
3749ba |
p11_buffer_add (&buffer, "Blah", -1);
|
|
Packit Service |
3749ba |
p11_buffer_add (&buffer, " blah", -1);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
assert_str_eq ("Blah blah", buffer.data);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static int mock_realloced = 0;
|
|
Packit Service |
3749ba |
static int mock_freed = 0;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void *
|
|
Packit Service |
3749ba |
mock_realloc (void *data,
|
|
Packit Service |
3749ba |
size_t size)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
mock_realloced++;
|
|
Packit Service |
3749ba |
return realloc (data, size);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
mock_free (void *data)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
mock_freed++;
|
|
Packit Service |
3749ba |
free (data);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_init_for_data (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
unsigned char *ret;
|
|
Packit Service |
3749ba |
size_t len;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
mock_realloced = 0;
|
|
Packit Service |
3749ba |
mock_freed = 0;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, 0,
|
|
Packit Service |
3749ba |
mock_realloc, mock_free);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
assert_ptr_not_null (buffer.data);
|
|
Packit Service |
3749ba |
assert_str_eq ("blah", (char *)buffer.data);
|
|
Packit Service |
3749ba |
assert_num_eq (4, buffer.len);
|
|
Packit Service |
3749ba |
assert_num_eq (0, buffer.flags);
|
|
Packit Service |
3749ba |
assert_num_eq (4, buffer.size);
|
|
Packit Service |
3749ba |
assert_ptr_eq (mock_free, buffer.ffree);
|
|
Packit Service |
3749ba |
assert_ptr_eq (mock_realloc, buffer.frealloc);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
assert_num_eq (0, mock_realloced);
|
|
Packit Service |
3749ba |
assert_num_eq (0, mock_freed);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
len = buffer.len;
|
|
Packit Service |
3749ba |
ret = p11_buffer_append (&buffer, 1024);
|
|
Packit Service |
3749ba |
assert_ptr_eq ((char *)buffer.data + len, ret);
|
|
Packit Service |
3749ba |
assert_num_eq (1, mock_realloced);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
assert_num_eq (1, mock_realloced);
|
|
Packit Service |
3749ba |
assert_num_eq (1, mock_freed);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_steal (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
char *string;
|
|
Packit Service |
3749ba |
size_t length;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
mock_freed = 0;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4,
|
|
Packit Service |
3749ba |
P11_BUFFER_NULL, mock_realloc, mock_free);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
assert_ptr_not_null (buffer.data);
|
|
Packit Service |
3749ba |
assert_str_eq ("blah", buffer.data);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_add (&buffer, " yada", -1);
|
|
Packit Service |
3749ba |
assert_str_eq ("blah yada", buffer.data);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
string = p11_buffer_steal (&buffer, &length);
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
assert_str_eq ("blah yada", string);
|
|
Packit Service |
3749ba |
assert_num_eq (9, length);
|
|
Packit Service |
3749ba |
assert_num_eq (0, mock_freed);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
free (string);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_add (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_buffer buffer;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_init (&buffer, 10);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_add (&buffer, (unsigned char *)"Planet Express", 15);
|
|
Packit Service |
3749ba |
assert_num_eq (15, buffer.len);
|
|
Packit Service |
3749ba |
assert_str_eq ("Planet Express", (char *)buffer.data);
|
|
Packit Service |
3749ba |
assert (p11_buffer_ok (&buffer));
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buffer);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
int
|
|
Packit Service |
3749ba |
main (int argc,
|
|
Packit Service |
3749ba |
char *argv[])
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_test (test_init_uninit, "/buffer/init-uninit");
|
|
Packit Service |
3749ba |
p11_test (test_init_for_data, "/buffer/init-for-data");
|
|
Packit Service |
3749ba |
p11_test (test_append, "/buffer/append");
|
|
Packit Service |
3749ba |
p11_test (test_null, "/buffer/null");
|
|
Packit Service |
3749ba |
p11_test (test_add, "/buffer/add");
|
|
Packit Service |
3749ba |
p11_test (test_steal, "/buffer/steal");
|
|
Packit Service |
3749ba |
return p11_test_run (argc, argv);
|
|
Packit Service |
3749ba |
}
|