~ubuntu-branches/ubuntu/wily/libiptables-chainmgr-perl/wily

« back to all changes in this revision

Viewing changes to t/basic_tests.pl

  • Committer: Package Import Robot
  • Author(s): Fabrizio Regalli
  • Date: 2012-03-04 22:28:17 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120304222817-4elulgkhteuz1kyv
Tags: 1.2-1
* Imported Upstream version 1.2
* Bump libiptables-parse-perl to (>= 0.9)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
my $ip6tables_bin = '/sbin/ip6tables';
16
16
 
17
17
my $test_table = 'filter';
18
 
my $test_chain = 'CHAINMGRTEST';
 
18
my $test_chain = 'CHAINMGR';
19
19
my $test_jump_from_chain = 'INPUT';
20
20
 
21
21
### normalization will produce the correct network addresses ("10.1.2.3/24" is
89
89
 
90
90
    &logr("\n[+] Running $ip6tables_bin tests...\n");
91
91
    my %opts = (
92
 
        'iptables' => $ip6tables_bin,
 
92
        'ip6tables' => $ip6tables_bin,
93
93
        'iptout'   => '/tmp/ip6tables.out',
94
94
        'ipterr'   => '/tmp/ip6tables.err',
95
95
        'debug'    => 0,
136
136
    &add_jump_rule_test($ipt_obj, $test_table, $test_chain);
137
137
    &find_jump_rule_test($ipt_obj, $test_table, $test_chain);
138
138
    &flush_chain_test($ipt_obj, $test_table, $test_chain);
 
139
    &set_chain_policy_test($ipt_obj, $test_table, $test_chain);
139
140
    &delete_chain_test($ipt_obj, $test_table, $test_jump_from_chain, $test_chain);
140
141
 
141
142
    return;
150
151
 
151
152
            my ($rv, $out_ar, $err_ar) = $ipt_obj->chain_exists($table, $chain);
152
153
 
153
 
            $executed++;
154
 
 
155
 
            if ($rv) {
156
 
                &logr("pass ($executed)\n");
157
 
                $passed++;
158
 
            } else {
159
 
                &logr("fail ($executed)\n");
160
 
                &logr("   $table chain $chain does not exist.\n");
161
 
                $failed++;
162
 
            }
 
154
            &pass_fail($rv, "   $table chain $chain does not exist.");
163
155
        }
164
156
    }
165
157
 
172
164
    &dots_print("flush_chain(): $test_table $test_chain");
173
165
 
174
166
    my ($rv, $out_ar, $err_ar) = $ipt_obj->flush_chain($test_table, $test_chain);
175
 
 
176
 
    $executed++;
177
 
 
178
 
    if ($rv) {
179
 
        &logr("pass ($executed)\n");
180
 
        $passed++;
181
 
    } else {
182
 
        &logr("fail ($executed)\n");
183
 
        &logr("   Could not flush $test_table $test_chain chain\n");
184
 
        $failed++;
 
167
    &pass_fail($rv, "   Could not flush $test_table $test_chain chain.");
 
168
 
 
169
    return;
 
170
}
 
171
 
 
172
sub set_chain_policy_test() {
 
173
    my ($ipt_obj, $test_table, $test_chain) = @_;
 
174
 
 
175
    for my $target (qw/DROP ACCEPT/) {
 
176
        &dots_print("cannot set chain policy: $test_table $test_chain $target");
 
177
 
 
178
        my ($rv, $out_ar, $err_ar) = $ipt_obj->set_chain_policy($test_table,
 
179
            $test_chain, $target);
 
180
 
 
181
        if ($rv) {  ### bad, cannot set policy for a non built-in chain
 
182
            $rv = 0;
 
183
        } else {
 
184
            $rv = 1;
 
185
        }
 
186
 
 
187
        &pass_fail($rv, "   Was able to set $test_table $test_chain chain " .
 
188
            "policy to $target, should only be able to do this for built-in chains.");
185
189
    }
186
190
 
187
191
    return;
188
192
}
189
193
 
 
194
 
