Exemple #1
0
int main()
{
    RTTEST hTest;
    int rc = RTTestInitAndCreate("tstRTNetIPv4", &hTest);
    if (rc)
        return rc;
    RTTestBanner(hTest);

    GOODADDR("1.2.3.4",         0x01020304);
    GOODADDR("0.0.0.0",         0x00000000);
    GOODADDR("255.255.255.255", 0xFFFFFFFF);

    /* leading and trailing whitespace is allowed */
    GOODADDR(" 1.2.3.4 ",       0x01020304);
    GOODADDR("\t1.2.3.4\t",     0x01020304);

    BADADDR("1.2.3.4x");
    BADADDR("1.2.3.4.");
    BADADDR("1.2.3");
    BADADDR("0x1.2.3.4");
    BADADDR("666.2.3.4");
    BADADDR("1.666.3.4");
    BADADDR("1.2.666.4");
    BADADDR("1.2.3.666");

    /*
     * Parsing itself is covered by the tests above, here we only
     * check trailers
     */
    CHECKADDREX("1.2.3.4",  "",   VINF_SUCCESS,           0x01020304);
    CHECKADDREX("1.2.3.4",  " ",  VINF_SUCCESS,           0x01020304);
    CHECKADDREX("1.2.3.4",  "x",  VINF_SUCCESS,           0x01020304);
    CHECKADDREX("1.2.3.444", "",  VERR_INVALID_PARAMETER,          0);


    IS_ANY("0.0.0.0");
    IS_ANY("\t 0.0.0.0 \t");

    NOT_ANY("1.1.1.1");         /* good address, but not INADDR_ANY */
    NOT_ANY("0.0.0.0x");        /* bad address */

    return RTTestSummaryAndDestroy(hTest);
}
int main()
{
    RTTEST hTest;
    int rc = RTTestInitAndCreate("tstRTNetIPv6", &hTest);
    if (rc)
        return rc;
    RTTestBanner(hTest);


    /* base case: eight groups fully spelled */
    GOODADDR("1:2:3:4:5:6:7:8", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("0001:0002:0003:0004:0005:0006:0007:0008", 0x00010002, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("D:E:A:D:b:e:e:f", 0x000d000e, 0x000a000d, 0x000b000e, 0x000e000f);

    /* ... too short or too long */
    BADADDR("1:2:3:4:5:6:7");
    BADADDR("1:2:3:4:5:6:7:8:9");

    /* ... hex group constraints */
    BADADDR("1:2:3:4:5:6:7:-8");
    BADADDR("1:2:3:4:5:6:7:0x8");
    BADADDR("1:2:3:4:5:6:7:88888");
    BADADDR("1:2:3:4:5:6:7:00008");


    /* embedded IPv4 at the end */
    GOODADDR("0:0:0:0:0:0:1.2.3.4", 0, 0, 0, 0x01020304);

    /* ... not at the end */
    BADADDR("0:0:0:0:0:1.2.3.4:0");

    /* ... too short or too long */
    BADADDR("0:0:0:0:0:0:0:1.2.3.4");
    BADADDR("0:0:0:0:0:1.2.3.4");

    /* ... invalid IPv4 address */
    BADADDR("0:0:0:0:0:0:111.222.333.444");


    /* "any" in compressed form */
    GOODADDR("::",  0, 0, 0, 0);

    /* compressed run at the beginning */
    GOODADDR("::8",                      0,          0,          0, 0x00000008);
    GOODADDR("::7:8",                    0,          0,          0, 0x00070008);
    GOODADDR("::6:7:8",                  0,          0, 0x00000006, 0x00070008);
    GOODADDR("::5:6:7:8",                0,          0, 0x00050006, 0x00070008);
    GOODADDR("::4:5:6:7:8",              0, 0x00000004, 0x00050006, 0x00070008);
    GOODADDR("::3:4:5:6:7:8",            0, 0x00030004, 0x00050006, 0x00070008);
    GOODADDR("::2:3:4:5:6:7:8", 0x00000002, 0x00030004, 0x00050006, 0x00070008);

    /* ... too long */
    BADADDR("::1:2:3:4:5:6:7:8");

    /* compressed run at the end */
    GOODADDR("1::",             0x00010000,          0,          0,          0);
    GOODADDR("1:2::",           0x00010002,          0,          0,          0);
    GOODADDR("1:2:3::",         0x00010002, 0x00030000,          0,          0);
    GOODADDR("1:2:3:4::",       0x00010002, 0x00030004,          0,          0);
    GOODADDR("1:2:3:4:5::",     0x00010002, 0x00030004, 0x00050000,          0);
    GOODADDR("1:2:3:4:5:6::",   0x00010002, 0x00030004, 0x00050006,          0);
    GOODADDR("1:2:3:4:5:6:7::", 0x00010002, 0x00030004, 0x00050006, 0x00070000);

    /* ... too long */
    BADADDR("1:2:3:4:5:6:7:8::");

    /* compressed run in the middle  */
    GOODADDR("1::8",            0x00010000,          0,          0, 0x00000008);
    GOODADDR("1:2::8",          0x00010002,          0,          0, 0x00000008);
    GOODADDR("1:2:3::8",        0x00010002, 0x00030000,          0, 0x00000008);
    GOODADDR("1:2:3:4::8",      0x00010002, 0x00030004,          0, 0x00000008);
    GOODADDR("1:2:3:4:5::8",    0x00010002, 0x00030004, 0x00050000, 0x00000008);
    GOODADDR("1:2:3:4:5:6::8",  0x00010002, 0x00030004, 0x00050006, 0x00000008);

    GOODADDR("1::7:8",          0x00010000,          0,          0, 0x00070008);
    GOODADDR("1::6:7:8",        0x00010000,          0, 0x00000006, 0x00070008);
    GOODADDR("1::5:6:7:8",      0x00010000,          0, 0x00050006, 0x00070008);
    GOODADDR("1::4:5:6:7:8",    0x00010000, 0x00000004, 0x00050006, 0x00070008);
    GOODADDR("1::3:4:5:6:7:8",  0x00010000, 0x00030004, 0x00050006, 0x00070008);

    /* ... too long */
    BADADDR("1::2:3:4:5:6:7:8");
    BADADDR("1:2::3:4:5:6:7:8");
    BADADDR("1:2:3::4:5:6:7:8");
    BADADDR("1:2:3:4::5:6:7:8");
    BADADDR("1:2:3:4:5::6:7:8");
    BADADDR("1:2:3:4:5:6::7:8");
    BADADDR("1:2:3:4:5:6:7::8");

    /* compressed with embedded IPv4 */
    GOODADDR("::0.0.0.0",        0, 0,          0,          0);
    GOODADDR("::1.2.3.4",        0, 0,          0, 0x01020304);
    GOODADDR("::ffff:1.2.3.4",   0, 0, 0x0000ffff, 0x01020304);
    GOODADDR("::ffff:0:1.2.3.4", 0, 0, 0xffff0000, 0x01020304);

    GOODADDR("1::1.2.3.4",         0x00010000,          0,          0, 0x01020304);
    GOODADDR("1:2::1.2.3.4",       0x00010002,          0,          0, 0x01020304);
    GOODADDR("1:2:3::1.2.3.4",     0x00010002, 0x00030000,          0, 0x01020304);
    GOODADDR("1:2:3:4::1.2.3.4",   0x00010002, 0x00030004,          0, 0x01020304);
    GOODADDR("1:2:3:4:5::1.2.3.4", 0x00010002, 0x00030004, 0x00050000, 0x01020304);

    /* ... too long */
    BADADDR("1:2:3:4:5:6::1.2.3.4");
    BADADDR("1:2:3:4:5::6:1.2.3.4");
    BADADDR("1:2:3:4::5:6:1.2.3.4");
    BADADDR("1:2:3::4:5:6:1.2.3.4");
    BADADDR("1:2::3:4:5:6:1.2.3.4");
    BADADDR("1::2:3:4:5:6:1.2.3.4");

    /* zone ids (beware, shaky ground) */
    GOODADDR("ff01::1%0",      0xff010000, 0, 0, 1);
    GOODADDR("ff01::1%eth0",   0xff010000, 0, 0, 1);
    GOODADDR("ff01::1%net1.0", 0xff010000, 0, 0, 1);

    GOODADDR(" ff01::1%net1.1\t", 0xff010000, 0, 0, 1);

    return RTTestSummaryAndDestroy(hTest);
}