/* Editor Settings: expandtabs and use 4 spaces for indentation
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 */

/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

#include "includes.h"


typedef struct _TEST_NET_USER_ADD
{
    PSTR    pszUserName;
    PSTR    pszComment;
    PSTR    pszDescription;
    PSTR    pszHomeDir;
    PSTR    pszScriptPath;
    DWORD   dwPrivilege;
    DWORD   dwFlags;
    DWORD   dwExpectedFlags;
    PSTR    pszPassword;
    DWORD   dwLevel;
    BOOLEAN bPreCleanup;
    BOOLEAN bPostCleanup;
    DWORD   dwError;
    DWORD   dwParmError;

} TEST_NET_USER_ADD, *PTEST_NET_USER_ADD;


static
TEST_NET_USER_ADD LocalUserAddValidationTest[] = {
    {
        .pszUserName     = "testuser1",
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .dwExpectedFlags = UF_NORMAL_ACCOUNT |
                           UF_ACCOUNTDISABLE,
        .pszPassword     = NULL,
        .dwLevel         = 1,
        .bPreCleanup     = TRUE,
        .bPostCleanup    = TRUE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .pszUserName     = "testuser1",
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .dwExpectedFlags = UF_NORMAL_ACCOUNT,
        .pszPassword     = "toPs3Cret%",
        .dwLevel         = 1,
        .bPreCleanup     = FALSE,
        .bPostCleanup    = TRUE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .pszUserName     = "testuser1",
        .pszComment      = "### Comment testuser1 ###",
        .pszDescription  = "### Description testuser1 ###",
        .pszHomeDir      = "c:\\tmp",
        .pszScriptPath   = "n:\\netlogon\\logon.cmd",
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .dwExpectedFlags = UF_NORMAL_ACCOUNT |
                           UF_ACCOUNTDISABLE,
        .pszPassword     = NULL,
        .dwLevel         = 1,
        .bPreCleanup     = FALSE,
        .bPostCleanup    = TRUE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .pszUserName     = "testuser1",
        .pszComment      = "### Comment testuser1 ###",
        .pszDescription  = "### Description testuser1 ###",
        .pszHomeDir      = "c:\\tmp",
        .pszScriptPath   = "n:\\netlogon\\logon.cmd",
        .dwPrivilege     = 0,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .dwExpectedFlags = UF_NORMAL_ACCOUNT |
                           UF_ACCOUNTDISABLE,
        .pszPassword     = NULL,
        .dwLevel         = 1,
        .bPreCleanup     = FALSE,
        .bPostCleanup    = TRUE,
        .dwError         = ERROR_INVALID_PARAMETER,
        .dwParmError     = USER_PRIV_PARMNUM
    }
};


typedef struct _TEST_NET_USER_DEL
{
    PSTR    pszUserName;
    BOOLEAN bPrepare;
    BOOLEAN bCleanup;
    DWORD   dwError;

} TEST_NET_USER_DEL, *PTEST_NET_USER_DEL;


static
TEST_NET_USER_DEL LocalUserDelValidationTest[] = {
    {
        .pszUserName   = "testuser1",
        .bPrepare      = TRUE,
        .bCleanup      = TRUE,
        .dwError       = ERROR_SUCCESS
    },
    {
        .pszUserName   = "testuser2",
        .bPrepare      = TRUE,
        .bCleanup      = TRUE,
        .dwError       = ERROR_SUCCESS
    },
    {
        .pszUserName   = "testuser2",
        .bPrepare      = FALSE,
        .bCleanup      = TRUE,
        .dwError       = NERR_UserNotFound
    },
    {
        .pszUserName   = "testuser1",
        .bPrepare      = TRUE,
        .bCleanup      = TRUE,
        .dwError       = ERROR_SUCCESS
    },
    {
        .pszUserName   = "testuser1",
        .bPrepare      = TRUE,
        .bCleanup      = TRUE,
        .dwError       = ERROR_SUCCESS
    }
};


typedef struct _TEST_NET_USER_SET_INFO
{
    DWORD   dwLevel;
    PSTR    pszUserName;
    PSTR    pszFullName;
    PSTR    pszComment;
    PSTR    pszDescription;
    PSTR    pszHomeDir;
    PSTR    pszScriptPath;
    DWORD   dwPrivilege;
    DWORD   dwFlags;
    DWORD   dwExpectedFlags;
    PSTR    pszPassword;
    BOOLEAN bPrepare;
    BOOLEAN bCleanup;
    DWORD   dwError;
    DWORD   dwParmError;

} TEST_NET_USER_SET_INFO, *PTEST_NET_USER_SET_INFO;


static
TEST_NET_USER_SET_INFO LocalUserSetInfoValidationTest[] = {
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .pszPassword     = NULL,
        .bPrepare        = TRUE,
        .bCleanup        = FALSE,
        /*
         * dwFlags has to include UF_ACCOUNTDISABLE because there's
         * no password set on the account yet
         */
        .dwError         = ERROR_PASSWORD_RESTRICTION,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .pszPassword     = "newPas88-+word",
        .bPrepare        = TRUE,
        .bCleanup        = FALSE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 2,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = "###Testing comment###",
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .pszPassword     = NULL,
        .bPrepare        = FALSE,
        .bCleanup        = FALSE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 2,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = "###Testing description###",
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = 0,
        .pszPassword     = NULL,
        .bPrepare        = FALSE,
        .bCleanup        = FALSE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = "###Testing homedir###",
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT,
        .pszPassword     = NULL,
        .bPrepare        = FALSE,
        .bCleanup        = FALSE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = "###Testing script path###",
        .dwPrivilege     = USER_PRIV_USER,
        .dwFlags         = UF_NORMAL_ACCOUNT | UF_ACCOUNTDISABLE,
        .pszPassword     = NULL,
        .bPrepare        = FALSE,
        .bCleanup        = FALSE,
        .dwError         = ERROR_SUCCESS,
        .dwParmError     = 0
    },
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_GUEST,
        .dwFlags         = 0,
        .pszPassword     = NULL,
        .bPrepare        = FALSE,
        .bCleanup        = FALSE,
        /*
         * dwPrivilege has to be the same as returned from NetUserGetInfo
         * and would have to be USER_PRIV_USER in this case
         */
        .dwError         = ERROR_INVALID_PARAMETER,
        .dwParmError     = USER_PRIV_PARMNUM
    },
    {
        .dwLevel         = 1,
        .pszUserName     = "testuser1",
        .pszFullName     = NULL,
        .pszComment      = NULL,
        .pszDescription  = NULL,
        .pszHomeDir      = NULL,
        .pszScriptPath   = NULL,
        .dwPrivilege     = USER_PRIV_GUEST,
        .dwFlags         = 0,
        .pszPassword     = "newPas88-+word",
        .bPrepare        = FALSE,
        .bCleanup        = TRUE,
        /*
         * dwPrivilege has to be the same as returned from NetUserGetInfo
         * and would have to be USER_PRIV_USER in this case
         */
        .dwError         = ERROR_INVALID_PARAMETER,
        .dwParmError     = USER_PRIV_PARMNUM
    }
};


static
DWORD
TestGetNetUserAddTestSet(
    PCSTR                pszTestSetName,
    PTEST_NET_USER_ADD  *ppTestSet,
    PDWORD               pdwNumTests
    )
{
    DWORD error = ERROR_SUCCESS;
    PTEST_NET_USER_ADD pTestSet = NULL;
    DWORD dwNumTests = 0;

    if (strcasecmp(pszTestSetName, "validation") == 0)
    {
        pTestSet = LocalUserAddValidationTest;
        dwNumTests = (sizeof(LocalUserAddValidationTest)/
                      sizeof(LocalUserAddValidationTest[0]));
    }
    else
    {
        error = ERROR_INVALID_PARAMETER;
    }
    BAIL_ON_WIN_ERROR(error);

    *ppTestSet   = pTestSet;
    *pdwNumTests = dwNumTests;

error:
    return error;
}


static
DWORD
TestGetNetUserDelTestSet(
    PCSTR                pszTestSetName,
    PTEST_NET_USER_DEL  *ppTestSet,
    PDWORD               pdwNumTests
    )
{
    DWORD error = ERROR_SUCCESS;
    PTEST_NET_USER_DEL pTestSet = NULL;
    DWORD dwNumTests = 0;

    if (strcasecmp(pszTestSetName, "validation") == 0)
    {
        pTestSet = LocalUserDelValidationTest;
        dwNumTests = (sizeof(LocalUserDelValidationTest)/
                      sizeof(LocalUserDelValidationTest[0]));
    }
    else
    {
        error = ERROR_INVALID_PARAMETER;
    }
    BAIL_ON_WIN_ERROR(error);

    *ppTestSet   = pTestSet;
    *pdwNumTests = dwNumTests;

error:
    return error;
}


static
DWORD
TestGetNetUserSetInfoTestSet(
    PCSTR                    pszTestSetName,
    PTEST_NET_USER_SET_INFO *ppTestSet,
    PDWORD                   pdwNumTests
    )
{
    DWORD error = ERROR_SUCCESS;
    PTEST_NET_USER_SET_INFO pTestSet = NULL;
    DWORD dwNumTests = 0;

    if (strcasecmp(pszTestSetName, "validation") == 0)
    {
        pTestSet = LocalUserSetInfoValidationTest;
        dwNumTests = (sizeof(LocalUserSetInfoValidationTest)/
                      sizeof(LocalUserSetInfoValidationTest[0]));
    }
    else
    {
        error = ERROR_INVALID_PARAMETER;
    }
    BAIL_ON_WIN_ERROR(error);

    *ppTestSet   = pTestSet;
    *pdwNumTests = dwNumTests;

error:
    return error;
}


static
DWORD
CleanupAccount(
    PCWSTR  pwszHostname,
    PWSTR   pwszUsername
    )
{
    DWORD error = ERROR_SUCCESS;

    error = NetUserDel(pwszHostname,
                     pwszUsername);
    if (error == NERR_UserNotFound)
    {
        error = ERROR_SUCCESS;
    }

    return error;
}


static
DWORD
EnsureUserAccount(
    PCWSTR     pwszHostname,
    PWSTR      pwszUsername,
    PBOOLEAN   pbCreated
    )
{
    DWORD error = ERROR_SUCCESS;
    USER_INFO_1 Info = {0};
    DWORD dwParmError = 0;

    Info.usri1_name  = pwszUsername;
    Info.usri1_priv  = USER_PRIV_USER;
    Info.usri1_flags = UF_NORMAL_ACCOUNT;

    error = NetUserAdd(pwszHostname,
                         1,
                         (PVOID)&Info,
                         &dwParmError);
    if (error == ERROR_SUCCESS)
    {
        *pbCreated = TRUE;
    }
    else if (error == NERR_UserExists)
    {
        *pbCreated = FALSE;
        error = ERROR_SUCCESS;
    }

    return error;
}


static
DWORD
CleanupLocalGroup(
    PCWSTR  pwszHostname,
    PWSTR   pwszAliasname
    )
{
    DWORD error = ERROR_SUCCESS;

    error = NetLocalGroupDel(pwszHostname,
                               pwszAliasname);
    if (error == NERR_GroupNotFound)
    {
        error = ERROR_SUCCESS;
    }

    return error;
}


