1 /* Copyright (C) 2021 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (test_stdstring.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <utils/md5.h>
29 #include <encodings/crc32.h>
30 #include <streams/file_stream.h>
32 #define SUITE_NAME "hash"
39 MD5_Final(output, &ctx);
41 "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e",
44 MD5_Update(&ctx, "The quick brown fox jumps over the lazy dog", 43);
45 MD5_Final(output, &ctx);
47 "\x9e\x10\x7d\x9d\x37\x2b\xb6\x82\x6b\xd8\x1d\x35\x42\xa4\x19\xd6",
50 MD5_Update(&ctx, "The quick brown fox jumps over the lazy dog", 43);
51 MD5_Update(&ctx, "The quick brown fox jumps over the lazy dog", 43);
52 MD5_Update(&ctx, "The quick brown fox jumps over the lazy dog", 43);
53 MD5_Final(output, &ctx);
55 "\x4e\x67\xdb\x4a\x7a\x40\x6b\x0c\xfd\xad\xd8\x87\xcd\xe7\x88\x8e",
60 START_TEST (test_crc32)
62 char buf1[] = "retroarch";
63 char buf2[] = "12345678";
64 char buf3[] = "The quick brown fox jumps over the lazy dog";
65 uint32_t test1 = encoding_crc32(0, (uint8_t*)buf1, strlen(buf1));
66 uint32_t test2 = encoding_crc32(0, (uint8_t*)buf2, strlen(buf2));
67 uint32_t test3 = encoding_crc32(0, (uint8_t*)buf3, strlen(buf3));
68 ck_assert_uint_eq(0x3cae141a, test1);
69 ck_assert_uint_eq(0x9ae0daaf, test2);
70 ck_assert_uint_eq(0x414fa339, test3);
74 #define CRC32_BUFFER_SIZE 1048576
75 #define CRC32_MAX_MB 64
78 * Calculate a CRC32 from the first part of the given file.
79 * "first part" being the first (CRC32_BUFFER_SIZE * CRC32_MAX_MB)
82 * Returns: the crc32, or 0 if there was an error.
84 static uint32_t file_crc32(uint32_t crc, const char *path)
88 unsigned char *buf = NULL;
92 if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
95 if (!(buf = (unsigned char*)malloc(CRC32_BUFFER_SIZE)))
97 filestream_close(file);
101 for (i = 0; i < CRC32_MAX_MB; i++)
103 int64_t nread = filestream_read(file, buf, CRC32_BUFFER_SIZE);
107 filestream_close(file);
111 crc = encoding_crc32(crc, buf, (size_t)nread);
112 if (filestream_eof(file))
116 filestream_close(file);
120 START_TEST (test_crc32_file)
125 fd = fopen(tmpfile, "wb");
126 ck_assert(fd != NULL);
127 fwrite("12345678", 1, 8, fd);
130 ck_assert_uint_eq(file_crc32(0, tmpfile), 0x9ae0daaf);
132 ck_assert_uint_eq(file_crc32(0, "/this/path/should/not/exist"), 0);
133 ck_assert_uint_eq(file_crc32(0, NULL), 0);
137 Suite *create_suite(void)
139 Suite *s = suite_create(SUITE_NAME);
141 TCase *tc_core = tcase_create("Core");
142 tcase_add_test(tc_core, test_md5);
143 tcase_add_test(tc_core, test_crc32);
144 tcase_add_test(tc_core, test_crc32_file);
145 suite_add_tcase(s, tc_core);
153 Suite *s = create_suite();
154 SRunner *sr = srunner_create(s);
155 srunner_run_all(sr, CK_NORMAL);
156 num_fail = srunner_ntests_failed(sr);
158 return (num_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;