Debian Codegraph @

test/short-outbuf.c

raw
1 /* Copyright (C) 2018 Björn Esser <[email protected]>
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted.
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
7 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
10 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16 * SUCH DAMAGE.
17 */
18
19 #include "crypt-port.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 struct testcase
25 {
26 const char *exp_rn;
27 const char *exp_ra;
28 };
29
30 static const struct testcase testcases[] =
31 {
32 { "", "*0" },
33 { "*", "*0" },
34 { "*0", "*0" },
35 };
36
37 int
38 main (void)
39 {
40 bool ok = true;
41 char result[5];
42
43 for (size_t i = 0; i < ARRAY_SIZE (testcases); i++)
44 {
45 size_t s = i + 1;
46 int j = (int) s;
47 char *outbuf = malloc (sizeof (char) * s);
48
49 crypt_rn ("@@", "@@", outbuf, j);
50
51 if (!strncmp (testcases[i].exp_rn, outbuf, s))
52 {
53 strcpy (result, "PASS");
54 }
55 else
56 {
57 strcpy (result, "FAIL");
58 ok = false;
59 }
60
61 printf ("Test %zu.0: %s, expected: \"%-2s\", got: \"%-2s\"\n",
62 s, result, testcases[i].exp_rn, outbuf);
63
64 crypt_ra ("@@", "@@", (void **) &outbuf, &j);
65
66 if (!strncmp (testcases[i].exp_ra, outbuf, strlen(outbuf)))
67 {
68 strcpy (result, "PASS");
69 }
70 else
71 {
72 strcpy (result, "FAIL");
73 ok = false;
74 }
75
76 printf ("Test %zu.1: %s, expected: \"%-2s\", got: \"%-2s\"\n",
77 s, result, testcases[i].exp_ra, outbuf);
78
79 j = -1;
80
81 crypt_ra ("@@", "@@", (void **) &outbuf, &j);
82
83 if (!strncmp (testcases[i].exp_ra, outbuf, strlen(outbuf)))
84 {
85 strcpy (result, "PASS");
86 }
87 else
88 {
89 strcpy (result, "FAIL");
90 ok = false;
91 }
92
93 printf ("Test %zu.2: %s, expected: \"%-2s\", got: \"%-2s\"\n",
94 s, result, testcases[i].exp_ra, outbuf);
95
96 free (outbuf);
97 outbuf = NULL;
98 j = sizeof (struct crypt_data);
99
100 crypt_ra ("@@", "@@", (void **) &outbuf, &j);
101
102 if (!strncmp (testcases[i].exp_ra, outbuf, strlen(outbuf)))
103 {
104 strcpy (result, "PASS");
105 }
106 else
107 {
108 strcpy (result, "FAIL");
109 ok = false;
110 }
111
112 printf ("Test %zu.3: %s, expected: \"%-2s\", got: \"%-2s\"\n",
113 s, result, testcases[i].exp_ra, outbuf);
114
115 free (outbuf);
116 }
117
118 return ok ? 0 : 1;
119 }