nsprpub/pr/tests/addrstr.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "prnetdb.h"
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9 #include <string.h>
michael@0 10 #include <stdlib.h>
michael@0 11
michael@0 12 const char *testaddrs[] = {
michael@0 13 "::", "::",
michael@0 14 "::1", "::1",
michael@0 15 "::ffff", "::ffff",
michael@0 16 "::1:0", "::0.1.0.0",
michael@0 17 "::127.0.0.1", "::127.0.0.1",
michael@0 18 "::FFFF:127.0.0.1", "::ffff:127.0.0.1",
michael@0 19 "::FFFE:9504:3501", "::fffe:9504:3501",
michael@0 20 "0:0:1:0:35c:0:0:0", "0:0:1:0:35c::",
michael@0 21 "0:0:3f4c:0:0:4552:0:0", "::3f4c:0:0:4552:0:0",
michael@0 22 "0:0:1245:0:0:0:0567:0", "0:0:1245::567:0",
michael@0 23 "0:1:2:3:4:5:6:7", "0:1:2:3:4:5:6:7",
michael@0 24 "1:2:3:0:4:5:6:7", "1:2:3:0:4:5:6:7",
michael@0 25 "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:0",
michael@0 26 "1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:8",
michael@0 27 "1:2:3:4:5:6::7", "1:2:3:4:5:6:0:7",
michael@0 28 0
michael@0 29 };
michael@0 30
michael@0 31 const char *badaddrs[] = {
michael@0 32 "::.1.2.3",
michael@0 33 "ffff::.1.2.3",
michael@0 34 "1:2:3:4:5:6:7::8",
michael@0 35 "1:2:3:4:5:6::7:8",
michael@0 36 "::ff99.2.3.4",
michael@0 37 0
michael@0 38 };
michael@0 39
michael@0 40 int failed_already = 0;
michael@0 41
michael@0 42 int main(int argc, char **argv)
michael@0 43 {
michael@0 44 const char **nexttestaddr = testaddrs;
michael@0 45 const char **nextbadaddr = badaddrs;
michael@0 46 const char *in, *expected_out;
michael@0 47 PRNetAddr addr;
michael@0 48 char buf[256];
michael@0 49 PRStatus rv;
michael@0 50
michael@0 51 while ((in = *nexttestaddr++) != 0) {
michael@0 52 expected_out = *nexttestaddr++;
michael@0 53 rv = PR_StringToNetAddr(in, &addr);
michael@0 54 if (rv) {
michael@0 55 printf("cannot convert %s to addr: %d\n", in, rv);
michael@0 56 failed_already = 1;
michael@0 57 continue;
michael@0 58 }
michael@0 59 rv = PR_NetAddrToString(&addr, buf, sizeof(buf));
michael@0 60 if (rv) {
michael@0 61 printf("cannot convert %s back to string: %d\n", in, rv);
michael@0 62 failed_already = 1;
michael@0 63 continue;
michael@0 64 }
michael@0 65 if (strcmp(buf, expected_out)) {
michael@0 66 /* This is not necessarily an error */
michael@0 67 printf("%s expected %s got %s\n", in, expected_out, buf);
michael@0 68 }
michael@0 69 }
michael@0 70 while ((in = *nextbadaddr++) != 0) {
michael@0 71 if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) {
michael@0 72 printf("converted bad addr %s\n", in);
michael@0 73 failed_already = 1;
michael@0 74 }
michael@0 75 }
michael@0 76 if (failed_already) {
michael@0 77 printf("FAIL\n");
michael@0 78 return 1;
michael@0 79 }
michael@0 80 printf("PASS\n");
michael@0 81 return 0;
michael@0 82 }

mercurial