1 | // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s |
2 | |
3 | #include <sys/param.h> |
4 | |
5 | #include <assert.h> |
6 | #include <endian.h> |
7 | #include <md5.h> |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | |
12 | void test1() { |
13 | MD5_CTX ctx; |
14 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
15 | uint8_t digest[MD5_DIGEST_LENGTH]; |
16 | |
17 | MD5Init(&ctx); |
18 | MD5Update(&ctx, entropy, __arraycount(entropy)); |
19 | MD5Final(digest, &ctx); |
20 | |
21 | printf(format: "test1: '" ); |
22 | for (size_t i = 0; i < __arraycount(digest); i++) |
23 | printf(format: "%02x" , digest[i]); |
24 | printf(format: "'\n" ); |
25 | } |
26 | |
27 | void test2() { |
28 | MD5_CTX ctx; |
29 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
30 | char digest[MD5_DIGEST_STRING_LENGTH]; |
31 | |
32 | MD5Init(&ctx); |
33 | MD5Update(&ctx, entropy, __arraycount(entropy)); |
34 | char *p = MD5End(&ctx, digest); |
35 | assert(p); |
36 | |
37 | printf(format: "test2: '%s'\n" , digest); |
38 | } |
39 | |
40 | void test3() { |
41 | MD5_CTX ctx; |
42 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
43 | |
44 | MD5Init(&ctx); |
45 | MD5Update(&ctx, entropy, __arraycount(entropy)); |
46 | char *p = MD5End(&ctx, NULL); |
47 | assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1); |
48 | |
49 | printf(format: "test3: '%s'\n" , p); |
50 | |
51 | free(ptr: p); |
52 | } |
53 | |
54 | void test4() { |
55 | char digest[MD5_DIGEST_STRING_LENGTH]; |
56 | |
57 | char *p = MD5File("/etc/fstab" , digest); |
58 | assert(p == digest); |
59 | |
60 | printf(format: "test4: '%s'\n" , p); |
61 | } |
62 | |
63 | void test5() { |
64 | char *p = MD5File("/etc/fstab" , NULL); |
65 | assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1); |
66 | |
67 | printf(format: "test5: '%s'\n" , p); |
68 | |
69 | free(ptr: p); |
70 | } |
71 | |
72 | void test6() { |
73 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
74 | char digest[MD5_DIGEST_STRING_LENGTH]; |
75 | |
76 | char *p = MD5Data(entropy, __arraycount(entropy), digest); |
77 | assert(p == digest); |
78 | |
79 | printf(format: "test6: '%s'\n" , p); |
80 | } |
81 | |
82 | void test7() { |
83 | uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; |
84 | |
85 | char *p = MD5Data(entropy, __arraycount(entropy), NULL); |
86 | assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1); |
87 | |
88 | printf(format: "test7: '%s'\n" , p); |
89 | |
90 | free(ptr: p); |
91 | } |
92 | |
93 | int main(void) { |
94 | printf(format: "MD5\n" ); |
95 | |
96 | test1(); |
97 | test2(); |
98 | test3(); |
99 | test4(); |
100 | test5(); |
101 | test6(); |
102 | test7(); |
103 | |
104 | // CHECK: MD5 |
105 | // CHECK: test1: '86e65b1ef4a830af347ac05ab4f0e999' |
106 | // CHECK: test2: '86e65b1ef4a830af347ac05ab4f0e999' |
107 | // CHECK: test3: '86e65b1ef4a830af347ac05ab4f0e999' |
108 | // CHECK: test4: '{{.*}}' |
109 | // CHECK: test5: '{{.*}}' |
110 | // CHECK: test6: '86e65b1ef4a830af347ac05ab4f0e999' |
111 | // CHECK: test7: '86e65b1ef4a830af347ac05ab4f0e999' |
112 | |
113 | return 0; |
114 | } |
115 | |