Browse Source

Add tests + bugfixes

Yann Weber 7 years ago
parent
commit
87bba12a12
5 changed files with 175 additions and 6 deletions
  1. 1
    1
      src/include/ttail.h
  2. 17
    2
      src/ttail.c
  3. 6
    2
      tests/Makefile.am
  4. 1
    1
      tests/ttail_argparse_check.c
  5. 150
    0
      tests/ttail_init_check.c

+ 1
- 1
src/include/ttail.h View File

@@ -47,7 +47,7 @@ ttail_t *ttail_init(int argc, char **argv);
47 47
 /**@brief Add a logfile
48 48
  *@param ttail_t*
49 49
  *@param const char * filename
50
- *@return 0 if no errors -1 if fatal error
50
+ *@return 0 if no errors 1 if unable to open file -1 if fatal error
51 51
  */
52 52
 int ttail_add_logfile(ttail_t*, const char*);
53 53
 

+ 17
- 2
src/ttail.c View File

@@ -68,6 +68,7 @@ for date format");
68 68
 
69 69
 				break;
70 70
 			case 'l':
71
+				fprintf(stderr,"-l with %s\n", optarg);
71 72
 				ret = ttail_add_logfile(res, optarg);
72 73
 				if(ret < 0)
73 74
 				{
@@ -85,7 +86,7 @@ for date format");
85 86
 				dates[c=='d'?0:1] = date;
86 87
 				break;
87 88
 			case '?':
88
-				fprintf(stderr, "Bad argument : %S\n", argv[optind]);
89
+				fprintf(stderr, "Bad argument : %s\n", argv[optind]);
89 90
 				goto init_badarg;
90 91
 				break;
91 92
 			default:
@@ -143,10 +144,23 @@ int ttail_add_logfile(ttail_t* res, const char* filename)
143 144
 	void *tmp;
144 145
 	FILE *fp;
145 146
 	char *fname;
147
+	int ret, i;
148
+
149
+	for(i=0; i<res->logfile_sz; i++)
150
+	{
151
+		if(strcmp(filename, res->logfile_name[i]) == 0)
152
+		{
153
+			fprintf(stderr, "File '%s' allready added\n",
154
+				filename);
155
+			return -1;
156
+		}
157
+	}
158
+
146 159
 	res->logfile_sz++;
147 160
 	tmp = res->logfile;
148 161
 	res->logfile = realloc(res->logfile,
149 162
 		sizeof(FILE*)*res->logfile_sz);
163
+	ret = 0;
150 164
 	if(!res->logfile)
151 165
 	{
152 166
 		perror("Unable to allocate memory for logfiles");
@@ -157,6 +171,7 @@ int ttail_add_logfile(ttail_t* res, const char* filename)
157 171
 	if(!fp)
158 172
 	{
159 173
 		fprintf(stderr, "Unable to open file : %s\n", filename);
174
+		ret = 1;
160 175
 	}
161 176
 	res->logfile[res->logfile_sz-1] = fp;
162 177
 
@@ -179,7 +194,7 @@ int ttail_add_logfile(ttail_t* res, const char* filename)
179 194
 	strcpy(fname, filename);
180 195
 	res->logfile_name[res->logfile_sz-1] = fname;
181 196
 	
182
-	return 0;
197
+	return ret;
183 198
 
184 199
 	ttail_add_logfile_fnalloc_err:
185 200
 	fclose(res->logfile[res->logfile_sz-2]);

+ 6
- 2
tests/Makefile.am View File

@@ -1,6 +1,10 @@
1
-TESTS = ttail_argparse_check
2
-check_PROGRAMS = ttail_argparse_check
1
+TESTS = ttail_argparse_check ttail_init_check
2
+check_PROGRAMS = ttail_argparse_check ttail_init_check
3 3
 
4 4
 ttail_argparse_check_SOURCES = ttail_argparse_check.c
5 5
 ttail_argparse_check_CFLAGS = @CHECK_CFLAGS@
6 6
 ttail_argparse_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@
7
+
8
+ttail_init_check_SOURCES = ttail_init_check.c
9
+ttail_init_check_CFLAGS = @CHECK_CFLAGS@
10
+ttail_init_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@

+ 1
- 1
tests/ttail_argparse_check.c View File

@@ -82,7 +82,7 @@ void setup_fname(void)
82 82
 			{
83 83
 				perror("Unable to create file for testing");
84 84
 				ck_abort_msg("Unable to create file for testing");
85
-			} else { printf("Created : %s\n", fname[i]);}
85
+			}
86 86
 			if(fclose(fp))
87 87
 			{
88 88
 				perror("Unable to close file for testing");

+ 150
- 0
tests/ttail_init_check.c View File

@@ -0,0 +1,150 @@
1
+#include <check.h>
2
+#include <errno.h>
3
+#include <stdio.h>
4
+#include <unistd.h>
5
+
6
+
7
+#include "ttail.h"
8
+
9
+ttail_t *ttail;
10
+#define __Fname_sz 5
11
+#define FNAME_NO_EXIST 0
12
+#define FNAME_EXIST 1
13
+char *fname[__Fname_sz];
14
+
15
+void teardown_ttail(void)
16
+{
17
+	ttail_free(ttail);
18
+}
19
+void setup_ttail_empty(void)
20
+{
21
+	ttail = ttail_init(1, (char**)&"foo");
22
+}
23
+START_TEST (test_init_empty_logfilename)
24
+{
25
+	ck_assert_msg(ttail->logfile_name == NULL,
26
+		"ttail_t.logfile_name should be NULL");
27
+}
28
+END_TEST
29
+
30
+START_TEST (test_init_nonexist_logfilename)
31
+{
32
+	char *fname;
33
+	int ret;
34
+	fname = tempnam(NULL, "ttail_check");
35
+
36
+	ret = ttail_add_logfile(ttail, fname);
37
+	ck_assert_int_eq(ret, 1);
38
+	ck_assert(ttail->logfile_name != NULL);
39
+	ck_assert_str_eq(ttail->logfile_name[0], fname);
40
+	ck_assert(ttail->logfile[0] == NULL);
41
+	free(fname);
42
+}
43
+END_TEST
44
+
45
+START_TEST (test_init_exist_logfilename)
46
+{
47
+	char *fname;
48
+	int ret;
49
+	FILE *fp;
50
+	fname = tempnam(NULL, "ttail_check");
51
+	if(!(fp = fopen(fname, "w+")))
52
+	{
53
+		perror("Unable to create file for testing");
54
+		ck_abort_msg("Unable to create file for testing");
55
+	}
56
+	if(fclose(fp))
57
+	{
58
+		perror("Unable to close file for testing");
59
+		ck_abort_msg("Unabe to close file for testing");
60
+	}
61
+
62
+	ret = ttail_add_logfile(ttail, fname);
63
+	ck_assert_int_eq(ret, 0);
64
+	ck_assert(ttail->logfile_name != NULL);
65
+	ck_assert_str_eq(ttail->logfile_name[0], fname);
66
+	ck_assert(ttail->logfile[0] != NULL);
67
+	free(fname);
68
+}
69
+END_TEST
70
+
71
+START_TEST (test_init_same_exist_logfilename)
72
+{
73
+	char *fname;
74
+	int ret;
75
+	FILE *fp;
76
+	fname = tempnam(NULL, "ttail_check");
77
+	if(!(fp = fopen(fname, "w+")))
78
+	{
79
+		perror("Unable to create file for testing");
80
+		ck_abort_msg("Unable to create file for testing");
81
+	}
82
+	if(fclose(fp))
83
+	{
84
+		perror("Unable to close file for testing");
85
+		ck_abort_msg("Unabe to close file for testing");
86
+	}
87
+
88
+	ret = ttail_add_logfile(ttail, fname);
89
+	ck_assert_int_eq(ret, 0);
90
+	ret = ttail_add_logfile(ttail, fname);
91
+	ck_assert_int_eq(ret, -1);
92
+	ck_assert_int_eq(ttail->logfile_sz, 1);
93
+	ck_assert(ttail->logfile_name != NULL);
94
+	ck_assert_str_eq(ttail->logfile_name[0], fname);
95
+	ck_assert(ttail->logfile[0] != NULL);
96
+	free(fname);
97
+}
98
+END_TEST
99
+
100
+START_TEST (test_init_same_nonexist_logfilename)
101
+{
102
+	char *fname;
103
+	int ret;
104
+	fname = tempnam(NULL, "ttail_check");
105
+
106
+	ret = ttail_add_logfile(ttail, fname);
107
+	ck_assert_int_eq(ret, 1);
108
+	ret = ttail_add_logfile(ttail, fname);
109
+	ck_assert_int_eq(ret, -1);
110
+	ck_assert_int_eq(ttail->logfile_sz, 1);
111
+	ck_assert(ttail->logfile_name != NULL);
112
+	ck_assert_str_eq(ttail->logfile_name[0], fname);
113
+	ck_assert(ttail->logfile[0] == NULL);
114
+	free(fname);
115
+}
116
+END_TEST
117
+
118
+Suite * ttail_init_suite(void)
119
+{
120
+	Suite *s;
121
+	TCase *tc_init_logfile;
122
+
123
+	s = suite_create("ttail init checks");
124
+
125
+	tc_init_logfile = tcase_create("logfile init checks");
126
+	tcase_add_checked_fixture(tc_init_logfile,
127
+		setup_ttail_empty, teardown_ttail);
128
+	tcase_add_test(tc_init_logfile, test_init_empty_logfilename);
129
+	tcase_add_test(tc_init_logfile, test_init_nonexist_logfilename);
130
+	tcase_add_test(tc_init_logfile, test_init_exist_logfilename);
131
+	tcase_add_test(tc_init_logfile, test_init_same_exist_logfilename);
132
+	tcase_add_test(tc_init_logfile, test_init_same_nonexist_logfilename);
133
+	suite_add_tcase(s, tc_init_logfile);
134
+	return s;
135
+}
136
+
137
+int main(void)
138
+{
139
+	int number_failed = 0;
140
+	SRunner *sr;
141
+
142
+	sr = srunner_create(ttail_init_suite());
143
+	srunner_set_fork_status(sr, CK_FORK);
144
+
145
+	srunner_run_all(sr,CK_VERBOSE);
146
+	number_failed = srunner_ntests_failed(sr);
147
+	srunner_free(sr);
148
+	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
149
+
150
+}

Loading…
Cancel
Save