static
DWORD
EnsureLocalGroup(
    PCWSTR     pwszHostname,
    PWSTR      pwszAliasname,
    PBOOLEAN  pbCreated
    )
{
    DWORD error = ERROR_SUCCESS;
    LOCALGROUP_INFO_0 Info = {0};
    DWORD dwParmError = 0;

    Info.lgrpi0_name = pwszAliasname;

    error = NetLocalGroupAdd(pwszHostname,
                               0,
                               (PVOID)&Info,
                               &dwParmError);
    if (error == ERROR_SUCCESS)
    {
        *pbCreated = TRUE;
    }
    else if (error == NERR_GroupExists)
    {
        *pbCreated = FALSE;
        error = ERROR_SUCCESS;
    }

    return error;
}


static
BOOLEAN
CallNetUserEnum(
    PCWSTR pwszHostname,
    DWORD  dwLevel,
    DWORD  dwFilter
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwLastTotal = 0;
    DWORD dwCalculatedTotal = 0;
    DWORD dwTotal = 0;
    DWORD dwResume = 0;

    /* max buffer size below 10 bytes doesn't make much sense */
    while (dwMaxLen > 10)
    {
        dwCalculatedTotal = 0;
        dwLastTotal       = (DWORD)-1;

        do
        {
            error = NetUserEnum(pwszHostname,
                                  dwLevel,
                                  dwFilter,
                                  &pBuffer,
                                  dwMaxLen,
                                  &dwNumEntries,
                                  &dwTotal,
                                  &dwResume);
            if (error != ERROR_SUCCESS &&
                error != ERROR_MORE_DATA &&
                error != ERROR_NOT_ENOUGH_MEMORY)
            {
                bRet = FALSE;
                goto done;
            }

            if (dwLastTotal != (DWORD)-1)
            {
                ASSERT_TEST(dwLastTotal == dwTotal);
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }

            dwLastTotal        = dwTotal;
            dwCalculatedTotal += dwNumEntries;
        }
        while (error == ERROR_MORE_DATA);

        if (dwMaxLen > 65536)
        {
            dwMaxLen /= 256;
        }
        else if (dwMaxLen <= 65536 && dwMaxLen > 512)
        {
            dwMaxLen /= 4;
        }
        else if (dwMaxLen <= 512)
        {
            dwMaxLen /= 2;
        }
        else if (dwMaxLen < 32)
        {
            dwMaxLen = 0;
        }

        ASSERT_TEST(dwCalculatedTotal == dwTotal);

        dwNumEntries = 0;
        dwTotal      = 0;
        dwResume     = 0;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
CallNetUserAdd(
    PCWSTR pwszHostname,
    DWORD  dwLevel,
    PWSTR  pwszUsername,
    PWSTR  pwszDescription,
    PWSTR  pwszComment,
    PWSTR  pwszHomedir,
    PWSTR  pwszScriptPath,
    PWSTR  pwszPassword,
    DWORD  dwFlags,
    DWORD  dwPrivilege,
    DWORD  dwExpectedFlags,
    DWORD  dwExpectedError,
    DWORD  dwExpectedParmError
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    USER_INFO_1 Info1 = {0};
    USER_INFO_2 Info2 = {0};
    USER_INFO_3 Info3 = {0};
    USER_INFO_4 Info4 = {0};
    DWORD dwParmError = 0;

    switch (dwLevel)
    {
    case 1:
        Info1.usri1_name        = pwszUsername;
        Info1.usri1_password    = pwszPassword;
        Info1.usri1_priv        = dwPrivilege;
        Info1.usri1_home_dir    = pwszHomedir;
        Info1.usri1_comment     = pwszDescription;
        Info1.usri1_flags       = dwFlags;
        Info1.usri1_script_path = pwszScriptPath;

        pBuffer = (PVOID)&Info1;
        break;

    case 2:
        Info2.usri2_name        = pwszUsername;
        Info2.usri2_password    = pwszPassword;
        Info2.usri2_priv        = dwPrivilege;
        Info2.usri2_home_dir    = pwszHomedir;
        Info2.usri2_comment     = pwszDescription;
        Info2.usri2_flags       = dwFlags;
        Info2.usri2_script_path = pwszScriptPath;
        Info2.usri2_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info2;
        break;

    case 3:
        Info3.usri3_name        = pwszUsername;
        Info3.usri3_password    = pwszPassword;
        Info3.usri3_priv        = dwPrivilege;
        Info3.usri3_home_dir    = pwszHomedir;
        Info3.usri3_comment     = pwszDescription;
        Info3.usri3_flags       = dwFlags;
        Info3.usri3_script_path = pwszScriptPath;
        Info3.usri3_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info3;
        break;

    case 4:
        Info4.usri4_name        = pwszUsername;
        Info4.usri4_password    = pwszPassword;
        Info4.usri4_priv        = dwPrivilege;
        Info4.usri4_home_dir    = pwszHomedir;
        Info4.usri4_comment     = pwszDescription;
        Info4.usri4_flags       = dwFlags;
        Info4.usri4_script_path = pwszScriptPath;
        Info4.usri4_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info4;
        break;
    }

    error = NetUserAdd(pwszHostname,
                         dwLevel,
                         pBuffer,
                         &dwParmError);

    ret = (error == dwExpectedError &&
           dwParmError == dwExpectedParmError);

    return ret;
}


static
BOOLEAN
CallNetUserSetInfo(
    PCWSTR   pwszHostname,
    DWORD    dwLevel,
    PWSTR    pwszUsername,
    PWSTR    pwszFullName,
    PWSTR    pwszDescription,
    PWSTR    pwszComment,
    PWSTR    pwszHomedir,
    PWSTR    pwszScriptPath,
    PWSTR    pwszPassword,
    DWORD    dwFlags,
    DWORD    dwPrivilege,
    DWORD    dwExpectedError,
    DWORD    dwExpectedParmError,
    PBOOLEAN pbRenamed
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    USER_INFO_0 Info0 = {0};
    USER_INFO_1 Info1 = {0};
    USER_INFO_2 Info2 = {0};
    USER_INFO_3 Info3 = {0};
    USER_INFO_4 Info4 = {0};
    USER_INFO_1003 Info1003 = {0};
    USER_INFO_1007 Info1007 = {0};
    USER_INFO_1008 Info1008 = {0};
    USER_INFO_1011 Info1011 = {0};
    USER_INFO_1012 Info1012 = {0};
    DWORD dwParmError = 0;

    switch (dwLevel)
    {
    case 0:
        Info0.usri0_name        = pwszUsername;

        pBuffer = (PVOID)&Info0;
        break;

    case 1:
        Info1.usri1_name        = pwszUsername;
        Info1.usri1_password    = pwszPassword;
        Info1.usri1_priv        = dwPrivilege;
        Info1.usri1_home_dir    = pwszHomedir;
        Info1.usri1_comment     = pwszDescription;
        Info1.usri1_flags       = dwFlags;
        Info1.usri1_script_path = pwszScriptPath;

        pBuffer = (PVOID)&Info1;
        break;

    case 2:
        Info2.usri2_name        = pwszUsername;
        Info2.usri2_password    = pwszPassword;
        Info2.usri2_priv        = dwPrivilege;
        Info2.usri2_home_dir    = pwszHomedir;
        Info2.usri2_comment     = pwszDescription;
        Info2.usri2_flags       = dwFlags;
        Info2.usri2_script_path = pwszScriptPath;
        Info2.usri2_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info2;
        break;

    case 3:
        Info3.usri3_name        = pwszUsername;
        Info3.usri3_password    = pwszPassword;
        Info3.usri3_priv        = dwPrivilege;
        Info3.usri3_home_dir    = pwszHomedir;
        Info3.usri3_comment     = pwszDescription;
        Info3.usri3_flags       = dwFlags;
        Info3.usri3_script_path = pwszScriptPath;
        Info3.usri3_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info3;
        break;

    case 4:
        Info4.usri4_name        = pwszUsername;
        Info4.usri4_password    = pwszPassword;
        Info4.usri4_priv        = dwPrivilege;
        Info4.usri4_home_dir    = pwszHomedir;
        Info4.usri4_comment     = pwszDescription;
        Info4.usri4_flags       = dwFlags;
        Info4.usri4_script_path = pwszScriptPath;
        Info4.usri4_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info4;
        break;

    case 1003:
        Info1003.usri1003_password = pwszPassword;

        pBuffer = (PVOID)&Info1003;
        break;

    case 1007:
        Info1007.usri1007_comment = pwszDescription;

        pBuffer = (PVOID)&Info1007;
        break;

    case 1008:
        Info1008.usri1008_flags = dwFlags;

        pBuffer = (PVOID)&Info1008;
        break;

    case 1011:
        Info1011.usri1011_full_name = pwszFullName;

        pBuffer = (PVOID)&Info1011;
        break;

    case 1012:
        Info1012.usri1012_usr_comment = pwszComment;

        pBuffer = (PVOID)&Info1012;
        break;
    }

    error = NetUserSetInfo(pwszHostname,
                             pwszUsername,
                             dwLevel,
                             pBuffer,
                             &dwParmError);

    ret = (error == dwExpectedError &&
           dwParmError == dwExpectedParmError);

    if (dwLevel == 0 && pbRenamed)
    {
        *pbRenamed = TRUE;
    }

    return ret;
}


static
BOOL
CallNetUserGetInfo(
    PCWSTR   pwszHostname,
    PWSTR    pwszUsername,
    PVOID  **pppUserInfo,
    PDWORD   pdwNumUserInfos
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD dwLevels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
    DWORD i = 0;
    PVOID *pBuffer = NULL;
    DWORD dwNumBuffers = sizeof(pBuffer)/sizeof(pBuffer[0]);
    DWORD dwLevel = 0;

    error = LwAllocateMemory(
                        sizeof(pBuffer[0]) * 25,
                        OUT_PPVOID(&pBuffer));
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < sizeof(dwLevels)/sizeof(dwLevels[0]); i++)
    {
        dwLevel = dwLevels[i];

        error = NetUserGetInfo(pwszHostname,
                                 pwszUsername,
                                 dwLevel,
                                 &pBuffer[dwLevel]);
        if (error != ERROR_SUCCESS)
        {
            ret = FALSE;
        }
    }

    *pppUserInfo     = pBuffer;
    *pdwNumUserInfos = dwNumBuffers;

cleanup:
    return ret;

error:
    for (i = 0; i < 25; i++)
    {
        if (pBuffer[i])
        {
            NetApiBufferFree(pBuffer[i]);
            pBuffer[i] = NULL;
        }
    }

    LW_SAFE_FREE_MEMORY(pBuffer);
    pBuffer = NULL;

    goto cleanup;
}


static
BOOLEAN
TestVerifyUserInfo(
    PWSTR    pwszUsername,
    PWSTR    pwszFullName,
    PWSTR    pwszDescription,
    PWSTR    pwszComment,
    PWSTR    pwszHomedir,
    PWSTR    pwszScriptPath,
    PWSTR    pwszPassword,
    DWORD    dwFlags,
    DWORD    dwPrivilege,
    PVOID   *ppUserInfo
    )
{
    BOOLEAN bRet = TRUE;

    if (pwszUsername)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                                pwszUsername);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_1)ppUserInfo[1])->usri1_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_2)ppUserInfo[2])->usri2_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_10)ppUserInfo[10])->usri10_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_11)ppUserInfo[11])->usri11_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_20)ppUserInfo[20])->usri20_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_0)ppUserInfo[0])->usri0_name,
                            ((PUSER_INFO_23)ppUserInfo[23])->usri23_name);

    if (pwszFullName)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                                pwszFullName);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_full_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_full_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_10)ppUserInfo[10])->usri10_full_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_11)ppUserInfo[11])->usri11_full_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_20)ppUserInfo[20])->usri20_full_name);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_full_name,
                            ((PUSER_INFO_23)ppUserInfo[23])->usri23_full_name);

    if (pwszDescription)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                                pwszDescription);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_10)ppUserInfo[10])->usri10_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_11)ppUserInfo[11])->usri11_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_20)ppUserInfo[20])->usri20_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_comment,
                            ((PUSER_INFO_23)ppUserInfo[23])->usri23_comment);

    if (pwszComment)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_usr_comment,
                                pwszComment);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_usr_comment,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_usr_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_usr_comment,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_usr_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_usr_comment,
                            ((PUSER_INFO_10)ppUserInfo[10])->usri10_usr_comment);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_2)ppUserInfo[2])->usri2_usr_comment,
                            ((PUSER_INFO_11)ppUserInfo[11])->usri11_usr_comment);

    if (pwszHomedir)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                                pwszHomedir);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                            ((PUSER_INFO_2)ppUserInfo[2])->usri2_home_dir);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_home_dir);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_home_dir);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_home_dir);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_home_dir,
                            ((PUSER_INFO_11)ppUserInfo[11])->usri11_home_dir);

    if (pwszScriptPath)
    {
        ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_script_path,
                                pwszScriptPath);
    }

    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_script_path,
                            ((PUSER_INFO_2)ppUserInfo[2])->usri2_script_path);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_script_path,
                            ((PUSER_INFO_3)ppUserInfo[3])->usri3_script_path);
    ASSERT_WC16STRING_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_script_path,
                            ((PUSER_INFO_4)ppUserInfo[4])->usri4_script_path);

    if (dwFlags)
    {
        ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_flags,
                           ((PUSER_INFO_1)ppUserInfo[1])->usri1_flags | dwFlags);
    }

    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_flags,
                       ((PUSER_INFO_2)ppUserInfo[2])->usri2_flags);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_flags,
                       ((PUSER_INFO_3)ppUserInfo[3])->usri3_flags);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_flags,
                       ((PUSER_INFO_4)ppUserInfo[4])->usri4_flags);

    if (dwPrivilege)
    {
        ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                           dwPrivilege);
    }

    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                       ((PUSER_INFO_2)ppUserInfo[2])->usri2_priv);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                       ((PUSER_INFO_3)ppUserInfo[3])->usri3_priv);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                       ((PUSER_INFO_4)ppUserInfo[4])->usri4_priv);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                       ((PUSER_INFO_3)ppUserInfo[3])->usri3_priv);
    ASSERT_DWORD_EQUAL(((PUSER_INFO_1)ppUserInfo[1])->usri1_priv,
                       ((PUSER_INFO_11)ppUserInfo[11])->usri11_priv);

    return bRet;
}


