summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/tests/addrstr.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /nsprpub/pr/tests/addrstr.c
parentInitial commit. (diff)
downloadfirefox-upstream/124.0.1.tar.xz
firefox-upstream/124.0.1.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--nsprpub/pr/tests/addrstr.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/nsprpub/pr/tests/addrstr.c b/nsprpub/pr/tests/addrstr.c
new file mode 100644
index 0000000000..b6736a74bf
--- /dev/null
+++ b/nsprpub/pr/tests/addrstr.c
@@ -0,0 +1,82 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "prnetdb.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+const char *testaddrs[] = {
+ "::", "::",
+ "::1", "::1",
+ "::ffff", "::ffff",
+ "::1:0", "::0.1.0.0",
+ "::127.0.0.1", "::127.0.0.1",
+ "::FFFF:127.0.0.1", "::ffff:127.0.0.1",
+ "::FFFE:9504:3501", "::fffe:9504:3501",
+ "0:0:1:0:35c:0:0:0", "0:0:1:0:35c::",
+ "0:0:3f4c:0:0:4552:0:0", "::3f4c:0:0:4552:0:0",
+ "0:0:1245:0:0:0:0567:0", "0:0:1245::567:0",
+ "0:1:2:3:4:5:6:7", "0:1:2:3:4:5:6:7",
+ "1:2:3:0:4:5:6:7", "1:2:3:0:4:5:6:7",
+ "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:0",
+ "1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:8",
+ "1:2:3:4:5:6::7", "1:2:3:4:5:6:0:7",
+ 0
+};
+
+const char *badaddrs[] = {
+ "::.1.2.3",
+ "ffff::.1.2.3",
+ "1:2:3:4:5:6:7::8",
+ "1:2:3:4:5:6::7:8",
+ "::ff99.2.3.4",
+ 0
+};
+
+int failed_already = 0;
+
+int main(int argc, char **argv)
+{
+ const char **nexttestaddr = testaddrs;
+ const char **nextbadaddr = badaddrs;
+ const char *in, *expected_out;
+ PRNetAddr addr;
+ char buf[256];
+ PRStatus rv;
+
+ while ((in = *nexttestaddr++) != 0) {
+ expected_out = *nexttestaddr++;
+ rv = PR_StringToNetAddr(in, &addr);
+ if (rv) {
+ printf("cannot convert %s to addr: %d\n", in, rv);
+ failed_already = 1;
+ continue;
+ }
+ rv = PR_NetAddrToString(&addr, buf, sizeof(buf));
+ if (rv) {
+ printf("cannot convert %s back to string: %d\n", in, rv);
+ failed_already = 1;
+ continue;
+ }
+ if (strcmp(buf, expected_out)) {
+ /* This is not necessarily an error */
+ printf("%s expected %s got %s\n", in, expected_out, buf);
+ }
+ }
+ while ((in = *nextbadaddr++) != 0) {
+ if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) {
+ printf("converted bad addr %s\n", in);
+ failed_already = 1;
+ }
+ }
+ if (failed_already) {
+ printf("FAIL\n");
+ return 1;
+ }
+ printf("PASS\n");
+ return 0;
+}