summaryrefslogtreecommitdiff
path: root/test/sanitizer_common/TestCases/NetBSD/md2.cc
blob: 7738aecdf8228c88c95b5754698e641af7bd5f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s

#include <sys/param.h>

#include <assert.h>
#include <endian.h>
#include <md2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void test1() {
  MD2_CTX ctx;
  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
  uint8_t digest[MD2_DIGEST_LENGTH];

  MD2Init(&ctx);
  MD2Update(&ctx, entropy, __arraycount(entropy));
  MD2Final(digest, &ctx);

  printf("test1: '");
  for (size_t i = 0; i < __arraycount(digest); i++)
    printf("%02x", digest[i]);
  printf("'\n");
}

void test2() {
  MD2_CTX ctx;
  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
  char digest[MD2_DIGEST_STRING_LENGTH];

  MD2Init(&ctx);
  MD2Update(&ctx, entropy, __arraycount(entropy));
  char *p = MD2End(&ctx, digest);
  assert(p == digest);

  printf("test2: '%s'\n", digest);
}

void test3() {
  MD2_CTX ctx;
  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};

  MD2Init(&ctx);
  MD2Update(&ctx, entropy, __arraycount(entropy));
  char *p = MD2End(&ctx, NULL);
  assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);

  printf("test3: '%s'\n", p);

  free(p);
}

void test4() {
  char digest[MD2_DIGEST_STRING_LENGTH];

  char *p = MD2File("/etc/fstab", digest);
  assert(p == digest);

  printf("test4: '%s'\n", p);
}

void test5() {
  char *p = MD2File("/etc/fstab", NULL);
  assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);

  printf("test5: '%s'\n", p);

  free(p);
}

void test6() {
  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
  char digest[MD2_DIGEST_STRING_LENGTH];

  char *p = MD2Data(entropy, __arraycount(entropy), digest);
  assert(p == digest);

  printf("test6: '%s'\n", p);
}

void test7() {
  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};

  char *p = MD2Data(entropy, __arraycount(entropy), NULL);
  assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);

  printf("test7: '%s'\n", p);

  free(p);
}

int main(void) {
  printf("MD2\n");

  test1();
  test2();
  test3();
  test4();
  test5();
  test6();
  test7();

  // CHECK: MD2
  // CHECK: test1: 'e303e49b34f981c2740cdf809200d51b'
  // CHECK: test2: 'e303e49b34f981c2740cdf809200d51b'
  // CHECK: test3: 'e303e49b34f981c2740cdf809200d51b'
  // CHECK: test4: '{{.*}}'
  // CHECK: test5: '{{.*}}'
  // CHECK: test6: 'e303e49b34f981c2740cdf809200d51b'
  // CHECK: test7: 'e303e49b34f981c2740cdf809200d51b'

  return 0;
}