1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
-
+
-
+
-
-
+
+
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
+
-
+
+
+
-
-
+
+
-
+
|
/*
* Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#include <string.h>
#import "TestsAppDelegate.h"
static OFString *module = @"OFMD5Hash";
static OFString *const module = @"OFMD5Hash";
const uint8_t testfile_md5[16] =
"\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38";
const uint8_t testFileMD5[16] =
"\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38";
@implementation TestsAppDelegate (OFMD5HashTests)
- (void)MD5HashTests
{
void *pool = objc_autoreleasePoolPush();
OFMD5Hash *md5, *copy;
OFFile *f = [OFFile fileWithPath: @"testfile.bin" mode: @"r"];
OFMD5Hash *MD5, *MD5Copy;
OFURL *URL = [OFURL URLWithString: @"objfw-embedded:///testfile.bin"];
OFStream *file = [[OFURLHandler handlerForURL: URL]
openItemAtURL: URL mode: @"r"];
TEST(@"+[hashWithAllowsSwappableMemory:]",
(md5 = [OFMD5Hash hashWithAllowsSwappableMemory: true]))
(MD5 = [OFMD5Hash hashWithAllowsSwappableMemory: true]))
while (!f.atEndOfStream) {
char buf[64];
size_t len = [f readIntoBuffer: buf length: 64];
[md5 updateWithBuffer: buf length: len];
while (!file.atEndOfStream) {
char buffer[64];
size_t length = [file readIntoBuffer: buffer length: 64];
[MD5 updateWithBuffer: buffer length: length];
}
[f close];
[file close];
TEST(@"-[copy]", (copy = [[md5 copy] autorelease]))
TEST(@"-[copy]", (MD5Copy = [[MD5 copy] autorelease]))
TEST(@"-[calculate]", R([MD5 calculate]) && R([MD5Copy calculate]))
TEST(@"-[digest]",
memcmp(md5.digest, testfile_md5, 16) == 0 &&
memcmp(copy.digest, testfile_md5, 16) == 0)
memcmp(MD5.digest, testFileMD5, 16) == 0 &&
memcmp(MD5Copy.digest, testFileMD5, 16) == 0)
EXPECT_EXCEPTION(@"Detect invalid call of "
@"-[updateWithBuffer:length]", OFHashAlreadyCalculatedException,
[md5 updateWithBuffer: "" length: 1])
[MD5 updateWithBuffer: "" length: 1])
objc_autoreleasePoolPop(pool);
}
@end
|