static
BOOLEAN
CallNetUserGetLocalGroups(
    PCWSTR pwszHostname,
    PCWSTR pwszUserName,
    DWORD  dwLevel,
    DWORD  dwFlags
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwLastTotal = 0;
    DWORD dwCalculatedTotal = 0;
    DWORD dwTotal = 0;

    /* max buffer size below 10 bytes doesn't make much sense */
    while (dwMaxLen > 10)
    {
        dwCalculatedTotal = 0;
        dwLastTotal       = (DWORD)-1;

        error = NetUserGetLocalGroups(
                            pwszHostname,
                            pwszUserName,
                            dwLevel,
                            dwFlags,
                            &pBuffer,
                            dwMaxLen,
                            &dwNumEntries,
                            &dwTotal);
        if (error != ERROR_SUCCESS &&
            error != ERROR_MORE_DATA &&
            error != ERROR_NOT_ENOUGH_MEMORY)
        {
            bRet = FALSE;
            goto done;
        }

        if (dwLastTotal != (DWORD)-1)
        {
            ASSERT_TEST(dwLastTotal == dwTotal);
        }

        if (pBuffer)
        {
            NetApiBufferFree(pBuffer);
            pBuffer = NULL;
        }

        dwLastTotal        = dwTotal;
        dwCalculatedTotal += dwNumEntries;

        if (dwMaxLen > 65536)
        {
            dwMaxLen /= 256;
        }
        else if (dwMaxLen <= 65536 && dwMaxLen > 512)
        {
            dwMaxLen /= 4;
        }
        else if (dwMaxLen <= 512)
        {
            dwMaxLen /= 2;
        }
        else if (dwMaxLen < 32)
        {
            dwMaxLen = 0;
        }

        ASSERT_TEST(dwCalculatedTotal == dwTotal);

        dwNumEntries = 0;
        dwTotal      = 0;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
CallNetLocalGroupEnum(
    PCWSTR pwszHostname,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwLastTotal = 0;
    DWORD dwCalculatedTotal = 0;
    DWORD dwTotal = 0;
    DWORD dwResume = 0;

    while (dwMaxLen)
    {
        dwCalculatedTotal = 0;
        dwLastTotal       = (DWORD)-1;

        do
        {
            error = NetLocalGroupEnum(pwszHostname,
                                        dwLevel,
                                        &pBuffer,
                                        dwMaxLen,
                                        &dwNumEntries,
                                        &dwTotal,
                                        &dwResume);
            if (error != ERROR_SUCCESS &&
                error != ERROR_MORE_DATA &&
                error != ERROR_NOT_ENOUGH_MEMORY)
            {
                bRet = FALSE;
                goto done;
            }

            if (dwLastTotal != (DWORD)-1)
            {
                ASSERT_TEST(dwLastTotal == dwTotal);
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }

            dwLastTotal        = dwTotal;
            dwCalculatedTotal += dwNumEntries;
        }
        while (error == ERROR_MORE_DATA);

        if (dwMaxLen > 65536)
        {
            dwMaxLen /= 256;
        }
        else if (dwMaxLen <= 65536 && dwMaxLen > 512)
        {
            dwMaxLen /= 4;
        }
        else if (dwMaxLen <= 512)
        {
            dwMaxLen /= 2;
        }
        else if (dwMaxLen < 32)
        {
            dwMaxLen = 0;
        }

        ASSERT_TEST(dwCalculatedTotal == dwTotal);

        dwNumEntries = 0;
        dwTotal      = 0;
        dwResume     = 0;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
CallNetLocalGroupAdd(
    PCWSTR pwszHostname,
    DWORD  dwLevel,
    PWSTR  pwszAliasname,
    PWSTR  pwszComment
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    LOCALGROUP_INFO_0 Info0 = {0};
    LOCALGROUP_INFO_1 Info1 = {0};
    DWORD dwParmErr = 0;

    switch (dwLevel)
    {
    case 0:
        Info0.lgrpi0_name    = pwszAliasname;

        pBuffer = &Info0;
        break;

    case 1:
        Info1.lgrpi1_name    = pwszAliasname;
        Info1.lgrpi1_comment = pwszComment;

        pBuffer = &Info1;
        break;
    }

    error = NetLocalGroupAdd(
                      pwszHostname,
                      dwLevel,
                      pBuffer,
                      &dwParmErr);
    ret = (error == ERROR_SUCCESS);

    return ret;
}


static
BOOLEAN
CallNetLocalGroupSetInfo(
    PCWSTR    pwszHostname,
    DWORD     dwLevel,
    PWSTR     pwszAliasname,
    PWSTR     pwszChangedAliasname,
    PWSTR     pwszComment,
    PBOOLEAN  pbRenamed
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    LOCALGROUP_INFO_0 Info0 = {0};
    LOCALGROUP_INFO_1 Info1 = {0};
    LOCALGROUP_INFO_1002 Info1002 = {0};
    DWORD dwParmErr = 0;

    switch (dwLevel)
    {
    case 0:
        Info0.lgrpi0_name       = pwszChangedAliasname;

        pBuffer = (PVOID)&Info0;
        break;

    case 1:
        /* Check if lgrpi1_name really is ignored */
        Info1.lgrpi1_name       = pwszChangedAliasname;
        Info1.lgrpi1_comment    = pwszComment;

        pBuffer = (PVOID)&Info1;
        break;

    case 1002:
        Info1002.lgrpi1002_comment = pwszComment;

        pBuffer = (PVOID)&Info1002;
        break;
    }

    error = NetLocalGroupSetInfo(
                         pwszHostname,
                         pwszAliasname,
                         dwLevel,
                         pBuffer,
                         &dwParmErr);

    ret = (error == ERROR_SUCCESS);

    if (dwLevel == 0 && pbRenamed)
    {
        *pbRenamed = TRUE;
    }

    return ret;
}


static
BOOLEAN
CallNetLocalGroupGetInfo(
    PCWSTR pwszHostname,
    PCWSTR pwszAliasname,
    DWORD  dwLevel
    )
{
    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;

    error = NetLocalGroupGetInfo(
                      pwszHostname,
                      pwszAliasname,
                      dwLevel,
                      &pBuffer);
    if (error != ERROR_SUCCESS)
    {
        ret = FALSE;
    }

    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return ret;
}


static
BOOLEAN
CallNetLocalGroupGetMembers(
    PCWSTR pwszHostname,
    PCWSTR pwszLocalGroupName,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwLastTotal = 0;
    DWORD dwCalculatedTotal = 0;
    DWORD dwTotal = 0;
    DWORD dwResume = 0;

    while (dwMaxLen)
    {
        dwCalculatedTotal = 0;
        dwLastTotal       = (DWORD)-1;

        do
        {
            error = NetLocalGroupGetMembers(
                              pwszHostname,
                              pwszLocalGroupName,
                              dwLevel,
                              &pBuffer,
                              dwMaxLen,
                              &dwNumEntries,
                              &dwTotal,
                              &dwResume);
            if (error != ERROR_SUCCESS &&
                error != ERROR_MORE_DATA &&
                error != ERROR_NOT_ENOUGH_MEMORY)
            {
                bRet = FALSE;
                goto done;
            }

            if (dwLastTotal != (DWORD)-1)
            {
                ASSERT_TEST(dwLastTotal == dwTotal);
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }

            dwLastTotal        = dwTotal;
            dwCalculatedTotal += dwNumEntries;
        }
        while (error == ERROR_MORE_DATA);

        if (dwMaxLen > 65536)
        {
            dwMaxLen /= 256;
        }
        else if (dwMaxLen <= 65536 && dwMaxLen > 512)
        {
            dwMaxLen /= 4;
        }
        else if (dwMaxLen <= 512)
        {
            dwMaxLen /= 2;
        }
        else if (dwMaxLen < 32)
        {
            dwMaxLen = 0;
        }

        ASSERT_TEST(dwCalculatedTotal == dwTotal);

        dwNumEntries = 0;
        dwTotal      = 0;
        dwResume     = 0;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
TestValidateDisplayUserInfo(
    PNET_DISPLAY_USER  pUser
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    size_t sUsernameLen = 0;

    ASSERT_TEST(pUser->usri1_name != NULL);

    if (pUser->usri1_name)
    {
        error = LwWc16sLen(pUser->usri1_name, &sUsernameLen);
        if (error)
        {
            bRet = FALSE;
            return bRet;
        }
    }

    ASSERT_TEST((pUser->usri1_flags & UF_NORMAL_ACCOUNT) ||
                (pUser->usri1_flags & UF_TEMP_DUPLICATE_ACCOUNT));
    ASSERT_TEST(pUser->usri1_user_id > 0);

    return bRet;
}


static
BOOLEAN
TestValidateDisplayMachineInfo(
    PNET_DISPLAY_MACHINE  pMachine
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    size_t sMachnameLen = 0;

    ASSERT_TEST(pMachine->usri2_name != NULL);

    if (pMachine->usri2_name)
    {
        error = LwWc16sLen(pMachine->usri2_name, &sMachnameLen);
        if (error)
        {
            bRet = FALSE;
            return bRet;
        }
    }

    ASSERT_TEST((pMachine->usri2_flags & UF_WORKSTATION_TRUST_ACCOUNT) ||
                (pMachine->usri2_flags & UF_SERVER_TRUST_ACCOUNT));
    ASSERT_TEST(pMachine->usri2_user_id > 0);

    return bRet;
}


static
BOOLEAN
TestValidateDisplayGroupInfo(
    PNET_DISPLAY_GROUP  pGroup
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    size_t sGroupnameLen = 0;

    ASSERT_TEST(pGroup->grpi3_name != NULL);

    if (pGroup->grpi3_name)
    {
        error = LwWc16sLen(pGroup->grpi3_name, &sGroupnameLen);
        if (error)
        {
            bRet = FALSE;
            return bRet;
        }
    }

    ASSERT_TEST(pGroup->grpi3_group_id > 0);

    return bRet;
}


static
BOOLEAN
TestValidateWkstaUserInfo(
    PVOID  pInfo,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PWKSTA_USER_INFO_0 pInfo0 = (PWKSTA_USER_INFO_0)pInfo;
    PWKSTA_USER_INFO_1 pInfo1 = (PWKSTA_USER_INFO_1)pInfo;
    size_t sUsernameLen = 0;
    size_t sLogonDomainLen = 0;
    size_t sOthDomainsLen = 0;
    size_t sLogonServerLen = 0;

    if (dwLevel == 0 ||
        dwLevel == 1)
    {
        ASSERT_TEST(pInfo0->wkui0_username != NULL);

        if (pInfo0->wkui0_username)
        {
            /*
             * Assuming the account name can be up to 63-chars long
             */
            error = LwWc16sLen(pInfo0->wkui0_username, &sUsernameLen);
            if (error != ERROR_SUCCESS ||
                !(sUsernameLen > 0 && sUsernameLen < 64))
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    if (dwLevel == 1)
    {
        ASSERT_TEST(pInfo1->wkui1_logon_domain != NULL);
        if (pInfo1->wkui1_logon_domain)
        {
            /*
             * Assuming the domain name can be up to 15-chars long
             */
            error = LwWc16sLen(pInfo1->wkui1_logon_domain, &sLogonDomainLen);
            if (error != ERROR_SUCCESS ||
                !(sLogonDomainLen > 0 && sLogonDomainLen < 16))
            {
                bRet = FALSE;
                return bRet;
            }
        }

        ASSERT_TEST(pInfo1->wkui1_oth_domains != NULL);
        if (pInfo1->wkui1_oth_domains)
        {
            error = LwWc16sLen(pInfo1->wkui1_oth_domains, &sOthDomainsLen);
            if (error)
            {
                bRet = FALSE;
                return bRet;
            }
        }

        ASSERT_TEST(pInfo1->wkui1_logon_server != NULL);
        if (pInfo1->wkui1_logon_server)
        {
            error = LwWc16sLen(pInfo1->wkui1_logon_server, &sLogonServerLen);
            if (error)
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    return bRet;
}




static
BOOLEAN
TestValidateSessionInfo(
    PVOID  pInfo,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PSESSION_INFO_0 pInfo0 = (PSESSION_INFO_0)pInfo;
    PSESSION_INFO_1 pInfo1 = (PSESSION_INFO_1)pInfo;
    PSESSION_INFO_2 pInfo2 = (PSESSION_INFO_2)pInfo;
    PSESSION_INFO_10 pInfo10 = (PSESSION_INFO_10)pInfo;
    PSESSION_INFO_502 pInfo502 = (PSESSION_INFO_502)pInfo;
    size_t sClientNameLen = 0;
    size_t sUserNameLen = 0;
    size_t sClientTypeNameLen = 0;
    size_t sTransportNameLen = 0;

    if (dwLevel == 0 ||
        dwLevel == 1 ||
        dwLevel == 2)
    {
        ASSERT_TEST(pInfo0->sesi0_cname != NULL);
        if (pInfo0->sesi0_cname)
        {
            error = LwWc16sLen(pInfo0->sesi0_cname, &sClientNameLen);
            if (error != ERROR_SUCCESS ||
                !(sClientNameLen > 0))
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    if (dwLevel == 1 ||
        dwLevel == 2 ||
        dwLevel == 10 ||
        dwLevel == 502)
    {
        ASSERT_TEST(pInfo1->sesi1_username != NULL);
        if (pInfo1->sesi1_username)
        {
            /*
             * Assuming the account name can be up to 63-chars long
             */
            error = LwWc16sLen(pInfo1->sesi1_username, &sUserNameLen);
            if (error != ERROR_SUCCESS ||
                !(sUserNameLen > 0))
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    if (dwLevel == 2 ||
        dwLevel == 502)
    {
        ASSERT_TEST(pInfo2->sesi2_time > 0);
    }

    if (dwLevel == 10)
    {
        ASSERT_TEST(pInfo10->sesi10_time > 0);
    }

    if (dwLevel == 2 ||
        dwLevel == 502)
    {
        ASSERT_TEST(pInfo2->sesi2_cltype_name != NULL);
        if (pInfo2->sesi2_cltype_name)
        {
            error = LwWc16sLen(pInfo2->sesi2_cltype_name,
                                 &sClientTypeNameLen);
            if (error != ERROR_SUCCESS ||
                !(sClientTypeNameLen > 0))
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    if (dwLevel == 502)
    {
        ASSERT_TEST(pInfo502->sesi502_transport != NULL);
        if (pInfo502->sesi502_transport)
        {
            error = LwWc16sLen(pInfo502->sesi502_transport,
                                 &sTransportNameLen);
            if (error != ERROR_SUCCESS ||
                !(sTransportNameLen > 0))
            {
                bRet = FALSE;
                return bRet;
            }
        }
    }

    return bRet;
}


static
BOOLEAN
TestValidateServerInfo(
    PVOID  pInfo,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PSERVER_INFO_100 pInfo100 = (PSERVER_INFO_100)pInfo;
    size_t sServerNameLen = 0;

    if (dwLevel == 100 ||
        dwLevel == 101)
    {
        ASSERT_TEST(pInfo100->sv100_name != NULL);
        if (pInfo100->sv100_name)
        {
            error = LwWc16sLen(pInfo100->sv100_name, &sServerNameLen);
            if (error)
            {
                bRet = FALSE;
                return bRet;
            }

            ASSERT_TEST(sServerNameLen > 0);
        }
    }

    return bRet;
}


static
BOOLEAN
CallNetQueryDisplayInfo(
    PCWSTR pwszHostname,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwIndex = 0;
    DWORD dwRequested = 50;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwPrevTotalNumEntries = 0;
    DWORD dwTotalNumEntries = 0;
    DWORD iEntry = 0;
    PNET_DISPLAY_USER pDispUser = NULL;
    PNET_DISPLAY_MACHINE pDispMachine = NULL;
    PNET_DISPLAY_GROUP pDispGroup = NULL;

    /* max buffer size below 10 bytes doesn't make much sense */
    while (dwPrefMaxLen > 10)
    {
        do
        {
            error = NetQueryDisplayInformation(
                                  pwszHostname,
                                  dwLevel,
                                  dwIndex,
                                  dwRequested,
                                  dwPrefMaxLen,
                                  &dwNumEntries,
                                  &pBuffer);
            if (error != ERROR_SUCCESS &&
                error != ERROR_MORE_DATA)
            {
                bRet = FALSE;
                goto done;
            }

            ASSERT_TEST(dwNumEntries <= dwRequested);

            dwTotalNumEntries += dwNumEntries;

            for (iEntry = 0; iEntry < dwNumEntries; iEntry++)
            {
                switch (dwLevel)
                {
                case 1:
                    pDispUser = &(((PNET_DISPLAY_USER)(pBuffer))[iEntry]);

                    bRet   &= TestValidateDisplayUserInfo(pDispUser);
                    dwIndex = pDispUser->usri1_next_index;
                    break;

                case 2:
                    pDispMachine = &(((PNET_DISPLAY_MACHINE)(pBuffer))[iEntry]);

                    bRet   &= TestValidateDisplayMachineInfo(pDispMachine);
                    dwIndex = pDispMachine->usri2_next_index;
                    break;

                case 3:
                    pDispGroup = &(((PNET_DISPLAY_GROUP)(pBuffer))[iEntry]);

                    bRet   &= TestValidateDisplayGroupInfo(pDispGroup);
                    dwIndex = pDispGroup->grpi3_next_index;
                    break;

                default:
                    bRet = FALSE;
                    goto done;
                }
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }
        }
        while (error == ERROR_MORE_DATA);

        if (dwPrefMaxLen > 65536)
        {
            dwPrefMaxLen /= 256;
        }
        else if (dwPrefMaxLen <= 65536 && dwPrefMaxLen > 512)
        {
            dwPrefMaxLen /= 4;
        }
        else if (dwPrefMaxLen <= 512)
        {
            dwPrefMaxLen /= 2;
        }
        else if (dwPrefMaxLen < 32)
        {
            dwPrefMaxLen = 0;
        }

        if (dwPrevTotalNumEntries)
        {
            ASSERT_TEST(dwPrevTotalNumEntries == dwTotalNumEntries);
        }

        dwPrevTotalNumEntries = dwTotalNumEntries;
        dwTotalNumEntries     = 0;
        dwIndex               = 0;
    }

    dwLevel      = 0;
    dwNumEntries = 0;
    error = NetQueryDisplayInformation(
                             pwszHostname,
                             dwLevel,
                             dwIndex,
                             dwRequested,
                             dwPrefMaxLen,
                             &dwNumEntries,
                             &pBuffer);
    if (error != ERROR_INVALID_LEVEL)
    {
        bRet = FALSE;
        goto done;
    }

    if (pBuffer)
    {
        /*
         * There shouldn't be any buffer returned
         */
        bRet = FALSE;
        goto done;
    }

    dwLevel      = 4;
    dwNumEntries = 0;
    error = NetQueryDisplayInformation(
                             pwszHostname,
                             dwLevel,
                             dwIndex,
                             dwRequested,
                             dwPrefMaxLen,
                             &dwNumEntries,
                             &pBuffer);
    if (error != ERROR_INVALID_LEVEL)
    {
        bRet = FALSE;
        goto done;
    }

    if (pBuffer)
    {
        /*
         * There shouldn't be any buffer returned
         */
        bRet = FALSE;
        goto done;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
CallNetWkstaUserEnum(
    PCWSTR pwszHostname,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PWSTR pwszHost = NULL;
    PVOID pBuffer = NULL;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwTotalNumEntries = 0;
    DWORD dwTotalCounted = 0;
    DWORD dwResume = 0;
    DWORD dwPrevTotalCounted = 0;
    DWORD iEntry = 0;
    PWKSTA_USER_INFO_0 pWkstaUserInfo0 = NULL;
    PWKSTA_USER_INFO_1 pWkstaUserInfo1 = NULL;

    error = LwAllocateWc16String(&pwszHost, pwszHostname);
    if (error)
    {
        bRet = FALSE;
        goto done;
    }

    /* max buffer size below 32 bytes doesn't make much sense */
    while (dwPrefMaxLen > 32)
    {
        do
        {
            error = NetWkstaUserEnum(
                               pwszHost,
                               dwLevel,
                               &pBuffer,
                               dwPrefMaxLen,
                               &dwNumEntries,
                               &dwTotalNumEntries,
                               &dwResume);
            if (error != ERROR_SUCCESS &&
                error != ERROR_MORE_DATA)
            {
                bRet = FALSE;
                goto done;
            }

            dwTotalCounted    += dwNumEntries;
            dwTotalNumEntries  = 0;

            for (iEntry = 0; iEntry < dwNumEntries; iEntry++)
            {
                switch (dwLevel)
                {
                case 0:
                    pWkstaUserInfo0 = &(((PWKSTA_USER_INFO_0)(pBuffer))[iEntry]);

                    bRet   &= TestValidateWkstaUserInfo(pWkstaUserInfo0, dwLevel);
                    break;

                case 1:
                    pWkstaUserInfo1 = &(((PWKSTA_USER_INFO_1)(pBuffer))[iEntry]);

                    bRet   &= TestValidateWkstaUserInfo(pWkstaUserInfo1, dwLevel);
                    break;

                default:
                    bRet = FALSE;
                    goto done;
                }
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }
        }
        while (error == ERROR_MORE_DATA);

        if (dwPrefMaxLen > 65536)
        {
            dwPrefMaxLen /= 256;
        }
        else if (dwPrefMaxLen <= 65536 && dwPrefMaxLen > 512)
        {
            dwPrefMaxLen /= 4;
        }
        else if (dwPrefMaxLen <= 512)
        {
            dwPrefMaxLen /= 2;
        }
        else if (dwPrefMaxLen < 32)
        {
            dwPrefMaxLen = 0;
        }

        if (dwPrevTotalCounted)
        {
            ASSERT_TEST(dwPrevTotalCounted == dwTotalCounted);
        }

        dwPrevTotalCounted  = dwTotalCounted;
        dwTotalCounted      = 0;
        dwResume            = 0;
    }

    dwLevel      = 2;
    dwNumEntries = 0;
    error = NetWkstaUserEnum(
                      pwszHost,
                      dwLevel,
                      &pBuffer,
                      dwPrefMaxLen,
                      &dwNumEntries,
                      &dwTotalNumEntries,
                      &dwResume);
    if (error != ERROR_INVALID_LEVEL)
    {
        bRet = FALSE;
        goto done;
    }

    if (pBuffer)
    {
        /*
         * There shouldn't be any buffer returned
         */
        bRet = FALSE;
        goto done;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
BOOLEAN
CallNetSessionEnum(
    PCWSTR pwszHostname,
    PWSTR  pwszClientName,
    PWSTR  pwszUserName,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    PWSTR pwszHost = NULL;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwTotalNumEntries = 0;
    DWORD dwTotalCounted = 0;
    DWORD dwResume = 0;
    DWORD dwPrevTotalCounted = 0;
    DWORD iEntry = 0;
    PSESSION_INFO_0 pSessionInfo0 = NULL;
    PSESSION_INFO_1 pSessionInfo1 = NULL;
    PSESSION_INFO_2 pSessionInfo2 = NULL;
    PSESSION_INFO_10 pSessionInfo10 = NULL;
    PSESSION_INFO_502 pSessionInfo502 = NULL;

    error = LwAllocateWc16String(&pwszHost, pwszHostname);
    if (error)
    {
        bRet = FALSE;
        goto done;
    }

    /* max buffer size below 32 bytes doesn't make much sense */
    while (dwPrefMaxLen > 32)
    {
        do
        {
            error = NetSessionEnum(
                                pwszHost,
                                pwszClientName,
                                pwszUserName,
                                dwLevel,
                                (PBYTE*)&pBuffer,
                                dwPrefMaxLen,
                                &dwNumEntries,
                                &dwTotalNumEntries,
                                &dwResume);
            if (error == NERR_BufTooSmall)
            {
                goto done;
            }
            else if (error != ERROR_SUCCESS &&
                     error != ERROR_MORE_DATA)
            {
                bRet = FALSE;
                goto done;
            }

            dwTotalCounted    += dwNumEntries;
            dwTotalNumEntries  = 0;

            for (iEntry = 0; iEntry < dwNumEntries; iEntry++)
            {
                switch (dwLevel)
                {
                case 0:
                    pSessionInfo0 = &(((PSESSION_INFO_0)(pBuffer))[iEntry]);

                    bRet   &= TestValidateSessionInfo(pSessionInfo0, dwLevel);
                    break;

                case 1:
                    pSessionInfo1 = &(((PSESSION_INFO_1)(pBuffer))[iEntry]);

                    bRet   &= TestValidateSessionInfo(pSessionInfo1, dwLevel);
                    break;

                case 2:
                    pSessionInfo2 = &(((PSESSION_INFO_2)(pBuffer))[iEntry]);

                    bRet   &= TestValidateSessionInfo(pSessionInfo2, dwLevel);
                    break;

                case 10:
                    pSessionInfo10 = &(((PSESSION_INFO_10)(pBuffer))[iEntry]);

                    bRet   &= TestValidateSessionInfo(pSessionInfo10, dwLevel);
                    break;

                case 502:
                    pSessionInfo502 = &(((PSESSION_INFO_502)(pBuffer))[iEntry]);

                    bRet   &= TestValidateSessionInfo(pSessionInfo502, dwLevel);
                    break;

                default:
                    bRet = FALSE;
                    goto done;
                }
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }
        }
        while (error == ERROR_MORE_DATA);

        if (dwPrefMaxLen > 65536)
        {
            dwPrefMaxLen /= 256;
        }
        else if (dwPrefMaxLen <= 65536 && dwPrefMaxLen > 512)
        {
            dwPrefMaxLen /= 4;
        }
        else if (dwPrefMaxLen <= 512)
        {
            dwPrefMaxLen /= 2;
        }
        else if (dwPrefMaxLen < 32)
        {
            dwPrefMaxLen = 0;
        }

        if (dwPrevTotalCounted)
        {
            ASSERT_TEST(dwPrevTotalCounted == dwTotalCounted);
        }

        dwPrevTotalCounted  = dwTotalCounted;
        dwTotalCounted      = 0;
        dwResume            = 0;
    }

    dwLevel      = 20;
    dwNumEntries = 0;
    error = NetSessionEnum(
                        pwszHost,
                        pwszClientName,
                        pwszUserName,
                        dwLevel,
                        (PBYTE*)&pBuffer,
                        dwPrefMaxLen,
                        &dwNumEntries,
                        &dwTotalNumEntries,
                        &dwResume);
    if (error != ERROR_INVALID_LEVEL)
    {
        bRet = FALSE;
        goto done;
    }

    if (pBuffer)
    {
        /*
         * There shouldn't be any buffer returned
         */
        bRet = FALSE;
        goto done;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    LW_SAFE_FREE_MEMORY(pwszHost);

    return bRet;
}


static
BOOLEAN
CallNetServerEnum(
    PCWSTR pwszHostname,
    PWSTR  pwszDomain,
    DWORD  dwLevel
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PVOID pBuffer = NULL;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwNumEntries = 0;
    DWORD dwTotalNumEntries = 0;
    DWORD dwTotalCounted = 0;
    DWORD dwResume = 0;
    DWORD dwPrevTotalCounted = 0;
    DWORD iEntry = 0;
    DWORD dwServerType = SV_TYPE_DOMAIN_CTRL;
    PSERVER_INFO_100 pServerInfo100 = NULL;
    PSERVER_INFO_101 pServerInfo101 = NULL;

    /* max buffer size below 10 bytes doesn't make much sense */
    while (dwPrefMaxLen > 10)
    {
        do
        {
            error = NetServerEnum(
                               pwszHostname,
                               dwLevel,
                               &pBuffer,
                               dwPrefMaxLen,
                               &dwNumEntries,
                               &dwTotalNumEntries,
                               dwServerType,
                                pwszDomain,
                               &dwResume);
            if (error == NERR_BufTooSmall)
            {
                goto done;
            }
            else if (error != ERROR_SUCCESS &&
                     error != ERROR_MORE_DATA)
            {
                bRet = FALSE;
                goto done;
            }

            ASSERT_TEST(pBuffer != NULL);
            ASSERT_TEST(dwNumEntries > 0);
            ASSERT_TEST(dwTotalNumEntries >= dwNumEntries);

            dwTotalCounted    += dwNumEntries;
            dwTotalNumEntries  = 0;

            for (iEntry = 0; iEntry < dwNumEntries; iEntry++)
            {
                switch (dwLevel)
                {
                case 100:
                    pServerInfo100 = &(((PSERVER_INFO_100)(pBuffer))[iEntry]);

                    bRet  &= TestValidateServerInfo(pServerInfo100, dwLevel);
                    break;

                case 101:
                    pServerInfo101 = &(((PSERVER_INFO_101)(pBuffer))[iEntry]);

                    bRet  &= TestValidateServerInfo(pServerInfo101, dwLevel);
                    break;

                default:
                    bRet = FALSE;
                    goto done;
                }
            }

            if (pBuffer)
            {
                NetApiBufferFree(pBuffer);
                pBuffer = NULL;
            }
        }
        while (error == ERROR_MORE_DATA);

        if (dwPrefMaxLen > 65536)
        {
            dwPrefMaxLen /= 256;
        }
        else if (dwPrefMaxLen <= 65536 && dwPrefMaxLen > 512)
        {
            dwPrefMaxLen /= 4;
        }
        else if (dwPrefMaxLen <= 512)
        {
            dwPrefMaxLen /= 2;
        }
        else if (dwPrefMaxLen < 32)
        {
            dwPrefMaxLen = 0;
        }

        if (dwPrevTotalCounted)
        {
            ASSERT_TEST(dwPrevTotalCounted == dwTotalCounted);
        }

        dwPrevTotalCounted  = dwTotalCounted;
        dwTotalCounted      = 0;
        dwResume            = 0;
    }

    dwLevel      = 50;
    dwNumEntries = 0;
    error = NetServerEnum(
                       pwszHostname,
                       dwLevel,
                       &pBuffer,
                       dwPrefMaxLen,
                       &dwNumEntries,
                       &dwTotalNumEntries,
                       dwServerType,
                       pwszDomain,
                       &dwResume);
    if (error != ERROR_INVALID_LEVEL)
    {
        bRet = FALSE;
        goto done;
    }

    if (pBuffer)
    {
        /*
         * There shouldn't be any buffer returned
         */
        bRet = FALSE;
        goto done;
    }

done:
    if (pBuffer)
    {
        NetApiBufferFree(pBuffer);
    }

    return bRet;
}


static
DWORD
TestNetUserEnum(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultFilter = 0;
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1, 2, 20 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;
    DWORD dwFilter = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "filter", PT_UINT32,
                       (UINT32*)&dwFilter, (UINT32*)&dwDefaultFilter);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        if (!dwFilter)
        {
            ret &= CallNetUserEnum(Server->Hostname,
                                   dwLevel,
                                   FILTER_NORMAL_ACCOUNT);

            ret &= CallNetUserEnum(Server->Hostname,
                                   dwLevel,
                                   FILTER_INTERDOMAIN_TRUST_ACCOUNT);

            ret &= CallNetUserEnum(Server->Hostname,
                                   dwLevel,
                                   FILTER_WORKSTATION_TRUST_ACCOUNT);

            ret &= CallNetUserEnum(Server->Hostname,
                                   dwLevel,
                                   FILTER_SERVER_TRUST_ACCOUNT);
        }
        else
        {
            ret &= CallNetUserEnum(Server->Hostname,
                                   dwLevel,
                                   dwFilter);
        }
    }

error:
    return ret;
}


static
BOOLEAN
CallNetUserDel(
    PCWSTR  pwszHostname,
    PWSTR   pwszUsername,
    DWORD   dwExpectedError
    )
{
    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;

    error = NetUserDel(pwszHostname,
                         pwszUsername);

    bRet = (error == dwExpectedError);

    return bRet;
}


static
DWORD
TestNetUserAdd(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultTestSetName = "validation";

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD dwLevel = 0;
    PWSTR pwszUsername = NULL;
    PWSTR pwszDescription = NULL;
    PWSTR pwszComment = NULL;
    PWSTR pwszHomedir = NULL;
    PWSTR pwszScriptPath = NULL;
    PWSTR pwszPassword = NULL;
    DWORD dwFlags = 0;
    DWORD dwPrivilege = 0;
    DWORD dwExpectedFlags = 0;
    DWORD dwExpectedError = 0;
    DWORD dwExpectedParmError = 0;
    PSTR pszTestSetName = NULL;
    PTEST_NET_USER_ADD pTestSet = NULL;
    DWORD dwNumTests = 0;
    DWORD i = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "testset", PT_CSTRING,
                       &pszTestSetName, &pszDefaultTestSetName);
    BAIL_ON_WIN_ERROR(error);

    error = TestGetNetUserAddTestSet(
                            pszTestSetName,
                            &pTestSet,
                            &dwNumTests);
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < dwNumTests; i++)
    {
        dwLevel = pTestSet[i].dwLevel;

        if (pTestSet[i].pszUserName)
        {
            error = LwMbsToWc16s(pTestSet[i].pszUserName,
                                   &pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszComment)
        {
            error = LwMbsToWc16s(pTestSet[i].pszComment,
                                   &pwszComment);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszDescription)
        {
            error = LwMbsToWc16s(pTestSet[i].pszDescription,
                                   &pwszDescription);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszHomeDir)
        {
            error = LwMbsToWc16s(pTestSet[i].pszHomeDir,
                                   &pwszHomedir);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszScriptPath)
        {
            error = LwMbsToWc16s(pTestSet[i].pszScriptPath,
                                   &pwszScriptPath);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszPassword)
        {
            error = LwMbsToWc16s(pTestSet[i].pszPassword,
                               &pwszPassword);
            BAIL_ON_WIN_ERROR(error);
        }

        dwFlags             = pTestSet[i].dwFlags;
        dwPrivilege         = pTestSet[i].dwPrivilege;
        dwExpectedFlags     = pTestSet[i].dwExpectedFlags;
        dwExpectedError     = pTestSet[i].dwError;
        dwExpectedParmError = pTestSet[i].dwParmError;

        if (pTestSet[i].bPreCleanup)
        {
            error = CleanupAccount(Server->Hostname,
                                   pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        ret &= CallNetUserAdd(Server->Hostname,
                              dwLevel,
                              pwszUsername,
                              pwszDescription,
                              pwszComment,
                              pwszHomedir,
                              pwszScriptPath,
                              pwszPassword,
                              dwFlags,
                              dwPrivilege,
                              dwExpectedFlags,
                              dwExpectedError,
                              dwExpectedParmError);

        if (pTestSet[i].bPostCleanup)
        {
            error = CleanupAccount(Server->Hostname,
                                   pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        LW_SAFE_FREE_MEMORY(pwszUsername);
        LW_SAFE_FREE_MEMORY(pwszDescription);
        LW_SAFE_FREE_MEMORY(pwszComment);
        LW_SAFE_FREE_MEMORY(pwszHomedir);
        LW_SAFE_FREE_MEMORY(pwszScriptPath);
        LW_SAFE_FREE_MEMORY(pwszPassword);

        pwszUsername    = NULL;
        pwszDescription = NULL;
        pwszComment     = NULL;
        pwszHomedir     = NULL;
        pwszScriptPath  = NULL;
        pwszPassword    = NULL;
    }

error:
    LW_SAFE_FREE_MEMORY(pwszUsername);
    LW_SAFE_FREE_MEMORY(pwszDescription);
    LW_SAFE_FREE_MEMORY(pwszComment);
    LW_SAFE_FREE_MEMORY(pwszHomedir);
    LW_SAFE_FREE_MEMORY(pwszScriptPath);
    LW_SAFE_FREE_MEMORY(pwszPassword);

    return (ret &&
            error == ERROR_SUCCESS);
}


static
DWORD
TestNetUserDel(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultTestSetName = "validation";

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PSTR pszTestSetName = NULL;
    PTEST_NET_USER_DEL pTestSet = NULL;
    DWORD dwNumTests = 0;
    DWORD i = 0;
    PWSTR pwszUsername = NULL;
    BOOLEAN bCreated = FALSE;
    DWORD dwExpectedError = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "testset", PT_CSTRING,
                       &pszTestSetName, &pszDefaultTestSetName);
    BAIL_ON_WIN_ERROR(error);

    error = TestGetNetUserDelTestSet(pszTestSetName,
                                       &pTestSet,
                                       &dwNumTests);
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < dwNumTests; i++)
    {
        if (pTestSet[i].pszUserName)
        {
            error = LwMbsToWc16s(pTestSet[i].pszUserName,
                                   &pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        dwExpectedError = pTestSet[i].dwError;

        if (pTestSet[i].bPrepare)
        {
            error = EnsureUserAccount(
                                   Server->Hostname,
                                   pwszUsername,
                                   &bCreated);
            BAIL_ON_WIN_ERROR(error);
        }

        ret &= CallNetUserDel(Server->Hostname,
                              pwszUsername,
                              dwExpectedError);

        if (pTestSet[i].bCleanup && bCreated)
        {
            error = EnsureUserAccount(
                                   Server->Hostname,
                                   pwszUsername,
                                   &bCreated);
            BAIL_ON_WIN_ERROR(error);
        }

        LW_SAFE_FREE_MEMORY(pwszUsername);

        pwszUsername = NULL;
    }

error:
    LW_SAFE_FREE_MEMORY(pwszUsername);

    return (ret &&
            error == ERROR_SUCCESS);
}


static
DWORD
TestNetUserGetInfo(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const PSTR pszDefaultUsername = "TestUser";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PWSTR pwszUsername = NULL;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1, 2, 20 };
    DWORD dwNumLevels = 0;
    PDWORD pdwLevels = NULL;
    DWORD i = 0;
    DWORD dwLevel = 0;
    BOOLEAN bCreated = FALSE;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &pwszUsername, &pszDefaultUsername);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        error = EnsureUserAccount(
                            Server->Hostname,
                            pwszUsername,
                            &bCreated);
        BAIL_ON_WIN_ERROR(error);

        if (bCreated)
        {
            error = NetUserDel(Server->Hostname, pwszUsername);
            if (error == ERROR_NO_SUCH_USER)
            {
                error = ERROR_SUCCESS;
            }
            else
            {
                BAIL_ON_WIN_ERROR(error);
            }
        }
    }

error:
    LW_SAFE_FREE_MEMORY(pwszUsername);

    return ret;
}


static
DWORD
TestNetUserSetInfo(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultTestSetName = "validation";

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    PSTR pszTestSetName = NULL;
    PTEST_NET_USER_SET_INFO pTestSet = NULL;
    DWORD dwNumTests = 0;
    DWORD dwLevel = 0;
    DWORD i = 0;
    PWSTR pwszUsername = NULL;
    PWSTR pwszFullName = NULL;
    PWSTR pwszChangedUsername = NULL;
    PWSTR pwszDescription = NULL;
    PWSTR pwszComment = NULL;
    PWSTR pwszHomedir = NULL;
    PWSTR pwszScriptPath = NULL;
    PWSTR pwszPassword = NULL;
    DWORD dwFlags = 0;
    DWORD dwPrivilege = 0;
    DWORD dwExpectedError = 0;
    DWORD dwExpectedParmError = 0;
    BOOLEAN bCreated = FALSE;
    BOOLEAN bRenamed = FALSE;
    PVOID *ppUserInfo = NULL;
    DWORD dwNumUserInfos = 0;
    DWORD iUserInfo = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "testset", PT_CSTRING,
                       &pszTestSetName, &pszDefaultTestSetName);
    BAIL_ON_WIN_ERROR(error);

    error = TestGetNetUserSetInfoTestSet(
                            pszTestSetName,
                            &pTestSet,
                            &dwNumTests);
    BAIL_ON_WIN_ERROR(error);

    for (i = 0; i < dwNumTests; i++)
    {
        dwLevel = pTestSet[i].dwLevel;

        if (pTestSet[i].pszUserName)
        {
            error = LwMbsToWc16s(
                               pTestSet[i].pszUserName,
                               &pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].bPrepare)
        {
            error = EnsureUserAccount(
                               Server->Hostname,
                               pwszUsername,
                               &bCreated);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszFullName)
        {
            error = LwMbsToWc16s(
                             pTestSet[i].pszFullName,
                             &pwszFullName);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszComment)
        {
            error = LwMbsToWc16s(
                             pTestSet[i].pszComment,
                             &pwszComment);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszHomeDir)
        {
            error = LwMbsToWc16s(
                             pTestSet[i].pszHomeDir,
                             &pwszHomedir);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszScriptPath)
        {
            error = LwMbsToWc16s(
                             pTestSet[i].pszScriptPath,
                             &pwszScriptPath);
            BAIL_ON_WIN_ERROR(error);
        }

        if (pTestSet[i].pszPassword)
        {
            error = LwMbsToWc16s(
                             pTestSet[i].pszPassword,
                             &pwszPassword);
            BAIL_ON_WIN_ERROR(error);
        }

        dwFlags             = pTestSet[i].dwFlags;
        dwPrivilege         = pTestSet[i].dwPrivilege;
        dwExpectedError     = pTestSet[i].dwError;
        dwExpectedParmError = pTestSet[i].dwParmError;

        bRet &= CallNetUserSetInfo(Server->Hostname,
                                   dwLevel,
                                   pwszUsername,
                                   pwszFullName,
                                   pwszDescription,
                                   pwszComment,
                                   pwszHomedir,
                                   pwszScriptPath,
                                   pwszPassword,
                                   dwFlags,
                                   dwPrivilege,
                                   dwExpectedError,
                                   dwExpectedParmError,
                                   &bRenamed);

        bRet &= CallNetUserGetInfo(Server->Hostname,
                                   pwszUsername,
                                   &ppUserInfo,
                                   &dwNumUserInfos);

        bRet &= TestVerifyUserInfo(pwszUsername,
                                   pwszFullName,
                                   pwszDescription,
                                   pwszComment,
                                   pwszHomedir,
                                   pwszScriptPath,
                                   pwszPassword,
                                   dwFlags,
                                   dwPrivilege,
                                   ppUserInfo);

        if (pTestSet[i].bCleanup)
        {
            error = CleanupAccount(
                               Server->Hostname,
                               pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }

        for (iUserInfo = 0;
             iUserInfo < dwNumUserInfos;
             iUserInfo++)
        {
            NetApiBufferFree(ppUserInfo[iUserInfo]);
            ppUserInfo[iUserInfo] = NULL;
        }

        LW_SAFE_FREE_MEMORY(pwszUsername);
        LW_SAFE_FREE_MEMORY(pwszFullName);
        LW_SAFE_FREE_MEMORY(pwszDescription);
        LW_SAFE_FREE_MEMORY(pwszComment);
        LW_SAFE_FREE_MEMORY(pwszHomedir);
        LW_SAFE_FREE_MEMORY(pwszScriptPath);
        LW_SAFE_FREE_MEMORY(pwszPassword);

        pwszUsername    = NULL;
        pwszFullName    = NULL;
        pwszDescription = NULL;
        pwszComment     = NULL;
        pwszHomedir     = NULL;
        pwszScriptPath  = NULL;
        pwszPassword    = NULL;
    }

error:
    for (iUserInfo = 0;
         iUserInfo < dwNumUserInfos;
         iUserInfo++)
    {
        NetApiBufferFree(ppUserInfo[iUserInfo]);
    }

    LW_SAFE_FREE_MEMORY(pwszUsername);
    LW_SAFE_FREE_MEMORY(pwszFullName);
    LW_SAFE_FREE_MEMORY(pwszDescription);
    LW_SAFE_FREE_MEMORY(pwszComment);
    LW_SAFE_FREE_MEMORY(pwszHomedir);
    LW_SAFE_FREE_MEMORY(pwszScriptPath);
    LW_SAFE_FREE_MEMORY(pwszPassword);
    LW_SAFE_FREE_MEMORY(pwszChangedUsername);

    return (bRet &&
            error == ERROR_SUCCESS);
}


static
DWORD
TestNetUserGetLocalGroups(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const PSTR pszDefaultUsername = "TestUser";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PWSTR pwszUsername = NULL;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0 };
    DWORD dwNumLevels = 0;
    PDWORD pdwLevels = NULL;
    DWORD i = 0;
    DWORD dwLevel = 0;
    BOOLEAN bCreated = FALSE;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &pwszUsername, &pszDefaultUsername);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        error = EnsureUserAccount(
                            Server->Hostname,
                            pwszUsername,
                            &bCreated);
        BAIL_ON_WIN_ERROR(error);

        ret &= CallNetUserGetLocalGroups(Server->Hostname,
                                         pwszUsername,
                                         dwLevel,
                                         0);
        if (bCreated)
        {
            error = CleanupAccount(
                                Server->Hostname,
                                pwszUsername);
            BAIL_ON_WIN_ERROR(error);
        }
    }

error:
    LW_SAFE_FREE_MEMORY(pwszUsername);

    return ret;
}


static
DWORD
TestNetJoinDomain(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const char *def_accountou = NULL;
    const int def_rejoin = 0;
    const int def_create = 1;
    const int def_deferspn = 0;

    DWORD error = ERROR_SUCCESS;
    PWSTR username = NULL;
    PWSTR password = NULL;
    PWSTR domain_name = NULL;
    PWSTR dnsDomainName = NULL;
    PWSTR accountou = NULL;
    PWSTR joined_domain_name = NULL;
    PWSTR machine_account = NULL;
    PWSTR machine_password = NULL;
    int rejoin, create, deferspn;
    int opts;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &username, NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "password", PT_WC16STRING,
                       &password, NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "domainname", PT_WC16STRING,
                       &domain_name, NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "accountou", PT_WC16STRING,
                       &accountou, &def_accountou);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "rejoin", PT_INT32,
                       &rejoin, &def_rejoin);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "create", PT_INT32,
                       &create, &def_create);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "deferspn", PT_INT32,
                       &deferspn, &def_deferspn);

    opts = NETSETUP_JOIN_DOMAIN;
    if (create) opts |= NETSETUP_ACCT_CREATE;
    if (rejoin) opts |= NETSETUP_DOMAIN_JOIN_IF_JOINED;
    if (deferspn) opts |= NETSETUP_DEFER_SPN_SET;

    CALL_WIN_API(error, NetJoinDomain(
                                 Server->Hostname,
                                 domain_name,
                                 accountou,
                                 username,
                                 password,
                                 opts));
    BAIL_ON_WIN_ERROR(error);

    error = GetMachinePassword(
                    &dnsDomainName,
                    &joined_domain_name,
                    &machine_account,
                    &machine_password,
                    NULL);
    BAIL_ON_WIN_ERROR(error);

error:
    LW_SAFE_FREE_MEMORY(username);
    LW_SECURE_FREE_WSTRING(password);
    LW_SAFE_FREE_MEMORY(domain_name);
    LW_SAFE_FREE_MEMORY(accountou);
    LW_SAFE_FREE_MEMORY(joined_domain_name);
    LW_SAFE_FREE_MEMORY(machine_account);
    LW_SECURE_FREE_WSTRING(machine_password);

    return (error == ERROR_SUCCESS);
}


static
DWORD
TestNetUnjoinDomain(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const int def_disable = 1;

    NTSTATUS status = STATUS_SUCCESS;
    DWORD error = ERROR_SUCCESS;
    wchar16_t *username, *password;
    int disable;
    int opts;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &username, NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "password", PT_WC16STRING,
                       &password, NULL);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "disable", PT_INT32,
                       &disable, &def_disable);
    BAIL_ON_WIN_ERROR(error);

    opts = (disable) ? NETSETUP_ACCT_DELETE : 0;

    CALL_WIN_API(error, NetUnjoinDomain(
                                NULL,
                                username,
                                password,
                                opts));
    BAIL_ON_WIN_ERROR(error);

error:
    LW_SAFE_FREE_MEMORY(username);
    LW_SAFE_FREE_MEMORY(password);

    return (error == ERROR_SUCCESS &&
            status == STATUS_SUCCESS);
}


static
DWORD
TestNetUserChangePassword(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const char *defusername = "TestUser";
    const char *defoldpass = "";
    const char *defnewpass = "newpassword";

    DWORD error = ERROR_SUCCESS;
    NTSTATUS status = STATUS_SUCCESS;
    wchar16_t *username, *oldpassword, *newpassword;
    BOOLEAN bCreated = FALSE;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &username, &defusername);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "oldpassword", PT_WC16STRING,
                       &oldpassword, &defoldpass);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "newpassword", PT_WC16STRING,
                       &newpassword, &defnewpass);
    BAIL_ON_WIN_ERROR(error);

    error = EnsureUserAccount(
                        Server->Hostname,
                        username,
                        &bCreated);
    BAIL_ON_WIN_ERROR(error);

    CALL_WIN_API(error, NetUserChangePassword(
                                   Server->Hostname,
                                   username,
                                   oldpassword,
                                   newpassword));
    BAIL_ON_WIN_ERROR(error);

error:
    LW_SAFE_FREE_MEMORY(username);
    LW_SAFE_FREE_MEMORY(oldpassword);
    LW_SAFE_FREE_MEMORY(newpassword);

    return (error == ERROR_SUCCESS &&
            status == STATUS_SUCCESS);
}


static
DWORD
TestNetLocalGroupEnum(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        ret &= CallNetLocalGroupEnum(Server->Hostname,
                                     dwLevel);
    }

error:
    return ret;
}


static
DWORD
TestNetLocalGroupAdd(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultAliasname = "TestLocalGroup";
    PCSTR pszDefaultComment = "Test comment for new local group";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    DWORD error = ERROR_SUCCESS;
    BOOLEAN ret = TRUE;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1 };
    DWORD dwLevel = 0;
    PDWORD pdwLevels = NULL;
    DWORD i = 0;
    DWORD dwNumLevels = 0;
    PWSTR pwszAliasname = NULL;
    PWSTR pwszComment = NULL;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "aliasname", PT_WC16STRING,
                       &pwszAliasname, &pszDefaultAliasname);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "comment", PT_WC16STRING,
                       &pwszComment, &pszDefaultComment);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        error = CleanupLocalGroup(
                              Server->Hostname,
                              pwszAliasname);
        BAIL_ON_WIN_ERROR(error);

        ret &= CallNetLocalGroupAdd(Server->Hostname,
                                    dwLevel,
                                    pwszAliasname,
                                    pwszComment);
    }

    error = CleanupLocalGroup(
                          Server->Hostname,
                          pwszAliasname);
    BAIL_ON_WIN_ERROR(error);

error:
    LW_SAFE_FREE_MEMORY(pwszAliasname);
    LW_SAFE_FREE_MEMORY(pwszComment);

    return ret;
}


static
DWORD
TestNetLocalGroupDel(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const char *def_aliasname = "TestAlias";

    NTSTATUS status = STATUS_SUCCESS;
    DWORD error = ERROR_SUCCESS;
    wchar16_t *aliasname;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "aliasname", PT_WC16STRING, &aliasname,
                       &def_aliasname);
    BAIL_ON_WIN_ERROR(error);

    error = NetLocalGroupDel(Server->Hostname, aliasname);
    BAIL_ON_WIN_ERROR(error);

error:
    LW_SAFE_FREE_MEMORY(aliasname);

    return (error == ERROR_SUCCESS &&
            status == STATUS_SUCCESS);
}


static
DWORD
TestNetLocalGroupGetInfo(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const PSTR pszDefaultAliasname = "TestLocalGroup";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    PWSTR pwszAliasname = NULL;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 1 };
    DWORD dwNumLevels = 0;
    PDWORD pdwLevels = NULL;
    DWORD i = 0;
    DWORD dwLevel = 0;
    BOOLEAN bCreated = FALSE;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "aliasname", PT_WC16STRING,
                       &pwszAliasname, &pszDefaultAliasname);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        error = EnsureLocalGroup(
                          Server->Hostname,
                          pwszAliasname,
                          &bCreated);
        BAIL_ON_WIN_ERROR(error);

        ret &= CallNetLocalGroupGetInfo(
                          Server->Hostname,
                          pwszAliasname,
                          dwLevel);

        if (bCreated)
        {
            error = CleanupLocalGroup(
                                Server->Hostname,
                                pwszAliasname);
            BAIL_ON_WIN_ERROR(error);
        }
    }

