VOID TestStringListRemoveShouldHaveCorrectLayout( VOID) { PCSTR ppszStrings[] = { "Test 1", "Test 2", "Test 3", "Test 4", "Test 5" }; PVMDIR_STRING_LIST pStringList = NULL; DWORD dwError = 0; DWORD i = 0; dwError = VmDirStringListInitialize(&pStringList, 10); ASSERT(dwError == 0); for (i = 0; i < VMDIR_ARRAY_SIZE(ppszStrings); ++i) { dwError = VmDirStringListAdd(pStringList, ppszStrings[i]); ASSERT(dwError == 0); } dwError = VmDirStringListRemove(pStringList, ppszStrings[2]); ASSERT(dwError == 0); ASSERT(pStringList->dwCount == VMDIR_ARRAY_SIZE(ppszStrings) - 1); ASSERT(pStringList->pStringList[0] == ppszStrings[0]); ASSERT(pStringList->pStringList[1] == ppszStrings[1]); ASSERT(pStringList->pStringList[2] == ppszStrings[3]); ASSERT(pStringList->pStringList[3] == ppszStrings[4]); }
DWORD VmDirStringListAddStrClone( PCSTR pszStr, PVMDIR_STRING_LIST pStrList ) { DWORD dwError = 0; PSTR pszLocalStr = NULL; if (!pszStr || !pStrList) { dwError = VMDIR_ERROR_INVALID_PARAMETER; BAIL_ON_VMDIR_ERROR(dwError); } dwError = VmDirAllocateStringA(pszStr, &pszLocalStr); BAIL_ON_VMDIR_ERROR(dwError); dwError = VmDirStringListAdd(pStrList, pszLocalStr); BAIL_ON_VMDIR_ERROR(dwError); pszLocalStr = NULL; cleanup: VMDIR_SAFE_FREE_MEMORY(pszLocalStr); return dwError; error: goto cleanup; }
VOID TestStringListAddLayout( VOID ) { PSTR ppszStrings[5]; DWORD dwError = 0; DWORD i = 0; PVMDIR_STRING_LIST pStringList; dwError = VmDirStringListInitialize(&pStringList, 10); ASSERT(dwError == 0); for (i = 0; i < VMDIR_ARRAY_SIZE(ppszStrings); ++i) { ppszStrings[i] = GenerateString(); dwError = VmDirStringListAdd(pStringList, ppszStrings[i]); ASSERT(dwError == 0); } ASSERT(pStringList->dwCount == VMDIR_ARRAY_SIZE(ppszStrings)); for (i = 0; i < VMDIR_ARRAY_SIZE(ppszStrings); ++i) { ASSERT(pStringList->pStringList[i] == ppszStrings[i]); } VmDirStringListFree(pStringList); }
VOID TestStringListAdd( PVMDIR_STRING_LIST pStringList ) { DWORD dwError = 0; PCSTR pszString = GenerateString(); dwError = VmDirStringListAdd(pStringList, pszString); ASSERT(dwError == 0); ASSERT(VmDirStringListContains(pStringList, pszString)); }
VOID TestStringListRemoveShouldSucceed( PVMDIR_STRING_LIST pStringList ) { DWORD dwError = 0; PCSTR pszString = GenerateString(); DWORD dwCount = 0; VmDirStringListAdd(pStringList, pszString); ASSERT(dwError == 0); ASSERT(VmDirStringListContains(pStringList, pszString)); dwCount = pStringList->dwCount; dwError = VmDirStringListRemove(pStringList, pszString); ASSERT(dwError == 0); ASSERT(!VmDirStringListContains(pStringList, pszString)); ASSERT(dwCount == pStringList->dwCount + 1); }
VOID TestStringListAddWithReallocation( PVMDIR_STRING_LIST pStringList ) { UINT i = 0; DWORD dwMaxSize = 0; DWORD dwError = 0; dwMaxSize = pStringList->dwSize + 5; for (i = pStringList->dwCount; i < dwMaxSize; ++i) { dwError = VmDirStringListAdd( pStringList, GenerateString()); ASSERT(dwError == 0); } ASSERT(pStringList->dwSize > pStringList->dwCount); ASSERT(pStringList->dwSize > dwMaxSize); ASSERT(pStringList->dwCount >= dwMaxSize); }
/* * read one schema element definition from file and normalize its definition. */ static DWORD _VmDirReadOneDefFromFile( FILE* fp, PVMDIR_STRING_LIST pStrList ) { DWORD dwError = 0; size_t iSize = VMDIR_SIZE_9216, iLen = 0; CHAR pDescBuf[VMDIR_SIZE_9216+1] = {0}; CHAR pbuf[VMDIR_SIZE_4096] = {0}; PCSTR pPrefix = "( "; size_t iPrefixLen = VmDirStringLenA(pPrefix); PSTR pOut = NULL; dwError = VmDirStringNCatA( pDescBuf+iLen, iSize-iLen, pPrefix, iPrefixLen); BAIL_ON_VMDIR_ERROR(dwError); iLen += iPrefixLen; while (fgets(pbuf, sizeof(pbuf), fp) != NULL) { size_t len = VmDirStringLenA(pbuf) - 1; if (pbuf[len] == '\n') { pbuf[len] = '\0'; } if ( pbuf[0] == '#') { continue; } if ( pbuf[0] == ' ') { dwError = VmDirStringNCatA( pDescBuf+iLen, iSize-iLen, pbuf, VmDirStringLenA(pbuf)); BAIL_ON_VMDIR_ERROR(dwError); iLen += VmDirStringLenA(pbuf); } else { break; } } if (pDescBuf[0] != '\0') { VmdDirNormalizeString(pDescBuf); dwError = VmDirAllocateStringA(pDescBuf, &pOut); BAIL_ON_VMDIR_ERROR(dwError); dwError = VmDirStringListAdd(pStrList, pOut); BAIL_ON_VMDIR_ERROR(dwError); pOut = NULL; } cleanup: return dwError; error: VMDIR_SAFE_FREE_MEMORY(pOut); goto cleanup; }
DWORD VdcLdapEnumerateObjects( LDAP *pLd, PCSTR pszBase, int ldapScope, PVMDIR_STRING_LIST *ppObjectDNs ) { DWORD dwError = 0; PCSTR ppszAttrs[] = {NULL}; LDAPMessage *pResult = NULL; LDAPMessage* pEntry = NULL; PSTR pszObjectDN = NULL; DWORD iEntryCount = 0; PVMDIR_STRING_LIST pObjectDNs = NULL; dwError = ldap_search_ext_s( pLd, pszBase, ldapScope, "(objectClass=*)", (PSTR*)ppszAttrs, 0, NULL, NULL, NULL, -1, &pResult); BAIL_ON_VMDIR_ERROR(dwError); iEntryCount = ldap_count_entries(pLd, pResult); if (iEntryCount > 0) { dwError = VmDirStringListInitialize(&pObjectDNs, iEntryCount); BAIL_ON_VMDIR_ERROR(dwError); pEntry = ldap_first_entry(pLd, pResult); for (; pEntry != NULL; pEntry = ldap_next_entry(pLd, pEntry)) { assert(pObjectDNs->dwCount < iEntryCount); dwError = VmDirAllocateStringA(ldap_get_dn(pLd, pEntry), &pszObjectDN); BAIL_ON_VMDIR_ERROR(dwError); dwError = VmDirStringListAdd(pObjectDNs, pszObjectDN); BAIL_ON_VMDIR_ERROR(dwError); pszObjectDN = NULL; } } *ppObjectDNs = pObjectDNs; pObjectDNs = NULL; cleanup: VmDirStringListFree(pObjectDNs); VMDIR_SAFE_FREE_STRINGA(pszObjectDN); if (pResult) { ldap_msgfree(pResult); } return dwError; error: goto cleanup; }
DWORD VmDirSchemaAttrIdMapUpdateDB( PVDIR_SCHEMA_ATTR_ID_MAP pAttrIdMap ) { DWORD dwError = 0; DWORD dwNumNewIds = 0; LW_HASHMAP_ITER iter = LW_HASHMAP_ITER_INIT; LW_HASHMAP_PAIR pair = {NULL, NULL}; PSTR pszMapStr = NULL; PVMDIR_STRING_LIST pMapStrList = NULL; VDIR_BACKEND_CTX beCtx = {0}; BOOLEAN bHasTxn = FALSE; if (!pAttrIdMap) { dwError = ERROR_INVALID_PARAMETER; BAIL_ON_VMDIR_ERROR(dwError); } dwNumNewIds = LwRtlHashMapGetCount(pAttrIdMap->pNewIds); if (dwNumNewIds == 0) { // No new Id goto cleanup; } dwError = VmDirStringListInitialize(&pMapStrList, dwNumNewIds); BAIL_ON_VMDIR_ERROR(dwError); while (LwRtlHashMapIterate(pAttrIdMap->pNewIds, &iter, &pair)) { dwError = VmDirAllocateStringPrintf(&pszMapStr, "%d%s%s", (USHORT)(uintptr_t)pair.pValue, SCHEMA_ATTR_ID_MAP_SEP, (PSTR)pair.pKey); BAIL_ON_VMDIR_ERROR(dwError); dwError = VmDirStringListAdd(pMapStrList, pszMapStr); BAIL_ON_VMDIR_ERROR(dwError); } beCtx.pBE = VmDirBackendSelect(PERSISTED_DSE_ROOT_DN); dwError = beCtx.pBE->pfnBETxnBegin(&beCtx, VDIR_BACKEND_TXN_WRITE, &bHasTxn); BAIL_ON_VMDIR_ERROR( dwError ); dwError = beCtx.pBE->pfnBEDupKeySetValues(&beCtx, ATTR_ID_MAP_KEY, pMapStrList); BAIL_ON_VMDIR_ERROR(dwError); if (bHasTxn) { dwError = beCtx.pBE->pfnBETxnCommit(&beCtx); bHasTxn = FALSE; BAIL_ON_VMDIR_ERROR(dwError); } LwRtlHashMapResetIter(&iter); while (LwRtlHashMapIterate(pAttrIdMap->pNewIds, &iter, &pair)) { dwError = LwRtlHashMapInsert(pAttrIdMap->pStoredIds, pair.pKey, pair.pValue, NULL); BAIL_ON_VMDIR_ERROR(dwError); } LwRtlHashMapClear(pAttrIdMap->pNewIds, VmDirNoopHashMapPairFree, NULL); cleanup: VmDirBackendCtxContentFree(&beCtx); VmDirStringListFree(pMapStrList); return dwError; error: if (bHasTxn) { beCtx.pBE->pfnBETxnAbort(&beCtx); } VMDIR_LOG_ERROR( VMDIR_LOG_MASK_ALL, "%s failed, error (%d)", __FUNCTION__, dwError ); goto cleanup; }