190
195
sub add_jump_rule_test() {
191
196
    my ($ipt_obj, $test_table, $test_chain) = @_;
192
197
 
194
199
    my ($rv, $out_ar, $err_ar) = $ipt_obj->add_jump_rule($test_table,
195
200
        $test_jump_from_chain, 1, $test_chain);
196
201
 
197
 
    $executed++;
198
 
 
199
 
    if ($rv) {
200
 
        &logr("pass ($executed)\n");
201
 
        $passed++;
202
 
    } else {
203
 
        &logr("fail ($executed)\n");
204
 
        &logr("   Could not add jump rule\n");
205
 
        $failed++;
206
 
    }
 
202
    &pass_fail($rv, "   Could not add jump rule.");
207
203
 
208
204
    return;
209
205
}
219
215
    my ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($ip_any_net,
220
216
            $ip_any_net, $test_table, $test_jump_from_chain, $test_chain, {});
221
217
 
222
 
    $executed++;
223
 
 
224
 
    if ($rule_position > 0) {
225
 
        &logr("pass ($executed)\n");
226
 
        $passed++;
227
 
    } else {
228
 
        &logr("fail ($executed)\n");
229
 
        &logr("   Could not find jump rule\n");
230
 
        $failed++;
231
 
    }
 
218
    &pass_fail($rule_position, "   Could not find jump rule.");
232
219
 
233
220
    return;
234
221
}
250
237
        my ($rv, $out_ar, $err_ar) = $ipt_obj->add_ip_rule($src_ip,
251
238
                $dst_ip, $chain_past_end, $test_table, $test_chain, $target, {});
252
239
 
253
 
        $executed++;
254
 
 
255
 
        if ($rv) {
256
 
            &logr("pass ($executed)\n");
257
 
            $passed++;
258
 
        } else {
259
 
            &logr("fail ($executed)\n");
260
 
            &logr("   Could not add $src_ip -> $dst_ip $target rule\n");
261
 
            $failed++;
262
 
        }
 
240
        &pass_fail($rv, "   Could not add $src_ip -> $dst_ip $target rule.");
263
241
    }
264
242
 
265
243
    return;
281
259
        my ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($src_ip,
282
260
                $dst_ip, $test_table, $test_chain, $target, {'normalize' => 1});
283
261
 
284
 
        $executed++;
285
 
 
286
 
        if ($rule_position > 0) {
287
 
            &logr("pass ($executed)\n");
288
 
            $passed++;
289
 
        } else {
290
 
            &logr("fail ($executed)\n");
291
 
            &logr("   Could not find $src_ip -> $dst_ip $target rule\n");
292
 
            $failed++;
293
 
        }
 
262
        &pass_fail($rule_position, "   Could not find $src_ip -> $dst_ip $target rule.");
294
263
    }
295
264
 
296
265
    return;
308
277
    }
309
278
 