error:
    LW_SAFE_FREE_MEMORY(pwszAliasname);

    return ret;
}


static
DWORD
TestNetLocalGroupSetInfo(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultAliasname = "TestLocalGroup";
    PCSTR pszChangedAliasname = "TestLocalGroupRename";
    PCSTR pszDefaultComment = "Test comment for local group";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1, 1002 };
    DWORD dwLevel = 0;
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    PWSTR pwszAliasname = NULL;
    PWSTR pwszChangedAliasname = NULL;
    PWSTR pwszComment = NULL;
    DWORD i = 0;
    BOOLEAN bCreated = FALSE;
    BOOLEAN bRenamed = FALSE;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "aliasname", PT_WC16STRING,
                       &pwszAliasname, &pszDefaultAliasname);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "comment", PT_WC16STRING,
                       &pwszComment, &pszDefaultComment);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    error = LwMbsToWc16s(
                     pszChangedAliasname,
                     &pwszChangedAliasname);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        error = EnsureLocalGroup(
                           Server->Hostname,
                           pwszAliasname,
                           &bCreated);
        BAIL_ON_WIN_ERROR(error);

        error = CleanupLocalGroup(
                           Server->Hostname,
                           pwszChangedAliasname);
        BAIL_ON_WIN_ERROR(error);

        ret &= CallNetLocalGroupSetInfo(Server->Hostname,
                                        dwLevel,
                                        pwszAliasname,
                                        pwszChangedAliasname,
                                        pwszComment,
                                        &bRenamed);

        if (bRenamed)
        {
            ret &= CallNetLocalGroupSetInfo(Server->Hostname,
                                            0,
                                            pwszChangedAliasname,
                                            pwszAliasname,
                                            NULL,
                                            &bRenamed);

            /* account has been renamed back to original name */
            bRenamed = FALSE;
        }
    }

    if (bCreated)
    {
        error = CleanupLocalGroup(
                             Server->Hostname,
                             pwszAliasname);
        BAIL_ON_WIN_ERROR(error);
    }

