Skip to content

Commit

Permalink
add coverage for chain target
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Mar 28, 2024
1 parent 42977f6 commit 11658da
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 25 deletions.
31 changes: 15 additions & 16 deletions include/test/helper/assert.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 2020 Joel E. Anderson
* Copyright 2020-2024 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,29 +17,28 @@
*/

#ifndef __STUMPLESS_TEST_HELPER_ASSERT_HPP
# define __STUMPLESS_TEST_HELPER_ASSERT_HPP
#define __STUMPLESS_TEST_HELPER_ASSERT_HPP

# include <cstddef>
# include <gtest/gtest.h>
# include <stumpless.h>
#include <cstddef>
#include <gtest/gtest.h>
#include <stumpless.h>

# define ASSERT_NOT_NULL( thing ) ASSERT_FALSE( (thing) == NULL )
#define ASSERT_NOT_NULL( thing ) ASSERT_FALSE( ( thing ) == NULL )

# define ASSERT_NULL( thing ) ASSERT_TRUE( (thing) == NULL )
#define ASSERT_NULL( thing ) ASSERT_TRUE( ( thing ) == NULL )

# define EXPECT_ERROR_ID_EQ( code ) \
error = stumpless_get_error( ); \
EXPECT_TRUE( error != NULL ); \
if( error ) { \
EXPECT_EQ( error->id, (code) ); \
#define EXPECT_ERROR_ID_EQ( code ) \
EXPECT_TRUE( stumpless_has_error() ); \
if( stumpless_has_error() ) { \
EXPECT_EQ( stumpless_get_error()->id, ( code ) ); \
}

# define EXPECT_NO_ERROR \
EXPECT_FALSE( stumpless_has_error( ) ); \
#define EXPECT_NO_ERROR \
EXPECT_FALSE( stumpless_has_error() ); \
stumpless_perror( "an unexpected error occurred" )

# define EXPECT_NOT_NULL( thing ) EXPECT_FALSE( (thing) == NULL )
#define EXPECT_NOT_NULL( thing ) EXPECT_FALSE( ( thing ) == NULL )

# define EXPECT_NULL( thing ) EXPECT_TRUE( (thing) == NULL )
#define EXPECT_NULL( thing ) EXPECT_TRUE( ( thing ) == NULL )

#endif /* __STUMPLESS_TEST_HELPER_ASSERT_HPP */
3 changes: 2 additions & 1 deletion src/target/chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ stumpless_add_target_to_chain( struct stumpless_target *chain,

void
stumpless_close_chain_and_contents( struct stumpless_target *chain ){
struct chain_target *internal_target;
const struct chain_target *internal_target;
size_t i;
size_t index;
struct stumpless_target *curr;
Expand Down Expand Up @@ -141,6 +141,7 @@ stumpless_get_chain_length( const struct stumpless_target *chain ){
return -1;
}

clear_error();
internal_chain = chain->id;
lock_chain_target( internal_chain );
result = internal_chain->target_count;
Expand Down
112 changes: 104 additions & 8 deletions test/function/target/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
*/

#include <cstddef>
#include <cstdlib>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/fixture.hpp"
#include "test/helper/memory_allocation.hpp"
#include "test/helper/rfc5424.hpp"

using::testing::HasSubstr;
Expand All @@ -32,7 +34,7 @@ namespace {
protected:
struct stumpless_target *chain = NULL;
char buffer[8192];
struct stumpless_target *target_1;
struct stumpless_target *target_1 = NULL;
struct stumpless_target *full_chain = NULL;

void
Expand Down Expand Up @@ -98,6 +100,8 @@ namespace {

std::string read_message( read_buffer, read_size );
EXPECT_THAT( read_message, HasSubstr( test_message ) );

stumpless_close_buffer_target( target );
}

TEST_F( ChainTargetTest, AddTargetToFullChain ){
Expand Down Expand Up @@ -139,6 +143,40 @@ namespace {

std::string read_message( read_buffer, read_size );
EXPECT_THAT( read_message, HasSubstr( test_message ) );

stumpless_close_buffer_target( target );
}

TEST_F( ChainTargetTest, AddTargetToFullChainMemoryFailure ){
size_t starting_length;
char target_buffer[1024];
struct stumpless_target *target;
struct stumpless_target *add_target_result;
size_t new_length;

starting_length = stumpless_get_chain_length( full_chain );
EXPECT_NO_ERROR;

target = stumpless_open_buffer_target( "chain-add-new",
target_buffer,
sizeof( target_buffer ) );
ASSERT_NOT_NULL( target );

stumpless_set_realloc( REALLOC_FAIL );
EXPECT_NO_ERROR;

add_target_result = stumpless_add_target_to_chain( full_chain, target );
EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE );
EXPECT_NULL( add_target_result );

new_length = stumpless_get_chain_length( full_chain );
EXPECT_NO_ERROR;
EXPECT_EQ( new_length, starting_length );

stumpless_set_realloc( realloc );
EXPECT_NO_ERROR;

stumpless_close_buffer_target( target );
}

TEST_F( ChainTargetTest, AddEntry ){
Expand All @@ -156,9 +194,23 @@ namespace {
stumpless_destroy_entry_and_contents( entry );
}

TEST_F( ChainTargetTest, AddEntryToFullChain ){
struct stumpless_entry *entry;
int result;

entry = create_entry();

result = stumpless_add_entry( full_chain, entry );
EXPECT_GE( result, 0 );
EXPECT_NO_ERROR;

TestRFC5424Compliance( buffer );

stumpless_destroy_entry_and_contents( entry );
}

TEST_F( ChainTargetTest, AddNullTargetToChain ){
struct stumpless_target *result;
const struct stumpless_error *error;

result = stumpless_add_target_to_chain( chain, NULL );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );
Expand All @@ -167,7 +219,6 @@ namespace {

TEST_F( ChainTargetTest, AddTargetToNonChain ){
struct stumpless_target *result;
const struct stumpless_error *error;

result = stumpless_add_target_to_chain( target_1, target_1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_TARGET_INCOMPATIBLE );
Expand All @@ -176,23 +227,18 @@ namespace {

TEST_F( ChainTargetTest, AddTargetToNullChain ){
struct stumpless_target *result;
const struct stumpless_error *error;

result = stumpless_add_target_to_chain( NULL, target_1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );
EXPECT_NULL( result );
}

TEST_F( ChainTargetTest, CloseIncompatibleTargetAndContents ){
const struct stumpless_error *error;

stumpless_close_chain_and_contents( target_1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_TARGET_INCOMPATIBLE );
}

TEST_F( ChainTargetTest, CloseIncompatibleTargetOnly ){
const struct stumpless_error *error;

stumpless_close_chain_only( target_1 );
EXPECT_ERROR_ID_EQ( STUMPLESS_TARGET_INCOMPATIBLE );
}
Expand All @@ -212,4 +258,54 @@ namespace {
stumpless_close_chain_only( chain );
stumpless_free_all();
}

TEST( CloseChainAndContentsTest, FullChain ){
struct stumpless_target *chain;
struct stumpless_target *sub_target;
char buffer[1024];
size_t i;

chain = stumpless_new_chain( "full-chain-to-close" );
EXPECT_NO_ERROR;
ASSERT_NOT_NULL( chain );

for( i = 0; i < STUMPLESS_CHAIN_TARGET_ARRAY_LENGTH + 1; i++ ){
sub_target = stumpless_open_buffer_target( "sub-target",
buffer,
sizeof( buffer ) );
EXPECT_NO_ERROR;

stumpless_add_target_to_chain( chain, sub_target );
EXPECT_NO_ERROR;
}

stumpless_close_chain_and_contents( chain );
EXPECT_NO_ERROR;
}

TEST( CloseChainAndContentsTest, NullTarget ){
stumpless_close_chain_and_contents( NULL );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );
}

TEST( CloseChainOnlyTest, NullTarget ){
stumpless_close_chain_only( NULL );
EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY );
}

TEST( NewChainTargetTest, MemoryFailure ){
const struct stumpless_target *result;

stumpless_set_malloc( MALLOC_FAIL );
EXPECT_NO_ERROR;

result = stumpless_new_chain( "memory-failure" );
EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE );
EXPECT_NULL( result );

stumpless_set_malloc( malloc );
EXPECT_NO_ERROR;

stumpless_free_all();
}
}

0 comments on commit 11658da

Please sign in to comment.