blob: 4be6f964963b119ef66eb56589b022d0c32e580f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2004 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/vec.h>
39#include <vppinfra/format.h>
40#include <vppinfra/error.h>
41#include <vppinfra/md5.h>
42
43#include <fcntl.h>
44#include <unistd.h>
45
Dave Barachc3799992016-08-15 11:12:27 -040046static clib_error_t *md5_test_suite (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Dave Barachc3799992016-08-15 11:12:27 -040048int
49main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -070050{
51 int i;
52
53 if (argc == 1)
54 {
Dave Barachc3799992016-08-15 11:12:27 -040055 clib_error_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 e = md5_test_suite ();
57 if (e)
58 {
59 clib_error_report (e);
60 exit (1);
61 }
62 }
63
64 for (i = 1; i < argc; i++)
65 {
66 md5_context_t m;
67 u8 digest[16];
Dave Barachc3799992016-08-15 11:12:27 -040068 u8 buffer[64 * 1024];
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 int fd, n;
70
71 fd = open (argv[i], 0);
72 if (fd < 0)
73 clib_unix_error ("can't open %s", argv[i]);
74
75 md5_init (&m);
76 while ((n = read (fd, buffer, sizeof (buffer))) > 0)
77 md5_add (&m, buffer, n);
78 close (fd);
79 md5_finish (&m, digest);
80 fformat (stdout, "%U %s\n",
Dave Barachc3799992016-08-15 11:12:27 -040081 format_hex_bytes, digest, sizeof (digest), argv[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 }
83
84 return 0;
85}
86
87static clib_error_t *
88md5_test_suite (void)
89{
Dave Barachc3799992016-08-15 11:12:27 -040090 typedef struct
91 {
92 char *input;
93 char *output;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 } md5_test_t;
95
96 static md5_test_t tests[] = {
Dave Barachc3799992016-08-15 11:12:27 -040097 {.input = "",
98 .output = "d41d8cd98f00b204e9800998ecf8427e",},
99 {.input = "a",
100 .output = "0cc175b9c0f1b6a831c399e269772661",},
101 {.input = "abc",
102 .output = "900150983cd24fb0d6963f7d28e17f72",},
103 {.input = "message digest",
104 .output = "f96b697d7cb7938d525a2f31aaf161d0",},
105 {.input = "abcdefghijklmnopqrstuvwxyz",
106 .output = "c3fcd3d76192e4007dfb496cca67e13b",},
107 {.input =
108 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
109 .output = "d174ab98d277d9f5a5611c2c9f419d9f",},
110 {.input =
111 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
112 .output = "57edf4a22be3c955ac49da2e2107b67a",},
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 };
114
115 int i;
Dave Barachc3799992016-08-15 11:12:27 -0400116 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 md5_context_t m;
118 u8 digest[16];
119
120 for (i = 0; i < ARRAY_LEN (tests); i++)
121 {
122 md5_init (&m);
123 md5_add (&m, tests[i].input, strlen (tests[i].input));
124 md5_finish (&m, digest);
125 s = format (0, "%U", format_hex_bytes, digest, sizeof (digest));
126 if (memcmp (s, tests[i].output, 2 * sizeof (digest)))
127 return clib_error_return
Dave Barachc3799992016-08-15 11:12:27 -0400128 (0, "%s -> %v expected %s", tests[i].input, s, tests[i].output);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 vec_free (s);
130 }
131
132 return 0;
133}
Dave Barachc3799992016-08-15 11:12:27 -0400134
135/*
136 * fd.io coding-style-patch-verification: ON
137 *
138 * Local Variables:
139 * eval: (c-set-style "gnu")
140 * End:
141 */