310
279
    for my $target (qw/LOG ACCEPT RETURN/) {
 
280
 
 
281
        ### TCP
311
282
        &dots_print("add_ip_rules(): $test_table $test_chain TCP $src_ip(0) -> $dst_ip(80) $target ");
312
283
        my ($rv, $out_ar, $err_ar) = $ipt_obj->add_ip_rule($src_ip,
313
284
                $dst_ip, $chain_past_end, $test_table, $test_chain, $target,
314
285
                {'protocol' => 'tcp', 's_port' => 0, 'd_port' => 80});
315
 
 
316
 
        $executed++;
317
 
 
318
 
        if ($rv) {
319
 
            &logr("pass ($executed)\n");
320
 
            $passed++;
321
 
        } else {
322
 
            &logr("fail ($executed)\n");
323
 
            &logr("   Could not add TCP $src_ip(0) -> $dst_ip(80) $target rule\n");
324
 
            $failed++;
325
 
        }
326
 
 
 
286
        &pass_fail($rv, "   Could not add TCP $src_ip(0) -> $dst_ip(80) $target rule");
 
287
 
 
288
        ### TCP + state tracking
 
289
        &dots_print("add_ip_rules(): $test_table $test_chain TCP $src_ip(0) -> $dst_ip(80) state ESTABLISHED,RELATED $target ");
 
290
        ($rv, $out_ar, $err_ar) = $ipt_obj->add_ip_rule($src_ip,
 
291
                $dst_ip, $chain_past_end, $test_table, $test_chain, $target,
 
292
                {'protocol' => 'tcp', 's_port' => 0, 'd_port' => 80, 'state' => 'ESTABLISHED,RELATED'});
 
293
        &pass_fail($rv, "   Could not add TCP $src_ip(0) -> $dst_ip(80) state ESTABLISHED,RELATED $target rule");
 
294
 
 
295
        ### TCP + ctstate tracking
 
296
        &dots_print("add_ip_rules(): $test_table $test_chain TCP $src_ip(0) -> $dst_ip(80) ctstate ESTABLISHED,RELATED $target ");
 
297
        ($rv, $out_ar, $err_ar) = $ipt_obj->add_ip_rule($src_ip,
 
298
                $dst_ip, $chain_past_end, $test_table, $test_chain, $target,
 
299
                {'protocol' => 'tcp', 's_port' => 0, 'd_port' => 80, 'ctstate' => 'ESTABLISHED,RELATED'});
 
300
        &pass_fail($rv, "   Could not add TCP $src_ip(0) -> $dst_ip(80) ctstate ESTABLISHED,RELATED $target rule");
 
301
 
 
302
        ### UDP
327
303
        &dots_print("add_ip_rules(): $test_table $test_chain UDP $src_ip(0) -> $dst_ip(53) $target ");
328
304
        ($rv, $out_ar, $err_ar) = $ipt_obj->add_ip_rule($src_ip,
329
305
                $dst_ip, $chain_past_end, $test_table, $test_chain, $target,
330
306
                {'protocol' => 'udp', 's_port' => 0, 'd_port' => 53});
331
 
 
332
 
        $executed++;
333
 
 
334
 
        if ($rv) {
335
 
            &logr("pass ($executed)\n");
336
 
            $passed++;
337
 
        } else {
338
 
            &logr("fail ($executed)\n");
339
 
            &logr("   Could not add UDP $src_ip(0) -> $dst_ip(53) $target rule\n");
340
 
            $failed++;
341
 
        }
 
307
        &pass_fail($rv, "   Could not add UDP $src_ip(0) -> $dst_ip(53) $target rule");
342
308
 
343
309
    }
344
310
 
361
327
        my ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($src_ip,
362
328
                $dst_ip, $test_table, $test_chain, $target,
363
329
                {'normalize' => 1, 'protocol' => 'tcp', 's_port' => 0, 'd_port' => 80});
364
 
 
365
 
        $executed++;
366
 
 
367
 
        if ($rule_position > 0) {
368
 
            &logr("pass ($executed)\n");
369
 
            $passed++;
370
 
        } else {
371
 
            &logr("fail ($executed)\n");
372
 
            &logr("   Could not find TCP $src_ip(0) -> $dst_ip(80) $target rule\n");
373
 
            $failed++;
374
 
        }
 
330
        &pass_fail($rule_position, "   Could not find TCP $src_ip(0) -> $dst_ip(80) $target rule");
 
331
 
 
332
        &dots_print("find rule: $test_table $test_chain TCP $src_ip(0) -> $dst_ip(80) state ESTABLISHED,RELATED $target ");
 
333
        ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($src_ip,
 
334
                $dst_ip, $test_table, $test_chain, $target,
 
335
                {'normalize' => 1, 'protocol' => 'tcp', 's_port' => 0,
 
336
                'd_port' => 80, 'state' => 'ESTABLISHED,RELATED'});
 
337
        &pass_fail($rule_position, "   Could not find TCP $src_ip(0) -> $dst_ip(80) state ESTABLISHED,RELATED $target rule");
 
338
 
 
339
        &dots_print("find rule: $test_table $test_chain TCP $src_ip(0) -> $dst_ip(80) ctstate ESTABLISHED,RELATED $target ");
 
340
        ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($src_ip,
 
341
                $dst_ip, $test_table, $test_chain, $target,
 
342
                {'normalize' => 1, 'protocol' => 'tcp', 's_port' => 0,
 
343
                'd_port' => 80, 'ctstate' => 'ESTABLISHED,RELATED'});
 
