Skip to content

Commit f22e60e

Browse files
authored
Merge pull request #34 from ktsaou/master
fix cmd line parsing and additional tests
2 parents 0fef242 + 6f7f917 commit f22e60e

File tree

29 files changed

+120
-55
lines changed

29 files changed

+120
-55
lines changed

iprange.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ int main(int argc, char **argv) {
349349
size_t ipset_reduce_factor = 120;
350350
size_t ipset_reduce_min_accepted = 16384;
351351
int ret = 0, quiet = 0;
352+
int inputs = 0;
352353

353354
ipset *root = NULL, *ips = NULL, *first = NULL, *second = NULL;
354355
int i, mode = MODE_COMBINE, header = 0, read_second = 0;
@@ -565,9 +566,8 @@ int main(int argc, char **argv) {
565566
}
566567
else {
567568
if(!strcmp(argv[i], "-")) {
568-
ips = ipset_load(NULL);
569-
570-
if(!ips) {
569+
inputs++;
570+
if(!(ips = ipset_load(NULL))) {
571571
fprintf(stderr, "%s: Cannot load ipset from stdin\n", PROG);
572572
exit(1);
573573
}
@@ -585,6 +585,8 @@ int main(int argc, char **argv) {
585585
}
586586
}
587587
else if(argv[i][0] == '@') {
588+
inputs++;
589+
588590
/* Handle @filename as a file list or directory */
589591
const char *listname = argv[i] + 1; /* Skip the @ character */
590592
struct stat st;
@@ -629,11 +631,10 @@ int main(int argc, char **argv) {
629631
fprintf(stderr, "%s: Loading file %s from directory %s\n", PROG, entry->d_name, listname);
630632

631633
/* Load the file as an independent ipset */
632-
ips = ipset_load(filepath);
633-
if(!ips) {
634+
if(!(ips = ipset_load(filepath))) {
634635
fprintf(stderr, "%s: Cannot load file %s from directory %s\n",
635636
PROG, filepath, listname);
636-
continue;
637+
exit(1);
637638
}
638639

639640
files_loaded = 1;
@@ -700,11 +701,10 @@ int main(int argc, char **argv) {
700701
fprintf(stderr, "%s: Loading file %s from list (line %d)\n", PROG, s, lineid);
701702

702703
/* Load the file as an independent ipset */
703-
ips = ipset_load(s);
704-
if(!ips) {
704+
if(!(ips = ipset_load(s))) {
705705
fprintf(stderr, "%s: Cannot load file %s from list %s (line %d)\n",
706706
PROG, s, listname, lineid);
707-
continue;
707+
exit(1);
708708
}
709709

710710
files_loaded = 1;
@@ -736,11 +736,10 @@ int main(int argc, char **argv) {
736736
}
737737
}
738738
else {
739-
ips = ipset_load(argv[i]);
740-
741-
if(!ips) {
739+
inputs++;
740+
if(!(ips = ipset_load(argv[i]))) {
742741
fprintf(stderr, "%s: Cannot load ipset: %s\n", PROG, argv[i]);
743-
continue; /* Continue with other arguments instead of exiting */
742+
exit(1);
744743
}
745744

746745
if(read_second) {
@@ -760,21 +759,21 @@ int main(int argc, char **argv) {
760759

761760
/*
762761
* if no ipset was given on the command line
763-
* assume stdin, but only if no other filenames were specified
762+
* assume stdin, regardless of whether other options were specified
764763
*/
765764

766-
if(!root && argc <= 1) {
765+
if(!inputs) {
767766
if(unlikely(debug))
768-
fprintf(stderr, "%s: No inputs provided, reading from stdin\n", PROG);
769-
770-
first = root = ipset_load(NULL);
771-
if(!root) {
772-
fprintf(stderr, "%s: No ipsets to merge.\n", PROG);
767+
fprintf(stderr, "%s: No input files provided, reading from stdin\n", PROG);
768+
769+
if(!(first = root = ipset_load(NULL))) {
770+
fprintf(stderr, "%s: Cannot load ipset from stdin\n", PROG);
773771
exit(1);
774772
}
775773
}
776-
else if(!root) {
777-
/* We had parameters but still ended up with no valid ipsets */
774+
775+
if(!root) {
776+
// impossible situation since we fail if no ipset is loaded
778777
fprintf(stderr, "%s: No valid ipsets to merge from the provided inputs.\n", PROG);
779778
exit(1);
780779
}

ipset_load.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,13 @@ ipset *ipset_load(const char *filename) {
610610
ips->flags |= IPSET_FLAG_OPTIMIZED;
611611

612612
if(!fgets(line, MAX_LINE, fp)) {
613-
if(likely(fp != stdin)) fclose(fp);
614-
/* Empty file - if not stdin, consider it an error */
615-
if(likely(filename && *filename)) {
616-
if(unlikely(debug)) fprintf(stderr, "%s: File %s is empty\n", PROG, filename);
617-
ipset_free(ips);
618-
return NULL;
619-
}
613+
if(likely(fp != stdin))
614+
fclose(fp);
615+
616+
/* For normal files, an empty file is valid too (return empty ipset) */
617+
if(unlikely(debug))
618+
fprintf(stderr, "%s: %s is empty\n", PROG, filename && *filename?filename:"stdin");
619+
620620
return ips;
621621
}
622622

tests.d/19-nonexistent-paths/cmd.sh

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,5 @@
22
# Test handling of non-existent files and directories
33
# The program should gracefully handle these cases
44

5-
echo "=== Test 1: Non-existent file ==="
6-
if ../../iprange nonexistent_file input1 2>error.log; then
7-
echo "FAILED: Should have shown error for non-existent file"
8-
else
9-
echo "PASSED: Correctly handled non-existent file"
10-
fi
11-
12-
echo "=== Test 2: Non-existent directory ==="
13-
if ../../iprange @nonexistent_dir input1 2>error2.log; then
14-
echo "FAILED: Should have shown error for non-existent directory"
15-
else
16-
echo "PASSED: Correctly handled non-existent directory"
17-
fi
18-
19-
echo "=== Test 3: Valid file works ==="
20-
../../iprange input1
21-
22-
# Cleanup
23-
rm -f error*.log
5+
../../iprange nonexistent_file input1 2>/dev/null || echo "FAILED AS EXPECTED 1"
6+
../../iprange input1 @nonexistent_file 2>/dev/null || echo "FAILED AS EXPECTED 2"

tests.d/19-nonexistent-paths/output

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
=== Test 1: Non-existent file ===
2-
192.168.1.1
3-
FAILED: Should have shown error for non-existent file
4-
=== Test 2: Non-existent directory ===
5-
PASSED: Correctly handled non-existent directory
6-
=== Test 3: Valid file works ===
7-
192.168.1.1
1+
FAILED AS EXPECTED 1
2+
FAILED AS EXPECTED 2

tests.d/22-binary-to-text/cmd.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Convert to binary and back to text
4+
echo "250.250.250.250" | ../../iprange input1 --print-binary | ../../iprange

tests.d/22-binary-to-text/input1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
192.168.1.1
2+
192.168.1.2
3+
10.0.0.1

tests.d/22-binary-to-text/output

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
10.0.0.1
2+
192.168.1.1
3+
192.168.1.2

tests.d/23-empty-input-count/cmd.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
# Input is an empty set (file)
4+
echo >empty_file
5+
../../iprange empty_file -C
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests.d/23-empty-input-count/output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0,0

0 commit comments

Comments
 (0)