error:
    LW_SAFE_FREE_MEMORY(pwszAliasname);
    LW_SAFE_FREE_MEMORY(pwszComment);
    LW_SAFE_FREE_MEMORY(pwszChangedAliasname);

    return ret;
}


static
DWORD
TestNetLocalGroupGetMembers(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    PCSTR pszDefaultLocalGroupName = "Administrators";
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 3 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;
    PWSTR pwszLocalGroupName = NULL;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "aliasname", PT_WC16STRING,
                       &pwszLocalGroupName, &pszDefaultLocalGroupName);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        ret &= CallNetLocalGroupGetMembers(Server->Hostname,
                                           pwszLocalGroupName,
                                           dwLevel);
    }

error:
    LW_SAFE_FREE_MEMORY(pwszLocalGroupName);

    return ret;
}


static
DWORD
TestNetGetDomainName(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    NTSTATUS status = STATUS_SUCCESS;
    DWORD error = ERROR_SUCCESS;
    wchar16_t *domain_name;

    TESTINFO(pTest, "");

    CALL_WIN_API(error, NetGetDomainName(
                                 Server->Hostname,
                                 &domain_name));
    BAIL_ON_WIN_ERROR(error);

    OUTPUT_ARG_WSTR(domain_name);

error:
    LW_SAFE_FREE_MEMORY(domain_name);

    return (error == ERROR_SUCCESS &&
            status == STATUS_SUCCESS);
}


