Skip to main content
summaryrefslogtreecommitdiffstats
blob: cfc8b0bf125ad1c75780a5871d2c9c2a130dd654 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2010, 2011 Meisam Fathi and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Meisam Fathi  - base API
 *******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;

import org.eclipse.cdt.codan.core.test.CheckerTestCase;

/**
 * Test for {@see FormatStringChecker} class
 */
public class FormatStringCheckerTest extends CheckerTestCase {
	@Override
	public void setUp() throws Exception {
		super.setUp();
		enableProblems("org.eclipse.cdt.codan.internal.checkers.ScanfFormatStringSecurityProblem"); //$NON-NLS-1$
	}

	// int f(){
	// return 0;
	// }
	public void testBase() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int f(){
	// char inputstr[5];
	// scanf("%s", inputstr); // here
	// return 0;
	// }
	public void testSimple() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(3);
	}

	// int main(void) {
	// char inputstr1[5];
	// int inputval;
	// int i = 5;
	// scanf("%i %4s", inputval, inputstr1); // no error here
	// printf("%d" ,i);
	// return 0;
	// }
	public void testIntRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int main(void) {
	// char inputstr[5];
	// int inputval;
	// int i = 5;
	// scanf("%d %9s", inputval, inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testIntWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(5);
	}

	// int main(void) {
	// char inputstr1[5];
	// int inputval;
	// int i = 5;
	// scanf("%4s %i", inputstr1, inputval);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testRightInt() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int main(void) {
	// char inputstr1[5];
	// char inputstr2[5];
	// int i = 5;
	// scanf("%4s %9s", inputstr1, inputstr2);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testRightWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(5);
	}

	// int main(void) {
	// char inputstr1[5];
	// int inputval;
	// int i = 5;
	// scanf("%9s %i", inputstr1, inputval);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testWrongInt() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(5);
	}

	// int main(void) {
	// char inputstr1[5];
	// char inputstr2[5];
	// int i = 5;
	// scanf("%9s %4s", inputstr1, inputstr2);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testWrongRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(5);
	}

	// int main(void) {
	// char inputstr[5];
	// int i = 5;
	// scanf("%s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testInfiniteSize() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(4);
	}

	// int main(void) {
	// puts("Enter a string whose length is bellow 5:");
	// char inputstr[5];
	// int i = 5;
	// scanf("%3s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int main(void) {
	// char inputstr1[5];
	// char inputstr2[5];
	// int i = 5;
	// scanf("%3s %4s", inputstr1, inputstr2);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testRightRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int main(void) {
	// char inputstr1[5];
	// char inputstr2[5];
	// int i = 5;
	// scanf("%8s %9s", inputstr1, inputstr2);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testWrongWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(5);
	}

	// char inputstr[5];
	// int foo(void){
	// char inputstr[15];
	// return 0;
	// }
	// int main(void) {
	// puts("Enter a string whose length is bellow 5:");
	// int i = 5;
	// scanf("%10s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testGlobalBeforeWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(9);
	}

	// int main(void) {
	// puts("Enter a string whose length is bellow 5:");
	// char inputstr[15];
	// int i = 5;
	// scanf("%10s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	// int foo(void){
	// char inputstr[5];
	// return 0;
	// }
	public void testNonglobalAfterRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int main(void) {
	// char inputstr[5];
	// int i = 5;
	// scanf("%10s", inputstr);
	// printf("%d" ,i);
	// return EXIT_SUCCESS;
	// }
	// int foo(void){
	// char inputstr[15];
	// return 0;
	// }
	public void testNonglobalAfterWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(4);
	}

	// int foo(void){
	// char inputstr[5];
	// return 0;
	// }
	// int main(void) {
	// char inputstr[15];
	// int i = 5;
	// scanf("%10s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testNonglobalBeforeRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}

	// int foo(void){
	// char inputstr[15];
	// return 0;
	// }
	// int main(void) {
	// char inputstr[5];
	// int i = 5;
	// scanf("%10s", inputstr);
	// printf("%d" ,i);
	// return 0;
	// }
	public void testNonglobalBeforeWrong() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkErrorLine(8);
	}

	// int main(void) {
	// char inputstr1[5];
	// int inputval;
	// int i = 5;
	// scanf("%i %as", inputval, inputstr1); // no error here
	// printf("%d" ,i);
	// return 0;
	// }
	public void testGaurdedRight() throws Exception {
		loadCodeAndRun(getAboveComment());
		checkNoErrors();
	}
}

Back to the top