Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / NetBSD / sha1.cpp
blobee5060a6601d7f8c91a8a2dd4965a99da9ab2c5b
1 // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
3 #include <assert.h>
4 #include <sha1.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 void test1() {
10 SHA1_CTX ctx;
11 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
12 uint8_t digest[SHA1_DIGEST_LENGTH];
14 SHA1Init(&ctx);
15 SHA1Update(&ctx, entropy, __arraycount(entropy));
16 SHA1Final(digest, &ctx);
18 printf("test1: '");
19 for (size_t i = 0; i < __arraycount(digest); i++)
20 printf("%02x", digest[i]);
21 printf("'\n");
24 void local_SHA1Update(SHA1_CTX *context, const uint8_t *data, unsigned int len)
26 unsigned int a, b;
28 b = context->count[0];
29 context->count[0] += len << 3;
30 if (context->count[0] < b)
31 context->count[1] += (len >> 29) + 1;
32 b = (b >> 3) & 63;
33 if ((b + len) > 63) {
34 memcpy(&context->buffer[b], data, (a = 64 - b));
35 SHA1Transform(context->state, context->buffer);
36 for ( ; a + 63 < len; a += 64)
37 SHA1Transform(context->state, &data[a]);
38 b = 0;
39 } else {
40 a = 0;
42 memcpy(&context->buffer[b], &data[a], len - a);
45 void test2() {
46 SHA1_CTX ctx;
47 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
48 uint8_t digest[SHA1_DIGEST_LENGTH];
50 SHA1Init(&ctx);
51 local_SHA1Update(&ctx, entropy, __arraycount(entropy));
52 SHA1Final(digest, &ctx);
54 printf("test2: '");
55 for (size_t i = 0; i < __arraycount(digest); i++)
56 printf("%02x", digest[i]);
57 printf("'\n");
60 void test3() {
61 SHA1_CTX ctx;
62 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
63 char digest[SHA1_DIGEST_STRING_LENGTH];
65 SHA1Init(&ctx);
66 SHA1Update(&ctx, entropy, __arraycount(entropy));
67 char *p = SHA1End(&ctx, digest);
68 assert(p == digest);
70 printf("test3: '%s'\n", digest);
73 void test4() {
74 SHA1_CTX ctx;
75 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
77 SHA1Init(&ctx);
78 SHA1Update(&ctx, entropy, __arraycount(entropy));
79 char *p = SHA1End(&ctx, NULL);
80 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);
82 printf("test4: '%s'\n", p);
84 free(p);
87 void test5() {
88 char digest[SHA1_DIGEST_STRING_LENGTH];
90 char *p = SHA1File("/etc/fstab", digest);
91 assert(p == digest);
93 printf("test5: '%s'\n", p);
96 void test6() {
97 char *p = SHA1File("/etc/fstab", NULL);
98 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);
100 printf("test6: '%s'\n", p);
102 free(p);
105 void test7() {
106 char digest[SHA1_DIGEST_STRING_LENGTH];
108 char *p = SHA1FileChunk("/etc/fstab", digest, 10, 20);
109 assert(p == digest);
111 printf("test7: '%s'\n", p);
114 void test8() {
115 char *p = SHA1FileChunk("/etc/fstab", NULL, 10, 20);
116 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);
118 printf("test8: '%s'\n", p);
120 free(p);
123 void test9() {
124 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
125 char digest[SHA1_DIGEST_STRING_LENGTH];
127 char *p = SHA1Data(entropy, __arraycount(entropy), digest);
128 assert(p == digest);
130 printf("test9: '%s'\n", p);
133 void test10() {
134 uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
136 char *p = SHA1Data(entropy, __arraycount(entropy), NULL);
137 assert(strlen(p) == SHA1_DIGEST_STRING_LENGTH - 1);
139 printf("test10: '%s'\n", p);
141 free(p);
144 int main(void) {
145 printf("SHA1\n");
147 test1();
148 test2();
149 test3();
150 test4();
151 test5();
152 test6();
153 test7();
154 test8();
155 test9();
156 test10();
158 // CHECK: SHA1
159 // CHECK: test1: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
160 // CHECK: test2: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
161 // CHECK: test3: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
162 // CHECK: test4: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
163 // CHECK: test5: '{{.*}}'
164 // CHECK: test6: '{{.*}}'
165 // CHECK: test7: '{{.*}}'
166 // CHECK: test8: '{{.*}}'
167 // CHECK: test9: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
168 // CHECK: test10: '57d1b759bf3d1811135748cb0328c73b51fa6f57'
170 return 0;