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