source: rtems/testsuites/libtests/crypt01/init.c @ a0b1b5ed

4.115
Last change on this file since a0b1b5ed was a0b1b5ed, checked in by Sebastian Huber <sebastian.huber@…>, on 12/15/14 at 13:19:43

Delete CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM

This define was superfluous, undocumented and used inconsistently.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2011 The FreeBSD Project. All rights reserved.
3 *
4 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
5 *
6 *  embedded brains GmbH
7 *  Dornierstr. 4
8 *  82178 Puchheim
9 *  Germany
10 *  <rtems@embedded-brains.de>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/* Based on:
35 * SHA256-based Unix crypt implementation. Released into the Public Domain by
36 * Ulrich Drepper <drepper@redhat.com>. */
37
38#ifdef HAVE_CONFIG_H
39  #include "config.h"
40#endif
41
42#include <crypt.h>
43#include <string.h>
44
45#include "tmacros.h"
46
47const char rtems_test_name[] = "CRYPT 1";
48
49typedef struct {
50  const char *salt;
51  const char *input;
52  const char *expected;
53} test_case;
54
55static void test_formats(void)
56{
57  struct crypt_data data;
58  char *s;
59
60  s = crypt_r("abc", "def", &data);
61  rtems_test_assert(strcmp(s, "abc") == 0);
62
63  crypt_add_format(&crypt_md5_format);
64  crypt_add_format(&crypt_sha256_format);
65  crypt_add_format(&crypt_sha512_format);
66}
67
68static void test_md5(void)
69{
70  const char key[] = "0.s0.l33t";
71  const char salt_0[] = "$1$deadbeef$0Huu6KHrKLVWfqa4WljDE0";
72  const char salt_1[] = "$1$cafebabe$0Huu6KHrKLVWfqa4WljDE0";
73  struct crypt_data data;
74  char *s;
75
76  printf("test crypt_md5_r()\n");
77
78  s = crypt_md5_r(&key[0], &salt_0[0], &data);
79  rtems_test_assert(strcmp(s, &salt_0[0]) == 0);
80
81  s = crypt_md5_r(&key[0], &salt_1[0], &data);
82  rtems_test_assert(strcmp(s, &salt_1[0]) != 0);
83}
84
85static const test_case test_cases_sha256[] = {
86  {
87    "$5$saltstring", "Hello world!",
88    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
89  }, {
90    "$5$rounds=10000$saltstringsaltstring", "Hello world!",
91    "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
92    "opqey6IcA"
93  }, {
94    "$5$rounds=5000$toolongsaltstring", "This is just a test",
95    "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
96    "mGRcvxa5"
97  }, {
98    "$5$rounds=1400$anotherlongsaltstring",
99    "a very much longer text to encrypt.  This one even stretches over more"
100    "than one line.",
101    "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
102    "oP84Bnq1"
103  }, {
104    "$5$rounds=77777$short",
105    "we have a short salt string but not a short password",
106    "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/"
107  }, {
108    "$5$rounds=123456$asaltof16chars..", "a short string",
109    "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
110    "cZKmF/wJvD"
111  }, {
112    "$5$rounds=10$roundstoolow", "the minimum number is still observed",
113    "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
114    "2bIC"
115  }
116};
117
118static void print_test_case(const test_case *c, const char *s)
119{
120  printf(
121    "input:    %s\nsalt:     %s\nexpected: %s\nactual:   %s\n",
122    c->input,
123    c->salt,
124    c->expected,
125    s
126  );
127}
128
129static void test_sha256(void)
130{
131  size_t i;
132
133  printf("test crypt_sha256_r()\n");
134
135  for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_sha256); ++i) {
136    const test_case *c = &test_cases_sha256[i];
137    struct crypt_data data;
138    char *s;
139
140    s = crypt_sha256_r(c->input, c->salt, &data);
141    print_test_case(c, s);
142    rtems_test_assert(strcmp(s, c->expected) == 0);
143  }
144}
145
146static const test_case test_cases_sha512[] = {
147  {
148    "$6$saltstring", "Hello world!",
149    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
150    "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
151  }, {
152    "$6$rounds=10000$saltstringsaltstring", "Hello world!",
153    "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb"
154    "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v."
155  }, {
156    "$6$rounds=5000$toolongsaltstring", "This is just a test",
157    "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ"
158    "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0"
159  }, {
160    "$6$rounds=1400$anotherlongsaltstring",
161    "a very much longer text to encrypt.  This one even stretches over more"
162    "than one line.",
163    "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP"
164    "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1"
165  }, {
166    "$6$rounds=77777$short",
167    "we have a short salt string but not a short password",
168    "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g"
169    "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0"
170  }, {
171    "$6$rounds=123456$asaltof16chars..", "a short string",
172    "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc"
173    "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1"
174  }, {
175    "$6$rounds=10$roundstoolow", "the minimum number is still observed",
176    "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x"
177    "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX."
178  }
179};
180
181static void test_sha512(void)
182{
183  size_t i;
184
185  printf("test crypt_sha512_r()\n");
186
187  for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_sha512); ++i) {
188    const test_case *c = &test_cases_sha512[i];
189    struct crypt_data data;
190    char *s;
191
192    s = crypt_sha512_r(c->input, c->salt, &data);
193    print_test_case(c, s);
194    rtems_test_assert(strcmp(s, c->expected) == 0);
195  }
196}
197
198static const test_case test_cases_generic[] = {
199  {
200    "saltstring", "Hello world!",
201    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
202    "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
203  }, {
204    "$1$deadbeef", "0.s0.l33t",
205    "$1$deadbeef$0Huu6KHrKLVWfqa4WljDE0"
206  }, {
207    "$5$saltstring", "Hello world!",
208    "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
209  }, {
210    "$6$saltstring", "Hello world!",
211    "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
212    "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
213  }
214};
215
216static void test_generic(void)
217{
218  size_t i;
219
220  printf("test crypt_r()\n");
221
222  for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_generic); ++i) {
223    const test_case *c = &test_cases_generic[i];
224    struct crypt_data data;
225    char *s;
226
227    s = crypt_r(c->input, c->salt, &data);
228    print_test_case(c, s);
229    rtems_test_assert(strcmp(s, c->expected) == 0);
230  }
231}
232
233static void Init(rtems_task_argument arg)
234{
235  TEST_BEGIN();
236
237  test_formats();
238  test_md5();
239  test_sha256();
240  test_sha512();
241  test_generic();
242
243  TEST_END();
244  rtems_test_exit(0);
245}
246
247#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
248#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
249
250#define CONFIGURE_MAXIMUM_TASKS 1
251
252#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
253
254#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
255
256#define CONFIGURE_INIT
257
258#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.