Index: .fossil-settings/clean-glob ================================================================== --- .fossil-settings/clean-glob +++ .fossil-settings/clean-glob @@ -50,11 +50,10 @@ tests/EBOOT.PBP tests/Info.plist tests/PARAM.SFO tests/objc_sync/objc_sync tests/terminal/terminal_tests -tests/testfile_bin.m tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds Index: .fossil-settings/ignore-glob ================================================================== --- .fossil-settings/ignore-glob +++ .fossil-settings/ignore-glob @@ -55,12 +55,10 @@ tests/iOS.xcodeproj/*.pbxuser tests/iOS.xcodeproj/project.xcworkspace tests/iOS.xcodeproj/xcuserdata tests/objc_sync/objc_sync tests/terminal/terminal_tests -tests/testfile_bin.m -tests/testfile_bin.m tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds Index: .gitignore ================================================================== --- .gitignore +++ .gitignore @@ -55,11 +55,10 @@ tests/iOS.xcodeproj/*.pbxuser tests/iOS.xcodeproj/project.xcworkspace tests/iOS.xcodeproj/xcuserdata tests/objc_sync/objc_sync tests/terminal/terminal_tests -tests/testfile_bin.m tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -10,10 +10,11 @@ OFColorTests.m \ OFConcreteArrayTests.m \ OFConcreteMutableArrayTests.m \ OFCryptographicHashTests.m \ OFDateTests.m \ + OFHMACTests.m \ OFIRITests.m \ OFInvocationTests.m \ OFJSONTests.m \ OFMatrix4x4Tests.m \ OFMethodSignatureTests.m \ Index: new_tests/OFCryptographicHashTests.m ================================================================== --- new_tests/OFCryptographicHashTests.m +++ new_tests/OFCryptographicHashTests.m @@ -20,11 +20,11 @@ #import "ObjFW.h" #import "ObjFWTest.h" @interface OFCryptographicHashTests: OTTestCase { - OFStream *_file; + OFStream *_stream; } @end const unsigned char testFileMD5[16] = "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38"; @@ -56,21 +56,23 @@ OFIRI *IRI; [super setUp]; IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - _file = [[OFIRIHandler openItemAtIRI: IRI mode: @"r"] retain]; + _stream = [[OFIRIHandler openItemAtIRI: IRI mode: @"r"] retain]; } - (void)tearDown { - [_file close]; + [_stream close]; + + [super tearDown]; } - (void)dealloc { - [_file release]; + [_stream release]; [super dealloc]; } - (void)testHash: (Class)hashClass @@ -80,13 +82,13 @@ [hashClass hashWithAllowsSwappableMemory: true]; id copy; OTAssertNotNil(hash); - while (!_file.atEndOfStream) { + while (!_stream.atEndOfStream) { char buffer[64]; - size_t length = [_file readIntoBuffer: buffer length: 64]; + size_t length = [_stream readIntoBuffer: buffer length: 64]; [hash updateWithBuffer: buffer length: length]; } copy = [[hash copy] autorelease]; ADDED new_tests/OFHMACTests.m Index: new_tests/OFHMACTests.m ================================================================== --- new_tests/OFHMACTests.m +++ new_tests/OFHMACTests.m @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * 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 + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFHMACTests: OTTestCase +{ + OFStream *_stream; +} +@end + +static const uint8_t key[] = + "yM9h8K6IWnJRvxC/0F8XRWG7RnACDBz8wqK2tbXrYVLoKC3vPLeJikyJSM47tVHc" + "DlXHww9zULAC2sJUlm2Kg1z4oz2aXY3Y1PQSB4VkC/m0DQ7hCI6cAg4TWnKdzWTy" + "cvYGX+Y6HWeDY79/PGSd8fNItme6I8w4HDBqU7BP2sum3jbePJqoiSnhcyJZQTeZ" + "jw0ZXoyrfHgOYD2M+NsTDaGpLblFtQ7n5CczjKtafG40PkEwx1dcrd46U9i3GyTK"; +static const size_t keyLength = sizeof(key); +static const uint8_t MD5Digest[] = + "\xCC\x1F\xEF\x09\x29\xA3\x25\x1A\x06\xA9\x83\x99\xF9\xBC\x8F\x42"; +static const uint8_t SHA1Digest[] = + "\x94\xB9\x0A\x6F\xFB\xA7\x13\x6A\x75\x55" + "\xD5\x7F\x5D\xB7\xF4\xCA\xEB\x4A\xDE\xBF"; +static const uint8_t RIPEMD160Digest[] = + "\x2C\xE1\xED\x41\xC6\xF3\x51\xA8\x04\xD2" + "\xC3\x9B\x08\x33\x3B\xD5\xC9\x00\x39\x50"; +static const uint8_t SHA256Digest[] = + "\xFB\x8C\xDA\x88\xB3\x81\x32\x16\xD7\xD8\x62\xD4\xA6\x26\x9D\x77" + "\x01\x99\x62\x65\x29\x02\x41\xE6\xEF\xA1\x02\x31\xA8\x9D\x77\x5D"; +static const uint8_t SHA384Digest[] = + "\x2F\x4A\x47\xAE\x13\x8E\x96\x52\xF1\x8F\x05\xFD\x65\xCD\x9A\x97" + "\x93\x2F\xC9\x02\xD6\xC6\xAB\x2E\x15\x76\xC0\xA7\xA0\x05\xF4\xEF" + "\x14\x52\x33\x4B\x9C\x5F\xD8\x07\x4E\x98\xAE\x97\x46\x29\x24\xB4"; +static const uint8_t SHA512Digest[] = + "\xF5\x8C\x3F\x9C\xA2\x2F\x0A\xF3\x26\xD8\xC0\x7E\x20\x63\x88\x61" + "\xC9\xE1\x1F\xD7\xC7\xE5\x59\x33\xD5\x2F\xAF\x56\x1C\x94\xC8\xA4" + "\x61\xB3\xF9\x1A\xE3\x09\x43\xA6\x5B\x85\xB1\x50\x5B\xCB\x1A\x2E" + "\xB7\xE8\x87\xC1\x73\x19\x63\xF6\xA2\x91\x8D\x7E\x2E\xCC\xEC\x99"; + +@implementation OFHMACTests +- (void)setUp +{ + OFIRI *IRI; + + [super setUp]; + + IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; + _stream = [[OFIRIHandler openItemAtIRI: IRI mode: @"r"] retain]; +} + +- (void)tearDown +{ + [_stream close]; + + [super tearDown]; +} + +- (void)dealloc +{ + [_stream release]; + + [super dealloc]; +} + +- (void)testWithHashClass: (Class)hashClass + expectedDigest: (const unsigned char *)expectedDigest +{ + OFHMAC *HMAC = [OFHMAC HMACWithHashClass: hashClass + allowsSwappableMemory: true]; + + OTAssertNotNil(HMAC); + + OTAssertThrowsSpecific([HMAC updateWithBuffer: "" length: 0], + OFInvalidArgumentException); + + [HMAC setKey: key length: keyLength]; + + while (!_stream.atEndOfStream) { + char buffer[64]; + size_t length = [_stream readIntoBuffer: buffer length: 64]; + [HMAC updateWithBuffer: buffer length: length]; + } + + [HMAC calculate]; + + OTAssertEqual(memcmp(HMAC.digest, expectedDigest, HMAC.digestSize), 0); +} + +- (void)testHMACWithMD5 +{ + [self testWithHashClass: [OFMD5Hash class] expectedDigest: MD5Digest]; +} + +- (void)testHMACWithRIPEMD160 +{ + [self testWithHashClass: [OFRIPEMD160Hash class] + expectedDigest: RIPEMD160Digest]; +} + +- (void)testHMACWithSHA1 +{ + [self testWithHashClass: [OFSHA1Hash class] expectedDigest: SHA1Digest]; +} + +- (void)testHMACWithSHA256 +{ + [self testWithHashClass: [OFSHA256Hash class] + expectedDigest: SHA256Digest]; +} + +- (void)testHMACWithSHA384 +{ + [self testWithHashClass: [OFSHA384Hash class] + expectedDigest: SHA384Digest]; +} + +- (void)testHMACWithSHA512 +{ + [self testWithHashClass: [OFSHA512Hash class] + expectedDigest: SHA512Digest]; +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -7,21 +7,19 @@ boot.dol \ ${PROG_NOINST}.arm9 \ ${PROG_NOINST}.nds \ ${PROG_NOINST}.nro \ ${PROG_NOINST}.rpx \ - testfile_bin.m \ testfile_ini.m DISTCLEAN = Info.plist PROG_NOINST = tests${PROG_SUFFIX} STATIC_LIB_NOINST = ${TESTS_STATIC_LIB} SRCS = ForwardingTests.m \ ${OF_BLOCK_TESTS_M} \ OFDataTests.m \ OFDictionaryTests.m \ - OFHMACTests.m \ OFINIFileTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMemoryStreamTests.m \ OFNotificationCenterTests.m \ @@ -39,11 +37,10 @@ TestsAppDelegate.m \ ${USE_SRCS_FILES} \ ${USE_SRCS_SOCKETS} \ ${USE_SRCS_THREADS} \ ${USE_SRCS_WINDOWS} \ - testfile_bin.m \ testfile_ini.m SRCS_SOCKETS = OFDNSResolverTests.m \ ${OF_HTTP_CLIENT_TESTS_M} \ OFHTTPCookieTests.m \ OFHTTPCookieManagerTests.m \ @@ -65,12 +62,10 @@ IOS_USER ?= mobile IOS_TMP ?= /tmp/objfw-test include ../buildsys.mk -testfile_bin.m: testfile.bin - ${SHELL} ../utils/objfw-embed testfile.bin testfile.bin $@ testfile_ini.m: testfile.ini ${SHELL} ../utils/objfw-embed testfile.ini testfile.ini $@ .PHONY: run run-on-ios run-on-android run: DELETED tests/OFHMACTests.m Index: tests/OFHMACTests.m ================================================================== --- tests/OFHMACTests.m +++ tests/OFHMACTests.m @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * 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 - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFHMAC"; -static const uint8_t key[] = - "yM9h8K6IWnJRvxC/0F8XRWG7RnACDBz8wqK2tbXrYVLoKC3vPLeJikyJSM47tVHc" - "DlXHww9zULAC2sJUlm2Kg1z4oz2aXY3Y1PQSB4VkC/m0DQ7hCI6cAg4TWnKdzWTy" - "cvYGX+Y6HWeDY79/PGSd8fNItme6I8w4HDBqU7BP2sum3jbePJqoiSnhcyJZQTeZ" - "jw0ZXoyrfHgOYD2M+NsTDaGpLblFtQ7n5CczjKtafG40PkEwx1dcrd46U9i3GyTK"; -static const size_t keyLength = sizeof(key); -static const uint8_t MD5Digest[] = - "\xCC\x1F\xEF\x09\x29\xA3\x25\x1A\x06\xA9\x83\x99\xF9\xBC\x8F\x42"; -static const uint8_t SHA1Digest[] = - "\x94\xB9\x0A\x6F\xFB\xA7\x13\x6A\x75\x55" - "\xD5\x7F\x5D\xB7\xF4\xCA\xEB\x4A\xDE\xBF"; -static const uint8_t RIPEMD160Digest[] = - "\x2C\xE1\xED\x41\xC6\xF3\x51\xA8\x04\xD2" - "\xC3\x9B\x08\x33\x3B\xD5\xC9\x00\x39\x50"; -static const uint8_t SHA256Digest[] = - "\xFB\x8C\xDA\x88\xB3\x81\x32\x16\xD7\xD8\x62\xD4\xA6\x26\x9D\x77" - "\x01\x99\x62\x65\x29\x02\x41\xE6\xEF\xA1\x02\x31\xA8\x9D\x77\x5D"; -static const uint8_t SHA384Digest[] = - "\x2F\x4A\x47\xAE\x13\x8E\x96\x52\xF1\x8F\x05\xFD\x65\xCD\x9A\x97" - "\x93\x2F\xC9\x02\xD6\xC6\xAB\x2E\x15\x76\xC0\xA7\xA0\x05\xF4\xEF" - "\x14\x52\x33\x4B\x9C\x5F\xD8\x07\x4E\x98\xAE\x97\x46\x29\x24\xB4"; -static const uint8_t SHA512Digest[] = - "\xF5\x8C\x3F\x9C\xA2\x2F\x0A\xF3\x26\xD8\xC0\x7E\x20\x63\x88\x61" - "\xC9\xE1\x1F\xD7\xC7\xE5\x59\x33\xD5\x2F\xAF\x56\x1C\x94\xC8\xA4" - "\x61\xB3\xF9\x1A\xE3\x09\x43\xA6\x5B\x85\xB1\x50\x5B\xCB\x1A\x2E" - "\xB7\xE8\x87\xC1\x73\x19\x63\xF6\xA2\x91\x8D\x7E\x2E\xCC\xEC\x99"; - -@implementation TestsAppDelegate (OFHMACTests) -- (void)HMACTests -{ - void *pool = objc_autoreleasePoolPush(); - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - OFHMAC *HMACMD5, *HMACSHA1, *HMACRMD160; - OFHMAC *HMACSHA256, *HMACSHA384, *HMACSHA512; - - TEST(@"+[HMACWithHashClass:] with MD5", - (HMACMD5 = [OFHMAC HMACWithHashClass: [OFMD5Hash class] - allowsSwappableMemory: true])) - TEST(@"+[HMACWithHashClass:] with SHA-1", - (HMACSHA1 = [OFHMAC HMACWithHashClass: [OFSHA1Hash class] - allowsSwappableMemory: true])) - TEST(@"+[HMACWithHashClass:] with RIPEMD-160", - (HMACRMD160 = [OFHMAC HMACWithHashClass: [OFRIPEMD160Hash class] - allowsSwappableMemory: true])) - TEST(@"+[HMACWithHashClass:] with SHA-256", - (HMACSHA256 = [OFHMAC HMACWithHashClass: [OFSHA256Hash class] - allowsSwappableMemory: true])) - TEST(@"+[HMACWithHashClass:] with SHA-384", - (HMACSHA384 = [OFHMAC HMACWithHashClass: [OFSHA384Hash class] - allowsSwappableMemory: true])) - TEST(@"+[HMACWithHashClass:] with SHA-512", - (HMACSHA512 = [OFHMAC HMACWithHashClass: [OFSHA512Hash class] - allowsSwappableMemory: true])) - - EXPECT_EXCEPTION(@"Detection of missing key", - OFInvalidArgumentException, - [HMACMD5 updateWithBuffer: "" length: 0]) - - TEST(@"-[setKey:length:] with MD5", - R([HMACMD5 setKey: key length: keyLength])) - TEST(@"-[setKey:length:] with SHA-1", - R([HMACSHA1 setKey: key length: keyLength])) - TEST(@"-[setKey:length:] with RIPEMD-160", - R([HMACRMD160 setKey: key length: keyLength])) - TEST(@"-[setKey:length:] with SHA-256", - R([HMACSHA256 setKey: key length: keyLength])) - TEST(@"-[setKey:length:] with SHA-384", - R([HMACSHA384 setKey: key length: keyLength])) - TEST(@"-[setKey:length:] with SHA-512", - R([HMACSHA512 setKey: key length: keyLength])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [HMACMD5 updateWithBuffer: buffer length: length]; - [HMACSHA1 updateWithBuffer: buffer length: length]; - [HMACRMD160 updateWithBuffer: buffer length: length]; - [HMACSHA256 updateWithBuffer: buffer length: length]; - [HMACSHA384 updateWithBuffer: buffer length: length]; - [HMACSHA512 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[calculate] with MD5", R([HMACMD5 calculate])) - TEST(@"-[calculate] with SHA-1", R([HMACSHA1 calculate])) - TEST(@"-[calculate] with RIPEMD-160", R([HMACRMD160 calculate])) - TEST(@"-[calculate] with SHA-256", R([HMACSHA256 calculate])) - TEST(@"-[calculate] with SHA-384", R([HMACSHA384 calculate])) - TEST(@"-[calculate] with SHA-512", R([HMACSHA512 calculate])) - - TEST(@"-[digest] with MD5", - memcmp(HMACMD5.digest, MD5Digest, HMACMD5.digestSize) == 0) - TEST(@"-[digest] with SHA-1", - memcmp(HMACSHA1.digest, SHA1Digest, HMACSHA1.digestSize) == 0) - TEST(@"-[digest] with RIPEMD-160", - memcmp(HMACRMD160.digest, RIPEMD160Digest, - HMACRMD160.digestSize) == 0) - TEST(@"-[digest] with SHA-256", - memcmp(HMACSHA256.digest, SHA256Digest, HMACSHA256.digestSize) == 0) - TEST(@"-[digest] with SHA-384", - memcmp(HMACSHA384.digest, SHA384Digest, HMACSHA384.digestSize) == 0) - TEST(@"-[digest] with SHA-512", - memcmp(HMACSHA512.digest, SHA512Digest, HMACSHA512.digestSize) == 0) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -101,14 +101,10 @@ @interface TestsAppDelegate (OFIPXSocketTests) - (void)IPXSocketTests; @end -@interface TestsAppDelegate (OFHMACTests) -- (void)HMACTests; -@end - @interface TestsAppDelegate (OFKernelEventObserverTests) - (void)kernelEventObserverTests; @end @interface TestsAppDelegate (OFListTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -386,11 +386,10 @@ [self setTests]; [self valueTests]; [self streamTests]; [self memoryStreamTests]; [self notificationCenterTests]; - [self HMACTests]; #ifdef HAVE_CODEPAGE_437 [self INIFileTests]; #endif #ifdef OF_HAVE_SOCKETS [self TCPSocketTests]; DELETED tests/testfile.bin Index: tests/testfile.bin ================================================================== --- tests/testfile.bin +++ tests/testfile.bin cannot compute difference between binary files