25
25
26
26
#define TEST_SUITE_NAME "src/editor/detect_linebreaks"
27
27
28
+ #include <sys/types.h>
29
+ #include <sys/stat.h>
30
+ #include <fcntl.h>
31
+
28
32
#include <check.h>
29
33
30
34
#include <config.h>
@@ -57,22 +61,9 @@ teardown (void)
57
61
START_TEST (test_detect_lb_type )
58
62
{
59
63
LineBreaks result ;
60
- /* prepare for test */
61
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
62
- int i ;
63
- if (fd == -1 )
64
- {
65
- fail ("unable to create test input file %s" ,filename );
66
- return ;
67
- }
68
- for (i = 0 ;i < 200 ;i ++ )
69
- {
70
- write (fd , "Test for detect line break\r\n" , 29 );
71
- }
72
- write (fd , "\r\n" , 2 );
73
- close (fd );
64
+ char buf [] = "Test for detect line break\r\n" ;
74
65
75
- result = detect_lb_type ((char * ) filename );
66
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen ( buf ) );
76
67
fail_unless (result == LB_WIN , "Incorrect lineBreak: result(%d) != LB_WIN(%d)" ,result , LB_WIN );
77
68
78
69
unlink (filename );
@@ -84,25 +75,16 @@ END_TEST
84
75
START_TEST (test_detect_lb_type_very_long_string )
85
76
{
86
77
LineBreaks result ;
87
- /* prepare for test */
88
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
78
+ char buf [1024 ] = "" ;
89
79
int i ;
90
- if (fd == -1 )
91
- {
92
- fail ("unable to create test input file %s" ,filename );
93
- return ;
94
- }
95
80
for (i = 0 ; i < 20 ; i ++ )
96
81
{
97
- write ( fd , "Very long string. " , 18 );
82
+ strcat ( buf , "Very long string. " );
98
83
}
99
- write (fd , "\r\n" , 2 );
100
- close (fd );
84
+ strcat (buf , "\r\n" );
101
85
102
- result = detect_lb_type ((char * ) filename );
86
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen ( buf ) );
103
87
fail_unless (result == LB_WIN , "Incorrect lineBreak: result(%d) != LB_WIN(%d)" ,result , LB_WIN );
104
-
105
- unlink (filename );
106
88
}
107
89
END_TEST
108
90
@@ -111,20 +93,11 @@ END_TEST
111
93
START_TEST (test_detect_lb_type_rrrrrn )
112
94
{
113
95
LineBreaks result ;
114
- /* prepare for test */
115
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
116
- if (fd == -1 )
117
- {
118
- fail ("unable to create test input file %s" ,filename );
119
- return ;
120
- }
121
- write (fd , "test\r\r\r\r\r\n" , 10 );
122
- close (fd );
96
+ char buf [1024 ] = "" ;
97
+ strcat (buf , "test\r\r\r\r\r\n" );
123
98
124
- result = detect_lb_type ((char * ) filename );
125
- fail_unless (result == LB_MAC , "Incorrect lineBreak: result(%d) != LB_MAC(%d)" ,result , LB_MAC );
126
-
127
- unlink (filename );
99
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen (buf ));
100
+ fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" , result , LB_ASIS );
128
101
}
129
102
END_TEST
130
103
@@ -133,20 +106,22 @@ END_TEST
133
106
START_TEST (test_detect_lb_type_nnnnnr )
134
107
{
135
108
LineBreaks result ;
136
- /* prepare for test */
137
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
138
- if (fd == -1 )
139
- {
140
- fail ("unable to create test input file %s" ,filename );
141
- return ;
142
- }
143
- write (fd , "test\n\n\n\n\n\r" , 10 );
144
- close (fd );
109
+ char buf [] = "test\n\n\n\n\n\r " ;
145
110
146
- result = detect_lb_type ((char * ) filename );
111
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen ( buf ) );
147
112
fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" ,result , LB_ASIS );
113
+ }
114
+ END_TEST
148
115
149
- unlink (filename );
116
+ /* --------------------------------------------------------------------------------------------- */
117
+
118
+ START_TEST (test_detect_lb_type_nnnrnrnnnn )
119
+ {
120
+ LineBreaks result ;
121
+ char buf [] = "test\n\n\n\r\n\r\n\r\n\n\n\n" ;
122
+
123
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen (buf ));
124
+ fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" ,result , LB_ASIS );
150
125
}
151
126
END_TEST
152
127
@@ -155,20 +130,10 @@ END_TEST
155
130
START_TEST (test_detect_lb_type_rrrrrr )
156
131
{
157
132
LineBreaks result ;
158
- /* prepare for test */
159
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
160
- if (fd == -1 )
161
- {
162
- fail ("unable to create test input file %s" ,filename );
163
- return ;
164
- }
165
- write (fd , "test\r\r\r\r\r\r" , 10 );
166
- close (fd );
133
+ char buf [1024 ] = "test\r\r\r\r\r\r" ;
167
134
168
- result = detect_lb_type ((char * ) filename );
169
- fail_unless (result == LB_MAC , "Incorrect lineBreak: result(%d) != LB_MAC(%d)" ,result , LB_MAC );
170
-
171
- unlink (filename );
135
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen (buf ));
136
+ fail_unless (result == LB_MAC , "Incorrect lineBreak: result(%d) != LB_MAC(%d)" , result , LB_MAC );
172
137
}
173
138
END_TEST
174
139
@@ -177,20 +142,10 @@ END_TEST
177
142
START_TEST (test_detect_lb_type_nnnnnn )
178
143
{
179
144
LineBreaks result ;
180
- /* prepare for test */
181
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
182
- if (fd == -1 )
183
- {
184
- fail ("unable to create test input file %s" ,filename );
185
- return ;
186
- }
187
- write (fd , "test\n\n\n\n\n\n" , 10 );
188
- close (fd );
189
-
190
- result = detect_lb_type ((char * ) filename );
191
- fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" ,result , LB_ASIS );
145
+ char buf [1024 ] = "test\n\n\n\n\n\n" ;
192
146
193
- unlink (filename );
147
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen (buf ));
148
+ fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" , result , LB_ASIS );
194
149
}
195
150
END_TEST
196
151
@@ -199,25 +154,15 @@ END_TEST
199
154
START_TEST (test_detect_lb_type_buffer_border )
200
155
{
201
156
LineBreaks result ;
202
- char buf [DETECT_LB_TYPE_BUFLEN ];
203
- /* prepare for test */
204
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
205
- if (fd == -1 )
206
- {
207
- fail ("unable to create test input file %s" ,filename );
208
- return ;
209
- }
157
+ char buf [DETECT_LB_TYPE_BUFLEN + 100 ];
210
158
memset (buf , ' ' , DETECT_LB_TYPE_BUFLEN );
159
+ buf [DETECT_LB_TYPE_BUFLEN - 101 ] = '\r' ;
160
+ buf [DETECT_LB_TYPE_BUFLEN - 100 ] = '\n' ;
211
161
buf [DETECT_LB_TYPE_BUFLEN - 1 ] = '\r' ;
162
+ buf [DETECT_LB_TYPE_BUFLEN ] = '\n' ;
212
163
213
- write (fd , buf , DETECT_LB_TYPE_BUFLEN );
214
- write (fd , "\n" , 1 );
215
- close (fd );
216
-
217
- result = detect_lb_type ((char * ) filename );
218
- fail_unless (result == LB_WIN , "Incorrect lineBreak: result(%d) != LB_WIN(%d)" ,result , LB_WIN );
219
-
220
- unlink (filename );
164
+ result = detect_lb_type_buf ((unsigned char * ) buf , DETECT_LB_TYPE_BUFLEN );
165
+ fail_unless (result == LB_WIN , "Incorrect lineBreak: result(%d) != LB_WIN(%d)" , result , LB_WIN );
221
166
}
222
167
END_TEST
223
168
@@ -226,25 +171,15 @@ END_TEST
226
171
START_TEST (test_detect_lb_type_buffer_border_overflow )
227
172
{
228
173
LineBreaks result ;
229
- char buf [DETECT_LB_TYPE_BUFLEN ];
230
- /* prepare for test */
231
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
232
- if (fd == -1 )
233
- {
234
- fail ("unable to create test input file %s" ,filename );
235
- return ;
236
- }
174
+ char buf [DETECT_LB_TYPE_BUFLEN + 100 ];
237
175
memset (buf , ' ' , DETECT_LB_TYPE_BUFLEN );
176
+ buf [DETECT_LB_TYPE_BUFLEN - 100 ] = '\r' ;
238
177
buf [DETECT_LB_TYPE_BUFLEN - 1 ] = '\r' ;
239
178
240
- write (fd , buf , DETECT_LB_TYPE_BUFLEN );
241
- write (fd , "bla-bla\r\n" , 9 );
242
- close (fd );
179
+ strcat (buf , "bla-bla\r\n" );
243
180
244
- result = detect_lb_type ((char * ) filename );
181
+ result = detect_lb_type_buf ((unsigned char * ) buf , DETECT_LB_TYPE_BUFLEN );
245
182
fail_unless (result == LB_MAC , "Incorrect lineBreak: result(%d) != LB_MAC(%d)" ,result , LB_MAC );
246
-
247
- unlink (filename );
248
183
}
249
184
END_TEST
250
185
@@ -253,24 +188,13 @@ END_TEST
253
188
START_TEST (test_detect_lb_type_buffer_border_more )
254
189
{
255
190
LineBreaks result ;
256
- char buf [DETECT_LB_TYPE_BUFLEN ];
257
- /* prepare for test */
258
- int fd = open (filename , O_WRONLY |O_CREAT , 0644 );
259
- if (fd == -1 )
260
- {
261
- fail ("unable to create test input file %s" ,filename );
262
- return ;
263
- }
191
+ char buf [DETECT_LB_TYPE_BUFLEN + 100 ];
264
192
memset (buf , ' ' , DETECT_LB_TYPE_BUFLEN );
265
193
266
- write (fd , buf , DETECT_LB_TYPE_BUFLEN );
267
- write (fd , "bla-bla\n" , 8 );
268
- close (fd );
194
+ strcat (buf , "bla-bla\n" );
269
195
270
- result = detect_lb_type ((char * ) filename );
196
+ result = detect_lb_type_buf ((unsigned char * ) buf , strlen ( buf ) );
271
197
fail_unless (result == LB_ASIS , "Incorrect lineBreak: result(%d) != LB_ASIS(%d)" ,result , LB_ASIS );
272
-
273
- unlink (filename );
274
198
}
275
199
END_TEST
276
200
@@ -292,6 +216,7 @@ main (void)
292
216
tcase_add_test (tc_core , test_detect_lb_type_very_long_string );
293
217
tcase_add_test (tc_core , test_detect_lb_type_rrrrrn );
294
218
tcase_add_test (tc_core , test_detect_lb_type_nnnnnr );
219
+ tcase_add_test (tc_core , test_detect_lb_type_nnnrnrnnnn );
295
220
tcase_add_test (tc_core , test_detect_lb_type_rrrrrr );
296
221
tcase_add_test (tc_core , test_detect_lb_type_nnnnnn );
297
222
tcase_add_test (tc_core , test_detect_lb_type_buffer_border );
0 commit comments