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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Buffer Overflow Safety Integration Tests
//!
//! **RED PHASE**: Comprehensive tests for C buffer overflow → Safe Rust
//!
//! This validates that dangerous C buffer overflow patterns are transpiled to safe
//! Rust code where buffer overflows are prevented by bounds checking and safe types.
//!
//! **Pattern**: EXTREME TDD - Test-First Development
//! **Reference**: CWE-120 (Buffer Copy without Checking Size), CWE-119 (Buffer Overflow)
//!
//! **Safety Goal**: ≤100 unsafe blocks per 1000 LOC
//! **Validation**: Buffer overflows prevented by bounds checking, Vec/String types
use decy_core::transpile;
// ============================================================================
// RED PHASE: Basic Buffer Overflow Prevention
// ============================================================================
#[test]
fn test_fixed_array_access() {
// Fixed array with bounded access (safe)
let c_code = r#"
int main() {
int arr[10];
int i;
for (i = 0; i < 10; i++) {
arr[i] = i * 2;
}
return arr[5];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 4,
"Fixed array access should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_string_buffer_safe_size() {
// String buffer with safe size
let c_code = r#"
int main() {
char str[20];
int i;
for (i = 0; i < 10; i++) {
str[i] = 'A' + i;
}
str[10] = '\0';
return 0;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 5, "String buffer should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: Array Bounds Checking
// ============================================================================
#[test]
fn test_array_index_validation() {
// Array access with index validation
let c_code = r#"
int main() {
int arr[5];
int index = 3;
if (index >= 0 && index < 5) {
arr[index] = 42;
return arr[index];
}
return 0;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 4, "Index validation should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_loop_bounds_checked() {
// Loop with proper bounds checking
let c_code = r#"
int main() {
int arr[10];
int sum = 0;
int i;
for (i = 0; i < 10; i++) {
arr[i] = i;
sum = sum + arr[i];
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 5,
"Loop bounds checking should minimize unsafe (found {})",
unsafe_count
);
}
// ============================================================================
// RED PHASE: Multi-Dimensional Arrays
// ============================================================================
#[test]
fn test_2d_array_access() {
// 2D array with bounds checking
let c_code = r#"
int main() {
int matrix[3][3];
int i;
int j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
matrix[i][j] = i * 3 + j;
}
}
return matrix[1][1];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 6, "2D array access should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: Buffer Copy Operations
// ============================================================================
#[test]
fn test_manual_buffer_copy() {
// Manual buffer copy with bounds
let c_code = r#"
int main() {
int src[5];
int dst[5];
int i;
for (i = 0; i < 5; i++) {
src[i] = i * 10;
}
for (i = 0; i < 5; i++) {
dst[i] = src[i];
}
return dst[2];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 6, "Buffer copy should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_partial_buffer_copy() {
// Partial buffer copy (copy first N elements)
let c_code = r#"
int main() {
int src[10];
int dst[10];
int count = 5;
int i;
for (i = 0; i < 10; i++) {
src[i] = i;
}
for (i = 0; i < count && i < 10; i++) {
dst[i] = src[i];
}
return dst[3];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 7, "Partial copy should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: String Operations
// ============================================================================
#[test]
fn test_string_initialization() {
// String buffer initialization
let c_code = r#"
int main() {
char str[10];
int i;
for (i = 0; i < 5; i++) {
str[i] = 'A';
}
str[5] = '\0';
return 0;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 5,
"String initialization should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_string_length_check() {
// String with length checking
let c_code = r#"
int main() {
char str[20];
int len = 0;
int i;
for (i = 0; i < 15; i++) {
str[i] = 'X';
len = len + 1;
}
str[len] = '\0';
return len;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 6,
"String length check should minimize unsafe (found {})",
unsafe_count
);
}
// ============================================================================
// RED PHASE: Off-by-One Prevention
// ============================================================================
#[test]
fn test_off_by_one_prevention() {
// Proper loop bounds (< not <=)
let c_code = r#"
int main() {
int arr[5];
int i;
for (i = 0; i < 5; i++) {
arr[i] = i * 2;
}
return arr[4];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 4,
"Off-by-one prevention should minimize unsafe (found {})",
unsafe_count
);
}
// ============================================================================
// RED PHASE: Dynamic Sizing
// ============================================================================
#[test]
fn test_variable_size_array() {
// Array with variable-based size
let c_code = r#"
int main() {
int size = 8;
int arr[10];
int i;
for (i = 0; i < size && i < 10; i++) {
arr[i] = i;
}
return arr[5];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 5, "Variable size should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: Struct with Array Members
// ============================================================================
#[test]
fn test_struct_with_array() {
// Struct containing array
let c_code = r#"
struct Buffer {
int data[5];
int size;
};
int main() {
struct Buffer buf;
int i;
buf.size = 5;
for (i = 0; i < buf.size; i++) {
buf.data[i] = i * 3;
}
return buf.data[2];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 5, "Struct with array should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: Nested Arrays
// ============================================================================
#[test]
fn test_nested_array_access() {
// Array of arrays
let c_code = r#"
int main() {
int arrays[3][4];
int i;
int j;
int sum = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
arrays[i][j] = i + j;
sum = sum + arrays[i][j];
}
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 7, "Nested arrays should minimize unsafe (found {})", unsafe_count);
}
// ============================================================================
// RED PHASE: Function with Array Parameters
// ============================================================================
#[test]
fn test_function_array_parameter() {
// Function taking array and size
let c_code = r#"
int sum_array(int arr[], int size) {
int sum = 0;
int i;
for (i = 0; i < size; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int values[5];
int i;
for (i = 0; i < 5; i++) {
values[i] = i + 1;
}
return sum_array(values, 5);
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 6,
"Function with array should minimize unsafe (found {})",
unsafe_count
);
}
// ============================================================================
// RED PHASE: Unsafe Density Target
// ============================================================================
#[test]
fn test_unsafe_block_count_target() {
// CRITICAL: Validate overall unsafe minimization
let c_code = r#"
int main() {
int buffer[100];
int i;
int sum = 0;
for (i = 0; i < 100; i++) {
buffer[i] = i;
}
for (i = 0; i < 100; i++) {
sum = sum + buffer[i];
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
// Count unsafe blocks and calculate density
let unsafe_count = result.matches("unsafe").count();
let lines_of_code = result.lines().count();
let unsafe_per_1000 =
if lines_of_code > 0 { (unsafe_count as f64 / lines_of_code as f64) * 1000.0 } else { 0.0 };
// Target: <=100 unsafe per 1000 LOC for buffer overflow prevention
assert!(
unsafe_per_1000 <= 100.0,
"Buffer overflow prevention should minimize unsafe (got {:.2} per 1000 LOC, want <=100)",
unsafe_per_1000
);
// Should have main function
assert!(result.contains("fn main"), "Should generate main function");
}
// ============================================================================
// RED PHASE: Compilation and Correctness
// ============================================================================
#[test]
fn test_transpiled_code_compiles() {
// Generated Rust should have valid syntax
let c_code = r#"
int main() {
int arr[10];
int i;
for (i = 0; i < 10; i++) {
arr[i] = i * i;
}
return arr[5];
}
"#;
let result = transpile(c_code).expect("Should transpile");
// Basic syntax validation
assert!(!result.is_empty(), "Should generate non-empty code");
assert!(result.contains("fn main"), "Should have main function");
// Should not have obvious syntax errors
let open_braces = result.matches('{').count();
let close_braces = result.matches('}').count();
assert_eq!(
open_braces, close_braces,
"Braces should be balanced: {} open, {} close",
open_braces, close_braces
);
}
#[test]
fn test_buffer_overflow_safety_documentation() {
// Validate generated code quality
let c_code = r#"
int main() {
int buffer[20];
int i;
for (i = 0; i < 20; i++) {
buffer[i] = i;
}
return buffer[10];
}
"#;
let result = transpile(c_code).expect("Should transpile");
// Generated code should be reasonable
assert!(result.contains("fn main"), "Should have main function");
// If unsafe blocks exist, they should be minimal
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count < 10, "Should have minimal unsafe blocks (found {})", unsafe_count);
}