Index: .fossil-settings/clean-glob ================================================================== --- .fossil-settings/clean-glob +++ .fossil-settings/clean-glob @@ -24,17 +24,28 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/tests +new_tests/tests.3dsx +new_tests/tests.arm9 +new_tests/tests.nds +new_tests/tests.nro +new_tests/tests.rpx src/Info.plist src/bridge/Info.plist src/libobjfw.* src/objfw-defs.h src/runtime/Info.plist src/runtime/libobjfwrt.* +src/test/Info.plist +src/test/libobjfwtest.* src/tls/Info.plist +src/tls/libobjfwtls.* tests/DerivedData tests/EBOOT.PBP tests/Info.plist tests/PARAM.SFO tests/objc_sync/objc_sync Index: .fossil-settings/ignore-glob ================================================================== --- .fossil-settings/ignore-glob +++ .fossil-settings/ignore-glob @@ -26,17 +26,28 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/tests +new_tests/tests.3dsx +new_tests/tests.arm9 +new_tests/tests.nds +new_tests/tests.nro +new_tests/tests.rpx src/Info.plist src/bridge/Info.plist src/libobjfw.* src/objfw-defs.h src/runtime/Info.plist src/runtime/libobjfwrt.* +src/test/Info.plist +src/test/libobjfwtest.* src/tls/Info.plist +src/tls/libobjfwtls.* tests/DerivedData tests/EBOOT.PBP tests/Info.plist tests/PARAM.SFO tests/iOS.xcodeproj/*.pbxuser Index: .gitignore ================================================================== --- .gitignore +++ .gitignore @@ -26,17 +26,28 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/tests +new_tests/tests.3dsx +new_tests/tests.arm9 +new_tests/tests.nds +new_tests/tests.nro +new_tests/tests.rpx src/Info.plist src/bridge/Info.plist src/libobjfw.* src/objfw-defs.h src/runtime/Info.plist src/runtime/libobjfwrt.* +src/test/Info.plist +src/test/libobjfwtest.* src/tls/Info.plist +src/tls/libobjfwtls.* tests/DerivedData tests/EBOOT.PBP tests/Info.plist tests/PARAM.SFO tests/iOS.xcodeproj/*.pbxuser Index: Makefile ================================================================== --- Makefile +++ Makefile @@ -1,8 +1,8 @@ include extra.mk -SUBDIRS = src utils tests +SUBDIRS = src utils tests new_tests DISTCLEAN = Info.plist \ aclocal.m4 \ autom4te.cache \ buildsys.mk \ config.h \ @@ -12,14 +12,15 @@ include buildsys.mk .PHONY: check docs release -utils tests: src +utils tests new_tests: src -check: tests +check: tests new_tests cd tests && ${MAKE} -s run + cd new_tests && ${MAKE} -s run docs: rm -fr docs doxygen >/dev/null ADDED new_tests/Makefile Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -0,0 +1,127 @@ +PROG_NOINST = tests${PROG_SUFFIX} +SRCS = OFPBKDF2Tests.m + +include ../buildsys.mk +include ../extra.mk + +.PHONY: run run-on-ios run-on-android +run: + rm -f libobjfw.so.${OBJFW_LIB_MAJOR} + rm -f libobjfw.so.${OBJFW_LIB_MAJOR_MINOR} + rm -f objfw${OBJFW_LIB_MAJOR}.dll libobjfw.${OBJFW_LIB_MAJOR}.dylib + rm -f libobjfwrt.so.${OBJFWRT_LIB_MAJOR} + rm -f libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR} + rm -f objfwrt${OBJFWRT_LIB_MAJOR}.dll + rm -f libobjfwrt.${OBJFWRT_LIB_MAJOR}.dylib + if test -f ../src/libobjfw.so; then \ + ${LN_S} ../src/libobjfw.so libobjfw.so.${OBJFW_LIB_MAJOR}; \ + ${LN_S} ../src/libobjfw.so \ + libobjfw.so.${OBJFW_LIB_MAJOR_MINOR}; \ + elif test -f ../src/libobjfw.so.${OBJFW_LIB_MAJOR_MINOR}; then \ + ${LN_S} ../src/libobjfw.so.${OBJFW_LIB_MAJOR_MINOR} \ + libobjfw.so.${OBJFW_LIB_MAJOR_MINOR}; \ + fi + if test -f ../src/objfw${OBJFW_LIB_MAJOR}.dll; then \ + ${LN_S} ../src/objfw${OBJFW_LIB_MAJOR}.dll \ + objfw${OBJFW_LIB_MAJOR}.dll; \ + fi + if test -f ../src/libobjfw.dylib; then \ + ${LN_S} ../src/libobjfw.dylib \ + libobjfw.${OBJFW_LIB_MAJOR}.dylib; \ + fi + if test -f ../src/runtime/libobjfwrt.so; then \ + ${LN_S} ../src/runtime/libobjfwrt.so \ + libobjfwrt.so.${OBJFWRT_LIB_MAJOR}; \ + ${LN_S} ../src/runtime/libobjfwrt.so \ + libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR}; \ + elif test -f ../src/runtime/libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR}; then \ + ${LN_S} ../src/runtime/libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR} libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR}; \ + fi + if test -f ../src/runtime/objfwrt${OBJFWRT_LIB_MAJOR}.dll; then \ + ${LN_S} ../src/runtime/objfwrt${OBJFWRT_LIB_MAJOR}.dll \ + objfwrt${OBJFWRT_LIB_MAJOR}.dll; \ + fi + if test -f ../src/runtime/libobjfwrt.dylib; then \ + ${LN_S} ../src/runtime/libobjfwrt.dylib \ + libobjfwrt.${OBJFWRT_LIB_MAJOR}.dylib; \ + fi + LD_LIBRARY_PATH=.$${LD_LIBRARY_PATH+:}$$LD_LIBRARY_PATH \ + DYLD_FRAMEWORK_PATH=../src:../src/runtime$${DYLD_FRAMEWORK_PATH+:}$$DYLD_FRAMEWORK_PATH \ + DYLD_LIBRARY_PATH=.$${DYLD_LIBRARY_PATH+:}$$DYLD_LIBRARY_PATH \ + LIBRARY_PATH=.$${LIBRARY_PATH+:}$$LIBRARY_PATH \ + ASAN_OPTIONS=allocator_may_return_null=1 \ + ${WRAPPER} ./${PROG_NOINST}; EXIT=$$?; \ + rm -f libobjfw.so.${OBJFW_LIB_MAJOR}; \ + rm -f libobjfw.so.${OBJFW_LIB_MAJOR_MINOR}; \ + rm -f objfw${OBJFW_LIB_MAJOR}.dll; \ + rm -f libobjfw.${OBJFW_LIB_MAJOR}.dylib; \ + rm -f libobjfwrt.so.${OBJFWRT_LIB_MAJOR}; \ + rm -f libobjfwrt.so.${OBJFWRT_LIB_MAJOR_MINOR}; \ + rm -f objfwrt${OBJFWRT_LIB_MAJOR}.dll; \ + rm -f libobjfwrt.${OBJFWRT_LIB_MAJOR}.dylib; \ + exit $$EXIT + +run-on-android: all + echo "Uploading files to Android device..." + if test -f ../src/libobjfw.so; then \ + adb push ../src/libobjfw.so \ + /data/local/tmp/objfw/libobjfw.so.${OBJFW_LIB_MAJOR}; \ + fi + if test -f ../src/runtime/libobjfwrt.so; then \ + adb push ../src/runtime/libobjfwrt.so \ + /data/local/tmp/objfw/libobjfwrt.so.${OBJFWRT_LIB_MAJOR}; \ + fi + adb push tests /data/local/tmp/objfw/tests + adb push testfile.txt /data/local/tmp/objfw/testfile.txt + if test -f plugin/TestPlugin.so; then \ + adb push plugin/TestPlugin.so \ + /data/local/tmp/objfw/plugin/TestPlugin.so; \ + fi + echo "Running tests binary on Android device..." + adb shell 'cd /data/local/tmp/objfw && LD_LIBRARY_PATH=. exec ${WRAPPER} ./tests' + +EBOOT.PBP: ${PROG_NOINST} + psp-fixup-imports ${PROG_NOINST} + mksfo "ObjFW Tests" PARAM.SFO + psp-strip ${PROG_NOINST} + pack-pbp $@ PARAM.SFO NULL NULL NULL NULL NULL ${PROG_NOINST} NULL + +boot.dol: ${PROG_NOINST} + elf2dol ${PROG_NOINST} $@ + +${PROG_NOINST}: ${LIBOBJFW_DEP} ${LIBOBJFWRT_DEP} ../src/test/libobjfwtest.a + +${PROG_NOINST}.3dsx: ${PROG_NOINST} + 3dsxtool $< $@ + +${PROG_NOINST}.arm9: ${PROG_NOINST} + arm-none-eabi-objcopy -O binary $< $@ + +${PROG_NOINST}.nds: ${PROG_NOINST}.arm9 testfile.txt + rm -fr nds-data + mkdir -p nds-data + cp testfile.txt nds-data + ndstool -c $@ -9 ${PROG_NOINST} -d nds-data + rm -fr nds-data + +${PROG_NOINST}.nro: ${PROG_NOINST} testfile.txt + rm -fr romfs + mkdir -p romfs + cp testfile.txt romfs + nacptool --create "ObjFW tests" "Jonathan Schleifer" \ + "${PACKAGE_VERSION}" tests.nacp + elf2nro ${PROG_NOINST} $@ --nacp=tests.nacp --romfsdir=romfs + rm -fr romfs tests.nacp + +${PROG_NOINST}.rpx: ${PROG_NOINST} + elf2rpl $< $@ + +CPPFLAGS += -I../src \ + -I../src/exceptions \ + -I../src/runtime \ + -I../src/test \ + -I.. \ + -DOBJFWTEST_LOCAL_INCLUDES +LIBS := -L../src/test -lobjfwtest ${TESTS_LIBS} ${LIBS} +LDFLAGS += ${MAP_LDFLAGS} +LD = ${OBJC} ADDED new_tests/OFPBKDF2Tests.m Index: new_tests/OFPBKDF2Tests.m ================================================================== --- new_tests/OFPBKDF2Tests.m +++ new_tests/OFPBKDF2Tests.m @@ -0,0 +1,168 @@ +/* + * 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 OFPBKDF2Tests: OTTestCase +{ + OFHMAC *_HMAC; +} +@end + +@implementation OFPBKDF2Tests +- (void)setUp +{ + _HMAC = [[OFHMAC alloc] initWithHashClass: [OFSHA1Hash class] + allowsSwappableMemory: true]; +} + +- (void)dealloc +{ + [_HMAC release]; + + [super dealloc]; +} + +/* Test vectors from RFC 6070 */ + +- (void)testRFC6070TestVector1 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 1, + .salt = (unsigned char *)"salt", + .saltLength = 4, + .password = "password", + .passwordLength = 8, + .key = key, + .keyLength = 20, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\x0C\x60\xC8\x0F\x96\x1F\x0E\x71\xF3\xA9\xB5" + "\x24\xAF\x60\x12\x06\x2F\xE0\x37\xA6", 20), 0); +} + +- (void)testRFC6070TestVector2 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 2, + .salt = (unsigned char *)"salt", + .saltLength = 4, + .password = "password", + .passwordLength = 8, + .key = key, + .keyLength = 20, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\xEA\x6C\x01\x4D\xC7\x2D\x6F\x8C\xCD\x1E\xD9" + "\x2A\xCE\x1D\x41\xF0\xD8\xDE\x89\x57", 20), 0); +} + +- (void)testRFC6070TestVector3 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 4096, + .salt = (unsigned char *)"salt", + .saltLength = 4, + .password = "password", + .passwordLength = 8, + .key = key, + .keyLength = 20, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\x4B\x00\x79\x01\xB7\x65\x48\x9A\xBE\xAD\x49" + "\xD9\x26\xF7\x21\xD0\x65\xA4\x29\xC1", 20), 0); +} + +#if 0 +/* This test takes too long, even on a fast machine. */ +- (void)testRFC6070TestVector4 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 16777216, + .salt = (unsigned char *)"salt", + .saltLength = 4, + .password = "password", + .passwordLength = 8, + .key = key, + .keyLength = 20, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\xEE\xFE\x3D\x61\xCD\x4D\xA4\xE4\xE9\x94\x5B" + "\x3D\x6B\xA2\x15\x8C\x26\x34\xE9\x84", 20), 0); +} +#endif + +- (void)testRFC6070TestVector5 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 4096, + .salt = (unsigned char *)"saltSALTsaltSALTsalt" + "SALTsaltSALTsalt", + .saltLength = 36, + .password = "passwordPASSWORDpassword", + .passwordLength = 24, + .key = key, + .keyLength = 25, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\x3D\x2E\xEC\x4F\xE4\x1C\x84\x9B\x80\xC8\xD8" + "\x36\x62\xC0\xE4\x4A\x8B\x29\x1A\x96\x4C\xF2\xF0\x70\x38", 25), 0); +} + +- (void)testRFC6070TestVector6 +{ + unsigned char key[25]; + + OFPBKDF2((OFPBKDF2Parameters){ + .HMAC = _HMAC, + .iterations = 4096, + .salt = (unsigned char *)"sa\0lt", + .saltLength = 5, + .password = "pass\0word", + .passwordLength = 9, + .key = key, + .keyLength = 16, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(key, "\x56\xFA\x6A\xA7\x55\x48\x09\x9D\xCC\x37\xD7" + "\xF0\x34\x25\xE0\xC3", 16), 0); +} +@end Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -1,9 +1,9 @@ include ../extra.mk SUBDIRS = ${RUNTIME} exceptions encodings forwarding -SUBDIRS_AFTER = ${BRIDGE} ${TLS} +SUBDIRS_AFTER = ${BRIDGE} ${TLS} test DISTCLEAN = Info.plist objfw-defs.h SHARED_LIB = ${OBJFW_SHARED_LIB} STATIC_LIB = ${OBJFW_STATIC_LIB} FRAMEWORK = ${OBJFW_FRAMEWORK} ADDED src/test/Makefile Index: src/test/Makefile ================================================================== --- src/test/Makefile +++ src/test/Makefile @@ -0,0 +1,50 @@ +include ../../extra.mk + +DISTCLEAN = Info.plist + +STATIC_LIB = libobjfwtest.a + +SRCS = OTAssert.m \ + OTTestCase.m +INCLUDES := ${SRCS:.m=.h} \ + ObjFWTest.h +SRCS += OTAppDelegate.m \ + OTAssertionFailedException.m + +includesubdir = ObjFWTest + +include ../../buildsys.mk + +CPPFLAGS += -I. \ + -I.. \ + -I../.. \ + -I../exceptions \ + -I../runtime \ + -DOBJFWTEST_LOCAL_INCLUDES +LD = ${OBJC} +FRAMEWORK_LIBS := -F.. \ + -framework ObjFW \ + -F../runtime \ + ${RUNTIME_FRAMEWORK_LIBS} \ + ${LIBS} +LIBS := -L.. -lobjfw -L../runtime ${RUNTIME_LIBS} ${LIBS} + +install-extra: + i=ObjFWTest.oc; \ + ${INSTALL_STATUS}; \ + if ${MKDIR_P} ${libdir}/objfw-config && ${INSTALL} -m 644 $$i ${libdir}/objfw-config/$$i; then \ + ${INSTALL_OK}; \ + else \ + ${INSTALL_FAILED}; \ + fi + +uninstall-extra: + i=ObjFWTest.oc; \ + if test -f ${libdir}/objfw-config/$$i; then \ + if rm -f ${libdir}/objfw-config/$$i; then \ + ${DELETE_OK}; \ + else \ + ${DELETE_FAILED}; \ + fi \ + fi; \ + rmdir ${libdir}/objfw-config >/dev/null 2>&1 || true ADDED src/test/OTAppDelegate.h Index: src/test/OTAppDelegate.h ================================================================== --- src/test/OTAppDelegate.h +++ src/test/OTAppDelegate.h @@ -0,0 +1,23 @@ +/* + * 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. + */ + +#import "OFApplication.h" + +OF_ASSUME_NONNULL_BEGIN + +@interface OTAppDelegate: OFObject +@end + +OF_ASSUME_NONNULL_END ADDED src/test/OTAppDelegate.m Index: src/test/OTAppDelegate.m ================================================================== --- src/test/OTAppDelegate.m +++ src/test/OTAppDelegate.m @@ -0,0 +1,167 @@ +/* + * 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" + +#import "OTAppDelegate.h" + +#import "OFColor.h" +#import "OFSet.h" +#import "OFStdIOStream.h" +#import "OFValue.h" + +#import "OTTestCase.h" + +#import "OTAssertionFailedException.h" + +OF_APPLICATION_DELEGATE(OTAppDelegate) + +@implementation OTAppDelegate +- (OFSet OF_GENERIC(Class) *)testClasses +{ + Class *classes = objc_copyClassList(NULL); + OFMutableSet *testClasses; + + if (classes == NULL) + return nil; + + @try { + testClasses = [OFMutableSet set]; + + for (Class *iter = classes; *iter != Nil; iter++) + if ([*iter isSubclassOfClass: [OTTestCase class]]) + [testClasses addObject: *iter]; + } @finally { + OFFreeMemory(classes); + } + + [testClasses removeObject: [OTTestCase class]]; + + [testClasses makeImmutable]; + return testClasses; +} + +- (OFSet OF_GENERIC(OFValue *) *)testsInClass: (Class)class +{ + Method *methods = class_copyMethodList(class, NULL); + OFMutableSet *tests; + + if (methods == NULL) + return nil; + + @try { + tests = [OFMutableSet set]; + + for (Method *iter = methods; *iter != NULL; iter++) { + SEL selector = method_getName(*iter); + + if (selector == NULL) + continue; + + if (strncmp(sel_getName(selector), "test", 4) == 0) + [tests addObject: + [OFValue valueWithPointer: selector]]; + } + } @finally { + OFFreeMemory(methods); + } + + [tests makeImmutable]; + return tests; +} + +- (void)applicationDidFinishLaunching: (OFNotification *)notification +{ + OFSet OF_GENERIC(Class) *testClasses = [self testClasses]; + size_t numSucceeded = 0, numFailed = 0; + + [OFStdOut writeFormat: @"Running %zu test case(s)\n", + testClasses.count]; + + for (Class class in testClasses) { + [OFStdOut writeFormat: @"Running tests in %@\n", class]; + + for (OFValue *test in [self testsInClass: class]) { + void *pool = objc_autoreleasePoolPush(); + bool failed = false; + OTTestCase *instance; + + [OFStdOut setForegroundColor: [OFColor yellow]]; + [OFStdOut writeFormat: + @"-[%@ %s]: ", + class, sel_getName(test.pointerValue)]; + + instance = [[[class alloc] init] autorelease]; + + @try { + [instance setUp]; + [instance performSelector: test.pointerValue]; + } @catch (OTAssertionFailedException *e) { + /* + * If an assertion during -[setUp], don't run + * the test. + * If an assertion fails during a test, abort + * the test. + */ + [OFStdOut setForegroundColor: [OFColor red]]; + [OFStdOut writeFormat: + @"\r-[%@ %s]: failed\n", + class, sel_getName(test.pointerValue)]; + [OFStdOut writeLine: e.description]; + + failed = true; + } + @try { + [instance tearDown]; + } @catch (OTAssertionFailedException *e) { + /* + * If an assertion fails during -[tearDown], + * abort the tear down. + */ + if (!failed) { + [OFStdOut setForegroundColor: + [OFColor red]]; + [OFStdOut writeFormat: + @"\r-[%@ %s]: failed\n", + class, + sel_getName(test.pointerValue)]; + [OFStdOut writeLine: e.description]; + + failed = true; + } + } + + if (!failed) { + [OFStdOut setForegroundColor: [OFColor green]]; + [OFStdOut writeFormat: + @"\r-[%@ %s]: ok\n", + class, sel_getName(test.pointerValue)]; + + numSucceeded++; + } else + numFailed++; + + [OFStdOut reset]; + + objc_autoreleasePoolPop(pool); + } + } + + [OFStdOut writeFormat: @"%zu test(s) succeeded, %zu test(s) failed.\n", + numSucceeded, numFailed]; + + [OFApplication terminate]; +} +@end ADDED src/test/OTAssert.h Index: src/test/OTAssert.h ================================================================== --- src/test/OTAssert.h +++ src/test/OTAssert.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-pragmas" +# pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#endif +#define OTAssert(cond, ...) \ + OTAssertImpl(self, _cmd, cond, @#cond, @__FILE__, __LINE__, \ + ## __VA_ARGS__, nil) +#define OTAssertTrue(cond, ...) OTAssert(cond == true, ## __VA_ARGS__) +#define OTAssertFalse(cond, ...) OTAssert(cond == false, ## __VA_ARGS__) +#define OTAssertEqual(a, b, ...) OTAssert(a == b, ## __VA_ARGS__) +#define OTAssertNotEqual(a, b, ...) OTAssert(a != b, ## __VA_ARGS__) +#define OTAssertEqualObjects(a, b, ...) OTAssert([a isEqual: b], ## __VA_ARGS__) +#define OTAssertNotEqualObjects(a, b, ...) \ + OTAssert(![a isEqual: b], ## __VA_ARGS__) +#ifdef __clang__ +# pragma clang diagnostic pop +#endif + +#ifdef __cplusplus +extern "C" { +#endif +extern void OTAssertImpl(id testCase, SEL test, bool condition, OFString *check, + OFString *file, size_t line, ...); +#ifdef __cplusplus +} +#endif ADDED src/test/OTAssert.m Index: src/test/OTAssert.m ================================================================== --- src/test/OTAssert.m +++ src/test/OTAssert.m @@ -0,0 +1,45 @@ +/* + * 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" + +#import "OFString.h" + +#import "OTAssertionFailedException.h" + +void +OTAssertImpl(id testCase, SEL test, bool condition, OFString *check, + OFString *file, size_t line, ...) +{ + va_list arguments; + OFConstantString *format; + OFString *message = nil; + + if (condition) + return; + + va_start(arguments, line); + format = va_arg(arguments, OFConstantString *); + + if (format != nil) + message = [[[OFString alloc] + initWithFormat: format + arguments: arguments] autorelease]; + + va_end(arguments); + + @throw [OTAssertionFailedException exceptionWithCondition: check + message: message]; +} ADDED src/test/OTAssertionFailedException.h Index: src/test/OTAssertionFailedException.h ================================================================== --- src/test/OTAssertionFailedException.h +++ src/test/OTAssertionFailedException.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#import "OFException.h" +#import "OFString.h" + +OF_ASSUME_NONNULL_BEGIN + +@interface OTAssertionFailedException: OFException +{ + OFString *_condition; + OFString *_Nullable _message; +} + +@property (readonly, nonatomic) OFString *condition; +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *message; + ++ (instancetype)exceptionWithCondition: (OFString *)condition + message: (nullable OFString *)message; ++ (instancetype)exception OF_UNAVAILABLE; +- (instancetype)initWithCondition: (OFString *)condition + message: (nullable OFString *)message; +- (instancetype)init OF_UNAVAILABLE; +@end + +OF_ASSUME_NONNULL_END ADDED src/test/OTAssertionFailedException.m Index: src/test/OTAssertionFailedException.m ================================================================== --- src/test/OTAssertionFailedException.m +++ src/test/OTAssertionFailedException.m @@ -0,0 +1,73 @@ +/* + * 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" + +#import "OTAssertionFailedException.h" + +@implementation OTAssertionFailedException +@synthesize condition = _condition, message = _message; + ++ (instancetype)exceptionWithCondition: (OFString *)condition + message: (OFString *)message +{ + return [[[self alloc] initWithCondition: condition + message: message] autorelease]; +} + ++ (instancetype)exception +{ + OF_UNRECOGNIZED_SELECTOR +} + +- (instancetype)initWithCondition: (OFString *)condition + message: (OFString *)message +{ + self = [super init]; + + @try { + _condition = [condition copy]; + _message = [message copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_condition release]; + [_message release]; + + [super dealloc]; +} + +- (OFString *)description +{ + if (_message != nil) + return [OFString stringWithFormat: @"Assertion failed: %@: %@", + _condition, _message]; + else + return [OFString stringWithFormat: @"Assertion failed: %@", + _condition]; +} +@end ADDED src/test/OTTestCase.h Index: src/test/OTTestCase.h ================================================================== --- src/test/OTTestCase.h +++ src/test/OTTestCase.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#ifdef OBJFWTEST_LOCAL_INCLUDES +# import "OFObject.h" +#else +# import +#endif + +OF_ASSUME_NONNULL_BEGIN + +@interface OTTestCase: OFObject +- (void)setUp; +- (void)tearDown; +@end + +OF_ASSUME_NONNULL_END ADDED src/test/OTTestCase.m Index: src/test/OTTestCase.m ================================================================== --- src/test/OTTestCase.m +++ src/test/OTTestCase.m @@ -0,0 +1,28 @@ +/* + * 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" + +#import "OTTestCase.h" + +@implementation OTTestCase: OFObject +- (void)setUp +{ +} + +- (void)tearDown +{ +} +@end ADDED src/test/ObjFWTest.h Index: src/test/ObjFWTest.h ================================================================== --- src/test/ObjFWTest.h +++ src/test/ObjFWTest.h @@ -0,0 +1,17 @@ +/* + * 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. + */ + +#import "OTTestCase.h" +#import "OTAssert.h" ADDED src/test/ObjFWTest.oc Index: src/test/ObjFWTest.oc ================================================================== --- src/test/ObjFWTest.oc +++ src/test/ObjFWTest.oc @@ -0,0 +1,3 @@ +package_format 1 +LIBS="-lobjfwtest $LIBS" +FRAMEWORK_LIBS="-lobjfwtest $FRAMEWORK_LIBS" Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -37,11 +37,10 @@ OFMemoryStreamTests.m \ OFMethodSignatureTests.m \ OFNotificationCenterTests.m \ OFNumberTests.m \ OFObjectTests.m \ - OFPBKDF2Tests.m \ OFPropertyListTests.m \ OFRIPEMD160HashTests.m \ OFSHA1HashTests.m \ OFSHA224HashTests.m \ OFSHA256HashTests.m \ DELETED tests/OFPBKDF2Tests.m Index: tests/OFPBKDF2Tests.m ================================================================== --- tests/OFPBKDF2Tests.m +++ tests/OFPBKDF2Tests.m @@ -1,125 +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 = @"OFPBKDF2"; - -@implementation TestsAppDelegate (OFPBKDF2Tests) -- (void)PBKDF2Tests -{ - void *pool = objc_autoreleasePoolPush(); - OFHMAC *HMAC = [OFHMAC HMACWithHashClass: [OFSHA1Hash class] - allowsSwappableMemory: true]; - unsigned char key[25]; - - /* Test vectors from RFC 6070 */ - - TEST(@"PBKDF2-SHA1, 1 iteration", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 1, - .salt = (unsigned char *)"salt", - .saltLength = 4, - .password = "password", - .passwordLength = 8, - .key = key, - .keyLength = 20, - .allowsSwappableMemory = true - })) && memcmp(key, "\x0C\x60\xC8\x0F\x96\x1F\x0E\x71\xF3\xA9\xB5" - "\x24\xAF\x60\x12\x06\x2F\xE0\x37\xA6", 20) == 0) - - TEST(@"PBKDF2-SHA1, 2 iterations", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 2, - .salt = (unsigned char *)"salt", - .saltLength = 4, - .password = "password", - .passwordLength = 8, - .key = key, - .keyLength = 20, - .allowsSwappableMemory = true - })) && memcmp(key, "\xEA\x6C\x01\x4D\xC7\x2D\x6F\x8C\xCD\x1E\xD9" - "\x2A\xCE\x1D\x41\xF0\xD8\xDE\x89\x57", 20) == 0) - - TEST(@"PBKDF2-SHA1, 4096 iterations", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 4096, - .salt = (unsigned char *)"salt", - .saltLength = 4, - .password = "password", - .passwordLength = 8, - .key = key, - .keyLength = 20, - .allowsSwappableMemory = true - })) && memcmp(key, "\x4B\x00\x79\x01\xB7\x65\x48\x9A\xBE\xAD\x49" - "\xD9\x26\xF7\x21\xD0\x65\xA4\x29\xC1", 20) == 0) - - /* This test takes too long, even on a fast machine. */ -#if 0 - TEST(@"PBKDF2-SHA1, 16777216 iterations", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 16777216, - .salt = (unsigned char *)"salt", - .saltLength = 4, - .password = "password", - .passwordLength = 8, - .key = key, - .keyLength = 20, - .allowsSwappableMemory = true - })) && memcmp(key, "\xEE\xFE\x3D\x61\xCD\x4D\xA4\xE4\xE9\x94\x5B" - "\x3D\x6B\xA2\x15\x8C\x26\x34\xE9\x84", 20) == 0) -#endif - - TEST(@"PBKDF2-SHA1, 4096 iterations, key > 1 block", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 4096, - .salt = (unsigned char *)"saltSALTsaltSALTsalt" - "SALTsaltSALTsalt", - .saltLength = 36, - .password = "passwordPASSWORDpassword", - .passwordLength = 24, - .key = key, - .keyLength = 25, - .allowsSwappableMemory = true - })) && - memcmp(key, "\x3D\x2E\xEC\x4F\xE4\x1C\x84\x9B\x80\xC8\xD8\x36\x62" - "\xC0\xE4\x4A\x8B\x29\x1A\x96\x4C\xF2\xF0\x70\x38", 25) == 0) - - TEST(@"PBKDF2-SHA1, 4096 iterations, key < 1 block", - R(OFPBKDF2((OFPBKDF2Parameters){ - .HMAC = HMAC, - .iterations = 4096, - .salt = (unsigned char *)"sa\0lt", - .saltLength = 5, - .password = "pass\0word", - .passwordLength = 9, - .key = key, - .keyLength = 16, - .allowsSwappableMemory = true - })) && memcmp(key, "\x56\xFA\x6A\xA7\x55\x48\x09\x9D\xCC\x37\xD7" - "\xF0\x34\x25\xE0\xC3", 16) == 0) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -173,14 +173,10 @@ @interface TestsAppDelegate (OFObjectTests) - (void)objectTests; @end -@interface TestsAppDelegate (OFPBKDF2Tests) -- (void)PBKDF2Tests; -@end - @interface TestsAppDelegate (OFPropertyListTests) - (void)propertyListTests; @end @interface TestsAppDelegate (OFPluginTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -401,11 +401,10 @@ [self SHA224HashTests]; [self SHA256HashTests]; [self SHA384HashTests]; [self SHA512HashTests]; [self HMACTests]; - [self PBKDF2Tests]; [self scryptTests]; #ifdef HAVE_CODEPAGE_437 [self INIFileTests]; #endif #ifdef OF_HAVE_SOCKETS