dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / t / t-buffer.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * t-buffer.c - test buffer handling
4 *
5 * Copyright © 2009-2011 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include <compat.h>
23
24#include <dpkg/test.h>
25#include <dpkg/buffer.h>
26#include <dpkg/dpkg.h>
27
28#include <sys/types.h>
29
30#include <unistd.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34static const char str_empty[] = "";
35static const char ref_hash_empty[] = "d41d8cd98f00b204e9800998ecf8427e";
36static const char str_test[] = "this is a test string\n";
37static const char ref_hash_test[] = "475aae3b885d70a9130eec23ab33f2b9";
38
39static void
40test_buffer_hash(void)
41{
42 char hash[MD5HASHLEN + 1];
43
44 buffer_md5(str_empty, hash, strlen(str_empty));
45 test_str(hash, ==, ref_hash_empty);
46
47 buffer_md5(str_test, hash, strlen(str_test));
48 test_str(hash, ==, ref_hash_test);
49}
50
51static void
52test_fdio_hash(void)
53{
54 char hash[MD5HASHLEN + 1];
55 char *test_file;
56 int fd;
57
58 test_file = test_alloc(strdup("test.XXXXXX"));
59 fd = mkstemp(test_file);
60 test_pass(fd >= 0);
61
62 test_pass(fd_md5(fd, hash, -1, NULL) >= 0);
63 test_str(hash, ==, ref_hash_empty);
64
65 test_pass(write(fd, str_test, strlen(str_test)) == (ssize_t)strlen(str_test));
66 test_pass(lseek(fd, 0, SEEK_SET) == 0);
67
68 test_pass(fd_md5(fd, hash, -1, NULL) >= 0);
69 test_str(hash, ==, ref_hash_test);
70
71 test_pass(unlink(test_file) == 0);
72}
73
74TEST_ENTRY(test)
75{
76 test_plan(10);
77
78 test_buffer_hash();
79 test_fdio_hash();
80}