Update copyright notices with scripts/update-copyrights
[glibc.git] / crypt / badsalttest.c
blob954b793fd8f249873b852a8e00bc1cc2453b6a94
1 /* Test program for bad DES salt detection in crypt.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <sys/mman.h>
22 #include <crypt.h>
24 static const char *tests[][2] =
26 { "no salt", "" },
27 { "single char", "/" },
28 { "first char bad", "!x" },
29 { "second char bad", "Z%" },
30 { "both chars bad", ":@" },
31 { "un$upported algorithm", "$2$" },
32 { "unsupported_algorithm", "_1" },
33 { "end of page", NULL }
36 static int
37 do_test (void)
39 int result = 0;
40 struct crypt_data cd;
41 size_t n = sizeof (tests) / sizeof (*tests);
42 size_t pagesize = (size_t) sysconf (_SC_PAGESIZE);
43 char *page;
45 /* Check that crypt won't look at the second character if the first
46 one is invalid. */
47 page = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
48 MAP_PRIVATE | MAP_ANON, -1, 0);
49 if (page == MAP_FAILED)
51 perror ("mmap");
52 n--;
54 else
56 if (mmap (page + pagesize, pagesize, 0,
57 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
58 -1, 0) != page + pagesize)
59 perror ("mmap 2");
60 page[pagesize - 1] = '*';
61 tests[n - 1][1] = &page[pagesize - 1];
64 for (size_t i = 0; i < n; i++)
66 if (crypt (tests[i][0], tests[i][1]))
68 result++;
69 printf ("%s: crypt returned non-NULL with salt \"%s\"\n",
70 tests[i][0], tests[i][1]);
73 if (crypt_r (tests[i][0], tests[i][1], &cd))
75 result++;
76 printf ("%s: crypt_r returned non-NULL with salt \"%s\"\n",
77 tests[i][0], tests[i][1]);
81 return result;
84 #define TIMEOUT 5
85 #define TEST_FUNCTION do_test ()
86 #include "../test-skeleton.c"