mapi32/tests: Use win_skip() to skip over unimplemented functionality.
[wine] / dlls / mapi32 / tests / util.c
1 /*
2  * Unit test suite for MAPI utility 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 #include "wine/test.h"
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "winerror.h"
26 #include "winnt.h"
27 #include "mapiutil.h"
28 #include "mapitags.h"
29
30 static HMODULE hMapi32 = 0;
31
32 static SCODE (WINAPI *pScInitMapiUtil)(ULONG);
33 static void  (WINAPI *pSwapPword)(PUSHORT,ULONG);
34 static void  (WINAPI *pSwapPlong)(PULONG,ULONG);
35 static void  (WINAPI *pHexFromBin)(LPBYTE,int,LPWSTR);
36 static void  (WINAPI *pFBinFromHex)(LPWSTR,LPBYTE);
37 static UINT  (WINAPI *pUFromSz)(LPCSTR);
38 static ULONG (WINAPI *pUlFromSzHex)(LPCSTR);
39 static ULONG (WINAPI *pCbOfEncoded)(LPCSTR);
40 static BOOL  (WINAPI *pIsBadBoundedStringPtr)(LPCSTR,ULONG);
41
42 static void test_SwapPword(void)
43 {
44     USHORT shorts[3];
45
46     pSwapPword = (void*)GetProcAddress(hMapi32, "SwapPword@8");
47     if (!pSwapPword)
48         return;
49
50     shorts[0] = 0xff01;
51     shorts[1] = 0x10ff;
52     shorts[2] = 0x2001;
53     pSwapPword(shorts, 2);
54     ok((shorts[0] == 0x01ff && shorts[1] == 0xff10 && shorts[2] == 0x2001) ||
55        broken(shorts[0] == 0xff01 && shorts[1] == 0x10ff && shorts[2] == 0x2001) /* Win9x */,
56        "Expected {0x01ff,0xff10,0x2001}, got {0x%04x,0x%04x,0x%04x}\n",
57        shorts[0], shorts[1], shorts[2]);
58 }
59
60 static void test_SwapPlong(void)
61 {
62     ULONG longs[3];
63
64     pSwapPlong = (void*)GetProcAddress(hMapi32, "SwapPlong@8");
65     if (!pSwapPlong)
66         return;
67
68     longs[0] = 0xffff0001;
69     longs[1] = 0x1000ffff;
70     longs[2] = 0x20000001;
71     pSwapPlong(longs, 2);
72     ok((longs[0] == 0x0100ffff && longs[1] == 0xffff0010 && longs[2] == 0x20000001) ||
73        broken(longs[0] == 0xffff0001 && longs[1] == 0x1000ffff && longs[2] == 0x20000001) /* Win9x */,
74        "Expected {0x0100ffff,0xffff0010,0x20000001}, got {0x%08x,0x%08x,0x%08x}\n",
75        longs[0], longs[1], longs[2]);
76 }
77
78 static void test_HexFromBin(void)
79 {
80     static char res[] =       { "000102030405060708090A0B0C0D0E0F101112131415161"
81       "718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B"
82       "3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F6"
83       "06162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F8081828384"
84       "85868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A"
85       "9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCD"
86       "CECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F"
87       "2F3F4F5F6F7F8F9FAFBFCFDFE\0X" };
88     BYTE data[255];
89     WCHAR strw[256];
90     BOOL bOk;
91     int i;
92
93     pHexFromBin = (void*)GetProcAddress(hMapi32, "HexFromBin@12");
94     pFBinFromHex = (void*)GetProcAddress(hMapi32, "FBinFromHex@8");
95     if (!pHexFromBin || !pFBinFromHex)
96         return;
97
98     for (i = 0; i < 255; i++)
99         data[i] = i;
100     memset(strw, 'X', sizeof(strw));
101     pHexFromBin(data, sizeof(data), strw);
102
103     ok(memcmp(strw, res, sizeof(res) - 1) == 0, "HexFromBin: Result differs\n");
104
105     memset(data, 0, sizeof(data));
106     pFBinFromHex((LPWSTR)res, data);
107     bOk = TRUE;
108     for (i = 0; i < 255; i++)
109         if (data[i] != i)
110             bOk = FALSE;
111     ok(bOk == TRUE, "FBinFromHex: Result differs\n");
112 }
113
114 static void test_UFromSz(void)
115 {
116     pUFromSz = (void*)GetProcAddress(hMapi32, "UFromSz@4");
117     if (!pUFromSz)
118         return;
119
120     ok(pUFromSz("105679") == 105679u,
121        "UFromSz: expected 105679, got %d\n", pUFromSz("105679"));
122
123     ok(pUFromSz(" 4") == 0, "UFromSz: exected 0. got %d\n",
124        pUFromSz(" 4"));
125 }
126
127 static void test_UlFromSzHex(void)
128 {
129     pUlFromSzHex = (void*)GetProcAddress(hMapi32, "UlFromSzHex@4");
130     if (!pUlFromSzHex)
131         return;
132
133     ok(pUlFromSzHex("fF") == 0xffu,
134        "UlFromSzHex: expected 0xff, got 0x%x\n", pUlFromSzHex("fF"));
135
136     ok(pUlFromSzHex(" c") == 0, "UlFromSzHex: exected 0x0. got 0x%x\n",
137        pUlFromSzHex(" c"));
138 }
139
140 static void test_CbOfEncoded(void)
141 {
142     char buff[129];
143     unsigned int i;
144
145     pCbOfEncoded = (void*)GetProcAddress(hMapi32, "CbOfEncoded@4");
146     if (!pCbOfEncoded)
147         return;
148
149     for (i = 0; i < sizeof(buff) - 1; i++)
150     {
151         ULONG ulRet, ulExpected = (((i | 3) >> 2) + 1) * 3;
152
153         memset(buff, '\0', sizeof(buff));
154         memset(buff, '?', i);
155         ulRet = pCbOfEncoded(buff);
156         ok(ulRet == ulExpected || broken(ulRet == 0) /* Win9x */,
157            "CbOfEncoded(length %d): expected %d, got %d\n",
158            i, ulExpected, ulRet);
159     }
160 }
161
162 static void test_IsBadBoundedStringPtr(void)
163 {
164     pIsBadBoundedStringPtr = (void*)GetProcAddress(hMapi32, "IsBadBoundedStringPtr@8");
165     if (!pIsBadBoundedStringPtr)
166         return;
167
168     ok(pIsBadBoundedStringPtr(NULL, 0) == TRUE, "IsBadBoundedStringPtr: expected TRUE\n");
169     ok(pIsBadBoundedStringPtr("TEST", 4) == TRUE, "IsBadBoundedStringPtr: expected TRUE\n");
170     ok(pIsBadBoundedStringPtr("TEST", 5) == FALSE, "IsBadBoundedStringPtr: expected FALSE\n");
171 }
172
173 START_TEST(util)
174 {
175     SCODE ret;
176
177     hMapi32 = LoadLibraryA("mapi32.dll");
178
179     pScInitMapiUtil = (void*)GetProcAddress(hMapi32, "ScInitMapiUtil@4");
180
181     if (!pScInitMapiUtil)
182     {
183         win_skip("ScInitMapiUtil is not available\n");
184         FreeLibrary(hMapi32);
185         return;
186     }
187
188     SetLastError(0xdeadbeef);
189     ret = pScInitMapiUtil(0);
190     if ((ret != S_OK) && (GetLastError() == ERROR_PROC_NOT_FOUND))
191     {
192         win_skip("ScInitMapiUtil is not implemented\n");
193         FreeLibrary(hMapi32);
194         return;
195     }
196
197     test_SwapPword();
198     test_SwapPlong();
199     test_HexFromBin();
200     test_UFromSz();
201     test_UlFromSzHex();
202     test_CbOfEncoded();
203     test_IsBadBoundedStringPtr();
204
205     FreeLibrary(hMapi32);
206 }