wininet: Fix a number of problems with InternetGetCookie.

This version incorporates Lei Zhang's changes to the test, with his consent.
This commit is contained in:
Hans Leidekker 2007-11-14 13:15:29 +01:00 committed by Alexandre Julliard
parent b6ee3c1d6b
commit 5ccfec6428
2 changed files with 44 additions and 31 deletions

View File

@ -258,9 +258,9 @@ static void COOKIE_deleteDomain(cookie_domain *deadDomain)
BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName, BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
LPWSTR lpCookieData, LPDWORD lpdwSize) LPWSTR lpCookieData, LPDWORD lpdwSize)
{ {
BOOL ret;
struct list * cursor; struct list * cursor;
int cnt = 0, domain_count = 0; unsigned int cnt = 0, domain_count = 0, cookie_count = 0;
int cookie_count = 0;
WCHAR hostName[2048], path[2048]; WCHAR hostName[2048], path[2048];
TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName), TRACE("(%s, %s, %p, %p)\n", debugstr_w(lpszUrl),debugstr_w(lpszCookieName),
@ -268,11 +268,13 @@ BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
if (!lpszUrl) if (!lpszUrl)
{ {
SetLastError(ERROR_INTERNET_UNRECOGNIZED_SCHEME); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0])); hostName[0] = 0;
ret = COOKIE_crackUrlSimple(lpszUrl, hostName, sizeof(hostName)/sizeof(hostName[0]), path, sizeof(path)/sizeof(path[0]));
if (!ret || !hostName[0]) return FALSE;
LIST_FOR_EACH(cursor, &domain_list) LIST_FOR_EACH(cursor, &domain_list)
{ {
@ -288,22 +290,29 @@ BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry); cookie *thisCookie = LIST_ENTRY(cursor, cookie, entry);
if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */ if (lpCookieData == NULL) /* return the size of the buffer required to lpdwSize */
{ {
if (cookie_count != 0) unsigned int len;
cnt += 2; /* '; ' */
if (cookie_count) cnt += 2; /* '; ' */
cnt += strlenW(thisCookie->lpCookieName); cnt += strlenW(thisCookie->lpCookieName);
if ((len = strlenW(thisCookie->lpCookieData)))
{
cnt += 1; /* = */ cnt += 1; /* = */
cnt += strlenW(thisCookie->lpCookieData); cnt += len;
}
} }
else else
{ {
static const WCHAR szsc[] = { ';',' ',0 }; static const WCHAR szsc[] = { ';',' ',0 };
static const WCHAR szpseq[] = { '%','s','=','%','s',0 }; static const WCHAR szname[] = { '%','s',0 };
if (cookie_count != 0) static const WCHAR szdata[] = { '=','%','s',0 };
cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szpseq, if (cookie_count) cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szsc);
thisCookie->lpCookieName, cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szname, thisCookie->lpCookieName);
thisCookie->lpCookieData);
TRACE("Cookie: %s=%s\n", debugstr_w(thisCookie->lpCookieName), debugstr_w(thisCookie->lpCookieData)); if (thisCookie->lpCookieData[0])
cnt += snprintfW(lpCookieData + cnt, *lpdwSize - cnt, szdata, thisCookie->lpCookieData);
TRACE("Cookie: %s\n", debugstr_w(lpCookieData));
} }
cookie_count++; cookie_count++;
} }
@ -319,15 +328,14 @@ BOOL WINAPI InternetGetCookieW(LPCWSTR lpszUrl, LPCWSTR lpszCookieName,
if (lpCookieData == NULL) if (lpCookieData == NULL)
{ {
cnt += 1; /* NULL */ *lpdwSize = (cnt + 1) * sizeof(WCHAR);
*lpdwSize = cnt*sizeof(WCHAR); TRACE("returning %u\n", *lpdwSize);
TRACE("returning\n");
return TRUE; return TRUE;
} }
*lpdwSize = (cnt + 1)*sizeof(WCHAR); *lpdwSize = cnt + 1;
TRACE("Returning %i (from %i domains): %s\n", cnt, domain_count, TRACE("Returning %u (from %u domains): %s\n", cnt, domain_count,
debugstr_w(lpCookieData)); debugstr_w(lpCookieData));
return (cnt ? TRUE : FALSE); return (cnt ? TRUE : FALSE);

View File

@ -285,7 +285,7 @@ static void test_null(void)
sz = 0; sz = 0;
r = InternetGetCookieW(NULL, NULL, NULL, &sz); r = InternetGetCookieW(NULL, NULL, NULL, &sz);
ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n"); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n");
ok( r == FALSE, "return wrong\n"); ok( r == FALSE, "return wrong\n");
r = InternetGetCookieW(szServer, NULL, NULL, &sz); r = InternetGetCookieW(szServer, NULL, NULL, &sz);
@ -294,26 +294,31 @@ static void test_null(void)
} }
ok( r == FALSE, "return wrong\n"); ok( r == FALSE, "return wrong\n");
sz = 0;
r = InternetGetCookieW(szUrlEmpty, szServer, NULL, &sz);
ok( r == FALSE, "return wrong\n");
sz = 0; sz = 0;
r = InternetGetCookieW(szUrl, szServer, NULL, &sz); r = InternetGetCookieW(szUrl, szServer, NULL, &sz);
ok( r == TRUE, "return wrong\n"); ok( r == TRUE, "return wrong\n");
todo_wine {
ok( sz == 30, "sz wrong\n"); /* sz is 14 on XP SP2 and beyond, 30 on XP SP1 and before */
} ok( sz == 14 || sz == 30, "sz wrong, got %u, expected 14 or 30\n", sz);
sz = 0x20; sz = 0x20;
memset(buffer, 0, sizeof buffer); memset(buffer, 0, sizeof buffer);
r = InternetGetCookieW(szUrl, szServer, buffer, &sz); r = InternetGetCookieW(szUrl, szServer, buffer, &sz);
ok( r == TRUE, "return wrong\n"); ok( r == TRUE, "return wrong\n");
todo_wine {
ok( sz == lstrlenW(buffer), "sz wrong\n"); /* sz == lstrlenW(buffer) only in XP SP1 */
ok( !lstrcmpW(szExpect, buffer), "cookie data wrong\n"); ok( sz == 1 + lstrlenW(buffer), "sz wrong\n");
}
/* before XP SP2, buffer is "server; server" */
ok( !lstrcmpW(szExpect, buffer) || !lstrcmpW(szServer, buffer), "cookie data wrong\n");
sz = sizeof(buffer); sz = sizeof(buffer);
r = InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, buffer, &sz); r = InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, buffer, &sz);
ok(r == TRUE, "ret %d\n", r); ok(r == TRUE, "ret %d\n", r);
} }
/* ############################### */ /* ############################### */