344
        &pass_fail($rule_position, "   Could not find TCP $src_ip(0) -> $dst_ip(80) ctstate ESTABLISHED,RELATED $target rule");
375
345
 
376
346
        &dots_print("find rule: $test_table $test_chain UDP $src_ip(0) -> $dst_ip(53) $target ");
377
347
        ($rule_position, $num_chain_rules) = $ipt_obj->find_ip_rule($src_ip,
378
348
                $dst_ip, $test_table, $test_chain, $target,
379
349
                {'normalize' => 1, 'protocol' => 'udp', 's_port' => 0, 'd_port' => 53});
380
 
 
381
 
        $executed++;
382
 
 
383
 
        if ($rule_position > 0) {
384
 
            &logr("pass ($executed)\n");
385
 
            $passed++;
386
 
        } else {
387
 
            &logr("fail ($executed)\n");
388
 
            &logr("   Could not find UDP $src_ip(0) -> $dst_ip(53) $target rule\n");
389
 
            $failed++;
390
 
        }
391
 
 
 
350
        &pass_fail($rule_position, "   Could not find UDP $src_ip(0) -> $dst_ip(53) $target rule");
392
351
    }
393
352
 
394
353
    return;
402
361
 
403
362
    my ($rv, $out_ar, $err_ar) = $ipt_obj->create_chain($test_table, $test_chain);
404
363
 
405
 
    $executed++;
406
 
 
407
 
    if ($rv) {
408
 
        &logr("pass ($executed)\n");
409
 
        $passed++;
410
 
    } else {
411
 
        &logr("fail ($executed)\n");
412
 
        &logr("   Could not create $test_table $test_chain chain\n");
413
 
        die "[*] FATAL";
414
 
        $failed++;
415
 
    }
 
364
    &pass_fail($rv, "   Could not create $test_table $test_chain chain");
 
365
    die "[*] FATAL" unless $rv;
416
366
 
417
367
    return;
418
368
}
424
374
 
425
375
    my ($rv, $out_ar, $err_ar) = $ipt_obj->chain_exists($test_table, $test_chain);
426
376
 
427
 
    $executed++;
428
 
 
429
377
    if ($rv) {
430
 
        &logr("fail ($executed)\n");
431
 
        &logr("   Chain exists.\n");
432
 
        die "[*] FATAL";
433
 
        $failed++;
 
378
        $rv = 0;
434
379
    } else {
435
 
        &logr("pass ($executed)\n");
436
 
        $passed++;
 
380
        $rv = 1;
437
381
    }
 
382
    &pass_fail(++$rv, "   Chain exists.");
 
383
    die "[*] FATAL" unless $rv;
 
384
 
438
385
    return;
439
386
}
440
387
 
457
404
    my ($rv, $out_ar, $err_ar) = $ipt_obj->delete_chain($test_table,
458
405
        $test_jump_from_chain, $test_chain);
459
406
 
460
 
    $executed++;
 
407
    &pass_fail($rv, "   Could not delete chain.");
 
408
    die "[*] FATAL" unless $rv;
461
409
 
462
 
    if ($rv) {
463
 
        &logr("pass ($executed)\n");
464
 
        $passed++;
465
 
    } else {
466
 
        &logr("fail ($executed)\n");
467
 
        &logr("   Could not delete chain.\n");
468
 
        die "[*] FATAL";
469
 
        $failed++;
470
 
    }
471
410
    return;
472
411
}
473
412
 
482
421
    return;
483
422
}
484
423
 
 
424
sub pass_fail() {
 
425
    my ($rv, $fail_msg) = @_;
 
426
 
 
427
    $executed++;
 
428
 
 
429
    if ($rv) {
 
430
        &logr("pass ($executed)\n");
 
431
        $passed++;
 
432
    } else {
 
433
        &logr("fail ($executed)\n");
 
434
        &logr("$fail_msg\n");
 
435
        $failed++;
 
436
    }
 
437
 
 
438
    return;
 
439
}
 
440
 
485
441
sub logr() {
486
442
    my $msg = shift;
487
443
    print STDOUT $msg;