makefiles: Add a standard header for all makefiles to replace the common variable...
[wine] / dlls / advapi32 / tests / crypt_md5.c
1 /*
2  * Unit tests for MD5 functions
3  *
4  * Copyright 2004 Hans Leidekker
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 <stdio.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27
28 typedef struct
29 {
30     unsigned int i[2];
31     unsigned int buf[4];
32     unsigned char in[64];
33     unsigned char digest[16];
34 } MD5_CTX;
35
36 typedef VOID (WINAPI *fnMD5Init)( MD5_CTX *ctx );
37 typedef VOID (WINAPI *fnMD5Update)( MD5_CTX *ctx, const unsigned char *src, const int len );
38 typedef VOID (WINAPI *fnMD5Final)( MD5_CTX *ctx );
39
40 fnMD5Init pMD5Init;
41 fnMD5Update pMD5Update;
42 fnMD5Final pMD5Final;
43
44 #define ctxcmp( a, b ) memcmp( a, b, FIELD_OFFSET( MD5_CTX, in ) )
45
46 static void test_md5_ctx(void)
47 {
48     static unsigned char message[] =
49         "In our Life there's If"
50         "In our beliefs there's Lie"
51         "In our business there is Sin"
52         "In our bodies, there is Die";
53
54     int size = sizeof(message) - 1;
55     HMODULE module;
56
57     MD5_CTX ctx;
58     MD5_CTX ctx_initialized = 
59     {
60         { 0, 0 },
61         { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }
62     };
63
64     MD5_CTX ctx_update1 =
65     {
66         { 0x00000338, 0 },
67         { 0x068cb64d, 0xb7a05790, 0x426979ee, 0xed67e221 }
68     };
69
70     MD5_CTX ctx_update2 =
71     {
72         { 0x00000670, 0 },
73         { 0x2f7afe58, 0xcc3e9315, 0x709c465c, 0xbf6414c8 }
74     };
75
76     unsigned char expect[16] =
77         { 0x43, 0x03, 0xdd, 0x8c, 0x60, 0xd9, 0x3a, 0x22,
78           0x0b, 0x28, 0xd0, 0xb2, 0x65, 0x93, 0xd0, 0x36 };
79
80     module = GetModuleHandleA("advapi32.dll");
81
82     pMD5Init = (fnMD5Init)GetProcAddress( module, "MD5Init" );
83     pMD5Update = (fnMD5Update)GetProcAddress( module, "MD5Update" );
84     pMD5Final = (fnMD5Final)GetProcAddress( module, "MD5Final" );
85
86     if (!pMD5Init || !pMD5Update || !pMD5Final)
87     {
88         win_skip("Needed functions are not available\n");
89         return;
90     }
91
92     memset( &ctx, 0, sizeof(ctx) );
93     pMD5Init( &ctx );
94     ok( !ctxcmp( &ctx, &ctx_initialized ), "invalid initialization\n" );
95
96     pMD5Update( &ctx, message, size );
97     ok( !ctxcmp( &ctx, &ctx_update1 ), "update doesn't work correctly\n" );
98
99     pMD5Update( &ctx, message, size );
100     ok( !ctxcmp( &ctx, &ctx_update2 ), "update doesn't work correctly\n" );
101
102     pMD5Final( &ctx );
103     ok( ctxcmp( &ctx, &ctx_initialized ), "context has changed\n" );
104     ok( !memcmp( ctx.digest, expect, sizeof(expect) ), "incorrect result\n" );
105 }
106
107 START_TEST(crypt_md5)
108 {
109     test_md5_ctx();
110 }