test_log.c revision 4c6011bfcf29cd75d6af86f192d709221f4d5d3a
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford/**
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * The contents of this file are subject to the terms of the Common Development and
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Distribution License (the License). You may not use this file except in compliance with the
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * License.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford *
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * specific language governing permission and limitations under the License.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford *
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * When distributing Covered Software, include this CDDL Header Notice in each file and include
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Header, with the fields enclosed by brackets [] replaced by your own identifying
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * information: "Portions copyright [year] [name of copyright owner]".
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford *
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * Copyright 2015 ForgeRock AS.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford#include <stdio.h>
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford#include <string.h>
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford#include <stdlib.h>
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs#include <setjmp.h>
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
49a10b9b220f316a19b54ef6885f87936f30791cMareks Malnacs#include "platform.h"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford#include "am.h"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford#include "log.h"
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs#include "cmocka.h"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford/**
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * This is the simplest of tests to check we can log things without crashing.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford *
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * In fact, because of the way logging works (differently) in test mode than it does in "agent mode"
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * all we're really doing here is to test that logging in test mode isn't broken. This may or may not
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford * bear any relation to whether logging works for the rest of the time.
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordvoid test_logging(void** state) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford static const char* text1 = "Now is the winter of our discontent,";
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford static const char* text2 = "Made glorious summer by this son of York";
4c6011bfcf29cd75d6af86f192d709221f4d5d3aMareks Malnacs
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_INFO(0, "instance id is zero and no args");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_INFO(0, "instance id is zero and incorrect args", text1);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_INFO(0, "instance id is zero and more incorrect args", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford /* we're testing this will not crash */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_INFO(0, NULL, text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford /* this will not appear, since the instance is greater than zero, but it should not crash either */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_ERROR(10, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_INFO(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_WARNING(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_ERROR(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_DEBUG(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_AUDIT(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_ALWAYS(0, "%s %s", text1, text2);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_ALWAYS(0, "Now %s the %s of our %s, %s summ%s of York",
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "is",
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "winter",
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "discontent",
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "Made glorious",
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford "er by this son");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford /* attempt to overflow the buffer */
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford AM_LOG_ALWAYS(0, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford}
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamfordvoid test_am_strncat(void** state) {
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford char buf[10] = "a";
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford am_strncat(buf, "bcdefghijklmnopq", sizeof(buf));
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(strlen(buf), sizeof(buf) - 1);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(buf, "abcdefghi");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford am_strncat(buf, "1234567890", sizeof(buf));
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_int_equal(strlen(buf), sizeof(buf) - 1);
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford assert_string_equal(buf, "abcdefghi");
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford
48e27400d23e2586530cf943524e50c74d79a915Tony Bamford}