xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_digest.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2011 Andres Mejia
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 /* Sanity test of internal digest functionality */
29 
30 #define __LIBARCHIVE_BUILD 1
31 #include "archive_digest_private.h"
32 
33 DEFINE_TEST(test_archive_md5)
34 {
35 	archive_md5_ctx ctx;
36 	unsigned char buf[] = "";
37 	unsigned char md[16];
38 	unsigned char actualmd[] = "\x93\xb8\x85\xad\xfe\x0d\xa0\x89"
39                              "\xcd\xf6\x34\x90\x4f\xd5\x9f\x71";
40 
41 	if (ARCHIVE_OK != archive_md5_init(&ctx)) {
42 		skipping("This platform does not support MD5");
43 		return;
44 	}
45 	assertEqualInt(ARCHIVE_OK, archive_md5_update(&ctx, buf, sizeof(buf)));
46 	assertEqualInt(ARCHIVE_OK, archive_md5_final(&ctx, md));
47 	assertEqualMem(md, actualmd, sizeof(md));
48 }
49 
50 DEFINE_TEST(test_archive_rmd160)
51 {
52 	archive_rmd160_ctx ctx;
53 	unsigned char buf[] = "";
54 	unsigned char md[20];
55 	unsigned char actualmd[] = "\xc8\x1b\x94\x93\x34\x20\x22\x1a\x7a\xc0"
56                              "\x04\xa9\x02\x42\xd8\xb1\xd3\xe5\x07\x0d";
57 
58 	if (ARCHIVE_OK != archive_rmd160_init(&ctx)) {
59 		skipping("This platform does not support RMD160");
60 		return;
61 	}
62 	assertEqualInt(ARCHIVE_OK, archive_rmd160_update(&ctx, buf, sizeof(buf)));
63 	assertEqualInt(ARCHIVE_OK, archive_rmd160_final(&ctx, md));
64 	assertEqualMem(md, actualmd, sizeof(md));
65 }
66 
67 DEFINE_TEST(test_archive_sha1)
68 {
69 	archive_sha1_ctx ctx;
70 	unsigned char buf[] = "";
71 	unsigned char md[20];
72 	unsigned char actualmd[] = "\x5b\xa9\x3c\x9d\xb0\xcf\xf9\x3f\x52\xb5"
73                              "\x21\xd7\x42\x0e\x43\xf6\xed\xa2\x78\x4f";
74 
75 	if (ARCHIVE_OK != archive_sha1_init(&ctx)) {
76 		skipping("This platform does not support SHA1");
77 		return;
78 	}
79 	assertEqualInt(ARCHIVE_OK, archive_sha1_update(&ctx, buf, sizeof(buf)));
80 	assertEqualInt(ARCHIVE_OK, archive_sha1_final(&ctx, md));
81 	assertEqualMem(md, actualmd, sizeof(md));
82 }
83 
84 DEFINE_TEST(test_archive_sha256)
85 {
86 	archive_sha256_ctx ctx;
87 	unsigned char buf[] = "";
88 	unsigned char md[32];
89 	unsigned char actualmd[] = "\x6e\x34\x0b\x9c\xff\xb3\x7a\x98"
90                              "\x9c\xa5\x44\xe6\xbb\x78\x0a\x2c"
91                              "\x78\x90\x1d\x3f\xb3\x37\x38\x76"
92                              "\x85\x11\xa3\x06\x17\xaf\xa0\x1d";
93 
94 	if (ARCHIVE_OK != archive_sha256_init(&ctx)) {
95 		skipping("This platform does not support SHA256");
96 		return;
97 	}
98 	assertEqualInt(ARCHIVE_OK, archive_sha256_update(&ctx, buf, sizeof(buf)));
99 	assertEqualInt(ARCHIVE_OK, archive_sha256_final(&ctx, md));
100 	assertEqualMem(md, actualmd, sizeof(md));
101 }
102 
103 DEFINE_TEST(test_archive_sha384)
104 {
105 	archive_sha384_ctx ctx;
106 	unsigned char buf[] = "";
107 	unsigned char md[48];
108 	unsigned char actualmd[] = "\xbe\xc0\x21\xb4\xf3\x68\xe3\x06"
109                              "\x91\x34\xe0\x12\xc2\xb4\x30\x70"
110                              "\x83\xd3\xa9\xbd\xd2\x06\xe2\x4e"
111                              "\x5f\x0d\x86\xe1\x3d\x66\x36\x65"
112                              "\x59\x33\xec\x2b\x41\x34\x65\x96"
113                              "\x68\x17\xa9\xc2\x08\xa1\x17\x17";
114 
115 	if (ARCHIVE_OK != archive_sha384_init(&ctx)) {
116 		skipping("This platform does not support SHA384");
117 		return;
118 	}
119 	assertEqualInt(ARCHIVE_OK, archive_sha384_update(&ctx, buf, sizeof(buf)));
120 	assertEqualInt(ARCHIVE_OK, archive_sha384_final(&ctx, md));
121 	assertEqualMem(md, actualmd, sizeof(md));
122 }
123 
124 DEFINE_TEST(test_archive_sha512)
125 {
126 	archive_sha512_ctx ctx;
127 	unsigned char buf[] = "";
128 	unsigned char md[64];
129 	unsigned char actualmd[] = "\xb8\x24\x4d\x02\x89\x81\xd6\x93"
130                              "\xaf\x7b\x45\x6a\xf8\xef\xa4\xca"
131                              "\xd6\x3d\x28\x2e\x19\xff\x14\x94"
132                              "\x2c\x24\x6e\x50\xd9\x35\x1d\x22"
133                              "\x70\x4a\x80\x2a\x71\xc3\x58\x0b"
134                              "\x63\x70\xde\x4c\xeb\x29\x3c\x32"
135                              "\x4a\x84\x23\x34\x25\x57\xd4\xe5"
136                              "\xc3\x84\x38\xf0\xe3\x69\x10\xee";
137 
138 	if (ARCHIVE_OK != archive_sha512_init(&ctx)) {
139 		skipping("This platform does not support SHA512");
140 		return;
141 	}
142 	assertEqualInt(ARCHIVE_OK, archive_sha512_update(&ctx, buf, sizeof(buf)));
143 	assertEqualInt(ARCHIVE_OK, archive_sha512_final(&ctx, md));
144 	assertEqualMem(md, actualmd, sizeof(md));
145 }
146