(file) Return to test_credcache.cpp CVS log (file) (dir) Up to [OMI] / omi / unittest / base

File: [OMI] / omi / unittest / base / Attic / test_credcache.cpp (download)
Revision: 1.1.1.1 (vendor branch), Wed May 30 21:47:39 2012 UTC (12 years, 1 month ago) by mike
Branch: TOG
CVS Tags: OMI_1_0_2_Branch, OMI_1_0_2, OMI_1_0_1_PRE, OMI_1_0_1, OMI_1_0_0
Changes since 1.1: +0 -0 lines
Initial Import

/*
**==============================================================================
**
** Open Management Infrastructure (OMI)
**
** Copyright (c) Microsoft Corporation
** 
** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
** use this file except in compliance with the License. You may obtain a copy 
** of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
** MERCHANTABLITY OR NON-INFRINGEMENT. 
**
** See the Apache 2 License for the specific language governing permissions 
** and limitations under the License.
**
**==============================================================================
*/

#include <vector>
#include <algorithm>
#include <ut/ut.h>
#include <base/credcache.h>

using namespace std;
using namespace ut;

//#define ENABLE_PRINT


static void setUp()
{
    CredCache_Clean();
}

static void cleanup()
{
}

static void TestCredUserAllowed()
{
    // First check that user is not allowed (not added yet)
    UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);

    CredCache_PutUser("user", "abc");

    // Verify that user is in cache now
    UT_ASSERT(CredCache_CheckUser("user", "abc") == 0);
}

static void TestCredInvalidPassword()
{
    // First check that user is not allowed (not added yet)
    UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);

    CredCache_PutUser("user", "abc");

    // Verify invalid passwords
    UT_ASSERT(CredCache_CheckUser("user", "") < 0);
    UT_ASSERT(CredCache_CheckUser("user", "Abc") < 0);
    UT_ASSERT(CredCache_CheckUser("user", "abc ") < 0);
    UT_ASSERT(CredCache_CheckUser("user", "-abc") < 0);
}

static void TestCredSeveralUsers()
{

    CredCache_PutUser("user1", "abc1");
    CredCache_PutUser("user2", "abc2");
    CredCache_PutUser("user3", "abc3");
    CredCache_PutUser("user4", "abc4");

    // Verify that users are in cache now
    UT_ASSERT(CredCache_CheckUser("user1", "abc1") == 0);
    UT_ASSERT(CredCache_CheckUser("user2", "abc2") == 0);
    UT_ASSERT(CredCache_CheckUser("user3", "abc3") == 0);
    UT_ASSERT(CredCache_CheckUser("user4", "abc4") == 0);
}

static void TestCredExpiration()
{

    CredCache_PutUser("user1", "abc1");

    CredCache_SetExpirationTimeout(1);

    sleep_ms(1);

    // Verify that users expired
    UT_ASSERT(CredCache_CheckUser("user1", "abc1") < 0);

    /* Restore default one */
    CredCache_SetExpirationTimeout(0);
}

static void TestCredOldestOverwritten()
{

    /* create oldest item */
    CredCache_PutUser("oldest", "oldest");

    UT_ASSERT(CredCache_CheckUser("oldest", "oldest") == 0);

    /* make it older than the rest */
    sleep_ms(1);

    for (int i = 0; i < 64; i++)
    {
        char user[64];

        sprintf(user, "user%d", i);
        // Add new user and verify it's added
        CredCache_PutUser(user, user);
        UT_ASSERT(CredCache_CheckUser(user, user) == 0);
    }

    // Verify that users expired
    UT_ASSERT(CredCache_CheckUser("oldest", "oldest") < 0);
}


static void RunTests()
{
    UT_TEST(TestCredUserAllowed);
    UT_TEST(TestCredInvalidPassword);
    UT_TEST(TestCredSeveralUsers);
    UT_TEST(TestCredExpiration);
    UT_TEST(TestCredOldestOverwritten);
}

UT_ENTRY_POINT(RunTests);

ViewCVS 0.9.2