static
DWORD
TestNetQueryDisplayInformation(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 1, 2, 3 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        ret &= CallNetQueryDisplayInfo(Server->Hostname,
                                       dwLevel);
    }

error:
    return ret;
}


static
DWORD
TestNetWkstaUserEnum(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultLevel = (DWORD)(-1);

    BOOLEAN ret = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        ret &= CallNetWkstaUserEnum(Server->Hostname,
                                    dwLevel);
    }

error:
    return ret;
}


static
DWORD
TestNetSessionEnum(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultLevel = (DWORD)(-1);
    PCSTR pszDefaultName = "(unknown)";

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 0, 1, 2, 10, 502 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;
    PWSTR pwszClientName = NULL;
    PWSTR pwszUserName = NULL;
    PWSTR pwszDefaultName = NULL;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "clientname", PT_WC16STRING,
                       &pwszClientName, &pszDefaultName);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "username", PT_WC16STRING,
                       &pwszUserName, &pszDefaultName);
    BAIL_ON_WIN_ERROR(error);

    error = LwMbsToWc16s(pszDefaultName, &pwszDefaultName);
    BAIL_ON_WIN_ERROR(error);

    if (wc16scmp(pwszClientName, pwszDefaultName) == 0)
    {
        LW_SAFE_FREE_MEMORY(pwszClientName);
        pwszClientName = NULL;
    }

    if (wc16scmp(pwszUserName, pwszDefaultName) == 0)
    {
        LW_SAFE_FREE_MEMORY(pwszUserName);
        pwszUserName = NULL;
    }

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        bRet &= CallNetSessionEnum(Server->Hostname,
                                   pwszClientName,
                                   pwszUserName,
                                   dwLevel);
    }

