Debian Codegraph @

test/canlocktest_legacy.c

raw
1 /*
2 * COPYRIGHT AND PERMISSION NOTICE
3 *
4 * Copyright (c) 2003 G.J. Andruk
5 *
6 * All rights reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, and/or sell copies of the Software, and to permit persons
13 * to whom the Software is furnished to do so, provided that the above
14 * copyright notice(s) and this permission notice appear in all copies of
15 * the Software and that both the above copyright notice(s) and this
16 * permission notice appear in supporting documentation.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
21 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
23 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
24 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * Except as contained in this notice, the name of a copyright holder
29 * shall not be used in advertising or otherwise to promote the sale, use
30 * or other dealings in this Software without prior written authorization
31 * of the copyright holder.
32 *
33 * SPDX-License-Identifier: ICU
34 */
35
36 /*
37 * Test program for legacy API from version 2
38 *
39 * This program doesn't really do anything but lightly exercise all the
40 * library functions, so you can make sure it all compiled correctly.
41 * Everything's kept simple so that you can also see how they would be
42 * called in a real application.
43 */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "canlock.h"
49
50
51 /* Size of the buffers for <scheme> */
52 #define BUFFSIZE 512
53
54
55 #if CL_API_V2
56 static int checker(char *key, char *lock)
57 {
58 int res = -1;
59 char *rawkey = NULL;
60 char *rawlock = NULL;
61 char keytype[BUFFSIZE], locktype[BUFFSIZE];
62
63 printf("L %s K %s ", lock, key);
64
65 /* lock_strip_alpha() returns a memory block that we must free() */
66 rawkey = lock_strip_alpha(key, keytype);
67 rawlock = lock_strip_alpha(lock, locktype);
68
69 if (!strcmp(keytype, locktype))
70 {
71 if (!strcmp(keytype, "sha1"))
72 {
73 if (!sha_verify(rawkey, rawlock))
74 {
75 printf(" GOOD\n");
76 res = 0;
77 }
78 else
79 printf(" BAD\n");
80 }
81 else
82 printf(" SCHEME NOT SUPPORTED\n");
83 }
84 else
85 printf(" SCHEME MISMATCH %s/%s\n", keytype, locktype);
86
87 free((void *) rawlock);
88 free((void *) rawkey);
89
90 return res;
91 }
92 #endif /* CL_API_V2 */
93
94
95 int main(void)
96 {
97 #if CL_API_V2
98 char cankey[256], canlock[256], *lkey, *llock;
99 unsigned char secret[] = "fluffy",
100 message[] = "<[email protected]>";
101 int rv, rv2;
102 int failed = 0;
103
104 printf("Secret %s\n", secret);
105 printf("Message %s\n", message);
106
107 llock = sha_lock(secret, strlen((char *) secret),
108 message, strlen((char *) message));
109 lkey = sha_key(secret, strlen((char *) secret),
110 message, strlen((char *)message));
111
112 printf("%s%s %s\n", "SHA Expect Lock/Key:\n",
113 "L sha1:ScU1gyAi9bd/aFEOyzg4m99lwXs=",
114 "K sha1:C1Me/4n0l/V778Ih3J2UnhAoHrA=");
115
116 rv = checker(lkey, llock);
117 free((void *) llock);
118 free((void *) lkey);
119 printf("---\n");
120 if (rv) failed = 1;
121
122 /*********/
123
124 printf("Testing against usefor cancel lock draft 01 samples...\n");
125
126 sprintf(canlock, "%s", "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=");
127 sprintf(cankey, "%s", "sha1:aaaBBBcccDDDeeeFFF");
128 rv = checker(cankey, canlock);
129 printf("---above should have been GOOD---\n");
130 if (rv) failed = 1;
131
132 /*********/
133
134 sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
135 sprintf(cankey, "%s", "sha1:chW8hNeDx3iNUsGBU6/ezDk88P4=");
136 rv = checker(cankey, canlock);
137
138 sprintf(canlock, "%s", "SHA1:H7/zsCUemvbvSDyARDaMs6AQu5s=");
139 sprintf(cankey, "%s", "sha1:4srkWaRIzvK51ArAP:Hc");
140 rv2 = checker(cankey, canlock);
141 printf("---above should have been GOOD and BAD---\n");
142 if (rv || !rv2) failed = 1;
143
144 /*********/
145
146 sprintf(canlock, "%s", "sha1:JyEBL4w9/abCBuzCxMIE/E73GM4=");
147 sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
148 rv = checker(cankey, canlock);
149
150 sprintf(canlock, "%s", "sha1:2Bmg+zWaY1noRiCdy8k3IapwSDU=");
151 sprintf(cankey, "%s", "sha1:K4rkWRjRcXmIzvK51ArAP:Jy");
152 rv2 = checker(cankey, canlock);
153 printf("---above should have been GOOD and BAD---\n");
154 if (rv || !rv2) failed = 1;
155 printf("\n");
156
157 /* Check for success */
158 if (!failed) exit(EXIT_SUCCESS);
159 exit(EXIT_FAILURE);
160 #else /* CL_API_V2 */
161 /* Execute as NOP and return 77 (defined by automake and means "skipped") */
162 printf("---legacy API tests were skipped (Version 2 API disabled)---\n");
163 exit(77);
164 #endif /* CL_API_V2 */
165 }