crypt32: Make some variables static and/or const.
[wine] / dlls / advapi32 / tests / crypt_sha.c
1 /*
2  * Unit tests for SHA functions
3  *
4  * Copyright (c) 2004 Filip Navara
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
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26
27 #include "wine/test.h"
28
29 typedef struct {
30    ULONG Unknown[6];
31    ULONG State[5];
32    ULONG Count[2];
33    UCHAR Buffer[64];
34 } SHA_CTX, *PSHA_CTX;
35
36 #define ctxcmp(a,b) memcmp((char*)a, (char*)b, FIELD_OFFSET(SHA_CTX, Buffer))
37
38 static void test_sha_ctx(void)
39 {
40    FARPROC pA_SHAInit, pA_SHAUpdate, pA_SHAFinal;
41    static const char test_buffer[] = "In our Life there's If"
42                        "In our beliefs there's Lie"
43                        "In our business there is Sin"
44                        "In our bodies, there is Die";
45    ULONG test_buffer_size = strlen(test_buffer);
46    HMODULE hmod;
47    SHA_CTX ctx;
48    SHA_CTX ctx_initialized = {{0, 0, 0, 0, 0}, {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0}, {0, 0}};
49    SHA_CTX ctx_update1 = {{0, 0, 0, 0, 0}, {0xdbe5eba8, 0x6b4335ca, 0xf7c94abe, 0xc9f34e31, 0x311023f0}, {0, 0x67}};
50    SHA_CTX ctx_update2 = {{0, 0, 0, 0, 0}, {0x5ecc818d, 0x52498169, 0xf6758559, 0xd035a164, 0x871dd125}, {0, 0xce}};
51    ULONG result[5];
52    ULONG result_correct[5] = {0xe014f93, 0xe09791ec, 0x6dcf96c8, 0x8e9385fc, 0x1611c1bb};
53
54    hmod = LoadLibrary("advapi32.dll");
55    pA_SHAInit = GetProcAddress(hmod, "A_SHAInit");
56    pA_SHAUpdate = GetProcAddress(hmod, "A_SHAUpdate");
57    pA_SHAFinal = GetProcAddress(hmod, "A_SHAFinal");
58
59    if (!pA_SHAInit || !pA_SHAUpdate || !pA_SHAFinal) return;
60
61    RtlZeroMemory(&ctx, sizeof(ctx));
62    pA_SHAInit(&ctx);
63    ok(!ctxcmp(&ctx, &ctx_initialized), "invalid initialization\n");
64
65    pA_SHAUpdate(&ctx, test_buffer, test_buffer_size);
66    ok(!ctxcmp(&ctx, &ctx_update1), "update doesn't work correctly\n");
67
68    pA_SHAUpdate(&ctx, test_buffer, test_buffer_size);
69    ok(!ctxcmp(&ctx, &ctx_update2), "update doesn't work correctly\n");
70
71    pA_SHAFinal(&ctx, result);
72    ok(!ctxcmp(&ctx, &ctx_initialized), "context hasn't been reinitialized\n");
73    ok(!memcmp(result, result_correct, sizeof(result)), "incorrect result\n");
74
75    FreeLibrary(hmod);
76 }
77
78 START_TEST(crypt_sha)
79 {
80     test_sha_ctx();
81 }