error:
    LW_SAFE_FREE_MEMORY(pwszDefaultName);
    LW_SAFE_FREE_MEMORY(pwszClientName);
    LW_SAFE_FREE_MEMORY(pwszUserName);

    return bRet;
}


static
DWORD
TestNetServerEnum(
    PTEST         pTest,
    PRPCSERVER    Server,
    PCREDENTIALS  pCreds,
    PPARAMETER    pOptions,
    DWORD         dwOptcount
    )
{
    const DWORD dwDefaultLevel = (DWORD)(-1);
    PCSTR pszDefaultName = "(unknown)";

    BOOLEAN bRet = TRUE;
    DWORD error = ERROR_SUCCESS;
    DWORD i = 0;
    DWORD dwSelectedLevels[] = { 0 };
    DWORD dwAvailableLevels[] = { 100, 101 };
    PDWORD pdwLevels = NULL;
    DWORD dwNumLevels = 0;
    DWORD dwLevel = 0;
    PWSTR pwszDefaultName = NULL;
    PWSTR pwszDomainName = NULL;

    TESTINFO(pTest, "");

    error = RpctFetchParameterValue(pOptions, dwOptcount, "level", PT_UINT32,
                       (UINT32*)&dwLevel, (UINT32*)&dwDefaultLevel);
    BAIL_ON_WIN_ERROR(error);

    error = RpctFetchParameterValue(pOptions, dwOptcount, "domainname", PT_WC16STRING,
                       &pwszDomainName, &pszDefaultName);
    BAIL_ON_WIN_ERROR(error);

    error = LwMbsToWc16s(pszDefaultName, &pwszDefaultName);
    BAIL_ON_WIN_ERROR(error);

    if (wc16scmp(pwszDomainName, pwszDefaultName) == 0)
    {
        LW_SAFE_FREE_MEMORY(pwszDomainName);
        pwszDomainName = NULL;
    }

    if (dwLevel == (DWORD)(-1))
    {
        pdwLevels   = dwAvailableLevels;
        dwNumLevels = sizeof(dwAvailableLevels)/sizeof(dwAvailableLevels[0]);
    }
    else
    {
        dwSelectedLevels[0] = dwLevel;
        pdwLevels   = dwSelectedLevels;
        dwNumLevels = sizeof(dwSelectedLevels)/sizeof(dwSelectedLevels[0]);
    }

    for (i = 0; i < dwNumLevels; i++)
    {
        dwLevel = pdwLevels[i];

        bRet &= CallNetServerEnum(Server->Hostname,
                                  pwszDomainName,
                                  dwLevel);
    }

error:
    LW_SAFE_FREE_MEMORY(pwszDefaultName);
    LW_SAFE_FREE_MEMORY(pwszDomainName);

    return bRet;
}


