Skip to content

Commit

Permalink
add better validation and more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <[email protected]>
  • Loading branch information
briandowns committed Oct 30, 2023
1 parent 66c0d15 commit 7f29abf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/ip.du
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Net;

print(Net.ip4Len);
print(Net.ip6Len);
const res = Net.parseIp("10.1.1.9").unwrap();
const res = Net.parseIp4("10.1.1.9").unwrap();
print(res);
6 changes: 6 additions & 0 deletions src/optionals/net.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <arpa/inet.h>
#include <string.h>

#include "net.h"
Expand All @@ -18,6 +19,11 @@ static Value parseIp4(DictuVM *vm, int argCount, Value *args) {
}

char *ipStr = AS_CSTRING(args[0]);
char ignore[4] = {0};

if (inet_pton(AF_INET, ipStr, ignore) == 0) {
return newResultError(vm, "invalid ip4 address");
}

unsigned char value[IP4_LEN] = {0};
size_t index = 0;
Expand Down
8 changes: 8 additions & 0 deletions tests/net/parseIp4.du
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class TestParseIp4 < UnitTest {
const parsed = res.unwrap();
this.assertEquals(parsed.len(), Net.ip4Len);
}

testParseBadIp4() {
const res = Net.parseIp4("900.8");
this.assertError(res);

const res2 = Net.parseIp4("255.256.255.255");
this.assertError(res2);
}
}

TestParseIp4().run();

0 comments on commit 7f29abf

Please sign in to comment.