gdi32/tests: Start of a framework for writing dib driver tests.
[wine] / dlls / gdi32 / tests / dib.c
1 /*
2  * DIB driver tests.
3  *
4  * Copyright 2011 Huw Davies
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 <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "wincrypt.h"
29
30 #include "wine/test.h"
31
32 static HCRYPTPROV crypt_prov;
33
34 static const char *sha1_graphics_a8r8g8b8[] =
35 {
36     "a3cadd34d95d3d5cc23344f69aab1c2e55935fcf",
37     NULL
38 };
39
40 static inline DWORD get_stride(BITMAPINFO *bmi)
41 {
42     return ((bmi->bmiHeader.biBitCount * bmi->bmiHeader.biWidth + 31) >> 3) & ~3;
43 }
44
45 static inline DWORD get_dib_size(BITMAPINFO *bmi)
46 {
47     return get_stride(bmi) * abs(bmi->bmiHeader.biHeight);
48 }
49
50 static char *hash_dib(BITMAPINFO *bmi, void *bits)
51 {
52     DWORD dib_size = get_dib_size(bmi);
53     HCRYPTHASH hash;
54     char *buf;
55     BYTE hash_buf[20];
56     DWORD hash_size = sizeof(hash_buf);
57     int i;
58     static const char *hex = "0123456789abcdef";
59
60     if(!crypt_prov) return NULL;
61
62     if(!CryptCreateHash(crypt_prov, CALG_SHA1, 0, 0, &hash)) return NULL;
63
64     CryptHashData(hash, bits, dib_size, 0);
65
66     CryptGetHashParam(hash, HP_HASHVAL, NULL, &hash_size, 0);
67     if(hash_size != sizeof(hash_buf)) return NULL;
68
69     CryptGetHashParam(hash, HP_HASHVAL, hash_buf, &hash_size, 0);
70     CryptDestroyHash(hash);
71
72     buf = HeapAlloc(GetProcessHeap(), 0, hash_size * 2 + 1);
73
74     for(i = 0; i < hash_size; i++)
75     {
76         buf[i * 2] = hex[hash_buf[i] >> 4];
77         buf[i * 2 + 1] = hex[hash_buf[i] & 0xf];
78     }
79     buf[i * 2] = '\0';
80
81     return buf;
82 }
83
84 static void compare_hash(BITMAPINFO *bmi, BYTE *bits, const char ***sha1, const char *info)
85 {
86     char *hash = hash_dib(bmi, bits);
87
88     if(!hash)
89     {
90         skip("SHA1 hashing unavailable on this platform\n");
91         return;
92     }
93
94     if(**sha1)
95     {
96         ok(!strcmp(hash, **sha1), "%d: %s: expected hash %s got %s\n",
97            bmi->bmiHeader.biBitCount, info, **sha1, hash);
98         (*sha1)++;
99     }
100     else trace("\"%s\",\n", hash);
101
102     HeapFree(GetProcessHeap(), 0, hash);
103 }
104
105 static void draw_graphics(HDC hdc, BITMAPINFO *bmi, BYTE *bits, const char ***sha1)
106 {
107     DWORD dib_size = get_dib_size(bmi);
108
109     memset(bits, 0xcc, dib_size);
110     compare_hash(bmi, bits, sha1, "empty");
111 }
112
113 static void test_simple_graphics(void)
114 {
115     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
116     BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
117     HDC mem_dc;
118     BYTE *bits;
119     HBITMAP dib, orig_bm;
120     const char **sha1;
121
122     /* a8r8g8b8 */
123     trace("8888\n");
124     memset(bmi, 0, sizeof(bmibuf));
125     bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
126     bmi->bmiHeader.biHeight = 512;
127     bmi->bmiHeader.biWidth = 512;
128     bmi->bmiHeader.biBitCount = 32;
129     bmi->bmiHeader.biPlanes = 1;
130     bmi->bmiHeader.biCompression = BI_RGB;
131
132     dib = CreateDIBSection(0, bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
133     ok(dib != NULL, "ret NULL\n");
134     mem_dc = CreateCompatibleDC(NULL);
135     orig_bm = SelectObject(mem_dc, dib);
136
137     sha1 = sha1_graphics_a8r8g8b8;
138     draw_graphics(mem_dc, bmi, bits, &sha1);
139
140     SelectObject(mem_dc, orig_bm);
141     DeleteObject(dib);
142     DeleteDC(mem_dc);
143 }
144
145 START_TEST(dib)
146 {
147     CryptAcquireContextW(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
148
149     test_simple_graphics();
150
151     CryptReleaseContext(crypt_prov, 0);
152 }