VOID
SetupNetApiTests(PTEST t)
{
    DWORD error = ERROR_SUCCESS;

    error = SrvSvcInitMemory();
    if (error) return;

    AddTest(t, "NETAPI-USER-ENUM", TestNetUserEnum);
    AddTest(t, "NETAPI-USER-ADD", TestNetUserAdd);
    AddTest(t, "NETAPI-USER-DEL", TestNetUserDel);
    AddTest(t, "NETAPI-USER-GETINFO", TestNetUserGetInfo);
    AddTest(t, "NETAPI-USER-SETINFO", TestNetUserSetInfo);
    AddTest(t, "NETAPI-USER-GET-LOCAL-GROUPS", TestNetUserGetLocalGroups);
    AddTest(t, "NETAPI-JOIN-DOMAIN", TestNetJoinDomain);
    AddTest(t, "NETAPI-UNJOIN-DOMAIN", TestNetUnjoinDomain);
    AddTest(t, "NETAPI-USER-CHANGE-PASSWORD", TestNetUserChangePassword);
    AddTest(t, "NETAPI-GET-DOMAIN-NAME", TestNetGetDomainName);
    AddTest(t, "NETAPI-LOCAL-GROUP-ENUM", TestNetLocalGroupEnum);
    AddTest(t, "NETAPI-LOCAL-GROUP-ADD", TestNetLocalGroupAdd);
    AddTest(t, "NETAPI-LOCAL-GROUP-DEL", TestNetLocalGroupDel);
    AddTest(t, "NETAPI-LOCAL-GROUP-GETINFO", TestNetLocalGroupGetInfo);
    AddTest(t, "NETAPI-LOCAL-GROUP-SETINFO", TestNetLocalGroupSetInfo);
    AddTest(t, "NETAPI-LOCAL-GROUP-MEMBERS", TestNetLocalGroupGetMembers);
    AddTest(t, "NETAPI-QUERY-DISP-INFO", TestNetQueryDisplayInformation);
    AddTest(t, "NETAPI-WKSTA-USER-ENUM", TestNetWkstaUserEnum);
    AddTest(t, "NETAPI-SESSION-ENUM", TestNetSessionEnum);
    AddTest(t, "NETAPI-SERVER-ENUM", TestNetServerEnum);
}


/*
local variables:
mode: c
c-basic-offset: 4
indent-tabs-mode: nil
tab-width: 4
end:
*/
