ole32: Fix memory leaks in the storage test.
[wine] / dlls / mapi32 / tests / imalloc.c
1 /*
2  * Unit test suite for MAPI IMalloc functions
3  *
4  * Copyright 2004 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winerror.h"
27 #include "winnt.h"
28 #include "mapiutil.h"
29 #include "mapi32_test.h"
30
31 static HMODULE hMapi32 = 0;
32
33 static SCODE    (WINAPI *pScInitMapiUtil)(ULONG);
34 static LPMALLOC (WINAPI *pMAPIGetDefaultMalloc)(void);
35
36 static void test_IMalloc(void)
37 {
38     LPVOID lpMem;
39     ULONG ulRef;
40     int iRet;
41     HRESULT hRet;
42     LPMALLOC lpMalloc;
43     LPVOID lpVoid;
44
45     pMAPIGetDefaultMalloc = (void*)GetProcAddress(hMapi32,
46                                                   "MAPIGetDefaultMalloc@0");
47     if (!pMAPIGetDefaultMalloc)
48         return;
49
50     lpMalloc = pMAPIGetDefaultMalloc();
51     if (!lpMalloc)
52         return;
53
54     lpVoid = NULL;
55     hRet = IMalloc_QueryInterface(lpMalloc, &IID_IUnknown, &lpVoid);
56     ok (hRet == S_OK && lpVoid != NULL,
57         "IID_IUnknown: expected S_OK, non-null, got 0x%08x, %p\n",
58         hRet, lpVoid);
59
60     lpVoid = NULL;
61     hRet = IMalloc_QueryInterface(lpMalloc, &IID_IMalloc, &lpVoid);
62     ok (hRet == S_OK && lpVoid != NULL,
63         "IID_IIMalloc: expected S_OK, non-null, got 0x%08x, %p\n",
64         hRet, lpVoid);
65
66     /* Prove that native mapi uses LocalAlloc/LocalFree */
67     lpMem = IMalloc_Alloc(lpMalloc, 61);
68     ok (lpMem && IMalloc_GetSize(lpMalloc, lpMem) == LocalSize(lpMem),
69         "Expected non-null, same size, got %p, %s size\n", lpMem,
70         lpMem ? "different" : "same");
71
72     iRet = IMalloc_DidAlloc(lpMalloc, lpMem);
73     ok (iRet == -1, "DidAlloc, expected -1. got %d\n", iRet);
74
75     IMalloc_HeapMinimize(lpMalloc);
76  
77     LocalFree(lpMem);
78  
79     ulRef = IMalloc_AddRef(lpMalloc);
80     ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef); 
81     
82     ulRef = IMalloc_Release(lpMalloc);
83     ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef);
84
85     IMalloc_Release(lpMalloc);
86 }
87
88 START_TEST(imalloc)
89 {
90     SCODE ret;
91
92     if (!HaveDefaultMailClient())
93     {
94         win_skip("No default mail client installed\n");
95         return;
96     }
97
98     hMapi32 = LoadLibraryA("mapi32.dll");
99
100     pScInitMapiUtil = (void*)GetProcAddress(hMapi32, "ScInitMapiUtil@4");
101     if (!pScInitMapiUtil)
102     {
103         win_skip("ScInitMapiUtil is not available\n");
104         FreeLibrary(hMapi32);
105         return;
106     }
107
108     SetLastError(0xdeadbeef);
109     ret = pScInitMapiUtil(0);
110     if ((ret != S_OK) && (GetLastError() == ERROR_PROC_NOT_FOUND))
111     {
112         win_skip("ScInitMapiUtil is not implemented\n");
113         FreeLibrary(hMapi32);
114         return;
115     }
116     else if ((ret == E_FAIL) && (GetLastError() == ERROR_INVALID_HANDLE))
117     {
118         win_skip("ScInitMapiUtil doesn't work on some Win98 and WinME systems\n");
119         FreeLibrary(hMapi32);
120         return;
121     }
122
123     test_IMalloc();
124
125     FreeLibrary(hMapi32);
126 }