2 * Copyright 2014 Akihiro Sagawa
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define WIN32_LEAN_AND_MEAN
27 #include "wine/test.h"
32 static HCRYPTPROV crypt_prov
;
34 static inline DWORD
get_stride(const BITMAPINFO
*bmi
)
36 return ((bmi
->bmiHeader
.biBitCount
* bmi
->bmiHeader
.biWidth
+ 31) >> 3) & ~3;
39 static inline DWORD
get_dib_size(const BITMAPINFO
*bmi
)
41 return get_stride(bmi
) * abs(bmi
->bmiHeader
.biHeight
);
44 static char *hash_dib(const BITMAPINFO
*bmi
, const void *bits
)
46 DWORD dib_size
= get_dib_size(bmi
);
50 DWORD hash_size
= sizeof(hash_buf
);
52 static const char *hex
= "0123456789abcdef";
54 if(!crypt_prov
) return NULL
;
56 if(!CryptCreateHash(crypt_prov
, CALG_SHA1
, 0, 0, &hash
)) return NULL
;
58 CryptHashData(hash
, bits
, dib_size
, 0);
60 CryptGetHashParam(hash
, HP_HASHVAL
, NULL
, &hash_size
, 0);
61 if(hash_size
!= sizeof(hash_buf
)) return NULL
;
63 CryptGetHashParam(hash
, HP_HASHVAL
, hash_buf
, &hash_size
, 0);
64 CryptDestroyHash(hash
);
66 buf
= HeapAlloc(GetProcessHeap(), 0, hash_size
* 2 + 1);
68 for(i
= 0; i
< hash_size
; i
++)
70 buf
[i
* 2] = hex
[hash_buf
[i
] >> 4];
71 buf
[i
* 2 + 1] = hex
[hash_buf
[i
] & 0xf];
78 static void init_bmi(BITMAPINFO
*bmi
, LONG width
, LONG height
, DWORD size
)
80 memset(bmi
, 0, sizeof(*bmi
));
81 bmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
82 bmi
->bmiHeader
.biWidth
= width
;
83 bmi
->bmiHeader
.biHeight
= height
;
84 bmi
->bmiHeader
.biPlanes
= 1;
85 bmi
->bmiHeader
.biBitCount
= 32;
86 bmi
->bmiHeader
.biCompression
= BI_RGB
;
87 bmi
->bmiHeader
.biSizeImage
= size
;
90 static void test_DrawDib_sizeimage(void)
97 /* [0] correct size */
98 { WIDTH
, HEIGHT
, WIDTH
* HEIGHT
* sizeof(RGBQUAD
), "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
100 { WIDTH
, HEIGHT
, 0, "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
102 { WIDTH
, -HEIGHT
, 0, "" },
103 { -WIDTH
, HEIGHT
, 0, "" },
104 { -WIDTH
, -HEIGHT
, 0, "" },
106 { 0, HEIGHT
, 0, "" },
108 /* [8] zero size (to compare [9], [10] ) */
109 { WIDTH
, HEIGHT
/2, 0, "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
110 /* [9] insufficient size */
111 { WIDTH
, HEIGHT
/2, sizeof(RGBQUAD
), "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
112 /* [10] too much size */
113 { WIDTH
, HEIGHT
/2, WIDTH
* HEIGHT
* sizeof(RGBQUAD
), "8b75bf6d54a8645380114fe77505ee0699ffffaa" },
116 DWORD src_dib_size
, dst_dib_size
;
119 BITMAPINFO src_info
, dst_info
;
120 RGBQUAD
*src_bits
= NULL
, *dst_bits
;
124 hdc
= CreateCompatibleDC(NULL
);
126 init_bmi(&dst_info
, WIDTH
, HEIGHT
, 0);
127 dib
= CreateDIBSection(NULL
, &dst_info
, DIB_RGB_COLORS
, (void **)&dst_bits
, NULL
, 0);
128 dst_dib_size
= get_dib_size(&dst_info
);
129 ok(dib
!= NULL
, "CreateDIBSection failed\n");
130 SelectObject(hdc
, dib
);
132 init_bmi(&src_info
, WIDTH
, HEIGHT
, 0);
133 src_dib_size
= get_dib_size(&src_info
);
134 src_bits
= HeapAlloc(GetProcessHeap(), 0, src_dib_size
);
135 ok(src_bits
!= NULL
, "Can't allocate memory\n");
136 memset(src_bits
, 0x88, src_dib_size
);
139 ok(hdd
!= NULL
, "DrawDibOpen failed\n");
141 for (i
= 0; i
< ARRAY_SIZE(test_data
); i
++) {
143 memset(dst_bits
, 0xff, dst_dib_size
);
144 init_bmi(&src_info
, test_data
[i
].width
, test_data
[i
].height
, test_data
[i
].size
);
145 r
= DrawDibDraw(hdd
, hdc
,
146 0, 0, -1, -1, &src_info
.bmiHeader
, src_bits
,
147 0, 0, test_data
[i
].width
, test_data
[i
].height
, 0);
148 if (test_data
[i
].hash
[0])
149 ok(r
, "[%u] DrawDibDraw failed, expected success\n", i
);
151 ok(!r
, "[%u] DrawDibDraw succeeded, expected failed\n", i
);
152 if (!r
|| !test_data
[i
].hash
[0])
155 hash
= hash_dib(&dst_info
, dst_bits
);
157 win_skip("This platform doesn't support SHA-1 hash\n");
160 ok(strcmp(hash
, test_data
[i
].hash
) == 0,
161 "[%u] got %s, expected %s\n",
162 i
, hash
, test_data
[i
].hash
);
163 HeapFree(GetProcessHeap(), 0, hash
);
166 r
= DrawDibClose(hdd
);
167 ok(r
, "DrawDibClose failed\n");
169 HeapFree(GetProcessHeap(), 0, src_bits
);
176 CryptAcquireContextW(&crypt_prov
, NULL
, NULL
, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
);
177 test_DrawDib_sizeimage();
178 CryptReleaseContext(crypt_prov
, 0);