コード例 #1
0
ファイル: buffer_test.c プロジェクト: cyphermaster/core
static void test_copyEqualBuffer(void **state)
{
    char element0[] = "element0";
    unsigned int element0size = strlen(element0);
    char element1[] = "element1";
    unsigned int element1size = strlen(element1);

    Buffer *buffer0 = NULL;
    Buffer *buffer1 = NULL;
    Buffer *buffer2 = NULL;

    // Empty buffers, all empty buffers are the same
    assert_int_equal(0, BufferNew(&buffer0));
    assert_int_equal(0, BufferNew(&buffer1));
    assert_true(BufferEqual(buffer0, buffer0));
    assert_true(BufferEqual(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_true(BufferEqual(buffer0, buffer2));

    // Add some flavour
    assert_int_equal(0, BufferDestroy(&buffer2));
    assert_int_equal(element0size, BufferSet(buffer0, element0, element0size));
    assert_int_equal(element1size, BufferSet(buffer1, element1, element1size));
    assert_true(BufferEqual(buffer0, buffer0));
    assert_false(BufferEqual(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_true(BufferEqual(buffer0, buffer2));

    // Destroy the buffers
    assert_int_equal(0, BufferDestroy(&buffer0));
    assert_int_equal(0, BufferDestroy(&buffer1));
    assert_int_equal(0, BufferDestroy(&buffer2));
}
コード例 #2
0
ファイル: buffer_test.c プロジェクト: lpefferkorn/core
static void test_zeroBuffer(void)
{
    char *element0 = xstrdup("element0");
    unsigned int element0size = strlen(element0);
    const char *element0pointer = NULL;

    Buffer *buffer = BufferNew();
    assert_int_equal(element0size, BufferSet(buffer, element0, element0size));
    element0pointer = buffer->buffer;
    assert_int_equal(element0size, buffer->used);
    assert_int_equal(element0size, BufferSize(buffer));
    BufferZero(buffer);
    assert_int_equal(DEFAULT_BUFFER_SIZE, buffer->capacity);
    assert_int_equal(0, buffer->used);
    assert_int_equal(0, BufferSize(buffer));
	const char *data = BufferData(buffer);
	assert_string_equal(data, "");
    assert_true(element0pointer == buffer->buffer);
    BufferZero(NULL);
    assert_int_equal(0, BufferDestroy(&buffer));
    /*
     * Release the resources
     */
    BufferDestroy(&buffer);
    free (element0);
}
コード例 #3
0
ファイル: buffer.c プロジェクト: GregorioDiStefano/core
// returns NULL on success, otherwise an error string
const char* BufferSearchAndReplace(Buffer *buffer, const char *pattern, const char *substitute, const char *options)
{
    assert(buffer);
    assert(pattern);
    assert(substitute);
    assert(options);

    int err;

    pcre_wrap_job *job = pcre_wrap_compile(pattern, substitute, options, &err);
    if (job == NULL)
    {
        return pcre_wrap_strerror(err);
    }

    size_t length = BufferSize(buffer);
    char *result;
    if (0 > (err = pcre_wrap_execute(job, (char*)BufferData(buffer), length, &result, &length)))
    {
        return pcre_wrap_strerror(err);
    }

    BufferSet(buffer, result, length);
    free(result);
    pcre_wrap_free_job(job);

    return NULL;
}
コード例 #4
0
static void test_generic_interface(void)
{
    /*
     * This test might seem short, but it is intentional.
     * All the parsing tests should be implemented directly
     * on the corresponding parser test. Keep this test as
     * lean as possible.
     */
    IPAddress *address = NULL;
    Buffer *buffer = NULL;

    buffer = BufferNew();
    assert_true(buffer != NULL);
    BufferSet(buffer, "127.0.0.1", strlen("127.0.0.1"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address));
    assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(0, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "127.0.0.1:8080", strlen("127.0.0.1:8080"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address));
    assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(8080, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "0:1:2:3:4:5:6:7", strlen("0:1:2:3:4:5:6:7"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address));
    assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(0, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "[0:1:2:3:4:5:6:7]:9090", strlen("[0:1:2:3:4:5:6:7]:9090"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address));
    assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(9090, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferDestroy(buffer);
}
コード例 #5
0
ファイル: buffer.c プロジェクト: GregorioDiStefano/core
void BufferRewrite(Buffer *buffer, BufferFilterFn filter, const bool invert)
{
    assert(buffer);

    Buffer *rewrite = BufferFilter(buffer, filter, invert);
    BufferSet(buffer, BufferData(rewrite), BufferSize(rewrite));
    BufferDestroy(rewrite);
}
コード例 #6
0
ファイル: buffer_test.c プロジェクト: lpefferkorn/core
static void test_copyCompareBuffer(void)
{
    char *element0 = xstrdup("element0");
    unsigned int element0size = strlen(element0);
    char *element1 = xstrdup("element1");
    unsigned int element1size = strlen(element1);

    Buffer *buffer0 = NULL;
    Buffer *buffer1 = NULL;
    Buffer *buffer2 = NULL;

    assert_int_equal(0, BufferCompare(buffer0, buffer1));
    buffer0 = BufferNew();
    assert_int_equal(-1, BufferCompare(NULL, buffer0));
    assert_int_equal(1, BufferCompare(buffer0, NULL));
    buffer1 = BufferNew();
    assert_int_equal(0, BufferCompare(buffer0, buffer0));
    assert_int_equal(0, BufferCompare(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_int_equal(0, BufferCompare(buffer0, buffer2));

    // Add some flavour
    assert_int_equal(0, BufferDestroy(&buffer2));
    assert_int_equal(element0size, BufferSet(buffer0, element0, element0size));
    assert_int_equal(element1size, BufferSet(buffer1, element1, element1size));
    assert_int_equal(0, BufferCompare(buffer0, buffer0));
    assert_int_equal(-1, BufferCompare(buffer0, buffer1));
    assert_int_equal(1, BufferCompare(buffer1, buffer0));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_int_equal(0, BufferCompare(buffer0, buffer2));

    // Destroy the buffers
    assert_int_equal(0, BufferDestroy(&buffer0));
    assert_int_equal(0, BufferDestroy(&buffer1));
    assert_int_equal(0, BufferDestroy(&buffer2));

    free (element0);
    free (element1);
}
コード例 #7
0
ファイル: buffer_test.c プロジェクト: cyphermaster/core
static void test_zeroBuffer(void **state)
{
    char element0[] = "element0";
    unsigned int element0size = strlen(element0);
    const char *element0pointer = NULL;

    Buffer *buffer = NULL;
    assert_int_equal(0, BufferNew(&buffer));
    assert_int_equal(element0size, BufferSet(buffer, element0, element0size));
    element0pointer = buffer->buffer;
    assert_int_equal(element0size, buffer->used);
    assert_int_equal(element0size, BufferSize(buffer));
    BufferZero(buffer);
    assert_int_equal(DEFAULT_BUFFER_SIZE, buffer->capacity);
    assert_int_equal(0, buffer->used);
    assert_int_equal(0, BufferSize(buffer));
    assert_true(element0pointer == buffer->buffer);
    BufferZero(NULL);
    assert_int_equal(0, BufferDestroy(&buffer));
}
コード例 #8
0
ファイル: buffer_test.c プロジェクト: lpefferkorn/core
static void test_setBuffer(void)
{
    char *element0 = xstrdup("element0");
    unsigned int element0size = strlen(element0);
    char *element1 = (char *)xmalloc(2 * DEFAULT_BUFFER_SIZE + 2);
    unsigned int element1size = 2 * DEFAULT_BUFFER_SIZE + 1;
    char *element2 = (char *)xmalloc(DEFAULT_MEMORY_CAP * 2);
    unsigned int element2size = 2 * DEFAULT_MEMORY_CAP;

    Buffer *buffer = BufferNew();
    assert_true(buffer != NULL);
    // Smaller than the allocated buffer
    assert_int_equal(element0size, BufferSet(buffer, element0, element0size));
    assert_int_equal(element0size, buffer->used);
    assert_int_equal(element0size, BufferSize(buffer));
    assert_string_equal(element0, buffer->buffer);
    assert_string_equal(element0, BufferData(buffer));
    assert_int_equal(DEFAULT_BUFFER_SIZE, buffer->capacity);
    // Larger than the allocated buffer
    int i = 0;
    for (i = 0; i < element1size; ++i)
        element1[i] = 'a';
    element1[element1size] = '\0';
    assert_int_equal(element1size, BufferSet(buffer, element1, element1size));
    assert_int_equal(element1size, buffer->used);
    assert_string_equal(element1, buffer->buffer);
    assert_string_equal(element1, BufferData(buffer));
    assert_int_equal(DEFAULT_BUFFER_SIZE * 3, buffer->capacity);
    /*
     * A buffer that is so large that it will get rejected by our memory cap
     */
    BufferZero(buffer);
    for (i = 0; i < element2size; ++i)
    {
        element2[i] = 'b';
    }
    element2[element2size - 1] = '\0';
    assert_int_equal(-1, BufferSet(buffer, element2, element2size));
    /*
     * Boundary checks, BUFFER_SIZE-1, BUFFER_SIZE and BUFFER_SIZE+1
     */
    Buffer *bm1 = BufferNew();
    Buffer *be = BufferNew();
    Buffer *bp1 = BufferNew();
    char buffer_m1[DEFAULT_BUFFER_SIZE - 1];
    char buffer_0[DEFAULT_BUFFER_SIZE];
    char buffer_p1[DEFAULT_BUFFER_SIZE + 1];
    unsigned int bm1_size = DEFAULT_BUFFER_SIZE - 1;
    unsigned int be_size = DEFAULT_BUFFER_SIZE;
    unsigned int bp1_size = DEFAULT_BUFFER_SIZE + 1;
    for (i = 0; i < DEFAULT_BUFFER_SIZE - 1; ++i)
    {
        buffer_m1[i] = 'c';
        buffer_0[i] = 'd';
        buffer_p1[i] = 'e';
    }
    /*
     * One shorter, that means the buffer remains the same size as before.
     */
    buffer_m1[DEFAULT_BUFFER_SIZE - 2] = '\0';
    assert_int_equal(bm1_size, BufferSet(bm1, buffer_m1, bm1_size));
    assert_int_equal(bm1->capacity, DEFAULT_BUFFER_SIZE);
    /*
     * Same size, it should allocate one more block
     */
    buffer_0[DEFAULT_BUFFER_SIZE - 1] = '\0';
    assert_int_equal(be_size, BufferSet(be, buffer_0, be_size));
    assert_int_equal(be->capacity, 2 * DEFAULT_BUFFER_SIZE);
    /*
     * 1 more, it should allocate one more block
     */
    buffer_p1[DEFAULT_BUFFER_SIZE] = '\0';
    assert_int_equal(bp1_size, BufferSet(bp1, buffer_p1, bp1_size));
    assert_int_equal(bp1->capacity, 2 * DEFAULT_BUFFER_SIZE);
    // Negative cases
    assert_int_equal(-1, BufferSet(NULL, element0, element0size));
    assert_int_equal(-1, BufferSet(NULL, NULL, element0size));
    assert_int_equal(-1, BufferSet(buffer, NULL, element0size));
    assert_int_equal(0, BufferSet(buffer, element0, 0));
    /*
     * Destroy the buffer and good night.
     */
    assert_int_equal(0, BufferDestroy(&buffer));
    assert_true(buffer == NULL);
    BufferDestroy(&bm1);
    BufferDestroy(&be);
    BufferDestroy(&bp1);
    free (element0);
    free (element1);
    free (element2);
}
コード例 #9
0
ファイル: verify_vars.c プロジェクト: baptr/core
void VerifyVarPromise(EvalContext *ctx, const Promise *pp, bool allow_duplicates)
{
    ConvergeVariableOptions opts = CollectConvergeVariableOptions(ctx, pp, allow_duplicates);
    if (!opts.should_converge)
    {
        return;
    }

    char *scope = NULL;
    if (strcmp("meta", pp->parent_promise_type->name) == 0)
    {
        scope = StringConcatenate(2, PromiseGetBundle(pp)->name, "_meta");
    }
    else
    {
        scope = xstrdup(PromiseGetBundle(pp)->name);
    }

    //More consideration needs to be given to using these
    //a.transaction = GetTransactionConstraints(pp);
    Attributes a = { {0} };
    a.classes = GetClassDefinitionConstraints(ctx, pp);

    Rval existing_var_rval;
    DataType existing_var_type = DATA_TYPE_NONE;
    EvalContextVariableGet(ctx, (VarRef) { NULL, scope, pp->promiser }, &existing_var_rval, &existing_var_type);
    Buffer *qualified_scope = BufferNew();
    int result = 0;
    if (strcmp(PromiseGetNamespace(pp), "default") == 0)
    {
        result = BufferSet(qualified_scope, scope, strlen(scope));
        if (result < 0)
        {
            /*
             * Even though there will be no problems with memory allocation, there
             * might be other problems.
             */
            UnexpectedError("Problems writing to buffer");
            free(scope);
            BufferDestroy(&qualified_scope);
            return;
        }
    }
    else
    {
        if (strchr(scope, ':') == NULL)
        {
            result = BufferPrintf(qualified_scope, "%s:%s", PromiseGetNamespace(pp), scope);
            if (result < 0)
            {
                /*
                 * Even though there will be no problems with memory allocation, there
                 * might be other problems.
                 */
                UnexpectedError("Problems writing to buffer");
                free(scope);
                BufferDestroy(&qualified_scope);
                return;
            }
        }
        else
        {
            result = BufferSet(qualified_scope, scope, strlen(scope));
            if (result < 0)
            {
                /*
                 * Even though there will be no problems with memory allocation, there
                 * might be other problems.
                 */
                UnexpectedError("Problems writing to buffer");
                free(scope);
                BufferDestroy(&qualified_scope);
                return;
            }
        }
    }

    PromiseResult promise_result;

    Rval rval = opts.cp_save->rval;

    if (rval.item != NULL)
    {
        FnCall *fp = (FnCall *) rval.item;

        if (opts.cp_save->rval.type == RVAL_TYPE_FNCALL)
        {
            if (existing_var_type != DATA_TYPE_NONE)
            {
                // Already did this
                free(scope);
                BufferDestroy(&qualified_scope);
                return;
            }

            FnCallResult res = FnCallEvaluate(ctx, fp, pp);

            if (res.status == FNCALL_FAILURE)
            {
                /* We do not assign variables to failed fn calls */
                RvalDestroy(res.rval);
                free(scope);
                BufferDestroy(&qualified_scope);
                return;
            }
            else
            {
                rval = res.rval;
            }
        }
        else
        {
            Buffer *conv = BufferNew();

            if (strcmp(opts.cp_save->lval, "int") == 0)
            {
                result = BufferPrintf(conv, "%ld", IntFromString(opts.cp_save->rval.item));
                if (result < 0)
                {
                    /*
                     * Even though there will be no problems with memory allocation, there
                     * might be other problems.
                     */
                    UnexpectedError("Problems writing to buffer");
                    free(scope);
                    BufferDestroy(&qualified_scope);
                    BufferDestroy(&conv);
                    return;
                }
                rval = RvalCopy((Rval) {(char *)BufferData(conv), opts.cp_save->rval.type});
            }
            else if (strcmp(opts.cp_save->lval, "real") == 0)
            {
                double real_value = 0.0;
                if (DoubleFromString(opts.cp_save->rval.item, &real_value))
                {
                    result = BufferPrintf(conv, "%lf", real_value);
                }
                else
                {
                    result = BufferPrintf(conv, "(double conversion error)");
                }

                if (result < 0)
                {
                    /*
                     * Even though there will be no problems with memory allocation, there
                     * might be other problems.
                     */
                    UnexpectedError("Problems writing to buffer");
                    free(scope);
                    BufferDestroy(&conv);
                    BufferDestroy(&qualified_scope);
                    return;
                }
                rval = RvalCopy((Rval) {(char *)BufferData(conv), opts.cp_save->rval.type});
            }
            else
            {
                rval = RvalCopy(opts.cp_save->rval);
            }

            if (rval.type == RVAL_TYPE_LIST)
            {
                Rlist *rval_list = RvalRlistValue(rval);
                RlistFlatten(ctx, &rval_list);
                rval.item = rval_list;
            }

            BufferDestroy(&conv);
        }

        if (Epimenides(ctx, PromiseGetBundle(pp)->name, pp->promiser, rval, 0))
        {
            Log(LOG_LEVEL_ERR, "Variable \"%s\" contains itself indirectly - an unkeepable promise", pp->promiser);
            exit(1);
        }
        else
        {
            /* See if the variable needs recursively expanding again */

            Rval returnval = EvaluateFinalRval(ctx, BufferData(qualified_scope), rval, true, pp);

            RvalDestroy(rval);

            // freed before function exit
            rval = returnval;
        }

        if (existing_var_type != DATA_TYPE_NONE)
        {
            if (opts.ok_redefine)    /* only on second iteration, else we ignore broken promises */
            {
                ScopeDeleteVariable(BufferData(qualified_scope), pp->promiser);
            }
            else if ((THIS_AGENT_TYPE == AGENT_TYPE_COMMON) && (CompareRval(existing_var_rval, rval) == false))
            {
                switch (rval.type)
                {
                case RVAL_TYPE_SCALAR:
                    Log(LOG_LEVEL_VERBOSE, "Redefinition of a constant scalar \"%s\" (was %s now %s)",
                          pp->promiser, RvalScalarValue(existing_var_rval), RvalScalarValue(rval));
                    PromiseRef(LOG_LEVEL_VERBOSE, pp);
                    break;

                case RVAL_TYPE_LIST:
                    {
                        Log(LOG_LEVEL_VERBOSE, "Redefinition of a constant list \"%s\".", pp->promiser);
                        Writer *w = StringWriter();
                        RlistWrite(w, existing_var_rval.item);
                        char *oldstr = StringWriterClose(w);
                        Log(LOG_LEVEL_VERBOSE, "Old value: %s", oldstr);
                        free(oldstr);

                        w = StringWriter();
                        RlistWrite(w, rval.item);
                        char *newstr = StringWriterClose(w);
                        Log(LOG_LEVEL_VERBOSE, " New value: %s", newstr);
                        free(newstr);
                        PromiseRef(LOG_LEVEL_VERBOSE, pp);
                    }
                    break;

                default:
                    break;
                }
            }
        }

        if (IsCf3VarString(pp->promiser))
        {
            // Unexpanded variables, we don't do anything with
            RvalDestroy(rval);
            free(scope);
            BufferDestroy(&qualified_scope);
            return;
        }

        if (!FullTextMatch("[a-zA-Z0-9_\200-\377.]+(\\[.+\\])*", pp->promiser))
        {
            Log(LOG_LEVEL_ERR, "Variable identifier contains illegal characters");
            PromiseRef(LOG_LEVEL_ERR, pp);
            RvalDestroy(rval);
            free(scope);
            BufferDestroy(&qualified_scope);
            return;
        }

        if (opts.drop_undefined && rval.type == RVAL_TYPE_LIST)
        {
            for (Rlist *rp = rval.item; rp != NULL; rp = rp->next)
            {
                if (IsNakedVar(rp->item, '@'))
                {
                    free(rp->item);
                    rp->item = xstrdup(CF_NULL_VALUE);
                }
            }
        }

        if (!EvalContextVariablePut(ctx, (VarRef) { NULL, BufferData(qualified_scope), pp->promiser }, rval, DataTypeFromString(opts.cp_save->lval)))
        {
            Log(LOG_LEVEL_VERBOSE, "Unable to converge %s.%s value (possibly empty or infinite regression)", BufferData(qualified_scope), pp->promiser);
            PromiseRef(LOG_LEVEL_VERBOSE, pp);
            promise_result = PROMISE_RESULT_FAIL;
        }
        else
        {
            promise_result = PROMISE_RESULT_CHANGE;
        }
    }
    else
    {
        Log(LOG_LEVEL_ERR, "Variable %s has no promised value", pp->promiser);
        Log(LOG_LEVEL_ERR, "Rule from %s at/before line %zu", PromiseGetBundle(pp)->source_path, opts.cp_save->offset.line);
        promise_result = PROMISE_RESULT_FAIL;
    }

    /*
     * FIXME: Variable promise are exempt from normal evaluation logic still, so
     * they are not pushed to evaluation stack before being evaluated. Due to
     * this reason, we cannot call cfPS here to set classes, as it will error
     * out with ProgrammingError.
     *
     * In order to support 'classes' body for variables as well, we call
     * ClassAuditLog explicitly.
     */
    ClassAuditLog(ctx, pp, a, promise_result);

    free(scope);
    BufferDestroy(&qualified_scope);
    RvalDestroy(rval);
}
コード例 #10
0
static void test_isipaddress(void)
{
    /*
     * This test is just a summary of the other tests.
     * We just check that this interface works accordingly, most of the
     * functionality has already been tested.
     * 1.2.3.4         -> ok
     * 1.2..3          -> not
     * 1.a.2.3         -> not
     * 256.255.255.255 -> not
     * 255.255.255.255 -> ok
     * 1:0:0:0:0:0:0:1 -> ok
     * 1:1:1:1:0:1:1:1 -> ok
     * a:b:c:d:e:f:0:1 -> ok
     * a:b:c:d:e:f:g:h -> not
     * ffff:ffff:fffff:0:0:0:0:1 -> not
     */
    IPAddress *address = NULL;
    Buffer *bufferAddress = NULL;

    bufferAddress = BufferNew();
    assert_true (bufferAddress != NULL);

    BufferSet(bufferAddress, "1.2.3.4", strlen("1.2.3.4"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1.2..3", strlen("1.2..3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "1.a.2.3", strlen("1.a.2.3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "256.255.255.255", strlen("256.255.255.255"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "255.255.255.255", strlen("255.255.255.255"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:1:1:1:0:1:1:1", strlen("1:1:1:1:0:1:1:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:0:1", strlen("a:b:c:d:e:f:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:g:h", strlen("a:b:c:d:e:f:g:h"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "ffff:ffff:fffff:0:0:0:0:1", strlen("ffff:ffff:fffff:0:0:0:0:1"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);
}
コード例 #11
0
static void test_ipv6_address_comparison(void)
{
    /*
     * We test different IPV6 combinations:
     * 1:1:1:1:1:1:1:1 vs 1:1:1:1:1:1:1:1 -> equal
     * 1:2:3:4:5:6:7:8 vs 1:1:1:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:1:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:1 -> not equal
     * 2:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> equal
     * Exotic variants
     * 1:0:0:0:0:0:0:1 vs 1::1            -> equal
     * 1:1:0:0:0:0:0:1 vs 1::1            -> not equal
     * 1:1:0:0:0:0:0:1 vs 1:1::1          -> equal
     * 1:0:0:0:0:0:1:1 vs 1::1:1          -> equal
     * Error conditions
     * 1::1:1 vs NULL                     -> error
     * 1::1:1 vs 1.2.3.4                  -> error
     */

    IPAddress *a = NULL;
    IPAddress *b = NULL;
    Buffer *bufferA = NULL;
    Buffer *bufferB = NULL;

    bufferA = BufferNew();
    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1"));
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    bufferB = BufferNew();
    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1"));
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:1:1:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:1:1:1:1:1:1", strlen("1:2:1:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:1:1:1:1:1", strlen("1:2:3:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:1:1:1:1", strlen("1:2:3:4:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:1:1:1", strlen("1:2:3:4:5:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:6:1:1", strlen("1:2:3:4:5:6:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:6:7:1", strlen("1:2:3:4:5:6:7:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "2:2:3:4:5:6:7:8", strlen("2:2:3:4:5:6:7:8"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1::1", strlen("1::1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:1:0:0:0:0:0:1", strlen("1:1:0:0:0:0:0:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:1::1", strlen("1:1::1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1::1:1", strlen("1::1:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:0:0:0:0:0:1:1", strlen("1:0:0:0:0:0:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_int_equal(IPAddressIsEqual(a, NULL), -1);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_int_equal(IPAddressIsEqual(a, b), -1);
}
コード例 #12
0
static void test_ipv4_address_comparison(void)
{
    /*
     * We test different IPV4 combinations:
     * 1.1.1.1 vs 1.1.1.1         -> equal
     * 1.2.3.4 vs 1.1.1.1         -> not equal
     * 1.2.3.4 vs 1.2.1.1         -> not equal
     * 1.2.3.4 vs 1.2.3.1         -> not equal
     * 2.2.3.4 vs 1.2.3.4         -> not equal
     * 1.2.3.4 vs 1.2.3.4         -> equal
     * 1.2.3.4 vs NULL            -> error
     * 1.2.3.4 vs 1:2:3:4:5:6:7:8 -> error
     */
    IPAddress *a = NULL;
    IPAddress *b = NULL;
    Buffer *bufferA = NULL;
    Buffer *bufferB = NULL;
            
    bufferA = BufferNew();
    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1.1.1.1", strlen("1.1.1.1"));
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    bufferB = BufferNew();
    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1.1.1.1", strlen("1.1.1.1"));
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);
    
    assert_true(IPAddressIsEqual(a, b));
    
    BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);
    
    assert_false(IPAddressIsEqual(a, b));       

    BufferSet(bufferB, "1.2.1.1", strlen("1.2.1.1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferB, "1.2.3.1", strlen("1.2.3.1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferA, "2.2.3.4", strlen("2.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_int_equal(IPAddressIsEqual(a, NULL), -1);
    assert_int_equal(IPAddressIsEqual(NULL, a), -1);

    BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:2:3:4:5:6:7:8"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_int_equal(IPAddressIsEqual(a, b), -1);
    assert_int_equal(IPAddressIsEqual(b, a), -1);
}