Skip to content

Commit

Permalink
Read test strings from corpora dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ade3p committed Aug 24, 2023
1 parent cd5ecd8 commit 7c1f9a6
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 64 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,9 @@ add_function_test(current_target
)

add_function_test(element
SOURCES test/function/element.cpp
SOURCES
test/function/element.cpp
$<TARGET_OBJECTS:test_helper_fixture>
)

add_function_test(element_leak
Expand Down Expand Up @@ -925,7 +927,9 @@ add_function_test(memory
)

add_function_test(param
SOURCES test/function/param.cpp
SOURCES
test/function/param.cpp
$<TARGET_OBJECTS:test_helper_fixture>
)

add_function_test(perror
Expand Down
23 changes: 23 additions & 0 deletions include/test/helper/fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,27 @@ create_entry( void );
const char *
load_corpus( const std::string& name );

struct stumpless_test_data {
int length;
char **test_strings;
};

/**
* Returns stumpless_test_data holding the contents of all the files at the named
* location in the test/corpora folder. For example, a name of "invalid_param_name"
* will return the contents of all the files under test/corpora/invalid_param_name
* directory in the form of an array.
*
* The allocated memory for test_strings must be safely freed up using free()
* and the buffer content (returned from load_corpus method) should also be
* destroyed using delete[] when no longer needed to avoid memory leaks.
*
* @param name The directory name.
*
* @return stumpless_test_data holding the test_strings pointing to contents of the
* directory as an array and corresponding size stored as length.
*/
stumpless_test_data
load_corpus_folder( const std::string& name );

#endif /* __STUMPLESS_TEST_HELPER_FIXTURE_HPP */
19 changes: 0 additions & 19 deletions include/test/helper/test_strings.hpp

This file was deleted.

Binary file added test/corpora/invalid_param_name/1
Binary file not shown.
Binary file added test/corpora/invalid_param_name/2
Binary file not shown.
Binary file added test/corpora/invalid_param_name/3
Binary file not shown.
Binary file added test/corpora/invalid_param_name/4
Binary file not shown.
Binary file added test/corpora/invalid_param_name/5
Binary file not shown.
Binary file added test/corpora/invalid_param_name/6
Binary file not shown.
63 changes: 41 additions & 22 deletions test/function/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"
#include "test/helper/test_strings.hpp"
#include "test/helper/fixture.hpp"

using::testing::HasSubstr;

Expand Down Expand Up @@ -361,13 +361,16 @@ namespace {
TEST_F( ElementTest, GetParamByNameInvalidName ) {
const struct stumpless_param *result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_param_by_name( element_with_params, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_param_by_name( element_with_params, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

free((void *) invalid_names.test_strings);
}

TEST_F( ElementTest, GetParamCount ) {
Expand Down Expand Up @@ -422,13 +425,16 @@ namespace {
TEST_F( ElementTest, GetParamIndexInvalidName ) {
size_t result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_param_index( element_with_params, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_param_index( element_with_params, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_EQ( result, 0 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

free((void *) invalid_names.test_strings);
}

TEST_F( ElementTest, GetParamNameCount ) {
Expand Down Expand Up @@ -464,13 +470,16 @@ namespace {
TEST_F( ElementTest, GetParamNameCountInvalidName ) {
size_t result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_param_name_count( basic_element, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_param_name_count( basic_element, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_EQ( result, 0 );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

free((void *) invalid_names.test_strings);
}

TEST_F( ElementTest, GetParamNameByIndex ) {
Expand Down Expand Up @@ -563,13 +572,16 @@ namespace {
TEST_F( ElementTest, GetParamValueByNameInvalidName ) {
const char *result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_param_value_by_name( element_with_params, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_param_value_by_name( element_with_params, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

free((void *) invalid_names.test_strings);
}

TEST_F( ElementTest, HasParam ) {
Expand All @@ -596,13 +608,16 @@ namespace {
TEST_F( ElementTest, HasParamInvalidName ) {
bool result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_element_has_param( element_with_params, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_element_has_param( element_with_params, invalid_names.test_strings[i] );
free((void *) invalid_names.test_strings[i]);
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

free((void *) invalid_names.test_strings);
}

TEST_F( ElementTest, SetName ) {
Expand Down Expand Up @@ -1041,13 +1056,15 @@ namespace {
TEST( NewElementTest, InvalidName ) {
struct stumpless_element *element;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
element = stumpless_new_element( invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
element = stumpless_new_element( invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_NULL( element );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
stumpless_free_all( );
}

Expand Down Expand Up @@ -1077,16 +1094,18 @@ namespace {
struct stumpless_element *element;
struct stumpless_element *result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

element = stumpless_new_element( "element" );
ASSERT_NOT_NULL( element );

for(const char *invalid_name : invalid_names){
result = stumpless_set_element_name( element, invalid_name);
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_set_element_name( element, invalid_names.test_strings[i]);
delete[] invalid_names.test_strings[i];
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
stumpless_destroy_element_and_contents( element );
stumpless_free_all( );
}
Expand Down
27 changes: 16 additions & 11 deletions test/function/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "test/helper/assert.hpp"
#include "test/helper/fixture.hpp"
#include "test/helper/memory_allocation.hpp"
#include "test/helper/test_strings.hpp"

using::testing::HasSubstr;

Expand Down Expand Up @@ -765,37 +764,43 @@ namespace {
TEST_F( EntryTest, HasElementInvalidName ) {
bool result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};

for(const char *invalid_name : invalid_names){
result = stumpless_entry_has_element( basic_entry, invalid_name );
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_entry_has_element( basic_entry, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
}

TEST_F( EntryTest, GetElementInvalidName ) {
bool result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_element_by_name( basic_entry, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_element_by_name( basic_entry, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
}

TEST_F( EntryTest, GetElementIdxInvalidName ) {
bool result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_get_element_index( basic_entry, invalid_name );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_get_element_index( basic_entry, invalid_names.test_strings[i] );
delete[] invalid_names.test_strings[i];
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
}

TEST_F( EntryTest, SetAppName ) {
Expand Down
26 changes: 16 additions & 10 deletions test/function/param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/memory_allocation.hpp"
#include "test/helper/test_strings.hpp"
#include "test/helper/fixture.hpp"

namespace {

Expand Down Expand Up @@ -246,13 +246,15 @@ namespace {
struct stumpless_param param;
const struct stumpless_param *result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
result = stumpless_load_param( &param, invalid_name, "test-value" );
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_load_param( &param, invalid_names.test_strings[i], "test-value" );
delete[] invalid_names.test_strings[i];
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
stumpless_free_all( );
}

Expand Down Expand Up @@ -344,13 +346,15 @@ namespace {
TEST( NewParamTest, InvalidName ) {
struct stumpless_param *param;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

for(const char *invalid_name : invalid_names){
param = stumpless_new_param( invalid_name, "test-value" );
for(int i = 0; i < invalid_names.length; ++i){
param = stumpless_new_param( invalid_names.test_strings[i], "test-value" );
delete[] invalid_names.test_strings[i];
EXPECT_NULL( param );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
stumpless_free_all( );
}

Expand Down Expand Up @@ -467,16 +471,18 @@ namespace {
struct stumpless_param *param;
struct stumpless_param *result;
const struct stumpless_error *error;
const char *invalid_names[] = {INVALID_NAMES};
stumpless_test_data invalid_names = load_corpus_folder("invalid_param_name");

param = stumpless_new_param( "param", "my-value" );
ASSERT_NOT_NULL( param );

for(const char *invalid_name : invalid_names){
result = stumpless_set_param_name( param, invalid_name);
for(int i = 0; i < invalid_names.length; ++i){
result = stumpless_set_param_name( param, invalid_names.test_strings[i]);
delete[] invalid_names.test_strings[i];
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
free((void *) invalid_names.test_strings);
stumpless_destroy_param( param );
stumpless_free_all( );
}
Expand Down
29 changes: 29 additions & 0 deletions test/helper/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fstream>
#include <string>
#include <stumpless.h>
#include <dirent.h>
#include "test/config.hpp"
#include "test/helper/fixture.hpp"

Expand Down Expand Up @@ -79,3 +80,31 @@ load_corpus( const string& name ) {

return buffer;
}

stumpless_test_data
load_corpus_folder( const string& name){

DIR *dir;
struct dirent *ent;
string corpora_dir ( FUZZ_CORPORA_DIR );
int fileIndex = 0;

char **test_strings;
if((dir = opendir((corpora_dir + "/" + name).c_str())) != NULL){
while((ent = readdir(dir)) != NULL){
const char *test_string = load_corpus(name + "/" + ent->d_name);
if(test_string != NULL){
if(fileIndex > 0){
test_strings = (char **) realloc(test_strings, sizeof(char *) * (fileIndex + 1));
}
else{
test_strings = (char **) malloc(sizeof(char *));
}
test_strings[fileIndex] = (char *)test_string;
++fileIndex;
}
}
closedir(dir);
}
return {fileIndex, test_strings};
}

0 comments on commit 7c1f9a6

Please sign in to comment.