Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test done for all of our components. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions test_program/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
TEST_CFLAGS=$(shell pkg-config --cflags cmockery2)
TETS_LDFLAGS=$(shell pkg-config --libs cmockery2)

test:md5.o sha1.o delete.o hash.o block.o stub.o catalog.o dedup.o Restore_file.o clean_buff.o dedup libdedup.a
dedup.o:dedup.c dedup.h
gcc -c dedup.c -lssl -lcrypto
gcc -c dedup.c -lssl -lcrypto
delete.o:delete.c delete.h
gcc -c delete.c
Restore_file.o:Restore_file.c restore.h
Expand Down
2 changes: 1 addition & 1 deletion test_program/Restore_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ restore_file()
}
ret=0;
out:
clean_buff(&path);
clean_buff(&path);
return ret;

}
Expand Down
22 changes: 16 additions & 6 deletions test_program/block.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include "block.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <inttypes.h>
#include<cmockery/pbc.h>
#include <cmockery/cmockery.h>
#include <cmockery/cmockery_override.h>

/*Function to create block file.
Input:void
Expand Down Expand Up @@ -30,8 +37,9 @@ int
insert_block(char *buffer,size_t length)
{

int ret = -1;

int ret = -1;
REQUIRE(buffer!=NULL);
REQUIRE(length>0);
if (length<= 0)
{
goto out;
Expand All @@ -54,6 +62,7 @@ insert_block(char *buffer,size_t length)
fprintf(stderr,"%s\n",strerror(errno));
goto out;
}
ENSURE(ret>=0);
out:
return ret;

Expand All @@ -63,7 +72,7 @@ insert_block(char *buffer,size_t length)
Input:int pos
Output:char*
*/
char*
char *
get_block(int pos)
{

Expand All @@ -73,7 +82,7 @@ get_block(int pos)
int ret = -1;
char* buffer = NULL;
int position= 1;

REQUIRE(pos>=0);
fstat(fd.fd_block, &st);
size = st.st_size;
// rewind the stream pointer to the start of block file
Expand Down Expand Up @@ -130,8 +139,9 @@ get_block(int pos)
out:
if (ret== -1)
{
memset(buffer,0,sizeof(buffer));
strcpy(buffer,"");
}
ENSURE(buffer!=NULL);
return buffer;

}
Expand All @@ -144,7 +154,7 @@ fini_block_store()
{

int ret = -1;

REQUIRE(fd.fd_block!=-1);
if (fd.fd_block != -1)
ret=close(fd.fd_block);
if(ret==-1)
Expand Down
5 changes: 4 additions & 1 deletion test_program/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
#endif
#define NAME_SIZE 100
#define INT_SIZE sizeof(int)
#include "clean_buff.h"

struct block_store
{
size_t fd_block;

size_t fd_block;

};

static struct block_store fd;
Expand Down
9 changes: 9 additions & 0 deletions test_program/block_Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

CFLAGS=$(shell pkg-config --libs --cflags cmockery2)

all:block_test

block_test:block.c block.h block_test.c clean_buff.c clean_buff.h block_Makefile
gcc -o block_test block.c block.h block_test.c clean_buff.c clean_buff.h $(CFLAGS)


60 changes: 60 additions & 0 deletions test_program/block_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <inttypes.h>
#include <cmockery/cmockery.h>
extern int get_block(int pos);
extern int init_block_store();
extern int insert_block(char *buffer,size_t length);
extern int fini_block_store();
static void test_return_values(void **state)
{

assert_int_equal(init_block_store(), 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. Nice, simple test.


}

/* Test case that fails as leak_memory() leaks a dynamically allocated block.*/
static void leak_memory_test(void **state)
{

get_block(5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are confusing. Normally every test should follow the following concept:

  1. setup state
  2. setup any expected mocks
  3. call the function to be tested
  4. assert results

But I do not see that in many of the tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ewen/Srinivas can we have this fixed asap. If u have a doubt please ask Luis about it.
Also this patch is old and will have merge conflicts so could u please fix this on a new patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi luis,

A gentle reminder about the doubt we had asked earlier.
We had a doubt about what a setup state is,setup any expected mocks is.

Regards,
Team_es

On Fri, May 8, 2015 at 11:56 AM, josephaug26 [email protected]
wrote:

In test_program/block_test.c
#12 (comment):

+extern int get_block(int pos);
+extern int init_block_store();
+extern int insert_block(char _buffer,size_t length);
+extern int fini_block_store();
+static void test_return_values(void *_state)
+{
+

  •    assert_int_equal(init_block_store(), 0);
    
    +}
    +
    +/* Test case that fails as leak_memory() leaks a dynamically allocated block._/
    +static void leak_memory_test(void *_state)
    +{
    +
  •    get_block(5);
    

Ewen/Srinivas can we have this fixed asap. If u have a doubt please ask
Luis about it.
Also this patch is old and will have merge conflicts so could u please fix
this on a new patch.


Reply to this email directly or view it on GitHub
https://github.com/YADL/yadl/pull/12/files#r29919168.


}
/* Test case that fails as buffer_overflow() corrupts an allocated block.*/
static void buffer_overflow_test(void **state)
{

get_block(5);

}

/* Test case that fails as buffer_underflow() corrupts an allocated block.*/
static void buffer_underflow_test(void **state)
{

get_block(5);

}

/* Test case to test return value of fini_block_store.*/
static void test_fini_block_store(void **state)
{

assert_int_equal(fini_block_store(), 0);

}

int main(int argc, char* argv[])
{

const UnitTest tests[] = {
unit_test(test_return_values),
unit_test(leak_memory_test),
unit_test(buffer_overflow_test),
unit_test(buffer_underflow_test),
unit_test(test_fini_block_store),
};
return run_tests(tests, "Block_allocate_module_test");

}
9 changes: 9 additions & 0 deletions test_program/cat_Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

CFLAGS=$(shell pkg-config --libs --cflags cmockery2)

all:catalog_test

catalog_test:catalog.c catalog.h catalog_test.c clean_buff.c clean_buff.h cat_Makefile
gcc -o catalog_test catalog.c catalog.h catalog_test.c clean_buff.c clean_buff.h $(CFLAGS)


37 changes: 22 additions & 15 deletions test_program/catalog.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
#include "stdio.h"
#include "catalog.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <inttypes.h>
#include<cmockery/pbc.h>
#include <cmockery/cmockery.h>
#include <cmockery/cmockery_override.h>
#include<assert.h>


/*Function to create catalog file.
Input:void
Expand All @@ -17,7 +27,8 @@ init_catalog_store()
}
ret=0;
out:
return ret;
ENSURE(ret==0);
return ret;

}

Expand All @@ -28,23 +39,20 @@ Output:int
int
writecatalog(char* filename)
{

int ret = -1;
char actualpath [PATH_MAX+1];
char *real_path = NULL;
int size_of_real_path = 0;

if (filename== "")
{
goto out;
}
REQUIRE(filename!=NULL);
real_path = realpath(filename, actualpath);
if (real_path== NULL)
{
fprintf(stderr,"%s\n",strerror(errno));
goto out;
}
size_of_real_path=strlen(real_path);
assert(size_of_real_path>0);
if (-1 == write(fd_cat,&size_of_real_path,int_size))
{
fprintf(stderr,"%s\n",strerror(errno));
Expand All @@ -58,9 +66,10 @@ writecatalog(char* filename)
ret=0;
out:
return ret;

}


/*Function to read all deduped files from a catalog file.
Input:void
Output:int
Expand Down Expand Up @@ -98,6 +107,7 @@ readfilecatalog()
fprintf(stderr,"%s\n",strerror(errno));
goto out;
}
assert(length>0);
buffer=(char*)calloc(1,length+1);
ret = read(fd_cat,buffer,length);
if (ret== -1)
Expand All @@ -108,11 +118,10 @@ readfilecatalog()
buffer[length]='\0';
printf("%s\n",buffer);
size-=(length+int_size);
memset(buffer,0,sizeof(buffer));
clean_buff(&buffer);
ret=1;
}
ret=0;
out:
return ret;

Expand All @@ -130,9 +139,8 @@ comparepath(char out[])
int size = 0;
size_t length = 0;
int ret = -1;
int flag = 1;
char* buffer = NULL;

REQUIRE(out!=NULL);
fstat(fd_cat, &st);
size = st.st_size;
// rewind the stream pointer to the start of catalog file
Expand Down Expand Up @@ -172,7 +180,6 @@ comparepath(char out[])
break;
}
size-=(length+int_size);
memset(buffer,0,sizeof(buffer));
clean_buff(&buffer);
ret=1;
}
Expand All @@ -181,6 +188,7 @@ comparepath(char out[])

}


/*Function to close catalog fd.
Input:void
Output:int*/
Expand All @@ -189,8 +197,7 @@ fini_catalog_store()
{

int ret = -1;

if (fd_cat != -1)
assert (fd_cat != -1);
ret=close(fd_cat);
if (ret== -1)
{
Expand Down
10 changes: 8 additions & 2 deletions test_program/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<errno.h>
#include<ctype.h>
Expand All @@ -20,10 +24,9 @@
#include<openssl/ssl.h>
#include<openssl/sha.h>
#endif
#define block 10
#define NAME_SIZE 100
#define int_size sizeof(int)

#include "clean_buff.h"
static size_t fd_cat;

/*@description:Function to create catalogstore
Expand Down Expand Up @@ -61,3 +64,6 @@ int comparepath(char out[]);
@out: int
@return: -1 for error and 0 if closed successfully */
int fini_catalog_store();


inline void clean_buff(char** buffer);
Loading