Index: .fossil-settings/clean-glob ================================================================== --- .fossil-settings/clean-glob +++ .fossil-settings/clean-glob @@ -24,10 +24,22 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/plugin/Info.plist +new_tests/subprocess/subprocess +new_tests/testfile_bin.m +new_tests/testfile_ini.m +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 @@ -38,15 +50,11 @@ tests/DerivedData tests/EBOOT.PBP tests/Info.plist tests/PARAM.SFO tests/objc_sync/objc_sync -tests/plugin/Info.plist -tests/subprocess/subprocess tests/terminal/terminal_tests -tests/testfile_bin.m -tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds tests/tests.nro Index: .fossil-settings/ignore-glob ================================================================== --- .fossil-settings/ignore-glob +++ .fossil-settings/ignore-glob @@ -26,10 +26,22 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/plugin/Info.plist +new_tests/subprocess/subprocess +new_tests/testfile_bin.m +new_tests/testfile_ini.m +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 @@ -43,15 +55,11 @@ tests/PARAM.SFO tests/iOS.xcodeproj/*.pbxuser tests/iOS.xcodeproj/project.xcworkspace tests/iOS.xcodeproj/xcuserdata tests/objc_sync/objc_sync -tests/plugin/Info.plist -tests/subprocess/subprocess tests/terminal/terminal_tests -tests/testfile_bin.m -tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds tests/tests.nro Index: .gitignore ================================================================== --- .gitignore +++ .gitignore @@ -26,10 +26,22 @@ configure docs extra.mk generators/library/gen_libraries generators/unicode/gen_tables +new_tests/EBOOT.PBP +new_tests/PARAM.SFO +new_tests/plugin/Info.plist +new_tests/subprocess/subprocess +new_tests/testfile_bin.m +new_tests/testfile_ini.m +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 @@ -43,15 +55,11 @@ tests/PARAM.SFO tests/iOS.xcodeproj/*.pbxuser tests/iOS.xcodeproj/project.xcworkspace tests/iOS.xcodeproj/xcuserdata tests/objc_sync/objc_sync -tests/plugin/Info.plist -tests/subprocess/subprocess tests/terminal/terminal_tests -tests/testfile_bin.m -tests/testfile_ini.m tests/tests tests/tests.3dsx tests/tests.arm9 tests/tests.nds tests/tests.nro 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 Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -439,11 +439,11 @@ AC_DEFINE_UNQUOTED(PLUGIN_SUFFIX, "$PLUGIN_SUFFIX", [Suffix for plugins]) AS_IF([test x"$enable_files" != x"no" -a x"$PLUGIN_SUFFIX" != x""], [ AC_SUBST(USE_SRCS_PLUGINS, '${SRCS_PLUGINS}') AC_SUBST(TESTPLUGIN, "plugin") AC_DEFINE(OF_HAVE_PLUGINS, 1, [Whether we have plugin support]) - AC_CONFIG_FILES(tests/plugin/Info.plist) + AC_CONFIG_FILES(new_tests/plugin/Info.plist) AS_IF([test x"$build_framework" = x"yes"], [ TESTPLUGIN_LIBS="-F../../src -F../../src/runtime" TESTPLUGIN_LIBS="$TESTPLUGIN_LIBS -framework ObjFW" TESTPLUGIN_LIBS="$TESTPLUGIN_LIBS \${RUNTIME_FRAMEWORK_LIBS}" @@ -1979,11 +1979,11 @@ break ]) ]) ]) AS_IF([test x"$have_subprocesses" = x"yes"], [ - AC_SUBST(USE_SRCS_SUBPROCESS, '${SRCS_SUBPROCESS}') + AC_SUBST(USE_SRCS_SUBPROCESSES, '${SRCS_SUBPROCESSES}') AC_SUBST(SUBPROCESS, "subprocess") AC_DEFINE(OF_HAVE_SUBPROCESSES, 1, [Whether we have subprocesses]) ]) AC_CHECK_HEADERS_ONCE([complex.h sys/ioctl.h sys/ttycom.h]) Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -84,11 +84,11 @@ USE_SRCS_APPLETALK = @USE_SRCS_APPLETALK@ USE_SRCS_FILES = @USE_SRCS_FILES@ USE_SRCS_IPX = @USE_SRCS_IPX@ USE_SRCS_PLUGINS = @USE_SRCS_PLUGINS@ USE_SRCS_SOCKETS = @USE_SRCS_SOCKETS@ -USE_SRCS_SUBPROCESS = @USE_SRCS_SUBPROCESS@ +USE_SRCS_SUBPROCESSES = @USE_SRCS_SUBPROCESSES@ USE_SRCS_TAGGED_POINTERS = @USE_SRCS_TAGGED_POINTERS@ USE_SRCS_THREADS = @USE_SRCS_THREADS@ USE_SRCS_UNIX_SOCKETS = @USE_SRCS_UNIX_SOCKETS@ USE_SRCS_WINDOWS = @USE_SRCS_WINDOWS@ WRAPPER = @WRAPPER@ ADDED new_tests/Makefile Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -0,0 +1,182 @@ +include ../extra.mk + +SUBDIRS = ${TESTPLUGIN} ${SUBPROCESS} + +CLEAN = EBOOT.PBP \ + boot.dol \ + ${PROG_NOINST}.arm9 \ + ${PROG_NOINST}.nds \ + ${PROG_NOINST}.nro \ + ${PROG_NOINST}.rpx \ + testfile_bin.m \ + testfile_ini.m + +PROG_NOINST = tests${PROG_SUFFIX} +SRCS = OFArrayTests.m \ + OFCharacterSetTests.m \ + OFColorTests.m \ + OFConcreteArrayTests.m \ + OFConcreteMutableArrayTests.m \ + OFCryptographicHashTests.m \ + OFDateTests.m \ + OFHMACTests.m \ + OFINIFileTests.m \ + OFIRITests.m \ + OFInvocationTests.m \ + OFJSONTests.m \ + OFMatrix4x4Tests.m \ + OFMethodSignatureTests.m \ + OFMutableArrayTests.m \ + OFNumberTests.m \ + OFPBKDF2Tests.m \ + OFPropertyListTests.m \ + OFScryptTests.m \ + ${USE_SRCS_PLUGINS} \ + ${USE_SRCS_SOCKETS} \ + ${USE_SRCS_SUBPROCESSES} \ + ${USE_SRCS_THREADS} \ + testfile_bin.m \ + testfile_ini.m +SRCS_PLUGINS = OFPluginTests.m +SRCS_SOCKETS = OFSocketTests.m +SRCS_SUBPROCESSES = OFSubprocessTests.m +SRCS_THREADS = OFThreadTests.m + +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: + 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 \ + -DPROG_SUFFIX=\"${PROG_SUFFIX}\" +# Repetition is required for Wii U, as otherwise it cannot find main. Just +# moving -lobjfwtest later doesn't work either, as then the linker cannot find +# ObjFW symbols. So the only solution is to list everything twice. +LIBS := -L../src/test \ + -lobjfwtest \ + ${TESTS_LIBS} \ + ${LIBS} \ + -lobjfwtest \ + ${TESTS_LIBS} \ + ${LIBS} +LDFLAGS += ${MAP_LDFLAGS} +LD = ${OBJC} ADDED new_tests/OFArrayTests.h Index: new_tests/OFArrayTests.h ================================================================== --- new_tests/OFArrayTests.h +++ new_tests/OFArrayTests.h @@ -0,0 +1,25 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFArrayTests: OTTestCase +{ + OFArray *_array; +} + +- (Class)arrayClass; +@end ADDED new_tests/OFArrayTests.m Index: new_tests/OFArrayTests.m ================================================================== --- new_tests/OFArrayTests.m +++ new_tests/OFArrayTests.m @@ -0,0 +1,342 @@ +/* + * 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 "OFArrayTests.h" + +@interface CustomArray: OFArray +{ + OFMutableArray *_array; +} +@end + +static OFString *const cArray[] = { + @"Foo", + @"Bar", + @"Baz" +}; + +@implementation OFArrayTests +- (Class)arrayClass +{ + return [CustomArray class]; +} + +- (void)setUp +{ + [super setUp]; + + _array = [[self.arrayClass alloc] + initWithObjects: cArray + count: sizeof(cArray) / sizeof(*cArray)]; +} + +- (void)dealloc +{ + [_array release]; + + [super dealloc]; +} + +- (void)testArray +{ + OFArray *array = [self.arrayClass array]; + + OTAssertNotNil(array); + OTAssertEqual(array.count, 0); +} + +- (void)testArrayWithObjects +{ + OFArray *array = [self.arrayClass arrayWithObjects: + @"Foo", @"Bar", @"Baz", nil]; + + OTAssertNotNil(array); + OTAssertEqual(array.count, 3); + OTAssertEqualObjects([array objectAtIndex: 0], @"Foo"); + OTAssertEqualObjects([array objectAtIndex: 1], @"Bar"); + OTAssertEqualObjects([array objectAtIndex: 2], @"Baz"); +} + +- (void)testArrayWithObjectsCount +{ + OFArray *array1 = [self.arrayClass arrayWithObjects: + @"Foo", @"Bar", @"Baz", nil]; + OFArray *array2 = [self.arrayClass arrayWithObjects: cArray count: 3]; + + OTAssertEqualObjects(array1, array2); +} + +- (void)testDescription +{ + OTAssertEqualObjects(_array.description, + @"(\n\tFoo,\n\tBar,\n\tBaz\n)"); +} + +- (void)testCount +{ + OTAssertEqual(_array.count, 3); +} + +- (void)testIsEqual +{ + OFArray *array = [self.arrayClass arrayWithObjects: cArray count: 3]; + + OTAssertEqualObjects(array, _array); + OTAssertNotEqual(array, _array); +} + +- (void)testObjectAtIndex +{ + OTAssertEqualObjects([_array objectAtIndex: 0], cArray[0]); + OTAssertEqualObjects([_array objectAtIndex: 1], cArray[1]); + OTAssertEqualObjects([_array objectAtIndex: 2], cArray[2]); +} + +- (void)testObjectAtIndexFailsWhenOutOfRange +{ + OTAssertThrowsSpecific([_array objectAtIndex: _array.count], + OFOutOfRangeException); +} + +- (void)testContainsObject +{ + OTAssertTrue([_array containsObject: cArray[1]]); + OTAssertFalse([_array containsObject: @"nonexistent"]); +} + +- (void)testContainsObjectIdenticalTo +{ + OTAssertTrue([_array containsObjectIdenticalTo: cArray[1]]); + OTAssertFalse([_array containsObjectIdenticalTo: + [OFString stringWithString: cArray[1]]]); +} + +- (void)testIndexOfObject +{ + OTAssertEqual([_array indexOfObject: cArray[1]], 1); + OTAssertEqual([_array indexOfObject: @"nonexistent"], OFNotFound); +} + +- (void)testIndexOfObjectIdenticalTo +{ + OTAssertEqual([_array indexOfObjectIdenticalTo: cArray[1]], 1); + OTAssertEqual([_array indexOfObjectIdenticalTo: + [OFString stringWithString: cArray[1]]], + OFNotFound); +} + +- (void)objectsInRange +{ + OTAssertEqualObjects([_array objectsInRange: OFMakeRange(1, 2)], + ([self.arrayClass arrayWithObjects: cArray[1], cArray[2], nil])); +} + +- (void)testEnumerator +{ + OFEnumerator *enumerator = [_array objectEnumerator]; + + OTAssertEqualObjects([enumerator nextObject], cArray[0]); + OTAssertEqualObjects([enumerator nextObject], cArray[1]); + OTAssertEqualObjects([enumerator nextObject], cArray[2]); + OTAssertNil([enumerator nextObject]); +} + +- (void)testFastEnumeration +{ + size_t i = 0; + + for (OFString *object in _array) { + OTAssert(i < 3); + OTAssertEqualObjects(object, cArray[i++]); + } +} + +- (void)testComponentsJoinedByString +{ + OFArray *array; + + array = [self.arrayClass arrayWithObjects: @"", @"a", @"b", @"c", nil]; + OTAssertEqualObjects([array componentsJoinedByString: @" "], + @" a b c"); + + array = [self.arrayClass arrayWithObject: @"foo"]; + OTAssertEqualObjects([array componentsJoinedByString: @" "], @"foo"); +} + +- (void)testComponentsJoinedByStringOptions +{ + OFArray *array; + + array = [self.arrayClass + arrayWithObjects: @"", @"foo", @"", @"", @"bar", @"", nil]; + OTAssertEqualObjects( + [array componentsJoinedByString: @" " + options: OFArraySkipEmptyComponents], + @"foo bar"); +} + +- (void)testSortedArray +{ + OFArray *array = [_array arrayByAddingObjectsFromArray: + [OFArray arrayWithObjects: @"0", @"z", nil]]; + + OTAssertEqualObjects([array sortedArray], + ([OFArray arrayWithObjects: @"0", @"Bar", @"Baz", @"Foo", @"z", + nil])); + + OTAssertEqualObjects( + [array sortedArrayUsingSelector: @selector(compare:) + options: OFArraySortDescending], + ([OFArray arrayWithObjects: @"z", @"Foo", @"Baz", @"Bar", @"0", + nil])); +} + +- (void)testReversedArray +{ + OTAssertEqualObjects(_array.reversedArray, + ([OFArray arrayWithObjects: cArray[2], cArray[1], cArray[0], nil])); +} + +#ifdef OF_HAVE_BLOCKS +- (void)testEnumerateObjectsUsingBlock +{ + __block size_t i = 0; + + [_array enumerateObjectsUsingBlock: + ^ (id object, size_t idx, bool *stop) { + OTAssertEqualObjects(object, [_array objectAtIndex: i++]); + }]; + + OTAssertEqual(i, _array.count); +} + +- (void)testMappedArrayUsingBlock +{ + OTAssertEqualObjects( + [_array mappedArrayUsingBlock: ^ id (id object, size_t idx) { + switch (idx) { + case 0: + return @"foobar"; + case 1: + return @"qux"; + } + + return @""; + }].description, + @"(\n\tfoobar,\n\tqux,\n\t\n)"); +} + +- (void)testFilteredArrayUsingBlock +{ + OTAssertEqualObjects( + [_array filteredArrayUsingBlock: ^ bool (id object, size_t idx) { + return [object isEqual: @"Foo"]; + }].description, + @"(\n\tFoo\n)"); + +} + +- (void)testFoldUsingBlock +{ + OTAssertEqualObjects( + [([self.arrayClass arrayWithObjects: [OFMutableString string], + @"foo", @"bar", @"baz", nil]) + foldUsingBlock: ^ id (id left, id right) { + [left appendString: right]; + return left; + }], + @"foobarbaz"); +} +#endif + +- (void)testValueForKey +{ + OTAssertEqualObjects( + [([self.arrayClass arrayWithObjects: @"foo", @"bar", @"quxqux", + nil]) valueForKey: @"length"], + ([self.arrayClass arrayWithObjects: [OFNumber numberWithInt: 3], + [OFNumber numberWithInt: 3], [OFNumber numberWithInt: 6], nil])); + + OTAssertEqualObjects( + [([self.arrayClass arrayWithObjects: @"1", @"2", nil]) + valueForKey: @"@count"], + [OFNumber numberWithInt: 2]); +} +@end + +@implementation CustomArray +- (instancetype)init +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] init]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithObject: (id)object arguments: (va_list)arguments +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] initWithObject: object + arguments: arguments]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithObjects: (id const *)objects count: (size_t)count +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] initWithObjects: objects + count: count]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_array release]; + + [super dealloc]; +} + +- (id)objectAtIndex: (size_t)idx +{ + return [_array objectAtIndex: idx]; +} + +- (size_t)count +{ + return [_array count]; +} +@end ADDED new_tests/OFCharacterSetTests.m Index: new_tests/OFCharacterSetTests.m ================================================================== --- new_tests/OFCharacterSetTests.m +++ new_tests/OFCharacterSetTests.m @@ -0,0 +1,100 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +#import "OFCharacterSet.h" +#import "OFBitSetCharacterSet.h" +#import "OFRangeCharacterSet.h" + +@interface OFCharacterSetTests: OTTestCase +@end + +@interface CustomCharacterSet: OFCharacterSet +@end + +@implementation CustomCharacterSet +- (bool)characterIsMember: (OFUnichar)character +{ + return (character % 2 == 0); +} +@end + +@implementation OFCharacterSetTests +- (void)testCustomCharacterSet +{ + OFCharacterSet *characterSet = + [[[CustomCharacterSet alloc] init] autorelease]; + + for (OFUnichar c = 0; c < 65536; c++) + if (c % 2 == 0) + OTAssertTrue([characterSet characterIsMember: c]); + else + OTAssertFalse([characterSet characterIsMember: c]); +} + +- (void)testBitSetCharacterSet +{ + OFCharacterSet *characterSet = + [OFCharacterSet characterSetWithCharactersInString: @"0123456789"]; + + OTAssertTrue( + [characterSet isKindOfClass: [OFBitSetCharacterSet class]]); + + for (OFUnichar c = 0; c < 65536; c++) + if (c >= '0' && c <= '9') + OTAssertTrue([characterSet characterIsMember: c]); + else if ([characterSet characterIsMember: c]) + OTAssertFalse([characterSet characterIsMember: c]); +} + +- (void)testRangeCharacterSet +{ + OFCharacterSet *characterSet = + [OFCharacterSet characterSetWithRange: OFMakeRange('0', 10)]; + + OTAssertTrue( + [characterSet isKindOfClass: [OFRangeCharacterSet class]]); + + for (OFUnichar c = 0; c < 65536; c++) + if (c >= '0' && c <= '9') + OTAssertTrue([characterSet characterIsMember: c]); + else + OTAssertFalse([characterSet characterIsMember: c]); +} + +- (void)testInvertedCharacterSet +{ + OFCharacterSet *characterSet = [[OFCharacterSet + characterSetWithRange: OFMakeRange('0', 10)] invertedSet]; + + for (OFUnichar c = 0; c < 65536; c++) + if (c >= '0' && c <= '9') + OTAssertFalse([characterSet characterIsMember: c]); + else + OTAssertTrue([characterSet characterIsMember: c]); +} + +- (void)testInvertingInvertedSetReturnsOriginal +{ + OFCharacterSet *characterSet = + [OFCharacterSet characterSetWithRange: OFMakeRange('0', 10)]; + + OTAssertEqual(characterSet, characterSet.invertedSet.invertedSet); +} +@end ADDED new_tests/OFColorTests.m Index: new_tests/OFColorTests.m ================================================================== --- new_tests/OFColorTests.m +++ new_tests/OFColorTests.m @@ -0,0 +1,62 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFColorTests: OTTestCase +{ + OFColor *_color; +} +@end + +@implementation OFColorTests +- (void)setUp +{ + [super setUp]; + + _color = [[OFColor alloc] initWithRed: 63.f / 255 + green: 127.f / 255 + blue: 1 + alpha: 1]; +} + +- (void)dealloc +{ + [_color release]; + + [super dealloc]; +} + +#ifdef OF_OBJFW_RUNTIME +- (void)testReturnsTaggedPointer +{ + OTAssertTrue(object_isTaggedPointer(_color)); +} +#endif + +- (void)testGetRedGreenBlueAlpha +{ + float red, green, blue, alpha; + + [_color getRed: &red green: &green blue: &blue alpha: &alpha]; + OTAssertEqual(red, 63.f / 255); + OTAssertEqual(green, 127.f / 255); + OTAssertEqual(blue, 1); + OTAssertEqual(alpha, 1); +} +@end ADDED new_tests/OFConcreteArrayTests.m Index: new_tests/OFConcreteArrayTests.m ================================================================== --- new_tests/OFConcreteArrayTests.m +++ new_tests/OFConcreteArrayTests.m @@ -0,0 +1,30 @@ +/* + * 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 "OFArrayTests.h" + +#import "OFConcreteArray.h" + +@interface OFConcreteArrayTests: OFArrayTests +@end + +@implementation OFConcreteArrayTests +- (Class)arrayClass +{ + return [OFConcreteArray class]; +} +@end ADDED new_tests/OFConcreteMutableArrayTests.m Index: new_tests/OFConcreteMutableArrayTests.m ================================================================== --- new_tests/OFConcreteMutableArrayTests.m +++ new_tests/OFConcreteMutableArrayTests.m @@ -0,0 +1,105 @@ +/* + * 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 "OFMutableArrayTests.h" + +#import "OFConcreteMutableArray.h" + +@interface OFConcreteMutableArrayTests: OFMutableArrayTests +@end + +static OFString *const cArray[] = { + @"Foo", + @"Bar", + @"Baz" +}; + +@implementation OFConcreteMutableArrayTests +- (Class)arrayClass +{ + return [OFConcreteMutableArray class]; +} + +- (void)testDetectMutationDuringEnumeration +{ + OFEnumerator *enumerator = [_mutableArray objectEnumerator]; + OFString *object; + size_t i; + + i = 0; + while ((object = [enumerator nextObject]) != nil) { + OTAssertEqualObjects(object, cArray[i]); + + [_mutableArray replaceObjectAtIndex: i withObject: @""]; + i++; + } + OTAssertEqual(i, _mutableArray.count); + + [_mutableArray removeObjectAtIndex: 0]; + OTAssertThrowsSpecific([enumerator nextObject], + OFEnumerationMutationException); +} + +- (void)testDetectMutationDuringFastEnumeration +{ + bool detected = false; + size_t i; + + i = 0; + for (OFString *object in _mutableArray) { + OTAssertEqualObjects(object, cArray[i]); + + [_mutableArray replaceObjectAtIndex: i withObject: @""]; + i++; + } + OTAssertEqual(i, _mutableArray.count); + + @try { + for (OFString *object in _mutableArray) { + (void)object; + [_mutableArray removeObjectAtIndex: 0]; + } + } @catch (OFEnumerationMutationException *e) { + detected = true; + } + OTAssertTrue(detected); +} + +#ifdef OF_HAVE_BLOCKS +- (void)testDetectMutationDuringEnumerateObjectsUsingBlock +{ + __block size_t i; + + i = 0; + [_mutableArray enumerateObjectsUsingBlock: + ^ (id object, size_t idx, bool *stop) { + OTAssertEqualObjects(object, cArray[idx]); + + [_mutableArray replaceObjectAtIndex: idx withObject: @""]; + i++; + }]; + OTAssertEqual(i, _mutableArray.count); + + OTAssertThrowsSpecific( + [_mutableArray enumerateObjectsUsingBlock: + ^ (id object, size_t idx, bool *stop) { + [_mutableArray removeObjectAtIndex: 0]; + }], + OFEnumerationMutationException); +} +#endif +@end ADDED new_tests/OFCryptographicHashTests.m Index: new_tests/OFCryptographicHashTests.m ================================================================== --- new_tests/OFCryptographicHashTests.m +++ new_tests/OFCryptographicHashTests.m @@ -0,0 +1,140 @@ +/* + * 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 OFCryptographicHashTests: OTTestCase +{ + OFStream *_stream; +} +@end + +const unsigned char testFileMD5[16] = + "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38"; +const unsigned char testFileRIPEMD160[20] = + "\x46\x02\x97\xF5\x85\xDF\xB9\x21\x00\xC8\xF9\x87\xC6\xEC\x84\x0D" + "\xCE\xE6\x08\x8B"; +const unsigned char testFileSHA1[20] = + "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96" + "\xF5\x94\xE7\x17"; +const unsigned char testFileSHA224[28] = + "\x27\x69\xD8\x04\x2D\x0F\xCA\x84\x6C\xF1\x62\x44\xBA\x0C\xBD\x46" + "\x64\x5F\x4F\x20\x02\x4D\x15\xED\x1C\x61\x1F\xF7"; +const unsigned char testFileSHA256[32] = + "\x1A\x02\xD6\x46\xF5\xA6\xBA\xAA\xFF\x7F\xD5\x87\xBA\xC3\xF6\xC6" + "\xB5\x67\x93\x8F\x0F\x44\x90\xB8\xF5\x35\x89\xF0\x5A\x23\x7F\x69"; +const unsigned char testFileSHA384[48] = + "\x7E\xDE\x62\xE2\x10\xA5\x1E\x18\x8A\x11\x7F\x78\xD7\xC7\x55\xB6" + "\x43\x94\x1B\xD2\x78\x5C\xCF\xF3\x8A\xB8\x98\x22\xC7\x0E\xFE\xF1" + "\xEC\x53\xE9\x1A\xB3\x51\x70\x8C\x1F\x3F\x56\x12\x44\x01\x91\x54"; +const unsigned char testFileSHA512[64] = + "\x8F\x36\x6E\x3C\x19\x4B\xBB\xC7\x82\xAA\xCD\x7D\x55\xA2\xD3\x29" + "\x29\x97\x6A\x3F\xEB\x9B\xB2\xCB\x75\xC9\xEC\xC8\x10\x07\xD6\x07" + "\x31\x4A\xB1\x30\x97\x82\x58\xA5\x1F\x71\x42\xE6\x56\x07\x99\x57" + "\xB2\xB8\x3B\xA1\x8A\x41\x64\x33\x69\x21\x8C\x2A\x44\x6D\xF2\xA0"; + +@implementation OFCryptographicHashTests +- (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)testHash: (Class)hashClass + expectedDigest: (const unsigned char *)expectedDigest +{ + id hash = + [hashClass hashWithAllowsSwappableMemory: true]; + id copy; + + OTAssertNotNil(hash); + + while (!_stream.atEndOfStream) { + char buffer[64]; + size_t length = [_stream readIntoBuffer: buffer length: 64]; + [hash updateWithBuffer: buffer length: length]; + } + + copy = [[hash copy] autorelease]; + + [hash calculate]; + [copy calculate]; + + OTAssertEqual(memcmp(hash.digest, expectedDigest, hash.digestSize), 0); + OTAssertEqual(memcmp(hash.digest, expectedDigest, hash.digestSize), 0); + + OTAssertThrowsSpecific([hash updateWithBuffer: "" length: 1], + OFHashAlreadyCalculatedException); +} + +- (void)testMD5 +{ + [self testHash: [OFMD5Hash class] expectedDigest: testFileMD5]; +} + +- (void)testRIPEMD160 +{ + [self testHash: [OFRIPEMD160Hash class] + expectedDigest: testFileRIPEMD160]; +} + +- (void)testSHA1 +{ + [self testHash: [OFSHA1Hash class] expectedDigest: testFileSHA1]; +} + +- (void)testSHA224 +{ + [self testHash: [OFSHA224Hash class] expectedDigest: testFileSHA224]; +} + +- (void)testSHA256 +{ + [self testHash: [OFSHA256Hash class] expectedDigest: testFileSHA256]; +} + +- (void)testSHA384 +{ + [self testHash: [OFSHA384Hash class] expectedDigest: testFileSHA384]; +} + +- (void)testSHA512 +{ + [self testHash: [OFSHA512Hash class] expectedDigest: testFileSHA512]; +} +@end ADDED new_tests/OFDateTests.m Index: new_tests/OFDateTests.m ================================================================== --- new_tests/OFDateTests.m +++ new_tests/OFDateTests.m @@ -0,0 +1,198 @@ +/* + * 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" + +#import "OFStrPTime.h" + +@interface OFDateTests: OTTestCase +{ + OFDate *_date[2]; +} +@end + +@implementation OFDateTests +- (void)setUp +{ + [super setUp]; + + _date[0] = [[OFDate alloc] initWithTimeIntervalSince1970: 0]; + _date[1] = [[OFDate alloc] + initWithTimeIntervalSince1970: 3600 * 25 + 5.000002]; +} + +- (void)dealloc +{ + [_date[0] release]; + [_date[1] release]; + + [super dealloc]; +} + +- (void)testStrPTime +{ + struct tm tm; + int16_t timeZone; + const char *dateString = "Wed, 09 Jun 2021 +0200x"; + + OTAssertEqual(OFStrPTime(dateString, "%a, %d %b %Y %z", &tm, &timeZone), + dateString + 22); + OTAssertEqual(tm.tm_wday, 3); + OTAssertEqual(tm.tm_mday, 9); + OTAssertEqual(tm.tm_mon, 5); + OTAssertEqual(tm.tm_year, 2021 - 1900); + OTAssertEqual(timeZone, 2 * 60); +} + +- (void)testDateByAddingTimeInterval +{ + OTAssertEqualObjects( + [_date[0] dateByAddingTimeInterval: 3600 * 25 + 5.000002], + _date[1]); +} + +- (void)testDescription +{ + OTAssertEqualObjects(_date[0].description, @"1970-01-01T00:00:00Z"); + OTAssertEqualObjects(_date[1].description, @"1970-01-02T01:00:05Z"); +} + +- (void)testDateWithDateStringFormat +{ + OTAssertEqualObjects( + [[OFDate dateWithDateString: @"2000-06-20T12:34:56+0200" + format: @"%Y-%m-%dT%H:%M:%S%z"] description], + @"2000-06-20T10:34:56Z"); +} + +- (void)testDateWithDateStringFormatFailsWithTrailingCharacters +{ + OTAssertThrowsSpecific( + [OFDate dateWithDateString: @"2000-06-20T12:34:56+0200x" + format: @"%Y-%m-%dT%H:%M:%S%z"], + OFInvalidFormatException); +} + +- (void)testDateWithLocalDateStringFormatFormat +{ + OTAssertEqualObjects( + [[OFDate dateWithLocalDateString: @"2000-06-20T12:34:56" + format: @"%Y-%m-%dT%H:%M:%S"] + localDateStringWithFormat: @"%Y-%m-%dT%H:%M:%S"], + @"2000-06-20T12:34:56"); + + OTAssertEqualObjects( + [[OFDate dateWithLocalDateString: @"2000-06-20T12:34:56-0200" + format: @"%Y-%m-%dT%H:%M:%S%z"] + description], + @"2000-06-20T14:34:56Z"); +} + +- (void)testDateWithLocalDateStringFormatFailsWithTrailingCharacters +{ + OTAssertThrowsSpecific( + [OFDate dateWithLocalDateString: @"2000-06-20T12:34:56x" + format: @"%Y-%m-%dT%H:%M:%S"], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [OFDate dateWithLocalDateString: @"2000-06-20T12:34:56+0200x" + format: @"%Y-%m-%dT%H:%M:%S%z"], + OFInvalidFormatException); +} + +- (void)testIsEqual +{ + OTAssertEqualObjects(_date[0], + [OFDate dateWithTimeIntervalSince1970: 0]); + + OTAssertNotEqualObjects(_date[0], + [OFDate dateWithTimeIntervalSince1970: 0.0000001]); +} + +- (void)testCompare +{ + OTAssertEqual([_date[0] compare: _date[1]], OFOrderedAscending); +} + +- (void)testSecond +{ + OTAssertEqual(_date[0].second, 0); + OTAssertEqual(_date[1].second, 5); +} + +- (void)testMicrosecond +{ + OTAssertEqual(_date[0].microsecond, 0); + OTAssertEqual(_date[1].microsecond, 2); +} + +- (void)testMinute +{ + OTAssertEqual(_date[0].minute, 0); + OTAssertEqual(_date[1].minute, 0); +} + +- (void)testHour +{ + OTAssertEqual(_date[0].hour, 0); + OTAssertEqual(_date[1].hour, 1); +} + +- (void)testDayOfMonth +{ + OTAssertEqual(_date[0].dayOfMonth, 1); + OTAssertEqual(_date[1].dayOfMonth, 2); +} + +- (void)testMonthOfYear +{ + OTAssertEqual(_date[0].monthOfYear, 1); + OTAssertEqual(_date[1].monthOfYear, 1); +} + +- (void)testYear +{ + OTAssertEqual(_date[0].year, 1970); + OTAssertEqual(_date[1].year, 1970); +} + +- (void)testDayOfWeek +{ + OTAssertEqual(_date[0].dayOfWeek, 4); + OTAssertEqual(_date[1].dayOfWeek, 5); +} + +- (void)testDayOfYear +{ + OTAssertEqual(_date[0].dayOfYear, 1); + OTAssertEqual(_date[1].dayOfYear, 2); +} + +- (void)testEarlierDate +{ + OTAssertEqualObjects([_date[0] earlierDate: _date[1]], _date[0]); +} + +- (void)testLaterDate +{ + OTAssertEqualObjects([_date[0] laterDate: _date[1]], _date[1]); +} +@end 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 ADDED new_tests/OFINIFileTests.m Index: new_tests/OFINIFileTests.m ================================================================== --- new_tests/OFINIFileTests.m +++ new_tests/OFINIFileTests.m @@ -0,0 +1,171 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFINIFileTests: OTTestCase +{ + OFINIFile *_file; +} +@end + +@implementation OFINIFileTests +- (void)setUp +{ + OFIRI *IRI; + + [super setUp]; + + IRI = [OFIRI IRIWithString: @"embedded:testfile.ini"]; + _file = [[OFINIFile alloc] initWithIRI: IRI + encoding: OFStringEncodingISO8859_1]; +} + +- (void)dealloc +{ + [_file release]; + + [super dealloc]; +} + +- (void)testCategoryForName +{ + OTAssertNotNil([_file categoryForName: @"tests"]); + OTAssertNotNil([_file categoryForName: @"foobar"]); + OTAssertNotNil([_file categoryForName: @"types"]); +} + +- (void)testStringValueForKey +{ + OTAssertEqualObjects( + [[_file categoryForName: @"tests"] stringValueForKey: @"foo"], + @"bar"); + + OTAssertEqualObjects([[_file categoryForName: @"foobar"] + stringValueForKey: @"quxquxqux"], + @"hello\"wörld"); +} + +- (void)testLongLongValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + longLongValueForKey: @"integer" + defaultValue: 2], + 0x20); +} + +- (void)testBoolValueForKeyDefaultValue +{ + OTAssertTrue([[_file categoryForName: @"types"] + boolValueForKey: @"bool" + defaultValue: false]); +} + +- (void)testFloatValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + floatValueForKey: @"float" + defaultValue: 1], + 0.5f); +} + +- (void)testDoubleValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + doubleValueForKey: @"double" + defaultValue: 3], + 0.25); +} + +- (void)testArrayValueForKey +{ + OFINICategory *types = [_file categoryForName: @"types"]; + OFArray *array = [OFArray arrayWithObjects: @"1", @"2", nil]; + + OTAssertEqualObjects([types arrayValueForKey: @"array1"], array); + OTAssertEqualObjects([types arrayValueForKey: @"array2"], array); + OTAssertEqualObjects([types arrayValueForKey: @"array3"], + [OFArray array]); +} + +- (void)testWriteToIRIEncoding +{ + OFString *expectedOutput = @"[tests]\r\n" + @"foo=baz\r\n" + @"foobar=baz\r\n" + @";comment\r\n" + @"new=new\r\n" + @"\r\n" + @"[foobar]\r\n" + @";foobarcomment\r\n" + @"qux=\" asd\"\r\n" + @"quxquxqux=\"hello\\\"wörld\"\r\n" + @"qux2=\"a\\f\"\r\n" + @"qux3=a\fb\r\n" + @"\r\n" + @"[types]\r\n" + @"integer=16\r\n" + @"bool=false\r\n" + @"float=0.25\r\n" + @"array1=foo\r\n" + @"array1=bar\r\n" + @"double=0.75\r\n"; + OFINICategory *tests = [_file categoryForName: @"tests"]; + OFINICategory *foobar = [_file categoryForName: @"foobar"]; + OFINICategory *types = [_file categoryForName: @"types"]; + OFArray *array = [OFArray arrayWithObjects: @"foo", @"bar", nil]; +#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) + OFIRI *writeIRI; +#endif + + [tests setStringValue: @"baz" forKey: @"foo"]; + [tests setStringValue: @"new" forKey: @"new"]; + [foobar setStringValue: @"a\fb" forKey: @"qux3"]; + [types setLongLongValue: 0x10 forKey: @"integer"]; + [types setBoolValue: false forKey: @"bool"]; + [types setFloatValue: 0.25f forKey: @"float"]; + [types setDoubleValue: 0.75 forKey: @"double"]; + [types setArrayValue: array forKey: @"array1"]; + + [foobar removeValueForKey: @"quxqux "]; + [types removeValueForKey: @"array2"]; + + /* FIXME: Find a way to write files on Nintendo DS */ +#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) + writeIRI = [OFSystemInfo temporaryDirectoryIRI]; + if (writeIRI == nil) + writeIRI = [[OFFileManager defaultManager] currentDirectoryIRI]; + writeIRI = [writeIRI IRIByAppendingPathComponent: @"objfw-tests.ini" + isDirectory: false]; + + [_file writeToIRI: writeIRI + encoding: OFStringEncodingISO8859_1]; + + @try { + OTAssertEqualObjects([OFString + stringWithContentsOfIRI: writeIRI + encoding: OFStringEncodingISO8859_1], + expectedOutput); + } @finally { + [[OFFileManager defaultManager] removeItemAtIRI: writeIRI]; + } +#else + (void)expectedOutput; +#endif +} +@end ADDED new_tests/OFIRITests.m Index: new_tests/OFIRITests.m ================================================================== --- new_tests/OFIRITests.m +++ new_tests/OFIRITests.m @@ -0,0 +1,539 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFIRITests: OTTestCase +{ + OFIRI *_IRI[11]; + OFMutableIRI *_mutableIRI; +} +@end + +static OFString *IRI0String = @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/" + @"pa%3Fth?que%23ry=1&f%26oo=b%3dar#frag%23ment"; + +@implementation OFIRITests +- (void)setUp +{ + [super setUp]; + + _IRI[0] = [[OFIRI alloc] initWithString: IRI0String]; + _IRI[1] = [[OFIRI alloc] initWithString: @"http://foo:80"]; + _IRI[2] = [[OFIRI alloc] initWithString: @"http://bar/"]; + _IRI[3] = [[OFIRI alloc] initWithString: @"file:///etc/passwd"]; + _IRI[4] = [[OFIRI alloc] + initWithString: @"http://foo/bar/qux/foo%2fbar"]; + _IRI[5] = [[OFIRI alloc] initWithString: @"https://[12:34::56:abcd]/"]; + _IRI[6] = [[OFIRI alloc] + initWithString: @"https://[12:34::56:abcd]:234/"]; + _IRI[7] = [[OFIRI alloc] initWithString: @"urn:qux:foo"]; + _IRI[8] = [[OFIRI alloc] initWithString: @"file:/foo?query#frag"]; + _IRI[9] = [[OFIRI alloc] + initWithString: @"file:foo@bar/qux?query#frag"]; + _IRI[10] = [[OFIRI alloc] initWithString: @"http://ä/ö?ü"]; + + _mutableIRI = [[OFMutableIRI alloc] initWithScheme: @"dummy"]; +} + +- (void)dealloc +{ + for (uint_fast8_t i = 0; i < 11; i++) + [_IRI[i] release]; + + [_mutableIRI release]; + + [super dealloc]; +} + +- (void)testIRIWithStringFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific([OFIRI IRIWithString: @"ht,tp://foo"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"http://f`oo"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"http://foo/`"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"http://foo/foo?`"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"http://foo/foo?foo#`"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"https://[g]/"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"https://[f]:/"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"https://[f]:f/"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFIRI IRIWithString: @"foo:"], + OFInvalidFormatException); +} + +- (void)testIRIWithStringRelativeToIRI +{ + OTAssertEqualObjects([[OFIRI IRIWithString: @"/foo" + relativeToIRI: _IRI[0]] string], + @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/foo"); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"foo/bar?q" + relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/quux"]] + string], + @"http://h/qux/foo/bar?q"); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"foo/bar" + relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/?x"]] + string], + @"http://h/qux/foo/bar"); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://foo/?q" + relativeToIRI: _IRI[0]] string], + @"http://foo/?q"); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"foo" + relativeToIRI: [OFIRI IRIWithString: @"http://foo/bar"]] + string], + @"http://foo/foo"); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"foo" + relativeToIRI: [OFIRI IRIWithString: @"http://foo"]] + string], + @"http://foo/foo"); +} + +- (void)testIRIWithStringRelativeToIRIFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific( + [OFIRI IRIWithString: @"`" relativeToIRI: _IRI[0]], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [OFIRI IRIWithString: @"/`" relativeToIRI: _IRI[0]], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [OFIRI IRIWithString: @"?`" relativeToIRI: _IRI[0]], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [OFIRI IRIWithString: @"#`" relativeToIRI: _IRI[0]], + OFInvalidFormatException); +} + +#ifdef OF_HAVE_FILES +- (void)testFileIRIWithPath +{ + OTAssertEqualObjects( + [[OFIRI fileIRIWithPath: @"testfile.txt"] fileSystemRepresentation], + [[OFFileManager defaultManager].currentDirectoryPath + stringByAppendingPathComponent: @"testfile.txt"]); +} + +# if defined(OF_WINDOWS) || defined(OF_MSDOS) +- (void)testFileIRWithPathC +{ + OFIRI *IRI = [OFIRI fileIRIWithPath: @"c:\\"]; + OTAssertEqualObjects(IRI.string, @"file:/c:/"); + OTAssertEqualObjects(IRI.fileSystemRepresentation, @"c:\\"); +} +# endif + +# ifdef OF_WINDOWS +- (void)testFileIRIWithPathUNC +{ + OFIRI *IRI; + + IRI = [OFIRI fileIRIWithPath: @"\\\\foo\\bar" isDirectory: false]; + OTAssertEqualObjects(IRI.host, @"foo"); + OTAssertEqualObjects(IRI.path, @"/bar"); + OTAssertEqualObjects(IRI.string, @"file://foo/bar"); + OTAssertEqualObjects(IRI.fileSystemRepresentation, @"\\\\foo\\bar"); + + IRI = [OFIRI fileIRIWithPath: @"\\\\test" isDirectory: true]; + OTAssertEqualObjects(IRI.host, @"test"); + OTAssertEqualObjects(IRI.path, @"/"); + OTAssertEqualObjects(IRI.string, @"file://test/"); + OTAssertEqualObjects(IRI.fileSystemRepresentation, @"\\\\test"); +} +# endif +#endif + +- (void)testString +{ + OTAssertEqualObjects(_IRI[0].string, IRI0String); + OTAssertEqualObjects(_IRI[1].string, @"http://foo:80"); + OTAssertEqualObjects(_IRI[2].string, @"http://bar/"); + OTAssertEqualObjects(_IRI[3].string, @"file:///etc/passwd"); + OTAssertEqualObjects(_IRI[4].string, @"http://foo/bar/qux/foo%2fbar"); + OTAssertEqualObjects(_IRI[5].string, @"https://[12:34::56:abcd]/"); + OTAssertEqualObjects(_IRI[6].string, @"https://[12:34::56:abcd]:234/"); + OTAssertEqualObjects(_IRI[7].string, @"urn:qux:foo"); + OTAssertEqualObjects(_IRI[8].string, @"file:/foo?query#frag"); + OTAssertEqualObjects(_IRI[9].string, @"file:foo@bar/qux?query#frag"); + OTAssertEqualObjects(_IRI[10].string, @"http://ä/ö?ü"); +} + +- (void)testScheme +{ + OTAssertEqualObjects(_IRI[0].scheme, @"ht+tp"); + OTAssertEqualObjects(_IRI[3].scheme, @"file"); + OTAssertEqualObjects(_IRI[8].scheme, @"file"); + OTAssertEqualObjects(_IRI[9].scheme, @"file"); + OTAssertEqualObjects(_IRI[10].scheme, @"http"); +} + +- (void)testUser +{ + OTAssertEqualObjects(_IRI[0].user, @"us:er"); + OTAssertNil(_IRI[3].user); + OTAssertNil(_IRI[9].user); + OTAssertNil(_IRI[10].user); +} + +- (void)testPassword +{ + OTAssertEqualObjects(_IRI[0].password, @"p@w"); + OTAssertNil(_IRI[3].password); + OTAssertNil(_IRI[9].password); + OTAssertNil(_IRI[10].password); +} + +- (void)testHost +{ + OTAssertEqualObjects(_IRI[0].host, @"ho:st"); + OTAssertEqualObjects(_IRI[5].host, @"12:34::56:abcd"); + OTAssertEqualObjects(_IRI[6].host, @"12:34::56:abcd"); + OTAssertNil(_IRI[7].host); + OTAssertNil(_IRI[8].host); + OTAssertNil(_IRI[9].host); + OTAssertEqualObjects(_IRI[10].host, @"ä"); +} + +- (void)testPort +{ + OTAssertEqual(_IRI[0].port.unsignedShortValue, 1234); + OTAssertNil(_IRI[3].port); + OTAssertEqual(_IRI[6].port.unsignedShortValue, 234); + OTAssertNil(_IRI[7].port); + OTAssertNil(_IRI[8].port); + OTAssertNil(_IRI[9].port); + OTAssertNil(_IRI[10].port); +} + +- (void)testPath +{ + OTAssertEqualObjects(_IRI[0].path, @"/pa?th"); + OTAssertEqualObjects(_IRI[3].path, @"/etc/passwd"); + OTAssertEqualObjects(_IRI[7].path, @"qux:foo"); + OTAssertEqualObjects(_IRI[8].path, @"/foo"); + OTAssertEqualObjects(_IRI[9].path, @"foo@bar/qux"); + OTAssertEqualObjects(_IRI[10].path, @"/ö"); +} + +- (void)testPathComponents +{ + OTAssertEqualObjects(_IRI[0].pathComponents, + ([OFArray arrayWithObjects: @"/", @"pa?th", nil])); + + OTAssertEqualObjects(_IRI[3].pathComponents, + ([OFArray arrayWithObjects: @"/", @"etc", @"passwd", nil])); + + OTAssertEqualObjects(_IRI[4].pathComponents, + ([OFArray arrayWithObjects: @"/", @"bar", @"qux", @"foo/bar", + nil])); +} + +- (void)testLastPathComponent +{ + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://host/foo//bar/baz"] + lastPathComponent], + @"baz"); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"http://host/foo//bar/baz/"] + lastPathComponent], + @"baz"); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://host/foo/"] + lastPathComponent], + @"foo"); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://host/"] + lastPathComponent], + @"/"); + + OTAssertEqualObjects(_IRI[4].lastPathComponent, @"foo/bar"); +} + +- (void)testQuery +{ + OTAssertEqualObjects(_IRI[0].query, @"que#ry=1&f&oo=b=ar"); + OTAssertNil(_IRI[3].query); + OTAssertEqualObjects(_IRI[8].query, @"query"); + OTAssertEqualObjects(_IRI[9].query, @"query"); + OTAssertEqualObjects(_IRI[10].query, @"ü"); +} + +- (void)testQueryItems +{ + OTAssertEqualObjects(_IRI[0].queryItems, + ([OFArray arrayWithObjects: + [OFPair pairWithFirstObject: @"que#ry" secondObject: @"1"], + [OFPair pairWithFirstObject: @"f&oo" secondObject: @"b=ar"], nil])); +} + +- (void)testFragment +{ + OTAssertEqualObjects(_IRI[0].fragment, @"frag#ment"); + OTAssertNil(_IRI[3].fragment); + OTAssertEqualObjects(_IRI[8].fragment, @"frag"); + OTAssertEqualObjects(_IRI[9].fragment, @"frag"); +} + +- (void)testCopy +{ + OTAssertEqualObjects(_IRI[0], [[_IRI[0] copy] autorelease]); +} + +- (void)testIsEqual +{ + OTAssertEqualObjects(_IRI[0], [OFIRI IRIWithString: IRI0String]); + OTAssertNotEqualObjects(_IRI[1], _IRI[2]); + OTAssertEqualObjects([OFIRI IRIWithString: @"HTTP://bar/"], _IRI[2]); +} + +- (void)testHash +{ + OTAssertEqual(_IRI[0].hash, [[OFIRI IRIWithString: IRI0String] hash]); + OTAssertNotEqual(_IRI[1].hash, _IRI[2].hash); +} + +- (void)testIRIWithStringFailsWithInvalidFormat +{ + OTAssertThrowsSpecific([OFIRI IRIWithString: @"http"], + OFInvalidFormatException); +} + +- (void)testIRIByAddingPercentEncodingForUnicodeCharacters +{ + OTAssertEqualObjects( + _IRI[10].IRIByAddingPercentEncodingForUnicodeCharacters, + [OFIRI IRIWithString: @"http://%C3%A4/%C3%B6?%C3%BC"]); +} + +- (void)testSetPercentEncodedSchemeFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.scheme = @"%20", + OFInvalidFormatException); +} + +- (void)testSetHost +{ + _mutableIRI.host = @"ho:st"; + OTAssertEqualObjects(_mutableIRI.percentEncodedHost, @"ho%3Ast"); + + _mutableIRI.host = @"12:34:ab"; + OTAssertEqualObjects(_mutableIRI.percentEncodedHost, @"[12:34:ab]"); + + _mutableIRI.host = @"12:34:aB"; + OTAssertEqualObjects(_mutableIRI.percentEncodedHost, @"[12:34:aB]"); + + _mutableIRI.host = @"12:34:g"; + OTAssertEqualObjects(_mutableIRI.percentEncodedHost, @"12%3A34%3Ag"); +} + +- (void)testSetPercentEncodedHost +{ + _mutableIRI.percentEncodedHost = @"ho%3Ast"; + OTAssertEqualObjects(_mutableIRI.host, @"ho:st"); + + _mutableIRI.percentEncodedHost = @"[12:34]"; + OTAssertEqualObjects(_mutableIRI.host, @"12:34"); + + _mutableIRI.percentEncodedHost = @"[12::ab]"; + OTAssertEqualObjects(_mutableIRI.host, @"12::ab"); +} + +- (void)testSetPercentEncodedHostFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedHost = @"/", + OFInvalidFormatException); + + OTAssertThrowsSpecific(_mutableIRI.percentEncodedHost = @"[12:34", + OFInvalidFormatException); + + OTAssertThrowsSpecific(_mutableIRI.percentEncodedHost = @"[a::g]", + OFInvalidFormatException); +} + +- (void)testSetUser +{ + _mutableIRI.user = @"us:er"; + OTAssertEqualObjects(_mutableIRI.percentEncodedUser, @"us%3Aer"); +} + +- (void)testSetPercentEncodedUser +{ + _mutableIRI.percentEncodedUser = @"us%3Aer"; + OTAssertEqualObjects(_mutableIRI.user, @"us:er"); +} + +- (void)testSetPercentEncodedUserFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedHost = @"/", + OFInvalidFormatException); +} + +- (void)testSetPassword +{ + _mutableIRI.password = @"pass:word"; + OTAssertEqualObjects(_mutableIRI.percentEncodedPassword, + @"pass%3Aword"); +} + +- (void)testSetPercentEncodedPassword +{ + _mutableIRI.percentEncodedPassword = @"pass%3Aword"; + OTAssertEqualObjects(_mutableIRI.password, @"pass:word"); +} + +- (void)testSetPercentEncodedPasswordFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedPassword = @"/", + OFInvalidFormatException); +} + +- (void)testSetPath +{ + _mutableIRI.path = @"pa/th@?"; + OTAssertEqualObjects(_mutableIRI.percentEncodedPath, @"pa/th@%3F"); +} + +- (void)testSetPercentEncodedPath +{ + _mutableIRI.percentEncodedPath = @"pa/th@%3F"; + OTAssertEqualObjects(_mutableIRI.path, @"pa/th@?"); +} + +- (void)testSetPercentEncodedPathFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedPath = @"?", + OFInvalidFormatException); +} + +- (void)testSetQuery +{ + _mutableIRI.query = @"que/ry?#"; + OTAssertEqualObjects(_mutableIRI.percentEncodedQuery, @"que/ry?%23"); +} + +- (void)testSetPercentEncodedQuery +{ + _mutableIRI.percentEncodedQuery = @"que/ry?%23"; + OTAssertEqualObjects(_mutableIRI.query, @"que/ry?#"); +} + +- (void)testSetPercentEncodedQueryFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedQuery = @"`", + OFInvalidFormatException); +} + +- (void)testSetQueryItems +{ + _mutableIRI.queryItems = [OFArray arrayWithObjects: + [OFPair pairWithFirstObject: @"foo&bar" secondObject: @"baz=qux"], + [OFPair pairWithFirstObject: @"f=oobar" secondObject: @"b&azqux"], + nil]; + OTAssertEqualObjects(_mutableIRI.percentEncodedQuery, + @"foo%26bar=baz%3Dqux&f%3Doobar=b%26azqux"); +} + +- (void)testSetFragment +{ + _mutableIRI.fragment = @"frag/ment?#"; + OTAssertEqualObjects(_mutableIRI.percentEncodedFragment, + @"frag/ment?%23"); +} + +- (void)testSetPercentEncodedFragment +{ + _mutableIRI.percentEncodedFragment = @"frag/ment?%23"; + OTAssertEqualObjects(_mutableIRI.fragment, @"frag/ment?#"); +} + +- (void)testSetPercentEncodedFragmentFailsWithInvalidCharacters +{ + OTAssertThrowsSpecific(_mutableIRI.percentEncodedFragment = @"`", + OFInvalidFormatException); +} + +-(void)testIRIByAppendingPathComponentIsDirectory +{ + OTAssertEqualObjects([[OFIRI IRIWithString: @"file:///foo/bar"] + IRIByAppendingPathComponent: @"qux" + isDirectory: false], + [OFIRI IRIWithString: @"file:///foo/bar/qux"]); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"file:///foo/bar/"] + IRIByAppendingPathComponent: @"qux" + isDirectory: false], + [OFIRI IRIWithString: @"file:///foo/bar/qux"]); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"file:///foo/bar/"] + IRIByAppendingPathComponent: @"qu?x" + isDirectory: false], + [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx"]); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"file:///foo/bar/"] + IRIByAppendingPathComponent: @"qu?x" + isDirectory: true], + [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx/"]); +} + +- (void)testIRIByStandardizingPath +{ + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://foo/bar/.."] + IRIByStandardizingPath], + [OFIRI IRIWithString: @"http://foo/"]); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"http://foo/bar/%2E%2E/../qux/"] + IRIByStandardizingPath], + [OFIRI IRIWithString: @"http://foo/bar/qux/"]); + + OTAssertEqualObjects( + [[OFIRI IRIWithString: @"http://foo/bar/./././qux/./"] + IRIByStandardizingPath], + [OFIRI IRIWithString: @"http://foo/bar/qux/"]); + + OTAssertEqualObjects([[OFIRI IRIWithString: @"http://foo/bar/../../qux"] + IRIByStandardizingPath], + [OFIRI IRIWithString: @"http://foo/../qux"]); +} +@end ADDED new_tests/OFInvocationTests.m Index: new_tests/OFInvocationTests.m ================================================================== --- new_tests/OFInvocationTests.m +++ new_tests/OFInvocationTests.m @@ -0,0 +1,110 @@ +/* + * 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 + +#if defined(HAVE_COMPLEX_H) && !defined(__STDC_NO_COMPLEX__) +# include +#endif + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFInvocationTests: OTTestCase +{ + OFInvocation *_invocation; +} +@end + +struct TestStruct { + unsigned char c; + unsigned int i; +}; + +@implementation OFInvocationTests +- (struct TestStruct)invocationTestMethod1: (unsigned char)c + : (unsigned int)i + : (struct TestStruct *)testStructPtr + : (struct TestStruct)testStruct +{ + return testStruct; +} + +- (void)setUp +{ + [super setUp]; + + SEL selector = @selector(invocationTestMethod1::::); + OFMethodSignature *signature = + [self methodSignatureForSelector: selector]; + + _invocation = [[OFInvocation alloc] initWithMethodSignature: signature]; +} + +- (void)dealloc +{ + [_invocation release]; + + [super dealloc]; +} + +- (void)testSetAndGetReturnValue +{ + struct TestStruct testStruct, testStruct2; + + memset(&testStruct, 0xFF, sizeof(testStruct)); + testStruct.c = 0x55; + testStruct.i = 0xAAAAAAAA; + + [_invocation setReturnValue: &testStruct]; + [_invocation getReturnValue: &testStruct2]; + OTAssertEqual(memcmp(&testStruct, &testStruct2, sizeof(testStruct)), 0); +} + +- (void)testSetAndGetArgumentAtIndex +{ + struct TestStruct testStruct, testStruct2; + struct TestStruct *testStructPtr = &testStruct, *testStructPtr2; + unsigned const char c = 0xAA; + unsigned char c2; + const unsigned int i = 0x55555555; + unsigned int i2; + + memset(&testStruct, 0xFF, sizeof(testStruct)); + testStruct.c = 0x55; + testStruct.i = 0xAAAAAAAA; + + memset(&testStruct2, 0, sizeof(testStruct2)); + + [_invocation setArgument: &c atIndex: 2]; + [_invocation setArgument: &i atIndex: 3]; + [_invocation setArgument: &testStructPtr atIndex: 4]; + [_invocation setArgument: &testStruct atIndex: 5]; + + [_invocation getArgument: &c2 atIndex: 2]; + OTAssertEqual(c, c2); + + [_invocation getArgument: &i2 atIndex: 3]; + OTAssertEqual(i, i2); + + [_invocation getArgument: &testStructPtr2 atIndex: 4]; + OTAssertEqual(testStructPtr, testStructPtr2); + + [_invocation getArgument: &testStruct2 atIndex: 5]; + OTAssertEqual(memcmp(&testStruct, &testStruct2, sizeof(testStruct)), 0); +} +@end ADDED new_tests/OFJSONTests.m Index: new_tests/OFJSONTests.m ================================================================== --- new_tests/OFJSONTests.m +++ new_tests/OFJSONTests.m @@ -0,0 +1,139 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFJSONTests: OTTestCase +{ + unsigned long long _hashSeed; + OFDictionary *_dictionary; +} +@end + +extern unsigned long long OFHashSeed; + +static OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF," + @"null//bar\n,\"foo\",false]}"; + +@implementation OFJSONTests +- (void)setUp +{ + [super setUp]; + + _hashSeed = OFHashSeed; + OFHashSeed = 0; + + _dictionary = [[OFDictionary alloc] initWithKeysAndObjects: + @"foo", @"b\na\r", + @"x", [OFArray arrayWithObjects: + [OFNumber numberWithFloat: .5f], + [OFNumber numberWithInt: 0xF], + [OFNull null], + @"foo", + [OFNumber numberWithBool: false], + nil], + nil]; +} + +- (void)tearDown +{ + OFHashSeed = _hashSeed; + + [super tearDown]; +} + +- (void)dealloc +{ + [_dictionary release]; + + [super dealloc]; +} + +- (void)testObjectByParsingJSON +{ + OTAssertEqualObjects(string.objectByParsingJSON, _dictionary); +} + +- (void)testJSONRepresentation +{ + OTAssertEqualObjects(_dictionary.JSONRepresentation, + @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"); +} + +- (void)testPrettyJSONRepresentation +{ + OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions: + OFJSONRepresentationOptionPretty], + @"{\n\t\"x\": [\n\t\t0.5,\n\t\t15,\n\t\tnull,\n\t\t" + @"\"foo\",\n\t\tfalse\n\t],\n\t\"foo\": \"b\\na\\r\"\n}"); +} + +- (void)testJSON5Representation +{ + OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions: + OFJSONRepresentationOptionJSON5], + @"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"); +} + +- (void)testObjectByParsingJSONFailsWithInvalidJSON +{ + OTAssertThrowsSpecific([@"{" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"]" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"bar" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"[\"a\" \"b\"]" objectByParsingJSON], + OFInvalidJSONException); +} + +- (void)testObjectByParsingJSONWithDeepNesting +{ + OTAssertEqualObjects( + @"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + .objectByParsingJSON, + [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: + [OFDictionary dictionary]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]); +} + +- (void)testObjectByParsingJSONFailsWithTooDeepNesting +{ + OTAssertThrowsSpecific( + [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + objectByParsingJSON], + OFInvalidJSONException); +} +@end ADDED new_tests/OFMatrix4x4Tests.m Index: new_tests/OFMatrix4x4Tests.m ================================================================== --- new_tests/OFMatrix4x4Tests.m +++ new_tests/OFMatrix4x4Tests.m @@ -0,0 +1,156 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFMatrix4x4Tests: OTTestCase +{ + OFMatrix4x4 *_matrix; +} +@end + +@implementation OFMatrix4x4Tests +- (void)setUp +{ + [super setUp]; + + _matrix = [[OFMatrix4x4 alloc] initWithValues: (const float [4][4]){ + { 1, 2, 3, 4 }, + { 5, 6, 7, 8 }, + { 9, 10, 11, 12 }, + { 13, 14, 15, 16 } + }]; +} + +- (void)dealloc +{ + [_matrix release]; + + [super dealloc]; +} + +- (void)testIdentityMatrix +{ + OTAssertEqual(memcmp([[OFMatrix4x4 identityMatrix] values], + (const float [4][4]){ + { 1, 0, 0, 0 }, + { 0, 1, 0, 0 }, + { 0, 0, 1, 0 }, + { 0, 0, 0, 1 } + }, 16 * sizeof(float)), + 0); +} + +- (void)testDescription +{ + OTAssertEqualObjects(_matrix.description, + @""); +} + +- (void)testIsEqual +{ + OTAssertEqualObjects([OFMatrix4x4 identityMatrix], + ([OFMatrix4x4 matrixWithValues: (const float [4][4]){ + { 1, 0, 0, 0 }, + { 0, 1, 0, 0 }, + { 0, 0, 1, 0 }, + { 0, 0, 0, 1 } + }])); +} + +/* TODO: testHash */ + +- (void)testCopy +{ + OTAssertEqualObjects(_matrix, [[_matrix copy] autorelease]); +} + +- (void)testMultiplyWithMatrix +{ + OFMatrix4x4 *matrix; + + matrix = [[_matrix copy] autorelease]; + [matrix multiplyWithMatrix: [OFMatrix4x4 identityMatrix]]; + OTAssertEqualObjects(matrix, _matrix); + + matrix = [OFMatrix4x4 matrixWithValues: (const float [4][4]){ + { 100, 200, 300, 400 }, + { 500, 600, 700, 800 }, + { 900, 1000, 1100, 1200 }, + { 1300, 1400, 1500, 1600 } + }]; + [matrix multiplyWithMatrix: _matrix]; + OTAssertEqualObjects(matrix, + ([OFMatrix4x4 matrixWithValues: (const float [4][4]){ + { 9000, 10000, 11000, 12000 }, + { 20200, 22800, 25400, 28000 }, + { 31400, 35600, 39800, 44000 }, + { 42600, 48400, 54200, 60000 } + }])); +} + +- (void)testTranslateWithVector +{ + OFMatrix4x4 *matrix = [OFMatrix4x4 identityMatrix]; + OFVector4D point; + + [matrix translateWithVector: OFMakeVector3D(1, 2, 3)]; + + point = [matrix transformedVector: OFMakeVector4D(2, 3, 4, 1)]; + OTAssertEqual(point.x, 3); + OTAssertEqual(point.y, 5); + OTAssertEqual(point.z, 7); + OTAssertEqual(point.w, 1); +} + +- (void)testScaleWithVector +{ + OFMatrix4x4 *matrix = [OFMatrix4x4 identityMatrix]; + OFVector4D point; + + [matrix translateWithVector: OFMakeVector3D(1, 2, 3)]; + [matrix scaleWithVector: OFMakeVector3D(-1, 0.5f, 2)]; + + point = [matrix transformedVector: OFMakeVector4D(2, 3, 4, 1)]; + OTAssertEqual(point.x, -3); + OTAssertEqual(point.y, 2.5); + OTAssertEqual(point.z, 14); + OTAssertEqual(point.w, 1); +} + +- (void)testTransformVectorsCount +{ + OFVector4D points[2] = {{ 1, 2, 3, 1 }, { 7, 8, 9, 2 }}; + + [_matrix transformVectors: points count: 2]; + + OTAssertEqual(points[0].x, 18); + OTAssertEqual(points[0].y, 46); + OTAssertEqual(points[0].z, 74); + OTAssertEqual(points[0].w, 102); + OTAssertEqual(points[1].x, 58); + OTAssertEqual(points[1].y, 162); + OTAssertEqual(points[1].z, 266); + OTAssertEqual(points[1].w, 370); +} +@end ADDED new_tests/OFMethodSignatureTests.m Index: new_tests/OFMethodSignatureTests.m ================================================================== --- new_tests/OFMethodSignatureTests.m +++ new_tests/OFMethodSignatureTests.m @@ -0,0 +1,174 @@ +/* + * 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 + +#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) +# include +#endif + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFMethodSignatureTests: OTTestCase +@end + +struct Test1Struct { + char c; + int i; + char d; +}; + +struct Test2Struct { + char c; + struct { + short s; + int i; + } st; + union { + char c; + int i; + } u; + double d; +}; + +#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) +struct Test3Struct { + char c; + complex double cd; +}; +#endif + +union Test3Union { + char c; + int i; + double d; +}; + +union Test4Union { + char c; + struct { + short x, y; + } st; + int i; + union { + float f; + double d; + } u; +}; + +@implementation OFMethodSignatureTests +- (void)testSignatureWithObjCTypes +{ + OFMethodSignature *methodSignature; + + methodSignature = + [OFMethodSignature signatureWithObjCTypes: "i28@0:8S16*20"]; + OTAssertEqual(methodSignature.numberOfArguments, 4); + OTAssertEqual(strcmp(methodSignature.methodReturnType, "i"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 0], "@"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 1], ":"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 2], "S"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 3], "*"), 0); + OTAssertEqual(methodSignature.frameLength, 28); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 0], 0); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 1], 8); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 2], 16); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 3], 20); + + methodSignature = [OFMethodSignature signatureWithObjCTypes: + "{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}24@0:8" + "^{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}16"]; + OTAssertEqual(methodSignature.numberOfArguments, 3); + OTAssertEqual(strcmp(methodSignature.methodReturnType, + "{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 0], "@"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 1], ":"), 0); + OTAssertEqual(strcmp([methodSignature argumentTypeAtIndex: 2], + "^{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}"), 0); + OTAssertEqual(methodSignature.frameLength, 24); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 0], 0); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 1], 8); + OTAssertEqual([methodSignature argumentOffsetAtIndex: 2], 16); +} + +- (void)testSignatureWithObjCTypesFailsWithInvalidFormat +{ + OTAssertThrowsSpecific( + [OFMethodSignature signatureWithObjCTypes: "{ii"], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFMethodSignature signatureWithObjCTypes: ""], + OFInvalidFormatException); + + OTAssertThrowsSpecific([OFMethodSignature signatureWithObjCTypes: "0"], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [OFMethodSignature signatureWithObjCTypes: "{{}0"], + OFInvalidFormatException); +} + +- (void)testSizeOfTypeEncoding +{ + OTAssertEqual(OFSizeOfTypeEncoding(@encode(struct Test1Struct)), + sizeof(struct Test1Struct)); + + OTAssertEqual(OFSizeOfTypeEncoding(@encode(struct Test2Struct)), + sizeof(struct Test2Struct)); + +#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) && \ + OF_GCC_VERSION >= 402 + OTAssertEqual(OFSizeOfTypeEncoding(@encode(struct Test3Struct)), + sizeof(struct Test3Struct)); +#endif + + OTAssertEqual(OFSizeOfTypeEncoding(@encode(union Test3Union)), + sizeof(union Test3Union)); + + OTAssertEqual(OFSizeOfTypeEncoding(@encode(union Test4Union)), + sizeof(union Test4Union)); + + OTAssertEqual(OFSizeOfTypeEncoding(@encode(struct Test1Struct [5])), + sizeof(struct Test1Struct [5])); +} + +- (void)testAlignmentOfTypeEncoding +{ + OTAssertEqual(OFAlignmentOfTypeEncoding(@encode(struct Test1Struct)), + OF_ALIGNOF(struct Test1Struct)); + + OTAssertEqual(OFAlignmentOfTypeEncoding(@encode(struct Test2Struct)), + OF_ALIGNOF(struct Test2Struct)); + +#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) && \ + OF_GCC_VERSION >= 402 + OTAssertEqual(OFAlignmentOfTypeEncoding(@encode(struct Test3Struct)), + OF_ALIGNOF(struct Test3Struct)); +#endif + + OTAssertEqual(OFAlignmentOfTypeEncoding(@encode(union Test3Union)), + OF_ALIGNOF(union Test3Union)); + + OTAssertEqual(OFAlignmentOfTypeEncoding(@encode(union Test4Union)), + OF_ALIGNOF(union Test4Union)); + + OTAssertEqual( + OFAlignmentOfTypeEncoding(@encode(struct Test1Struct [5])), + OF_ALIGNOF(struct Test1Struct [5])); +} +@end ADDED new_tests/OFMutableArrayTests.h Index: new_tests/OFMutableArrayTests.h ================================================================== --- new_tests/OFMutableArrayTests.h +++ new_tests/OFMutableArrayTests.h @@ -0,0 +1,22 @@ +/* + * 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 "OFArrayTests.h" + +@interface OFMutableArrayTests: OFArrayTests +{ + OFMutableArray *_mutableArray; +} +@end ADDED new_tests/OFMutableArrayTests.m Index: new_tests/OFMutableArrayTests.m ================================================================== --- new_tests/OFMutableArrayTests.m +++ new_tests/OFMutableArrayTests.m @@ -0,0 +1,252 @@ +/* + * 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 "OFMutableArrayTests.h" + +#import "OFArray+Private.h" + +@interface CustomMutableArray: OFMutableArray +{ + OFMutableArray *_array; +} +@end + +static OFString *const cArray[] = { + @"Foo", + @"Bar", + @"Baz" +}; + +@implementation OFMutableArrayTests +- (Class)arrayClass +{ + return [CustomMutableArray class]; +} + +- (void)setUp +{ + [super setUp]; + + _mutableArray = [[self.arrayClass alloc] + initWithObjects: cArray + count: sizeof(cArray) / sizeof(*cArray)]; +} + +- (void)dealloc +{ + [_mutableArray release]; + + [super dealloc]; +} + +- (void)testAddObject +{ + [_mutableArray addObject: cArray[0]]; + [_mutableArray addObject: cArray[2]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", @"Foo", @"Baz", + nil])); +} + +- (void)testInsertObjectAtIndex +{ + [_mutableArray insertObject: cArray[1] atIndex: 1]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Bar", @"Bar", @"Baz", nil])); +} + +- (void)testReplaceObjectWithObject +{ + [_mutableArray insertObject: cArray[1] atIndex: 1]; + [_mutableArray replaceObject: cArray[1] withObject: cArray[0]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Foo", @"Foo", @"Baz", nil])); +} + +- (void)testReplaceObjectIdenticalToWithObject +{ + [_mutableArray insertObject: [[cArray[1] mutableCopy] autorelease] + atIndex: 1]; + [_mutableArray replaceObjectIdenticalTo: cArray[1] + withObject: cArray[0]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Bar", @"Foo", @"Baz", nil])); +} + +- (void)testReplaceObjectAtIndexWithObject +{ + [_mutableArray replaceObjectAtIndex: 1 + withObject: cArray[0]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Foo", @"Baz", nil])); +} + +- (void)testRemoveObject +{ + [_mutableArray removeObject: cArray[1]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Baz", nil])); +} + +- (void)testRemoveObjectIdenticalTo +{ + [_mutableArray removeObjectIdenticalTo: cArray[1]]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Baz", nil])); +} + +- (void)testRemoveObjectAtIndex +{ + [_mutableArray removeObjectAtIndex: 1]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Foo", @"Baz", nil])); +} + +- (void)testRemoveObjectsInRange +{ + [_mutableArray removeObjectsInRange: OFMakeRange(1, 2)]; + + OTAssertEqualObjects(_mutableArray, [OFArray arrayWithObject: @"Foo"]); +} + +- (void)testRemoveObjectsInRangeFailsWhenOutOfRange +{ + OTAssertThrowsSpecific([_mutableArray removeObjectsInRange: + OFMakeRange(0, _mutableArray.count + 1)], OFOutOfRangeException); +} + +- (void)testReverse +{ + [_mutableArray reverse]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"Baz", @"Bar", @"Foo", nil])); +} + +#ifdef OF_HAVE_BLOCKS +- (void)testReplaceObjectsUsingBlock +{ + [_mutableArray replaceObjectsUsingBlock: ^ id (id object, size_t idx) { + return [object lowercaseString]; + }]; + + OTAssertEqualObjects(_mutableArray, + ([OFArray arrayWithObjects: @"foo", @"bar", @"baz", nil])); +} +#endif + +- (void)testSetValueForKey +{ + OFMutableArray *array = [self.arrayClass arrayWithObjects: + [OFMutableIRI IRIWithString: @"http://foo.bar/"], + [OFMutableIRI IRIWithString: @"http://bar.qux/"], + [OFMutableIRI IRIWithString: @"http://qux.quxqux/"], nil]; + + [array setValue: [OFNumber numberWithShort: 1234] + forKey: @"port"]; + OTAssertEqualObjects(array, ([OFArray arrayWithObjects: + [OFIRI IRIWithString: @"http://foo.bar:1234/"], + [OFIRI IRIWithString: @"http://bar.qux:1234/"], + [OFIRI IRIWithString: @"http://qux.quxqux:1234/"], nil])); +} +@end + +@implementation CustomMutableArray +- (instancetype)init +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] init]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithObject: (id)object arguments: (va_list)arguments +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] initWithObject: object + arguments: arguments]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithObjects: (id const *)objects count: (size_t)count +{ + self = [super init]; + + @try { + _array = [[OFMutableArray alloc] initWithObjects: objects + count: count]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_array release]; + + [super dealloc]; +} + +- (id)objectAtIndex: (size_t)idx +{ + return [_array objectAtIndex: idx]; +} + +- (size_t)count +{ + return [_array count]; +} + +- (void)insertObject: (id)object atIndex: (size_t)idx +{ + [_array insertObject: object atIndex: idx]; +} + +- (void)replaceObjectAtIndex: (size_t)idx withObject: (id)object +{ + [_array replaceObjectAtIndex: idx withObject: object]; +} + +- (void)removeObjectAtIndex: (size_t)idx +{ + [_array removeObjectAtIndex: idx]; +} +@end ADDED new_tests/OFNumberTests.m Index: new_tests/OFNumberTests.m ================================================================== --- new_tests/OFNumberTests.m +++ new_tests/OFNumberTests.m @@ -0,0 +1,124 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFNumberTests: OTTestCase +@end + +extern unsigned long long OFHashSeed; + +@implementation OFNumberTests +- (void)testIsEqual +{ + OFNumber *number = [OFNumber numberWithLongLong: 123456789]; + OTAssertEqualObjects(number, [OFNumber numberWithLong: 123456789]); +} + +- (void)testHash +{ + unsigned long long hashSeed = OFHashSeed; + OFHashSeed = 0; + @try { + OFNumber *number = [OFNumber numberWithLongLong: 123456789]; + OTAssertEqual(number.hash, 0x82D8BC42); + } @finally { + OFHashSeed = hashSeed; + }; +} + +- (void)testCharValue +{ + OFNumber *number = [OFNumber numberWithLongLong: 123456789]; + OTAssertEqual(number.charValue, 21); +} + +- (void)testDoubleValue +{ + OFNumber *number = [OFNumber numberWithLongLong: 123456789]; + OTAssertEqual(number.doubleValue, 123456789.L); +} + +- (void)testSignedCharMinAndMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithChar: SCHAR_MIN] charValue], + SCHAR_MIN); + OTAssertEqual([[OFNumber numberWithChar: SCHAR_MAX] charValue], + SCHAR_MAX); +} + +- (void)testShortMinAndMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithShort: SHRT_MIN] shortValue], + SHRT_MIN); + OTAssertEqual([[OFNumber numberWithShort: SHRT_MAX] shortValue], + SHRT_MAX); +} + +- (void)testIntMinAndMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithInt: INT_MIN] intValue], INT_MIN); + OTAssertEqual([[OFNumber numberWithInt: INT_MAX] intValue], INT_MAX); +} + +- (void)testLongMinAndMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithLong: LONG_MIN] longValue], + LONG_MIN); + OTAssertEqual([[OFNumber numberWithLong: LONG_MAX] longValue], + LONG_MAX);; +} + +- (void)testLongLongMinAndMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithLongLong: LLONG_MIN] longLongValue], + LLONG_MIN); + OTAssertEqual([[OFNumber numberWithLongLong: LLONG_MAX] longLongValue], + LLONG_MAX); +} + +- (void)testUnsignedCharMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithUnsignedChar: UCHAR_MAX] + unsignedCharValue], UCHAR_MAX); +} + +- (void)testUnsignedShortMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithUnsignedShort: USHRT_MAX] + unsignedShortValue], USHRT_MAX); +} + +- (void)testUnsignedIntMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithUnsignedInt: UINT_MAX] + unsignedIntValue], UINT_MAX); +} + +- (void)testUnsignedLongMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithUnsignedLong: ULONG_MAX] + unsignedLongValue], ULONG_MAX); +} + +- (void)testUnsignedLongLongMaxUnmodified +{ + OTAssertEqual([[OFNumber numberWithUnsignedLongLong: ULLONG_MAX] + unsignedLongLongValue], ULLONG_MAX); +} +@end ADDED new_tests/OFPBKDF2Tests.m Index: new_tests/OFPBKDF2Tests.m ================================================================== --- new_tests/OFPBKDF2Tests.m +++ new_tests/OFPBKDF2Tests.m @@ -0,0 +1,170 @@ +/* + * 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 +{ + [super 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 ADDED new_tests/OFPluginTests.m Index: new_tests/OFPluginTests.m ================================================================== --- new_tests/OFPluginTests.m +++ new_tests/OFPluginTests.m @@ -0,0 +1,54 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +#import "plugin/TestPlugin.h" + +@interface OFPluginTests: OTTestCase +@end + +@implementation OFPluginTests +- (void)testPlugin +{ + TestPlugin *test = nil; + OFString *path; + OFPlugin *plugin; + Class (*class)(void); + +#ifndef OF_IOS + path = [OFPlugin pathForName: @"plugin/TestPlugin"]; +#else + path = [OFPlugin pathForName: @"PlugIns/TestPlugin"]; +#endif + OTAssertNotNil(path); + + plugin = [OFPlugin pluginWithPath: path]; + OTAssertNotNil(plugin); + + class = (Class (*)(void))(uintptr_t)[plugin addressForSymbol: @"class"]; + OTAssert(class != NULL); + + @try { + test = [[class() alloc] init]; + OTAssertEqual([test test: 1234], 2468); + } @finally { + [test release]; + } +} +@end ADDED new_tests/OFPropertyListTests.m Index: new_tests/OFPropertyListTests.m ================================================================== --- new_tests/OFPropertyListTests.m +++ new_tests/OFPropertyListTests.m @@ -0,0 +1,114 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFPropertyListTests: OTTestCase +@end + +#define PLIST(x) \ + @"" \ + @"" \ + @"\n" \ + x @"\n" \ + @"" + +@implementation OFPropertyListTests +- (void)testObjectByParsingPropertyList +{ + OFArray *array = [OFArray arrayWithObjects: + @"Hello", + [OFData dataWithItems: "World!" count: 6], + [OFDate dateWithTimeIntervalSince1970: 1521030896], + [OFNumber numberWithBool: true], + [OFNumber numberWithBool: false], + [OFNumber numberWithFloat: 12.25f], + [OFNumber numberWithInt: -10], + nil]; + + OTAssertEqualObjects([PLIST( + @"Hello") objectByParsingPropertyList], + @"Hello"); + OTAssertEqualObjects([PLIST( + @"" + @" Hello" + @" V29ybGQh" + @" 2018-03-14T12:34:56Z" + @" " + @" " + @" 12.25" + @" -10" + @"") objectByParsingPropertyList], + array); + OTAssertEqualObjects([PLIST( + @"" + @" array" + @" " + @" Hello" + @" V29ybGQh" + @" 2018-03-14T12:34:56Z" + @" " + @" " + @" 12.25" + @" -10" + @" " + @" foo" + @" bar" + @"") objectByParsingPropertyList], + ([OFDictionary dictionaryWithKeysAndObjects: + @"array", array, + @"foo", @"bar", + nil])); +} + +- (void)testDetectUnsupportedVersion +{ + OTAssertThrowsSpecific( + [[PLIST(@"") + stringByReplacingOccurrencesOfString: @"1.0" + withString: @"1.1"] + objectByParsingPropertyList], + OFUnsupportedVersionException); +} + +- (void)testDetectInvalidFormat +{ + OTAssertThrowsSpecific( + [PLIST(@"") objectByParsingPropertyList], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [PLIST(@"") objectByParsingPropertyList], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [PLIST(@"") objectByParsingPropertyList], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [PLIST(@"") + objectByParsingPropertyList], + OFInvalidFormatException); + + OTAssertThrowsSpecific( + [PLIST(@"") + objectByParsingPropertyList], + OFInvalidFormatException); +} +@end ADDED new_tests/OFScryptTests.m Index: new_tests/OFScryptTests.m ================================================================== --- new_tests/OFScryptTests.m +++ new_tests/OFScryptTests.m @@ -0,0 +1,251 @@ +/* + * 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 OFScryptTests: OTTestCase +@end + +/* Test vectors form RFC 7914 */ +static const unsigned char salsa20Input[64] = { + 0x7E, 0x87, 0x9A, 0x21, 0x4F, 0x3E, 0xC9, 0x86, 0x7C, 0xA9, 0x40, 0xE6, + 0x41, 0x71, 0x8F, 0x26, 0xBA, 0xEE, 0x55, 0x5B, 0x8C, 0x61, 0xC1, 0xB5, + 0x0D, 0xF8, 0x46, 0x11, 0x6D, 0xCD, 0x3B, 0x1D, 0xEE, 0x24, 0xF3, 0x19, + 0xDF, 0x9B, 0x3D, 0x85, 0x14, 0x12, 0x1E, 0x4B, 0x5A, 0xC5, 0xAA, 0x32, + 0x76, 0x02, 0x1D, 0x29, 0x09, 0xC7, 0x48, 0x29, 0xED, 0xEB, 0xC6, 0x8D, + 0xB8, 0xB8, 0xC2, 0x5E +}; +static const unsigned char salsa20Output[64] = { + 0xA4, 0x1F, 0x85, 0x9C, 0x66, 0x08, 0xCC, 0x99, 0x3B, 0x81, 0xCA, 0xCB, + 0x02, 0x0C, 0xEF, 0x05, 0x04, 0x4B, 0x21, 0x81, 0xA2, 0xFD, 0x33, 0x7D, + 0xFD, 0x7B, 0x1C, 0x63, 0x96, 0x68, 0x2F, 0x29, 0xB4, 0x39, 0x31, 0x68, + 0xE3, 0xC9, 0xE6, 0xBC, 0xFE, 0x6B, 0xC5, 0xB7, 0xA0, 0x6D, 0x96, 0xBA, + 0xE4, 0x24, 0xCC, 0x10, 0x2C, 0x91, 0x74, 0x5C, 0x24, 0xAD, 0x67, 0x3D, + 0xC7, 0x61, 0x8F, 0x81 +}; +static const union { + unsigned char uc[128]; + uint32_t u32[32]; +} blockMixInput = { .uc = { + 0xF7, 0xCE, 0x0B, 0x65, 0x3D, 0x2D, 0x72, 0xA4, 0x10, 0x8C, 0xF5, 0xAB, + 0xE9, 0x12, 0xFF, 0xDD, 0x77, 0x76, 0x16, 0xDB, 0xBB, 0x27, 0xA7, 0x0E, + 0x82, 0x04, 0xF3, 0xAE, 0x2D, 0x0F, 0x6F, 0xAD, 0x89, 0xF6, 0x8F, 0x48, + 0x11, 0xD1, 0xE8, 0x7B, 0xCC, 0x3B, 0xD7, 0x40, 0x0A, 0x9F, 0xFD, 0x29, + 0x09, 0x4F, 0x01, 0x84, 0x63, 0x95, 0x74, 0xF3, 0x9A, 0xE5, 0xA1, 0x31, + 0x52, 0x17, 0xBC, 0xD7, + 0x89, 0x49, 0x91, 0x44, 0x72, 0x13, 0xBB, 0x22, 0x6C, 0x25, 0xB5, 0x4D, + 0xA8, 0x63, 0x70, 0xFB, 0xCD, 0x98, 0x43, 0x80, 0x37, 0x46, 0x66, 0xBB, + 0x8F, 0xFC, 0xB5, 0xBF, 0x40, 0xC2, 0x54, 0xB0, 0x67, 0xD2, 0x7C, 0x51, + 0xCE, 0x4A, 0xD5, 0xFE, 0xD8, 0x29, 0xC9, 0x0B, 0x50, 0x5A, 0x57, 0x1B, + 0x7F, 0x4D, 0x1C, 0xAD, 0x6A, 0x52, 0x3C, 0xDA, 0x77, 0x0E, 0x67, 0xBC, + 0xEA, 0xAF, 0x7E, 0x89 +}}; +static const unsigned char blockMixOutput[128] = { + 0xA4, 0x1F, 0x85, 0x9C, 0x66, 0x08, 0xCC, 0x99, 0x3B, 0x81, 0xCA, 0xCB, + 0x02, 0x0C, 0xEF, 0x05, 0x04, 0x4B, 0x21, 0x81, 0xA2, 0xFD, 0x33, 0x7D, + 0xFD, 0x7B, 0x1C, 0x63, 0x96, 0x68, 0x2F, 0x29, 0xB4, 0x39, 0x31, 0x68, + 0xE3, 0xC9, 0xE6, 0xBC, 0xFE, 0x6B, 0xC5, 0xB7, 0xA0, 0x6D, 0x96, 0xBA, + 0xE4, 0x24, 0xCC, 0x10, 0x2C, 0x91, 0x74, 0x5C, 0x24, 0xAD, 0x67, 0x3D, + 0xC7, 0x61, 0x8F, 0x81, + 0x20, 0xED, 0xC9, 0x75, 0x32, 0x38, 0x81, 0xA8, 0x05, 0x40, 0xF6, 0x4C, + 0x16, 0x2D, 0xCD, 0x3C, 0x21, 0x07, 0x7C, 0xFE, 0x5F, 0x8D, 0x5F, 0xE2, + 0xB1, 0xA4, 0x16, 0x8F, 0x95, 0x36, 0x78, 0xB7, 0x7D, 0x3B, 0x3D, 0x80, + 0x3B, 0x60, 0xE4, 0xAB, 0x92, 0x09, 0x96, 0xE5, 0x9B, 0x4D, 0x53, 0xB6, + 0x5D, 0x2A, 0x22, 0x58, 0x77, 0xD5, 0xED, 0xF5, 0x84, 0x2C, 0xB9, 0xF1, + 0x4E, 0xEF, 0xE4, 0x25 +}; +static const unsigned char ROMixInput[128] = { + 0xF7, 0xCE, 0x0B, 0x65, 0x3D, 0x2D, 0x72, 0xA4, 0x10, 0x8C, 0xF5, 0xAB, + 0xE9, 0x12, 0xFF, 0xDD, 0x77, 0x76, 0x16, 0xDB, 0xBB, 0x27, 0xA7, 0x0E, + 0x82, 0x04, 0xF3, 0xAE, 0x2D, 0x0F, 0x6F, 0xAD, 0x89, 0xF6, 0x8F, 0x48, + 0x11, 0xD1, 0xE8, 0x7B, 0xCC, 0x3B, 0xD7, 0x40, 0x0A, 0x9F, 0xFD, 0x29, + 0x09, 0x4F, 0x01, 0x84, 0x63, 0x95, 0x74, 0xF3, 0x9A, 0xE5, 0xA1, 0x31, + 0x52, 0x17, 0xBC, 0xD7, 0x89, 0x49, 0x91, 0x44, 0x72, 0x13, 0xBB, 0x22, + 0x6C, 0x25, 0xB5, 0x4D, 0xA8, 0x63, 0x70, 0xFB, 0xCD, 0x98, 0x43, 0x80, + 0x37, 0x46, 0x66, 0xBB, 0x8F, 0xFC, 0xB5, 0xBF, 0x40, 0xC2, 0x54, 0xB0, + 0x67, 0xD2, 0x7C, 0x51, 0xCE, 0x4A, 0xD5, 0xFE, 0xD8, 0x29, 0xC9, 0x0B, + 0x50, 0x5A, 0x57, 0x1B, 0x7F, 0x4D, 0x1C, 0xAD, 0x6A, 0x52, 0x3C, 0xDA, + 0x77, 0x0E, 0x67, 0xBC, 0xEA, 0xAF, 0x7E, 0x89 +}; +static const unsigned char ROMixOutput[128] = { + 0x79, 0xCC, 0xC1, 0x93, 0x62, 0x9D, 0xEB, 0xCA, 0x04, 0x7F, 0x0B, 0x70, + 0x60, 0x4B, 0xF6, 0xB6, 0x2C, 0xE3, 0xDD, 0x4A, 0x96, 0x26, 0xE3, 0x55, + 0xFA, 0xFC, 0x61, 0x98, 0xE6, 0xEA, 0x2B, 0x46, 0xD5, 0x84, 0x13, 0x67, + 0x3B, 0x99, 0xB0, 0x29, 0xD6, 0x65, 0xC3, 0x57, 0x60, 0x1F, 0xB4, 0x26, + 0xA0, 0xB2, 0xF4, 0xBB, 0xA2, 0x00, 0xEE, 0x9F, 0x0A, 0x43, 0xD1, 0x9B, + 0x57, 0x1A, 0x9C, 0x71, 0xEF, 0x11, 0x42, 0xE6, 0x5D, 0x5A, 0x26, 0x6F, + 0xDD, 0xCA, 0x83, 0x2C, 0xE5, 0x9F, 0xAA, 0x7C, 0xAC, 0x0B, 0x9C, 0xF1, + 0xBE, 0x2B, 0xFF, 0xCA, 0x30, 0x0D, 0x01, 0xEE, 0x38, 0x76, 0x19, 0xC4, + 0xAE, 0x12, 0xFD, 0x44, 0x38, 0xF2, 0x03, 0xA0, 0xE4, 0xE1, 0xC4, 0x7E, + 0xC3, 0x14, 0x86, 0x1F, 0x4E, 0x90, 0x87, 0xCB, 0x33, 0x39, 0x6A, 0x68, + 0x73, 0xE8, 0xF9, 0xD2, 0x53, 0x9A, 0x4B, 0x8E +}; +static const unsigned char testVector1[64] = { + 0x77, 0xD6, 0x57, 0x62, 0x38, 0x65, 0x7B, 0x20, 0x3B, 0x19, 0xCA, 0x42, + 0xC1, 0x8A, 0x04, 0x97, 0xF1, 0x6B, 0x48, 0x44, 0xE3, 0x07, 0x4A, 0xE8, + 0xDF, 0xDF, 0xFA, 0x3F, 0xED, 0xE2, 0x14, 0x42, 0xFC, 0xD0, 0x06, 0x9D, + 0xED, 0x09, 0x48, 0xF8, 0x32, 0x6A, 0x75, 0x3A, 0x0F, 0xC8, 0x1F, 0x17, + 0xE8, 0xD3, 0xE0, 0xFB, 0x2E, 0x0D, 0x36, 0x28, 0xCF, 0x35, 0xE2, 0x0C, + 0x38, 0xD1, 0x89, 0x06 +}; +static const unsigned char testVector2[64] = { + 0xFD, 0xBA, 0xBE, 0x1C, 0x9D, 0x34, 0x72, 0x00, 0x78, 0x56, 0xE7, 0x19, + 0x0D, 0x01, 0xE9, 0xFE, 0x7C, 0x6A, 0xD7, 0xCB, 0xC8, 0x23, 0x78, 0x30, + 0xE7, 0x73, 0x76, 0x63, 0x4B, 0x37, 0x31, 0x62, 0x2E, 0xAF, 0x30, 0xD9, + 0x2E, 0x22, 0xA3, 0x88, 0x6F, 0xF1, 0x09, 0x27, 0x9D, 0x98, 0x30, 0xDA, + 0xC7, 0x27, 0xAF, 0xB9, 0x4A, 0x83, 0xEE, 0x6D, 0x83, 0x60, 0xCB, 0xDF, + 0xA2, 0xCC, 0x06, 0x40 +}; +/* The third test vector is too expensive for m68k. */ +#ifndef OF_M68K +static const unsigned char testVector3[64] = { + 0x70, 0x23, 0xBD, 0xCB, 0x3A, 0xFD, 0x73, 0x48, 0x46, 0x1C, 0x06, 0xCD, + 0x81, 0xFD, 0x38, 0xEB, 0xFD, 0xA8, 0xFB, 0xBA, 0x90, 0x4F, 0x8E, 0x3E, + 0xA9, 0xB5, 0x43, 0xF6, 0x54, 0x5D, 0xA1, 0xF2, 0xD5, 0x43, 0x29, 0x55, + 0x61, 0x3F, 0x0F, 0xCF, 0x62, 0xD4, 0x97, 0x05, 0x24, 0x2A, 0x9A, 0xF9, + 0xE6, 0x1E, 0x85, 0xDC, 0x0D, 0x65, 0x1E, 0x40, 0xDF, 0xCF, 0x01, 0x7B, + 0x45, 0x57, 0x58, 0x87 +}; +#endif +/* The forth test vector is too expensive to include it in the tests. */ +#if 0 +static const unsigned char testVector4[64] = { + 0x21, 0x01, 0xCB, 0x9B, 0x6A, 0x51, 0x1A, 0xAE, 0xAD, 0xDB, 0xBE, 0x09, + 0xCF, 0x70, 0xF8, 0x81, 0xEC, 0x56, 0x8D, 0x57, 0x4A, 0x2F, 0xFD, 0x4D, + 0xAB, 0xE5, 0xEE, 0x98, 0x20, 0xAD, 0xAA, 0x47, 0x8E, 0x56, 0xFD, 0x8F, + 0x4B, 0xA5, 0xD0, 0x9F, 0xFA, 0x1C, 0x6D, 0x92, 0x7C, 0x40, 0xF4, 0xC3, + 0x37, 0x30, 0x40, 0x49, 0xE8, 0xA9, 0x52, 0xFB, 0xCB, 0xF4, 0x5C, 0x6F, + 0xA7, 0x7A, 0x41, 0xA4 +}; +#endif + +@implementation OFScryptTests +- (void)testSalsa20_8Core +{ + uint32_t salsa20Buffer[16]; + + memcpy(salsa20Buffer, salsa20Input, 64); + OFSalsa20_8Core(salsa20Buffer); + OTAssertEqual(memcmp(salsa20Buffer, salsa20Output, 64), 0); +} + +- (void)testBlockMix +{ + uint32_t blockMixBuffer[32]; + + OFScryptBlockMix(blockMixBuffer, blockMixInput.u32, 1); + OTAssertEqual(memcmp(blockMixBuffer, blockMixOutput, 128), 0); +} + +- (void)testROMix +{ + uint32_t ROMixBuffer[32], ROMixTmp[17 * 32]; + + memcpy(ROMixBuffer, ROMixInput, 128); + OFScryptROMix(ROMixBuffer, 1, 16, ROMixTmp); + OTAssertEqual(memcmp(ROMixBuffer, ROMixOutput, 128), 0); +} + +- (void)testRFC7941TestVector1 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ + .blockSize = 1, + .costFactor = 16, + .parallelization = 1, + .salt = (unsigned char *)"", + .saltLength = 0, + .password = "", + .passwordLength = 0, + .key = output, + .keyLength = 64, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(output, testVector1, 64), 0); +} + +- (void)testRFC7941TestVector2 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ + .blockSize = 8, + .costFactor = 1024, + .parallelization = 16, + .salt = (unsigned char *)"NaCl", + .saltLength = 4, + .password = "password", + .passwordLength = 8, + .key = output, + .keyLength = 64, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(output, testVector2, 64), 0); +} + +/* The third test vector is too expensive for m68k. */ +#ifndef OF_M68K +- (void)testRFC7941TestVector3 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ + .blockSize = 8, + .costFactor = 16384, + .parallelization = 1, + .salt = (unsigned char *)"SodiumChloride", + .saltLength = 14, + .password = "pleaseletmein", + .passwordLength = 13, + .key = output, + .keyLength = 64, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(output, testVector3, 64), 0); +} +#endif + +/* The forth test vector is too expensive to include it in the tests. */ +#if 0 +- (void)testRFC7941TestVector4 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ + .blockSize = 8, + .costFactor = 1048576, + .parallelization = 1, + .salt = (unsigned char *)"SodiumChloride", + .saltLength = 14, + .password = "pleaseletmein", + .passwordLength = 13, + .key = output, + .keyLength = 64, + .allowsSwappableMemory = true + }); + + OTAssertEqual(memcmp(output, testVector4, 64), 0); +} +#endif +@end ADDED new_tests/OFSocketTests.m Index: new_tests/OFSocketTests.m ================================================================== --- new_tests/OFSocketTests.m +++ new_tests/OFSocketTests.m @@ -0,0 +1,231 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFSocketTests: OTTestCase +@end + +#define COMPARE_V6(a, a0, a1, a2, a3, a4, a5, a6, a7) \ + (a.sockaddr.in6.sin6_addr.s6_addr[0] == (a0 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[1] == (a0 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[2] == (a1 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[3] == (a1 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[4] == (a2 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[5] == (a2 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[6] == (a3 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[7] == (a3 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[8] == (a4 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[9] == (a4 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[10] == (a5 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[11] == (a5 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[12] == (a6 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[13] == (a6 & 0xFF) && \ + a.sockaddr.in6.sin6_addr.s6_addr[14] == (a7 >> 8) && \ + a.sockaddr.in6.sin6_addr.s6_addr[15] == (a7 & 0xFF)) +#define SET_V6(a, a0, a1, a2, a3, a4, a5, a6, a7) \ + a.sockaddr.in6.sin6_addr.s6_addr[0] = a0 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[1] = a0 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[2] = a1 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[3] = a1 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[4] = a2 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[5] = a2 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[6] = a3 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[7] = a3 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[8] = a4 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[9] = a4 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[10] = a5 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[11] = a5 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[12] = a6 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[13] = a6 & 0xFF; \ + a.sockaddr.in6.sin6_addr.s6_addr[14] = a7 >> 8; \ + a.sockaddr.in6.sin6_addr.s6_addr[15] = a7 & 0xFF; + +@implementation OFSocketTests +- (void)testParseIPv4 +{ + OFSocketAddress address = OFSocketAddressParseIP(@"127.0.0.1", 1234); + + OTAssertEqual(OFFromBigEndian32(address.sockaddr.in.sin_addr.s_addr), + 0x7F000001); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in.sin_port), 1234); +} + +- (void)testParseRejectsInvalidIPv4 +{ + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"127.0.0.0.1", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"127.0.0.256", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"127.0.0. 1", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@" 127.0.0.1", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"127.0.a.1", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"127.0..1", 1234), + OFInvalidFormatException); +} + +- (void)testPortForIPv4 +{ + OFSocketAddress address = OFSocketAddressParseIP(@"127.0.0.1", 1234); + + OTAssertEqual(OFSocketAddressIPPort(&address), 1234); +} + +- (void)testStringForIPv4 +{ + OFSocketAddress address = OFSocketAddressParseIP(@"127.0.0.1", 1234); + + OTAssertEqualObjects(OFSocketAddressString(&address), @"127.0.0.1"); +} + +- (void)testParseIPv6 +{ + OFSocketAddress address; + + address = OFSocketAddressParseIP( + @"1122:3344:5566:7788:99aa:bbCc:ddee:ff00", 1234); + OTAssert(COMPARE_V6(address, + 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"::", 1234); + OTAssert(COMPARE_V6(address, 0, 0, 0, 0, 0, 0, 0, 0)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"aaAa::bBbb", 1234); + OTAssert(COMPARE_V6(address, 0xAAAA, 0, 0, 0, 0, 0, 0, 0xBBBB)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"aaAa::", 1234); + OTAssert(COMPARE_V6(address, 0xAAAA, 0, 0, 0, 0, 0, 0, 0)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"::aaAa", 1234); + OTAssert(COMPARE_V6(address, 0, 0, 0, 0, 0, 0, 0, 0xAAAA)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"fd00::1%123", 1234); + OTAssert(COMPARE_V6(address, 0xFD00, 0, 0, 0, 0, 0, 0, 1)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + OTAssertEqual(address.sockaddr.in6.sin6_scope_id, 123); + + address = OFSocketAddressParseIP(@"::ffff:127.0.0.1", 1234); + OTAssert(COMPARE_V6(address, 0, 0, 0, 0, 0, 0xFFFF, 0x7F00, 1)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); + + address = OFSocketAddressParseIP(@"64:ff9b::127.0.0.1", 1234); + OTAssert(COMPARE_V6(address, 0x64, 0xFF9B, 0, 0, 0, 0, 0x7F00, 1)); + OTAssertEqual(OFFromBigEndian16(address.sockaddr.in6.sin6_port), 1234); +} + +- (void)testParseRejectsInvalidIPv6 +{ + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1:::2", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1: ::2", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1:: :2", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1::2::3", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"10000::1", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"::10000", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"::1::", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1:2:3:4:5:6:7:", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1:2:3:4:5:6:7::", 1234), + OFInvalidFormatException); + + OTAssertThrowsSpecific(OFSocketAddressParseIP(@"1:2", 1234), + OFInvalidFormatException); +} + +- (void)testPortForIPv6 +{ + OFSocketAddress address = OFSocketAddressParseIP(@"::", 1234); + + OTAssertEqual(OFSocketAddressIPPort(&address), 1234); +} + +- (void)testStringForIPv6 +{ + OFSocketAddress address = OFSocketAddressParseIP(@"::", 1234); + + OTAssertEqualObjects(OFSocketAddressString(&address), @"::"); + + SET_V6(address, 0, 0, 0, 0, 0, 0, 0, 1) + OTAssertEqualObjects(OFSocketAddressString(&address), @"::1"); + + SET_V6(address, 1, 0, 0, 0, 0, 0, 0, 0) + OTAssertEqualObjects(OFSocketAddressString(&address), @"1::"); + + SET_V6(address, + 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"1122:3344:5566:7788:99aa:bbcc:ddee:ff00"); + + SET_V6(address, + 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"1122:3344:5566:7788:99aa:bbcc:ddee:0"); + + SET_V6(address, 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"1122:3344:5566:7788:99aa:bbcc::"); + + SET_V6(address, + 0, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"0:3344:5566:7788:99aa:bbcc:ddee:ff00"); + + SET_V6(address, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"::5566:7788:99aa:bbcc:ddee:ff00"); + + SET_V6(address, 0, 0, 0x5566, 0, 0, 0, 0xDDEE, 0xFF00) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"0:0:5566::ddee:ff00"); + + SET_V6(address, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0) + OTAssertEqualObjects(OFSocketAddressString(&address), + @"::5566:7788:99aa:bbcc:0:0"); + + address.sockaddr.in6.sin6_scope_id = 123; + OTAssertEqualObjects(OFSocketAddressString(&address), + @"::5566:7788:99aa:bbcc:0:0%123"); +} +@end ADDED new_tests/OFSubprocessTests.m Index: new_tests/OFSubprocessTests.m ================================================================== --- new_tests/OFSubprocessTests.m +++ new_tests/OFSubprocessTests.m @@ -0,0 +1,52 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFSubprocessTests: OTTestCase +@end + +@implementation OFSubprocessTests +- (void)testSubprocess +{ +#ifdef OF_HAVE_FILES + OFString *program = [@"subprocess" stringByAppendingPathComponent: + @"subprocess" @PROG_SUFFIX]; +#else + OFString *program = @"subprocess/subprocess" @PROG_SUFFIX; +#endif + OFArray *arguments = [OFArray arrayWithObjects: @"tést", @"123", nil]; + OFMutableDictionary *environment = + [[[OFApplication environment] mutableCopy] autorelease]; + OFSubprocess *subprocess; + + [environment setObject: @"yés" forKey: @"tëst"]; + + subprocess = [OFSubprocess subprocessWithProgram: program + programName: program + arguments: arguments + environment: environment]; + + [subprocess writeLine: @"Hellö world!"]; + OTAssertEqualObjects([subprocess readLine], @"HELLÖ WORLD!"); + + [subprocess closeForWriting]; + + OTAssertEqual([subprocess waitForTermination], 0); +} +@end ADDED new_tests/OFThreadTests.m Index: new_tests/OFThreadTests.m ================================================================== --- new_tests/OFThreadTests.m +++ new_tests/OFThreadTests.m @@ -0,0 +1,48 @@ +/* + * 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 "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFThreadTests: OTTestCase +@end + +@interface TestThread: OFThread +@end + +@implementation TestThread +- (id)main +{ + [[OFThread threadDictionary] setObject: @"bar" forKey: @"foo"]; + OFEnsure([[[OFThread threadDictionary] + objectForKey: @"foo"] isEqual: @"bar"]); + + return @"success"; +} +@end + +@implementation OFThreadTests +- (void)testThread +{ + TestThread *thread = [TestThread thread]; + + [thread start]; + + OTAssertEqualObjects([thread join], @"success"); + OTAssertNil([[OFThread threadDictionary] objectForKey: @"foo"]); +} +@end ADDED new_tests/plugin/Info.plist.in Index: new_tests/plugin/Info.plist.in ================================================================== --- new_tests/plugin/Info.plist.in +++ new_tests/plugin/Info.plist.in @@ -0,0 +1,22 @@ + + + + + CFBundleExecutable + TestPlugin + CFBundleName + TestPlugin + CFBundleIdentifier + im.nil.objfw.tests.plugin + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + BNDL + CFBundleVersion + @BUNDLE_VERSION@ + CFBundleShortVersionString + @BUNDLE_SHORT_VERSION@ + MinimumOSVersion + 9.0 + + ADDED new_tests/plugin/Makefile Index: new_tests/plugin/Makefile ================================================================== --- new_tests/plugin/Makefile +++ new_tests/plugin/Makefile @@ -0,0 +1,11 @@ +DISTCLEAN = Info.plist + +PLUGIN_NOINST = TestPlugin${PLUGIN_SUFFIX} +SRCS = TestPlugin.m + +include ../../buildsys.mk +include ../../extra.mk + +CPPFLAGS += -I../.. -I../../src -I../../src/runtime +LIBS := ${TESTPLUGIN_LIBS} ${LIBS} +LD = ${OBJC} ADDED new_tests/plugin/TestPlugin.h Index: new_tests/plugin/TestPlugin.h ================================================================== --- new_tests/plugin/TestPlugin.h +++ new_tests/plugin/TestPlugin.h @@ -0,0 +1,20 @@ +/* + * 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 "OFObject.h" + +@interface TestPlugin: OFObject +- (int)test: (int)num; +@end ADDED new_tests/plugin/TestPlugin.m Index: new_tests/plugin/TestPlugin.m ================================================================== --- new_tests/plugin/TestPlugin.m +++ new_tests/plugin/TestPlugin.m @@ -0,0 +1,51 @@ +/* + * 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 "TestPlugin.h" + +#ifdef OF_OBJFW_RUNTIME +# import "runtime/private.h" + +OF_DESTRUCTOR() +{ + Class class = objc_getClass("TestPlugin"); + + if (class == Nil) + /* + * musl has broken dlclose(): Instead of calling the destructor + * on dlclose(), they call it on exit(). This of course means + * that our tests might have already called objc_deinit() and + * the class is already gone. + */ + return; + + objc_unregisterClass(class); +} +#endif + +@implementation TestPlugin +- (int)test: (int)num +{ + return num * 2; +} +@end + +Class +class(void) +{ + return [TestPlugin class]; +} ADDED new_tests/subprocess/Makefile Index: new_tests/subprocess/Makefile ================================================================== --- new_tests/subprocess/Makefile +++ new_tests/subprocess/Makefile @@ -0,0 +1,9 @@ +PROG_NOINST = subprocess${PROG_SUFFIX} +SRCS = Subprocess.m + +include ../../buildsys.mk +include ../../extra.mk + +CPPFLAGS += -I../../src -I../../src/exceptions -I../../src/runtime -I../.. +LIBS := -L../../src -lobjfw -L../../src/runtime ${RUNTIME_LIBS} ${LIBS} +LD = ${OBJC} ADDED new_tests/subprocess/Subprocess.m Index: new_tests/subprocess/Subprocess.m ================================================================== --- new_tests/subprocess/Subprocess.m +++ new_tests/subprocess/Subprocess.m @@ -0,0 +1,43 @@ +/* + * 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 "ObjFW.h" + +@interface Subprocess: OFObject +@end + +OF_APPLICATION_DELEGATE(Subprocess) + +@implementation Subprocess +- (void)applicationDidFinishLaunching: (OFNotification *)notification +{ + OFString *line; + + if (![[OFApplication arguments] isEqual: + [OFArray arrayWithObjects: @"tést", @"123", nil]]) + [OFApplication terminateWithStatus: 1]; + + if (![[[OFApplication environment] objectForKey: @"tëst"] + isEqual: @"yés"]) + [OFApplication terminateWithStatus: 2]; + + while ((line = [OFStdIn readLine]) != nil) + [OFStdOut writeLine: line.uppercaseString]; + + [OFApplication terminate]; +} +@end ADDED new_tests/testfile.bin Index: new_tests/testfile.bin ================================================================== --- new_tests/testfile.bin +++ new_tests/testfile.bin cannot compute difference between binary files ADDED new_tests/testfile.ini Index: new_tests/testfile.ini ================================================================== --- new_tests/testfile.ini +++ new_tests/testfile.ini @@ -0,0 +1,21 @@ +[tests] +foo = bar +foobar=baz +;comment + +[foobar] +;foobarcomment +qux=" asd" +"quxqux " = asd +quxquxqux="hello\"wörld" +qux2="a\f" + +[types] +integer = 0x20 +bool = true +float = 0.5 +array1 = 1 +array2 = 1 +double = 0.25 +array1 = 2 +array2 = 2 Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -9,18 +9,27 @@ FRAMEWORK = ${OBJFW_FRAMEWORK} LIB_MAJOR = ${OBJFW_LIB_MAJOR} LIB_MINOR = ${OBJFW_LIB_MINOR} LIB_PATCH = ${OBJFW_LIB_PATCH} -SRCS = OFApplication.m \ +SRCS = OFASN1BitString.m \ + OFASN1Enumerated.m \ + OFASN1IA5String.m \ + OFASN1NumericString.m \ + OFASN1ObjectIdentifier.m \ + OFASN1OctetString.m \ + OFASN1PrintableString.m \ + OFASN1Value.m \ + OFApplication.m \ OFArray.m \ OFBlock.m \ OFCharacterSet.m \ OFColor.m \ OFConstantString.m \ OFCountedSet.m \ OFData.m \ + OFData+ASN1DERParsing.m \ OFData+CryptographicHashing.m \ OFData+MessagePackParsing.m \ OFDate.m \ OFDictionary.m \ OFEnumerator.m \ @@ -109,11 +118,11 @@ OFZIPArchive.m \ OFZIPArchiveEntry.m \ ${USE_SRCS_FILES} \ ${USE_SRCS_PLUGINS} \ ${USE_SRCS_SOCKETS} \ - ${USE_SRCS_SUBPROCESS} \ + ${USE_SRCS_SUBPROCESSES} \ ${USE_SRCS_THREADS} \ ${USE_SRCS_WINDOWS} SRCS_FILES = OFFile.m \ OFString+PathAdditions.m SRCS_PLUGINS = OFPlugin.m @@ -155,11 +164,11 @@ SRCS_IPX = OFIPXSocket.m \ OFSPXSocket.m \ OFSPXStreamSocket.m SRCS_UNIX_SOCKETS = OFUNIXDatagramSocket.m \ OFUNIXStreamSocket.m -SRCS_SUBPROCESS = OFSubprocess.m +SRCS_SUBPROCESSES = OFSubprocess.m SRCS_THREADS = OFCondition.m \ OFMutex.m \ OFPlainCondition.m \ OFPlainMutex.m \ OFPlainThread.m \ @@ -172,10 +181,11 @@ platform/GCC4.7/OFAtomic.h \ platform/PowerPC/OFAtomic.h \ platform/macOS/OFAtomic.h \ platform/x86/OFAtomic.h INCLUDES := ${SRCS:.m=.h} \ + OFASN1DERRepresentation.h \ OFArchiveEntry.h \ OFCollection.h \ OFCryptographicHash.h \ OFJSONRepresentation.h \ OFKernelEventObserver.h \ ADDED src/OFASN1BitString.h Index: src/OFASN1BitString.h ================================================================== --- src/OFASN1BitString.h +++ src/OFASN1BitString.h @@ -0,0 +1,84 @@ +/* + * 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 "OFObject.h" +#import "OFASN1DERRepresentation.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFData; + +/** + * @brief An ASN.1 BitString. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1BitString: OFObject +{ + OFData *_bitStringValue; + size_t _bitStringLength; +} + +/** + * @brief The BitString value. + */ +@property (readonly, nonatomic) OFData *bitStringValue; + +/** + * @brief The length of the BitString in bits. + */ +@property (readonly, nonatomic) size_t bitStringLength; + +/** + * @brief Creates an ASN.1 BitString with the specified BitString value and + * length. + * + * @param bitString The value of the BitString + * @param length The length of the BitString in bits + * @return A new, autoreleased OFASN1BitString + */ ++ (instancetype)bitStringWithBitString: (OFData *)bitString + length: (size_t)length; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated ASN.1 BitString with the specified + * BitString value and length. + * + * @param bitString The value of the BitString + * @param length The length of the BitString in bits + * @return An initialized OFASN1BitString + */ +- (instancetype)initWithBitString: (OFData *)bitString + length: (size_t)length OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 BitString with the specified + * arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized OFASN1BitString + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1BitString.m Index: src/OFASN1BitString.m ================================================================== --- src/OFASN1BitString.m +++ src/OFASN1BitString.m @@ -0,0 +1,178 @@ +/* + * 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 "OFASN1BitString.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" +#import "OFInvalidFormatException.h" +#import "OFOutOfRangeException.h" + +@implementation OFASN1BitString +@synthesize bitStringValue = _bitStringValue; +@synthesize bitStringLength = _bitStringLength; + ++ (instancetype)bitStringWithBitString: (OFData *)bitString + length: (size_t)length +{ + return [[[self alloc] initWithBitString: bitString + length: length] autorelease]; +} + +- (instancetype)initWithBitString: (OFData *)bitString length: (size_t)length +{ + self = [super init]; + + @try { + if (bitString.count * bitString.itemSize != + OFRoundUpToPowerOf2(8, length) / 8) + @throw [OFInvalidFormatException exception]; + + _bitStringValue = [bitString copy]; + _bitStringLength = length; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + void *pool = objc_autoreleasePoolPush(); + OFData *bitString; + size_t length; + + @try { + unsigned char unusedBits; + size_t count = DEREncodedContents.count; + + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberBitString || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1 || count == 0) + @throw [OFInvalidFormatException exception]; + + unusedBits = + *(unsigned char *)[DEREncodedContents itemAtIndex: 0]; + + if (unusedBits > 7) + @throw [OFInvalidFormatException exception]; + + /* + * Can't have any bits of the last byte unused if we have no + * byte. + */ + if (count == 1 && unusedBits != 0) + @throw [OFInvalidFormatException exception]; + + if (SIZE_MAX / 8 < count - 1) + @throw [OFOutOfRangeException exception]; + + length = (count - 1) * 8; + bitString = [DEREncodedContents subdataWithRange: + OFMakeRange(1, count - 1)]; + + if (unusedBits != 0) + length -= unusedBits; + } @catch (id e) { + [self release]; + @throw e; + } + + self = [self initWithBitString: bitString length: length]; + + objc_autoreleasePoolPop(pool); + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_bitStringValue release]; + + [super dealloc]; +} + +- (OFData *)ASN1DERRepresentation +{ + size_t bitStringValueCount = _bitStringValue.count; + size_t roundedUpLength = OFRoundUpToPowerOf2(8, _bitStringLength); + unsigned char unusedBits = roundedUpLength - _bitStringLength; + unsigned char header[] = { + OFASN1TagNumberBitString, + bitStringValueCount + 1, + unusedBits + }; + OFMutableData *data; + + if (bitStringValueCount + 1 > UINT8_MAX || + bitStringValueCount != roundedUpLength / 8) + @throw [OFInvalidFormatException exception]; + + data = [OFMutableData + dataWithCapacity: sizeof(header) + bitStringValueCount]; + [data addItems: header count: sizeof(header)]; + [data addItems: _bitStringValue.items count: bitStringValueCount]; + + [data makeImmutable]; + + return data; +} + +- (bool)isEqual: (id)object +{ + OFASN1BitString *bitString; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1BitString class]]) + return false; + + bitString = object; + + if (![bitString->_bitStringValue isEqual: _bitStringValue]) + return false; + if (bitString->_bitStringLength != _bitStringLength) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _bitStringValue.hash + (unsigned long)_bitStringLength; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _bitStringValue, _bitStringLength]; +} +@end ADDED src/OFASN1DERRepresentation.h Index: src/OFASN1DERRepresentation.h ================================================================== --- src/OFASN1DERRepresentation.h +++ src/OFASN1DERRepresentation.h @@ -0,0 +1,36 @@ +/* + * 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 "OFObject.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFData; + +/** + * @protocol OFASN1DERRepresentation \ + * OFASN1DERRepresentation.h ObjFW/OFASN1DERRepresentation.h + * + * @brief A protocol implemented by classes that support encoding to ASN.1 DER + * representation. + */ +@protocol OFASN1DERRepresentation +/** + * @brief The object in ASN.1 DER representation. + */ +@property (readonly, nonatomic) OFData *ASN1DERRepresentation; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1Enumerated.h Index: src/OFASN1Enumerated.h ================================================================== --- src/OFASN1Enumerated.h +++ src/OFASN1Enumerated.h @@ -0,0 +1,70 @@ +/* + * 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 "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +/** + * @brief An ASN.1 Enumerated. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1Enumerated: OFObject +{ + long long _longLongValue; +} + +/** + * @brief The integer value. + */ +@property (readonly, nonatomic) long long longLongValue; + +/** + * @brief Creates an ASN.1 Enumerated with the specified integer value. + * + * @param value The `long long` value of the Enumerated + * @return A new, autoreleased OFASN1Enumerated + */ ++ (instancetype)enumeratedWithLongLong: (long long)value; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated ASN.1 Enumerated with the specified + * integer value. + * + * @param value The `long long` value of the Enumerated + * @return An initialized OFASN1Enumerated + */ +- (instancetype)initWithLongLong: (long long)value OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 Enumerated with the specified + * arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized OFASN1Enumerated + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1Enumerated.m Index: src/OFASN1Enumerated.m ================================================================== --- src/OFASN1Enumerated.m +++ src/OFASN1Enumerated.m @@ -0,0 +1,102 @@ +/* + * 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 "OFASN1Enumerated.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" + +extern long long OFASN1DERIntegerParse(const unsigned char *buffer, + size_t length); + +@implementation OFASN1Enumerated +@synthesize longLongValue = _longLongValue; + ++ (instancetype)enumeratedWithLongLong: (long long)value +{ + return [[[self alloc] initWithLongLong: value] autorelease]; +} + +- (instancetype)initWithLongLong: (long long)value +{ + self = [super init]; + + _longLongValue = value; + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + long long value; + + @try { + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberEnumerated || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + + value = OFASN1DERIntegerParse( + DEREncodedContents.items, DEREncodedContents.count); + } @catch (id e) { + [self release]; + @throw e; + } + + return [self initWithLongLong: value]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (bool)isEqual: (id)object +{ + OFASN1Enumerated *enumerated; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1Enumerated class]]) + return false; + + enumerated = object; + + if (enumerated->_longLongValue != _longLongValue) + return false; + + return true; +} + +- (unsigned long)hash +{ + return (unsigned long)_longLongValue; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _longLongValue]; +} +@end ADDED src/OFASN1IA5String.h Index: src/OFASN1IA5String.h ================================================================== --- src/OFASN1IA5String.h +++ src/OFASN1IA5String.h @@ -0,0 +1,77 @@ +/* + * 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 "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFString; + +/** + * @brief An ASN.1 IA5String. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1IA5String: OFObject +{ + OFString *_IA5StringValue; +} + +/** + * @brief The IA5String value. + */ +@property (readonly, nonatomic) OFString *IA5StringValue; + +/** + * @brief The string value. + */ +@property (readonly, nonatomic) OFString *stringValue; + +/** + * @brief Creates an IA5String with the specified string value. + * + * @param string The string value of the IA5String + * @return A new, autoreleased OFASN1IA5String + */ ++ (instancetype)stringWithString: (OFString *)string; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated IA5String with the specified string + * value. + * + * @param string The string value of the IA5String + * @return An initialized OFASN1IA5String + */ +- (instancetype)initWithString: (OFString *)string OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 IA5String with the specified + * arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized OFASN1IA5String + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1IA5String.m Index: src/OFASN1IA5String.m ================================================================== --- src/OFASN1IA5String.m +++ src/OFASN1IA5String.m @@ -0,0 +1,123 @@ +/* + * 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 "OFASN1IA5String.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" + +@implementation OFASN1IA5String +@synthesize IA5StringValue = _IA5StringValue; + ++ (instancetype)stringWithString: (OFString *)string +{ + return [[[self alloc] initWithString: string] autorelease]; +} + +- (instancetype)initWithString: (OFString *)string +{ + self = [super init]; + + @try { + _IA5StringValue = [string copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + void *pool = objc_autoreleasePoolPush(); + OFString *IA5String; + + @try { + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberIA5String || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + + IA5String= [OFString + stringWithCString: DEREncodedContents.items + encoding: OFStringEncodingASCII + length: DEREncodedContents.count]; + } @catch (id e) { + [self release]; + @throw e; + } + + self = [self initWithString: IA5String]; + + objc_autoreleasePoolPop(pool); + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_IA5StringValue release]; + + [super dealloc]; +} + +- (OFString *)stringValue +{ + return self.IA5StringValue; +} + +- (bool)isEqual: (id)object +{ + OFASN1IA5String *IA5String; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1IA5String class]]) + return false; + + IA5String = object; + + if (![IA5String->_IA5StringValue isEqual: _IA5StringValue]) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _IA5StringValue.hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _IA5StringValue]; +} +@end ADDED src/OFASN1NumericString.h Index: src/OFASN1NumericString.h ================================================================== --- src/OFASN1NumericString.h +++ src/OFASN1NumericString.h @@ -0,0 +1,77 @@ +/* + * 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 "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFString; + +/** + * @brief An ASN.1 NumericString. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1NumericString: OFObject +{ + OFString *_numericStringValue; +} + +/** + * @brief The NumericString value. + */ +@property (readonly, nonatomic) OFString *numericStringValue; + +/** + * @brief The string value. + */ +@property (readonly, nonatomic) OFString *stringValue; + +/** + * @brief Creates an NumericString with the specified string value. + * + * @param string The string value of the NumericString + * @return A new, autoreleased OFASN1NumericString + */ ++ (instancetype)stringWithString: (OFString *)string; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated NumericString with the specified + * string value. + * + * @param string The string value of the NumericString + * @return An initialized OFASN1NumericString + */ +- (instancetype)initWithString: (OFString *)string OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 NumericString with the + * specified arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized ASN.1 NumericString + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1NumericString.m Index: src/OFASN1NumericString.m ================================================================== --- src/OFASN1NumericString.m +++ src/OFASN1NumericString.m @@ -0,0 +1,134 @@ +/* + * 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 "OFASN1NumericString.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" +#import "OFInvalidEncodingException.h" + +@implementation OFASN1NumericString +@synthesize numericStringValue = _numericStringValue; + ++ (instancetype)stringWithString: (OFString *)string +{ + return [[[self alloc] initWithString: string] autorelease]; +} + +- (instancetype)initWithString: (OFString *)string +{ + self = [super init]; + + @try { + void *pool = objc_autoreleasePoolPush(); + const char *cString = string.UTF8String; + size_t length = string.UTF8StringLength; + + for (size_t i = 0; i < length; i++) + if (!OFASCIIIsDigit(cString[i]) && cString[i] != ' ') + @throw [OFInvalidEncodingException exception]; + + _numericStringValue = [string copy]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + void *pool = objc_autoreleasePoolPush(); + OFString *numericString; + + @try { + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberNumericString || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + + numericString = [OFString + stringWithCString: DEREncodedContents.items + encoding: OFStringEncodingASCII + length: DEREncodedContents.count]; + } @catch (id e) { + [self release]; + @throw e; + } + + self = [self initWithString: numericString]; + + objc_autoreleasePoolPop(pool); + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_numericStringValue release]; + + [super dealloc]; +} + +- (OFString *)stringValue +{ + return self.numericStringValue; +} + +- (bool)isEqual: (id)object +{ + OFASN1NumericString *numericString; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1NumericString class]]) + return false; + + numericString = object; + + if (![numericString->_numericStringValue isEqual: _numericStringValue]) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _numericStringValue.hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _numericStringValue]; +} +@end ADDED src/OFASN1ObjectIdentifier.h Index: src/OFASN1ObjectIdentifier.h ================================================================== --- src/OFASN1ObjectIdentifier.h +++ src/OFASN1ObjectIdentifier.h @@ -0,0 +1,75 @@ +/* + * 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 "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFArray OF_GENERIC(ObjetType); +@class OFNumber; + +/** + * @brief An ASN.1 ObjectIdentifier. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1ObjectIdentifier: OFObject +{ + OFArray OF_GENERIC(OFNumber *) *_subidentifiers; +} + +/** + * @brief The subidentifiers of the ObjectIdentifier. + */ +@property (readonly, nonatomic) OFArray OF_GENERIC(OFNumber *) *subidentifiers; + +/** + * @brief Creates an ASN.1 ObjectIdentifier with the specified subidentifiers. + * + * @param subidentifiers The subidentifiers of the ASN.1 ObjectIdentifier + * @return A new, autoreleased OFASN1ObjectIdentifier + */ ++ (instancetype)objectIdentifierWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated ASN.1 ObjectIdentifier with the + * specified subidentifiers. + * + * @param subidentifiers The subidentifiers of the ASN.1 ObjectIdentifier + * @return An initialized OFASN1ObjectIdentifier + */ +- (instancetype)initWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 ObjectIdentifier with the + * specified arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized OFASN1ObjectIdentifier + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1ObjectIdentifier.m Index: src/OFASN1ObjectIdentifier.m ================================================================== --- src/OFASN1ObjectIdentifier.m +++ src/OFASN1ObjectIdentifier.m @@ -0,0 +1,180 @@ +/* + * 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 "OFASN1ObjectIdentifier.h" +#import "OFArray.h" +#import "OFData.h" +#import "OFNumber.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" +#import "OFInvalidFormatException.h" +#import "OFOutOfRangeException.h" + +@implementation OFASN1ObjectIdentifier +@synthesize subidentifiers = _subidentifiers; + ++ (instancetype)objectIdentifierWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers +{ + return [[[self alloc] + initWithSubidentifiers: subidentifiers] autorelease]; +} + +- (instancetype)initWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers +{ + self = [super init]; + + @try { + if (subidentifiers.count < 1) + @throw [OFInvalidFormatException exception]; + + switch ([[subidentifiers objectAtIndex: 0] longLongValue]) { + case 0: + case 1: + case 2: + break; + default: + @throw [OFInvalidFormatException exception]; + } + + _subidentifiers = [subidentifiers copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + void *pool = objc_autoreleasePoolPush(); + OFMutableArray OF_GENERIC(OFNumber *) *subidentifiers; + + @try { + const unsigned char *items = DEREncodedContents.items; + size_t count = DEREncodedContents.count; + unsigned long long value = 0; + uint_fast8_t bits = 0; + + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberObjectIdentifier || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1 || count == 0) + @throw [OFInvalidArgumentException exception]; + + subidentifiers = [OFMutableArray array]; + + for (size_t i = 0; i < count; i++) { + if (bits == 0 && items[i] == 0x80) + @throw [OFInvalidFormatException exception]; + + value = (value << 7) | (items[i] & 0x7F); + bits += 7; + + if (bits > sizeof(unsigned long long) * 8) + @throw [OFOutOfRangeException exception]; + + if (items[i] & 0x80) + continue; + + if (subidentifiers.count == 0) { + if (value < 40) + [subidentifiers addObject: + [OFNumber numberWithInt: 0]]; + else if (value < 80) { + [subidentifiers addObject: + [OFNumber numberWithInt: 1]]; + value -= 40; + } else { + [subidentifiers addObject: + [OFNumber numberWithInt: 2]]; + value -= 80; + } + } + + [subidentifiers addObject: + [OFNumber numberWithUnsignedLongLong: value]]; + + value = 0; + bits = 0; + } + + if (items[count - 1] & 0x80) + @throw [OFInvalidFormatException exception]; + + [subidentifiers makeImmutable]; + } @catch (id e) { + [self release]; + @throw e; + } + + self = [self initWithSubidentifiers: subidentifiers]; + + objc_autoreleasePoolPop(pool); + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_subidentifiers release]; + + [super dealloc]; +} + +- (bool)isEqual: (id)object +{ + OFASN1ObjectIdentifier *objectIdentifier; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1ObjectIdentifier class]]) + return false; + + objectIdentifier = object; + + if (![objectIdentifier->_subidentifiers isEqual: _subidentifiers]) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _subidentifiers.hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"", + [_subidentifiers componentsJoinedByString: @"."]]; +} +@end ADDED src/OFASN1OctetString.h Index: src/OFASN1OctetString.h ================================================================== --- src/OFASN1OctetString.h +++ src/OFASN1OctetString.h @@ -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. + */ + +#import "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFData; + +/** + * @brief An ASN.1 OctetString. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1OctetString: OFObject +{ + OFData *_octetStringValue; +} + +/** + * @brief The OctetString value. + */ +@property (readonly, nonatomic) OFData *octetStringValue; + +/** + * @brief Creates an OctetString with the specified value. + * + * @param octetString The OctetString value + * @return A new, autoreleased OFASN1OctetString + */ ++ (instancetype)octetStringWithOctetString: (OFData *)octetString; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated OctetString with the specified + * value. + * + * @param octetString The OctetString value + * @return An initialized OFASN1OctetString + */ +- (instancetype)initWithOctetString: (OFData *)octetString + OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 OctetString with the specified + * arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized ASN.1 OctetString + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1OctetString.m Index: src/OFASN1OctetString.m ================================================================== --- src/OFASN1OctetString.m +++ src/OFASN1OctetString.m @@ -0,0 +1,106 @@ +/* + * 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 "OFASN1OctetString.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" + +@implementation OFASN1OctetString +@synthesize octetStringValue = _octetStringValue; + ++ (instancetype)octetStringWithOctetString: (OFData *)octetString +{ + return [[[self alloc] initWithOctetString: octetString] autorelease]; +} + +- (instancetype)initWithOctetString: (OFData *)octetString +{ + self = [super init]; + + @try { + _octetStringValue = [octetString copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + @try { + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberOctetString || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + } @catch (id e) { + [self release]; + @throw e; + } + + return [self initWithOctetString: DEREncodedContents]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_octetStringValue release]; + + [super dealloc]; +} + +- (bool)isEqual: (id)object +{ + OFASN1OctetString *octetString; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1OctetString class]]) + return false; + + octetString = object; + + if (![octetString->_octetStringValue isEqual: _octetStringValue]) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _octetStringValue.hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _octetStringValue]; +} +@end ADDED src/OFASN1PrintableString.h Index: src/OFASN1PrintableString.h ================================================================== --- src/OFASN1PrintableString.h +++ src/OFASN1PrintableString.h @@ -0,0 +1,77 @@ +/* + * 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 "OFObject.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +@class OFString; + +/** + * @brief An ASN.1 PrintableString. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1PrintableString: OFObject +{ + OFString *_printableStringValue; +} + +/** + * @brief The PrintableString value. + */ +@property (readonly, nonatomic) OFString *printableStringValue; + +/** + * @brief The string value. + */ +@property (readonly, nonatomic) OFString *stringValue; + +/** + * @brief Creates a PrintableString with the specified string value. + * + * @param string The string value of the PrintableString + * @return A new, autoreleased OFASN1PrintableString + */ ++ (instancetype)stringWithString: (OFString *)string; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated PrintableString with the specified + * string value. + * + * @param string The string value of the PrintableString + * @return An initialized OFASN1PrintableString + */ +- (instancetype)initWithString: (OFString *)string OF_DESIGNATED_INITIALIZER; + +/** + * @brief Initializes an already allocated ASN.1 PrintableString with the + * specified arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized OFASN1PrintableString + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1PrintableString.m Index: src/OFASN1PrintableString.m ================================================================== --- src/OFASN1PrintableString.m +++ src/OFASN1PrintableString.m @@ -0,0 +1,154 @@ +/* + * 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 "OFASN1PrintableString.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" +#import "OFInvalidEncodingException.h" + +@implementation OFASN1PrintableString +@synthesize printableStringValue = _printableStringValue; + ++ (instancetype)stringWithString: (OFString *)string +{ + return [[[self alloc] initWithString: string] autorelease]; +} + +- (instancetype)initWithString: (OFString *)string +{ + self = [super init]; + + @try { + void *pool = objc_autoreleasePoolPush(); + const char *cString = string.UTF8String; + size_t length = string.UTF8StringLength; + + for (size_t i = 0; i < length; i++) { + if (OFASCIIIsAlnum(cString[i])) + continue; + + switch (cString[i]) { + case ' ': + case '\'': + case '(': + case ')': + case '+': + case ',': + case '-': + case '.': + case '/': + case ':': + case '=': + case '?': + continue; + default: + @throw [OFInvalidEncodingException exception]; + } + } + + _printableStringValue = [string copy]; + + objc_autoreleasePoolPop(pool); + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + void *pool = objc_autoreleasePoolPush(); + OFString *printableString; + + @try { + if (tagClass != OFASN1TagClassUniversal || + tagNumber != OFASN1TagNumberPrintableString || constructed) + @throw [OFInvalidArgumentException exception]; + + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + + printableString = [OFString + stringWithCString: DEREncodedContents.items + encoding: OFStringEncodingASCII + length: DEREncodedContents.count]; + } @catch (id e) { + [self release]; + @throw e; + } + + self = [self initWithString: printableString]; + + objc_autoreleasePoolPop(pool); + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_printableStringValue release]; + + [super dealloc]; +} + +- (OFString *)stringValue +{ + return self.printableStringValue; +} + +- (bool)isEqual: (id)object +{ + OFASN1PrintableString *printableString; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1PrintableString class]]) + return false; + + printableString = object; + + if (![printableString->_printableStringValue isEqual: + _printableStringValue]) + return false; + + return true; +} + +- (unsigned long)hash +{ + return _printableStringValue.hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: @"", + _printableStringValue]; +} +@end ADDED src/OFASN1Value.h Index: src/OFASN1Value.h ================================================================== --- src/OFASN1Value.h +++ src/OFASN1Value.h @@ -0,0 +1,135 @@ +/* + * 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 "OFObject.h" + +OF_ASSUME_NONNULL_BEGIN + +/** @file */ + +@class OFData; + +/** + * @brief ASN.1 tag class. + */ +typedef enum { + /** Universal */ + OFASN1TagClassUniversal = 0x0, + /** Application */ + OFASN1TagClassApplication = 0x1, + /** Context specific */ + OFASN1TagClassContextSpecific = 0x2, + /** Private */ + OFASN1TagClassPrivate = 0x3 +} OFASN1TagClass; + +/** + * @brief ASN.1 tag number. + */ +typedef enum { + /** Boolean */ + OFASN1TagNumberBoolean = 0x01, + /** Integer */ + OFASN1TagNumberInteger = 0x02, + /** Bit string */ + OFASN1TagNumberBitString = 0x03, + /** Octet string */ + OFASN1TagNumberOctetString = 0x04, + /** Null */ + OFASN1TagNumberNull = 0x05, + /** Object Identifier */ + OFASN1TagNumberObjectIdentifier = 0x06, + /** Enumerated */ + OFASN1TagNumberEnumerated = 0x0A, + /** UTF-8 string */ + OFASN1TagNumberUTF8String = 0x0C, + /** Sequence */ + OFASN1TagNumberSequence = 0x10, + /** Set */ + OFASN1TagNumberSet = 0x11, + /** NumericString */ + OFASN1TagNumberNumericString = 0x12, + /** PrintableString */ + OFASN1TagNumberPrintableString = 0x13, + /** IA5String */ + OFASN1TagNumberIA5String = 0x16 +} OFASN1TagNumber; + +/** + * @brief A class representing an ASN.1 value. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFASN1Value: OFObject +{ + OFASN1TagClass _tagClass; + OFASN1TagNumber _tagNumber; + bool _constructed; + OFData *_DEREncodedContents; +} + +/** + * @brief The tag class of the value's type. + */ +@property (readonly, nonatomic) OFASN1TagClass tagClass; + +/** + * @brief The tag number of the value's type. + */ +@property (readonly, nonatomic) OFASN1TagNumber tagNumber; + +/** + * @brief Whether the value if of a constructed type. + */ +@property (readonly, nonatomic, getter=isConstructed) bool constructed; + +/** + * @brief The DER-encoded contents octets of the value. + */ +@property (readonly, nonatomic) OFData *DEREncodedContents; + +/** + * @brief Creates a new ASN.1 value with the specified arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return A new ASN.1 value + */ ++ (instancetype)valueWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents; + +- (instancetype)init OF_UNAVAILABLE; + +/** + * @brief Initializes an already allocated ASN.1 value with the specified + * arguments. + * + * @param tagClass The tag class of the value's type + * @param tagNumber The tag number of the value's type + * @param constructed Whether the value if of a constructed type + * @param DEREncodedContents The DER-encoded contents octets of the value. + * @return An initialized ASN.1 value + */ +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents + OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFASN1Value.m Index: src/OFASN1Value.m ================================================================== --- src/OFASN1Value.m +++ src/OFASN1Value.m @@ -0,0 +1,128 @@ +/* + * 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 "OFASN1Value.h" +#import "OFData.h" +#import "OFString.h" + +#import "OFInvalidFormatException.h" + +@implementation OFASN1Value +@synthesize tagClass = _tagClass, tagNumber = _tagNumber; +@synthesize constructed = _constructed; +@synthesize DEREncodedContents = _DEREncodedContents; + ++ (instancetype)valueWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + return [[[self alloc] + initWithTagClass: tagClass + tagNumber: tagNumber + constructed: constructed + DEREncodedContents: DEREncodedContents] autorelease]; +} + +- (instancetype)initWithTagClass: (OFASN1TagClass)tagClass + tagNumber: (OFASN1TagNumber)tagNumber + constructed: (bool)constructed + DEREncodedContents: (OFData *)DEREncodedContents +{ + self = [super init]; + + @try { + if (DEREncodedContents.itemSize != 1) + @throw [OFInvalidFormatException exception]; + + _tagClass = tagClass; + _tagNumber = tagNumber; + _constructed = constructed; + _DEREncodedContents = [DEREncodedContents copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + [_DEREncodedContents release]; + + [super dealloc]; +} + +- (bool)isEqual: (id)object +{ + OFASN1Value *value; + + if (object == self) + return true; + + if (![object isKindOfClass: [OFASN1Value class]]) + return false; + + value = object; + + if (value->_tagClass != _tagClass) + return false; + if (value->_tagNumber != _tagNumber) + return false; + if (value->_constructed != _constructed) + return false; + if (![value->_DEREncodedContents isEqual: _DEREncodedContents]) + return false; + + return true; +} + +- (unsigned long)hash +{ + unsigned long hash; + + OFHashInit(&hash); + + OFHashAddByte(&hash, _tagClass & 0xFF); + OFHashAddByte(&hash, _tagNumber & 0xFF); + OFHashAddByte(&hash, _constructed); + OFHashAddHash(&hash, _DEREncodedContents.hash); + + OFHashFinalize(&hash); + + return hash; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"", + _tagClass, _tagNumber, _constructed, + _DEREncodedContents.description]; +} +@end ADDED src/OFData+ASN1DERParsing.h Index: src/OFData+ASN1DERParsing.h ================================================================== --- src/OFData+ASN1DERParsing.h +++ src/OFData+ASN1DERParsing.h @@ -0,0 +1,52 @@ +/* + * 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 "OFData.h" +#import "OFASN1Value.h" + +OF_ASSUME_NONNULL_BEGIN + +#ifdef __cplusplus +extern "C" { +#endif +extern int _OFData_ASN1DERParsing_reference; +#ifdef __cplusplus +} +#endif + +@interface OFData (ASN1DERParsing) +/** + * @brief The data interpreted as ASN.1 in DER representation and parsed as an + * object. + * + * This is either an OFArray (for a sequence), an OFSet (for a set) or an + * OFASN1Value. + */ +@property (readonly, nonatomic) id objectByParsingASN1DER; + +/** + * @brief Parses the ASN.1 DER representation and returns it as an object. + * + * This is either an OFArray (for a sequence), an OFSet (for a set) or an + * OFASN1Value. + * + * @param depthLimit The maximum depth the parser should accept (defaults to 32 + * if not specified, 0 means no limit (insecure!)) + * @return The ASN.1 DER representation as an object + */ +- (id)objectByParsingASN1DERWithDepthLimit: (size_t)depthLimit; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFData+ASN1DERParsing.m Index: src/OFData+ASN1DERParsing.m ================================================================== --- src/OFData+ASN1DERParsing.m +++ src/OFData+ASN1DERParsing.m @@ -0,0 +1,292 @@ +/* + * 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 "OFData+ASN1DERParsing.h" +#import "OFASN1BitString.h" +#import "OFASN1Enumerated.h" +#import "OFASN1IA5String.h" +#import "OFASN1NumericString.h" +#import "OFASN1ObjectIdentifier.h" +#import "OFASN1OctetString.h" +#import "OFASN1PrintableString.h" +#import "OFASN1Value.h" +#import "OFArray.h" +#import "OFNull.h" +#import "OFNumber.h" +#import "OFSet.h" +#import "OFString.h" + +#import "OFInvalidArgumentException.h" +#import "OFInvalidFormatException.h" +#import "OFOutOfRangeException.h" +#import "OFTruncatedDataException.h" + +enum { + tagConstructedMask = 0x20 +}; + +int _OFData_ASN1DERParsing_reference; + +static size_t parseObject(OFData *self, id *object, size_t depthLimit); + +long long +OFASN1DERIntegerParse(const unsigned char *buffer, size_t length) +{ + unsigned long long value = 0; + + /* TODO: Support for big numbers */ + if (length > sizeof(unsigned long long) && + (length != sizeof(unsigned long long) + 1 || buffer[0] != 0)) + @throw [OFOutOfRangeException exception]; + + if (length >= 2 && ((buffer[0] == 0 && !(buffer[1] & 0x80)) || + (buffer[0] == 0xFF && buffer[1] & 0x80))) + @throw [OFInvalidFormatException exception]; + + if (length >= 1 && buffer[0] & 0x80) + value = ~0ull; + + while (length--) + value = (value << 8) | *buffer++; + + return value; +} + +static OFArray * +parseSequence(OFData *contents, size_t depthLimit) +{ + OFMutableArray *ret = [OFMutableArray array]; + size_t count = contents.count; + + if (depthLimit == 0) + @throw [OFOutOfRangeException exception]; + + while (count > 0) { + id object; + size_t objectLength; + + objectLength = parseObject(contents, &object, depthLimit); + + count -= objectLength; + contents = [contents subdataWithRange: + OFMakeRange(objectLength, count)]; + + [ret addObject: object]; + } + + [ret makeImmutable]; + + return ret; +} + +static OFSet * +parseSet(OFData *contents, size_t depthLimit) +{ + OFMutableSet *ret = [OFMutableSet set]; + size_t count = contents.count; + OFData *previousObjectData = nil; + + if (depthLimit == 0) + @throw [OFOutOfRangeException exception]; + + while (count > 0) { + id object; + size_t objectLength; + OFData *objectData; + + objectLength = parseObject(contents, &object, depthLimit); + objectData = [contents subdataWithRange: + OFMakeRange(0, objectLength)]; + + if (previousObjectData != nil && + [objectData compare: previousObjectData] != + OFOrderedDescending) + @throw [OFInvalidFormatException exception]; + + count -= objectLength; + contents = [contents subdataWithRange: + OFMakeRange(objectLength, count)]; + + [ret addObject: object]; + + previousObjectData = objectData; + } + + [ret makeImmutable]; + + return ret; +} + +static size_t +parseObject(OFData *self, id *object, size_t depthLimit) +{ + const unsigned char *items = self.items; + size_t count = self.count; + unsigned char tag; + size_t contentsLength, bytesConsumed = 0; + Class valueClass; + OFData *contents; + + if (count < 2) + @throw [OFTruncatedDataException exception]; + + tag = *items++; + contentsLength = *items++; + bytesConsumed += 2; + + if (contentsLength > 127) { + uint_fast8_t lengthLength = contentsLength & 0x7F; + + if (lengthLength > sizeof(size_t)) + @throw [OFOutOfRangeException exception]; + + if (count - bytesConsumed < lengthLength) + @throw [OFTruncatedDataException exception]; + + if (lengthLength == 0 || + (lengthLength == 1 && items[0] < 0x80) || + (lengthLength >= 2 && items[0] == 0)) + @throw [OFInvalidFormatException exception]; + + contentsLength = 0; + + for (uint_fast8_t i = 0; i < lengthLength; i++) + contentsLength = (contentsLength << 8) | *items++; + + bytesConsumed += lengthLength; + + if (contentsLength <= 127) + @throw [OFInvalidFormatException exception]; + } + + if (count - bytesConsumed < contentsLength) + @throw [OFTruncatedDataException exception]; + + contents = [self subdataWithRange: + OFMakeRange(bytesConsumed, contentsLength)]; + bytesConsumed += contentsLength; + + switch (tag & ~tagConstructedMask) { + case OFASN1TagNumberBoolean:; + unsigned char boolValue; + + if (tag & tagConstructedMask) + @throw [OFInvalidFormatException exception]; + + if (contents.count != 1) + @throw [OFInvalidFormatException exception]; + + boolValue = *(unsigned char *)[contents itemAtIndex: 0]; + + if (boolValue != 0 && boolValue != 0xFF) + @throw [OFInvalidFormatException exception]; + + *object = [OFNumber numberWithBool: boolValue]; + return bytesConsumed; + case OFASN1TagNumberInteger: + if (tag & tagConstructedMask) + @throw [OFInvalidFormatException exception]; + + *object = [OFNumber numberWithLongLong: + OFASN1DERIntegerParse(contents.items, contents.count)]; + return bytesConsumed; + case OFASN1TagNumberBitString: + valueClass = [OFASN1BitString class]; + break; + case OFASN1TagNumberOctetString: + valueClass = [OFASN1OctetString class]; + break; + case OFASN1TagNumberNull: + if (tag & tagConstructedMask) + @throw [OFInvalidFormatException exception]; + + if (contents.count != 0) + @throw [OFInvalidFormatException exception]; + + *object = [OFNull null]; + return bytesConsumed; + case OFASN1TagNumberObjectIdentifier: + valueClass = [OFASN1ObjectIdentifier class]; + break; + case OFASN1TagNumberEnumerated: + valueClass = [OFASN1Enumerated class]; + break; + case OFASN1TagNumberUTF8String: + if (tag & tagConstructedMask) + @throw [OFInvalidFormatException exception]; + + *object = [OFString stringWithUTF8String: contents.items + length: contents.count]; + return bytesConsumed; + case OFASN1TagNumberSequence: + if (!(tag & tagConstructedMask)) + @throw [OFInvalidFormatException exception]; + + *object = parseSequence(contents, depthLimit - 1); + return bytesConsumed; + case OFASN1TagNumberSet: + if (!(tag & tagConstructedMask)) + @throw [OFInvalidFormatException exception]; + + *object = parseSet(contents, depthLimit - 1); + return bytesConsumed; + case OFASN1TagNumberNumericString: + valueClass = [OFASN1NumericString class]; + break; + case OFASN1TagNumberPrintableString: + valueClass = [OFASN1PrintableString class]; + break; + case OFASN1TagNumberIA5String: + valueClass = [OFASN1IA5String class]; + break; + default: + valueClass = [OFASN1Value class]; + break; + } + + *object = [[[valueClass alloc] + initWithTagClass: tag >> 6 + tagNumber: tag & 0x1F + constructed: tag & tagConstructedMask + DEREncodedContents: contents] autorelease]; + return bytesConsumed; +} + +@implementation OFData (ASN1DERParsing) +- (id)objectByParsingASN1DER +{ + return [self objectByParsingASN1DERWithDepthLimit: 32]; +} + +- (id)objectByParsingASN1DERWithDepthLimit: (size_t)depthLimit +{ + void *pool = objc_autoreleasePoolPush(); + id object; + + if (self.itemSize != 1) + @throw [OFInvalidArgumentException exception]; + + if (parseObject(self, &object, depthLimit) != self.count) + @throw [OFInvalidFormatException exception]; + + [object retain]; + + objc_autoreleasePoolPop(pool); + + return [object autorelease]; +} +@end Index: src/OFData.h ================================================================== --- src/OFData.h +++ src/OFData.h @@ -373,7 +373,8 @@ @end OF_ASSUME_NONNULL_END #import "OFMutableData.h" +#import "OFData+ASN1DERParsing.h" #import "OFData+CryptographicHashing.h" #import "OFData+MessagePackParsing.h" Index: src/OFData.m ================================================================== --- src/OFData.m +++ src/OFData.m @@ -51,10 +51,11 @@ /* References for static linking */ void _references_to_categories_of_OFData(void) { + _OFData_ASN1DERParsing_reference = 1; _OFData_CryptographicHashing_reference = 1; _OFData_MessagePackParsing_reference = 1; } @implementation OFPlaceholderData Index: src/OFNull.h ================================================================== --- src/OFNull.h +++ src/OFNull.h @@ -12,10 +12,11 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" +#import "OFASN1DERRepresentation.h" #import "OFJSONRepresentation.h" #import "OFMessagePackRepresentation.h" OF_ASSUME_NONNULL_BEGIN @@ -24,15 +25,15 @@ * * @brief A class for representing null values in collections. */ OF_SUBCLASSING_RESTRICTED @interface OFNull: OFObject + OFMessagePackRepresentation, OFASN1DERRepresentation> /** * @brief Returns an OFNull singleton. * * @return An OFNull singleton */ + (OFNull *)null; @end OF_ASSUME_NONNULL_END Index: src/OFNull.m ================================================================== --- src/OFNull.m +++ src/OFNull.m @@ -71,8 +71,14 @@ - (OFData *)messagePackRepresentation { uint8_t type = 0xC0; return [OFData dataWithItems: &type count: 1]; } + +- (OFData *)ASN1DERRepresentation +{ + const unsigned char bytes[] = { OFASN1TagNumberNull, 0 }; + return [OFData dataWithItems: bytes count: sizeof(bytes)]; +} OF_SINGLETON_METHODS @end Index: src/OFNumber.h ================================================================== --- src/OFNumber.h +++ src/OFNumber.h @@ -24,10 +24,11 @@ #ifdef OF_HAVE_SYS_TYPES_H # include #endif +#import "OFASN1DERRepresentation.h" #import "OFJSONRepresentation.h" #import "OFMessagePackRepresentation.h" #import "OFValue.h" OF_ASSUME_NONNULL_BEGIN @@ -37,12 +38,12 @@ /** * @class OFNumber OFNumber.h ObjFW/OFNumber.h * * @brief Provides a way to store a number in an object. */ -@interface OFNumber: OFValue +@interface OFNumber: OFValue /** * @brief The OFNumber as a `bool`. */ @property (readonly, nonatomic) bool boolValue; Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -23,10 +23,11 @@ #import "OFString.h" #import "OFTaggedPointerNumber.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" +#import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" @interface OFNumber () - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options @@ -708,10 +709,25 @@ return [OFString stringWithFormat: @"%llu", self.unsignedLongLongValue]; @throw [OFInvalidFormatException exception]; } + +- (OFData *)ASN1DERRepresentation +{ + if (self.objCType[0] == 'B' && self.objCType[1] == '\0') { + char buffer[] = { + OFASN1TagNumberBoolean, + 1, + (self.boolValue ? 0xFF : 0x00) + }; + + return [OFData dataWithItems: buffer count: sizeof(buffer)]; + } else + @throw [OFNotImplementedException exceptionWithSelector: _cmd + object: self]; +} - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -141,10 +141,19 @@ #import "OFMatrix4x4.h" #ifdef OF_WINDOWS # import "OFWindowsRegistryKey.h" #endif + +#import "OFASN1BitString.h" +#import "OFASN1Enumerated.h" +#import "OFASN1IA5String.h" +#import "OFASN1NumericString.h" +#import "OFASN1ObjectIdentifier.h" +#import "OFASN1OctetString.h" +#import "OFASN1PrintableString.h" +#import "OFASN1Value.h" #import "OFAllocFailedException.h" #import "OFAlreadyOpenException.h" #import "OFException.h" #import "OFChangeCurrentDirectoryFailedException.h" Index: src/test/OTAppDelegate.m ================================================================== --- src/test/OTAppDelegate.m +++ src/test/OTAppDelegate.m @@ -16,10 +16,11 @@ #include "config.h" #import "OTAppDelegate.h" #import "OFColor.h" +#import "OFMethodSignature.h" #import "OFSet.h" #import "OFStdIOStream.h" #import "OFValue.h" #import "OTTestCase.h" @@ -87,22 +88,40 @@ @try { tests = [OFMutableSet set]; for (Method *iter = methods; *iter != NULL; iter++) { SEL selector = method_getName(*iter); + void *pool; + OFMethodSignature *sig; if (selector == NULL) continue; - if (strncmp(sel_getName(selector), "test", 4) == 0) + if (strncmp(sel_getName(selector), "test", 4) != 0) + continue; + + pool = objc_autoreleasePoolPush(); + sig = [OFMethodSignature signatureWithObjCTypes: + method_getTypeEncoding(*iter)]; + + if (strcmp(sig.methodReturnType, "v") == 0 && + sig.numberOfArguments == 2 && + strcmp([sig argumentTypeAtIndex: 0], "@") == 0 && + strcmp([sig argumentTypeAtIndex: 1], ":") == 0) [tests addObject: [OFValue valueWithPointer: selector]]; + + objc_autoreleasePoolPop(pool); } } @finally { OFFreeMemory(methods); } + if (class_getSuperclass(class) != Nil) + [tests unionSet: + [self testsInClass: class_getSuperclass(class)]]; + [tests makeImmutable]; return tests; } - (void)printStatusForTest: (SEL)test Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -1,55 +1,31 @@ include ../extra.mk -SUBDIRS = ${TESTPLUGIN} \ - ${OBJC_SYNC} \ - ${SUBPROCESS} \ +SUBDIRS = ${OBJC_SYNC} \ terminal CLEAN = EBOOT.PBP \ boot.dol \ ${PROG_NOINST}.arm9 \ ${PROG_NOINST}.nds \ ${PROG_NOINST}.nro \ - ${PROG_NOINST}.rpx \ - testfile_bin.m \ - testfile_ini.m + ${PROG_NOINST}.rpx DISTCLEAN = Info.plist PROG_NOINST = tests${PROG_SUFFIX} STATIC_LIB_NOINST = ${TESTS_STATIC_LIB} SRCS = ForwardingTests.m \ - OFArrayTests.m \ + OFASN1DERParsingTests.m \ + OFASN1DERRepresentationTests.m \ ${OF_BLOCK_TESTS_M} \ - OFCharacterSetTests.m \ - OFColorTests.m \ OFDataTests.m \ - OFDateTests.m \ OFDictionaryTests.m \ - OFHMACTests.m \ - OFINIFileTests.m \ - OFIRITests.m \ - OFInvocationTests.m \ - OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ - OFMD5HashTests.m \ - OFMatrix4x4Tests.m \ OFMemoryStreamTests.m \ - OFMethodSignatureTests.m \ OFNotificationCenterTests.m \ - OFNumberTests.m \ OFObjectTests.m \ - OFPBKDF2Tests.m \ - OFPropertyListTests.m \ - OFRIPEMD160HashTests.m \ - OFSHA1HashTests.m \ - OFSHA224HashTests.m \ - OFSHA256HashTests.m \ - OFSHA384HashTests.m \ - OFSHA512HashTests.m \ - OFScryptTests.m \ OFSetTests.m \ OFStreamTests.m \ OFStringTests.m \ OFSystemInfoTests.m \ OFValueTests.m \ @@ -58,24 +34,17 @@ OFXMLParserTests.m \ RuntimeTests.m \ ${RUNTIME_ARC_TESTS_M} \ TestsAppDelegate.m \ ${USE_SRCS_FILES} \ - ${USE_SRCS_PLUGINS} \ ${USE_SRCS_SOCKETS} \ - ${USE_SRCS_SUBPROCESS} \ - ${USE_SRCS_THREADS} \ - ${USE_SRCS_WINDOWS} \ - testfile_bin.m \ - testfile_ini.m -SRCS_PLUGINS = OFPluginTests.m + ${USE_SRCS_WINDOWS} SRCS_SOCKETS = OFDNSResolverTests.m \ ${OF_HTTP_CLIENT_TESTS_M} \ OFHTTPCookieTests.m \ OFHTTPCookieManagerTests.m \ OFKernelEventObserverTests.m \ - OFSocketTests.m \ OFTCPSocketTests.m \ OFUDPSocketTests.m \ ${USE_SRCS_APPLETALK} \ ${USE_SRCS_IPX} \ ${USE_SRCS_UNIX_SOCKETS} @@ -83,24 +52,17 @@ SRCS_IPX = OFIPXSocketTests.m \ OFSPXSocketTests.m \ OFSPXStreamSocketTests.m SRCS_UNIX_SOCKETS = OFUNIXDatagramSocketTests.m \ OFUNIXStreamSocketTests.m -SRCS_SUBPROCESS = OFSubprocessTests.m -SRCS_THREADS = OFThreadTests.m SRCS_WINDOWS = OFWindowsRegistryKeyTests.m 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: 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 @@ -154,28 +116,10 @@ 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-ios: all - if [ -z "${IOS_HOST}" ]; then \ - echo "Please set IOS_HOST to the hostname of your iOS host!"; \ - exit 1; \ - fi - echo "Uploading files to iOS device ${IOS_HOST} at ${IOS_TMP}..." - ssh ${IOS_USER}@${IOS_HOST} \ - 'rm -fr ${IOS_TMP} && mkdir -p ${IOS_TMP}/plugin' - destname=libobjfw.${OBJFW_LIB_MAJOR}.dylib; \ - scp -q ../src/libobjfw.dylib \ - ${IOS_USER}@${IOS_HOST}:${IOS_TMP}/$$destname - scp -q tests testfile.txt ${IOS_USER}@${IOS_HOST}:${IOS_TMP}/ - scp -q plugin/TestPlugin.bundle \ - ${IOS_USER}@${IOS_HOST}:${IOS_TMP}/plugin/ - echo "Running tests binary on iOS device ${IOS_HOST}..." - ssh ${IOS_USER}@${IOS_HOST} \ - 'cd ${IOS_TMP} && DYLD_LIBRARY_PATH=. ${WRAPPER} ./tests' - 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}; \ @@ -184,14 +128,10 @@ 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} @@ -231,11 +171,10 @@ CPPFLAGS += -I../src \ -I../src/exceptions \ -I../src/runtime \ -I.. \ - -DSTDOUT \ - -DPROG_SUFFIX=\"${PROG_SUFFIX}\" + -DSTDOUT OBJCFLAGS_RuntimeARCTests.m = -fobjc-arc -fobjc-arc-exceptions LIBS := ${TESTS_LIBS} ${LIBS} LDFLAGS += ${MAP_LDFLAGS} LD = ${OBJC} ADDED tests/OFASN1DERParsingTests.m Index: tests/OFASN1DERParsingTests.m ================================================================== --- tests/OFASN1DERParsingTests.m +++ tests/OFASN1DERParsingTests.m @@ -0,0 +1,479 @@ +/* + * 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 "TestsAppDelegate.h" + +static OFString *module = @"OFData+ASN1DERParsing"; + +@implementation TestsAppDelegate (OFASN1DERParsingTests) +- (void)ASN1DERParsingTests +{ + void *pool = objc_autoreleasePoolPush(); + OFASN1BitString *bitString; + OFArray *array; + OFSet *set; + OFEnumerator *enumerator; + + /* Boolean */ + TEST(@"Parsing of boolean", + ![[[OFData dataWithItems: "\x01\x01\x00" + count: 3] objectByParsingASN1DER] boolValue] && + [[[OFData dataWithItems: "\x01\x01\xFF" + count: 3] objectByParsingASN1DER] boolValue]) + + EXPECT_EXCEPTION(@"Detection of invalid boolean #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x01\x01\x01" + count: 3] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid boolean #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x01\x02\x00\x00" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid boolean #3", + OFInvalidFormatException, + [[OFData dataWithItems: "\x01\x00" + count: 2] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated boolean", + OFTruncatedDataException, + [[OFData dataWithItems: "\x01\x01" + count: 2] objectByParsingASN1DER]) + + /* Integer */ + TEST(@"Parsing of integer", + [[[OFData dataWithItems: "\x02\x00" + count: 2] objectByParsingASN1DER] + longLongValue] == 0 && + [[[OFData dataWithItems: "\x02\x01\x01" + count: 3] objectByParsingASN1DER] + longLongValue] == 1 && + [[[OFData dataWithItems: "\x02\x02\x01\x04" + count: 4] objectByParsingASN1DER] + longLongValue] == 260 && + [[[OFData dataWithItems: "\x02\x01\xFF" + count: 3] objectByParsingASN1DER] + longLongValue] == -1 && + [[[OFData dataWithItems: "\x02\x03\xFF\x00\x00" + count: 5] objectByParsingASN1DER] + longLongValue] == -65536 && + (unsigned long long)[[[OFData dataWithItems: "\x02\x09\x00\xFF\xFF" + "\xFF\xFF\xFF\xFF\xFF" + "\xFF" + count: 11] + objectByParsingASN1DER] longLongValue] == ULLONG_MAX) + + EXPECT_EXCEPTION(@"Detection of invalid integer #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x02\x02\x00\x00" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid integer #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x02\x02\x00\x7F" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid integer #3", + OFInvalidFormatException, + [[OFData dataWithItems: "\x02\x02\xFF\x80" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range integer", + OFOutOfRangeException, + [[OFData dataWithItems: "\x02\x09\x01" + "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated integer", + OFTruncatedDataException, + [[OFData dataWithItems: "\x02\x02\x00" + count: 3] objectByParsingASN1DER]) + + /* Bit string */ + TEST(@"Parsing of bit string", + (bitString = [[OFData dataWithItems: "\x03\x01\x00" + count: 3] objectByParsingASN1DER]) && + [bitString.bitStringValue isEqual: [OFData dataWithItems: "" + count: 0]] && + bitString.bitStringLength == 0 && + (bitString = [[OFData dataWithItems: "\x03\x0D\x01Hello World\x80" + count: 15] objectByParsingASN1DER]) && + [bitString.bitStringValue + isEqual: [OFData dataWithItems: "Hello World\x80" + count: 12]] && + bitString.bitStringLength == 95 && + (bitString = [[OFData dataWithItems: "\x03\x81\x80\x00xxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxx" + count: 131] objectByParsingASN1DER]) && + [bitString.bitStringValue + isEqual: [OFData dataWithItems: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxx" + count: 127]] && + bitString.bitStringLength == 127 * 8) + + EXPECT_EXCEPTION(@"Detection of invalid bit string #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x03\x00" + count: 2] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid bit string #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x03\x01\x01" + count: 3] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range bit string", + OFOutOfRangeException, + [[OFData dataWithItems: "\x03\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated bit string", + OFTruncatedDataException, + [[OFData dataWithItems: "\x03\x01" + count: 2] objectByParsingASN1DER]) + + /* Octet string */ + TEST(@"Parsing of octet string", + [[[[OFData dataWithItems: "\x04\x0CHello World!" + count: 14] objectByParsingASN1DER] + octetStringValue] isEqual: [OFData dataWithItems: "Hello World!" + count: 12]] && + [[[[OFData dataWithItems: "\x04\x81\x80xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxx" + count: 131] objectByParsingASN1DER] + octetStringValue] isEqual: + [OFData dataWithItems: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + count: 128]]) + + EXPECT_EXCEPTION(@"Detection of out of range octet string", + OFOutOfRangeException, + [[OFData dataWithItems: "\x04\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated octet string", + OFTruncatedDataException, + [[OFData dataWithItems: "\x04\x01" + count: 2] objectByParsingASN1DER]) + + /* Null */ + TEST(@"Parsing of null", + [[[OFData dataWithItems: "\x05\x00" + count: 2] objectByParsingASN1DER] + isEqual: [OFNull null]]) + + EXPECT_EXCEPTION(@"Detection of invalid null", + OFInvalidFormatException, + [[OFData dataWithItems: "\x05\x01\x00" + count: 3] objectByParsingASN1DER]) + + /* Object Identifier */ + TEST(@"Parsing of Object Identifier", + (array = [[[OFData dataWithItems: "\x06\x01\x27" + count: 3] objectByParsingASN1DER] + subidentifiers]) && array.count == 2 && + [[array objectAtIndex: 0] unsignedLongLongValue] == 0 && + [[array objectAtIndex: 1] unsignedLongLongValue] == 39 && + (array = [[[OFData dataWithItems: "\x06\x01\x4F" + count: 3] objectByParsingASN1DER] + subidentifiers]) && array.count == 2 && + [[array objectAtIndex: 0] unsignedLongLongValue] == 1 && + [[array objectAtIndex: 1] unsignedLongLongValue] == 39 && + (array = [[[OFData dataWithItems: "\x06\x02\x88\x37" + count: 4] objectByParsingASN1DER] + subidentifiers]) && array.count == 2 && + [[array objectAtIndex: 0] unsignedLongLongValue] == 2 && + [[array objectAtIndex: 1] unsignedLongLongValue] == 999 && + (array = [[[OFData dataWithItems: "\x06\x09\x2A\x86\x48\x86\xF7\x0D" + "\x01\x01\x0B" + count: 11] objectByParsingASN1DER] + subidentifiers]) && array.count == 7 && + [[array objectAtIndex: 0] unsignedLongLongValue] == 1 && + [[array objectAtIndex: 1] unsignedLongLongValue] == 2 && + [[array objectAtIndex: 2] unsignedLongLongValue] == 840 && + [[array objectAtIndex: 3] unsignedLongLongValue] == 113549 && + [[array objectAtIndex: 4] unsignedLongLongValue] == 1 && + [[array objectAtIndex: 5] unsignedLongLongValue] == 1 && + [[array objectAtIndex: 6] unsignedLongLongValue] == 11) + + EXPECT_EXCEPTION(@"Detection of invalid Object Identifier #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x06\x01\x81" + count: 3] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid Object Identifier #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x06\x02\x80\x01" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range Object Identifier", + OFOutOfRangeException, + [[OFData dataWithItems: "\x06\x0A\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + "\xFF\x7F" + count: 12] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated Object Identifier", + OFTruncatedDataException, + [[OFData dataWithItems: "\x06\x02\x00" + count: 3] objectByParsingASN1DER]) + + /* Enumerated */ + TEST(@"Parsing of enumerated", + [[[OFData dataWithItems: "\x0A\x00" + count: 2] objectByParsingASN1DER] longLongValue] == + 0 && + [[[OFData dataWithItems: "\x0A\x01\x01" + count: 3] objectByParsingASN1DER] longLongValue] == + 1 && + [[[OFData dataWithItems: "\x0A\x02\x01\x04" + count: 4] objectByParsingASN1DER] longLongValue] == + 260 && + [[[OFData dataWithItems: "\x0A\x01\xFF" + count: 3] objectByParsingASN1DER] longLongValue] == + -1 && + [[[OFData dataWithItems: "\x0A\x03\xFF\x00\x00" + count: 5] objectByParsingASN1DER] longLongValue] == + -65536 && + (unsigned long long)[[[OFData dataWithItems: "\x0A\x09\x00\xFF\xFF" + "\xFF\xFF\xFF\xFF\xFF" + "\xFF" + count: 11] + objectByParsingASN1DER] longLongValue] == ULLONG_MAX) + + EXPECT_EXCEPTION(@"Detection of invalid enumerated #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x0A\x02\x00\x00" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid enumerated #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x0A\x02\x00\x7F" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid enumerated #3", + OFInvalidFormatException, + [[OFData dataWithItems: "\x0A\x02\xFF\x80" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range enumerated", + OFOutOfRangeException, + [[OFData dataWithItems: "\x0A\x09\x01" + "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated enumerated", + OFTruncatedDataException, + [[OFData dataWithItems: "\x0A\x02\x00" + count: 3] objectByParsingASN1DER]) + + /* UTF-8 string */ + TEST(@"Parsing of UTF-8 string", + [[[OFData dataWithItems: "\x0C\x0EHällo Wörld!" + count: 16] objectByParsingASN1DER] + isEqual: @"Hällo Wörld!"] && + [[[OFData dataWithItems: "\x0C\x81\x80xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxx" + count: 131] objectByParsingASN1DER] + isEqual: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxx"]) + + EXPECT_EXCEPTION(@"Detection of out of range UTF-8 string", + OFOutOfRangeException, + [[OFData dataWithItems: "\x0C\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated UTF-8 string", + OFTruncatedDataException, + [[OFData dataWithItems: "\x0C\x01" + count: 2] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated length", + OFTruncatedDataException, + [[OFData dataWithItems: "\x0C\x83\x01\x01" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid / inefficient length #1", + OFInvalidFormatException, + [[OFData dataWithItems: "\x0C\x81\x7F" + count: 3] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of invalid / inefficient length #2", + OFInvalidFormatException, + [[OFData dataWithItems: "\x0C\x82\x00\x80xxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxx" + count: 132] objectByParsingASN1DER]) + + /* Sequence */ + TEST(@"Parsing of sequence", + (array = [[OFData dataWithItems: "\x30\x00" + count: 2] objectByParsingASN1DER]) && + [array isKindOfClass: [OFArray class]] && array.count == 0 && + (array = [[OFData dataWithItems: "\x30\x09\x02\x01\x7B\x0C\x04Test" + count: 11] objectByParsingASN1DER]) && + [array isKindOfClass: [OFArray class]] && array.count == 2 && + [[array objectAtIndex: 0] longLongValue] == 123 && + [[array objectAtIndex: 1] isEqual: @"Test"]) + + EXPECT_EXCEPTION(@"Detection of truncated sequence #1", + OFTruncatedDataException, + [[OFData dataWithItems: "\x30\x01" + count: 2] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated sequence #2", + OFTruncatedDataException, + [[OFData dataWithItems: "\x30\x04\x02\x01\x01\x00\x00" + count: 7] objectByParsingASN1DER]) + + /* Set */ + TEST(@"Parsing of set", + (set = [[OFData dataWithItems: "\x31\x00" + count: 2] objectByParsingASN1DER]) && + [set isKindOfClass: [OFSet class]] && set.count == 0 && + (set = [[OFData dataWithItems: "\x31\x09\x02\x01\x7B\x0C\x04Test" + count: 11] objectByParsingASN1DER]) && + [set isKindOfClass: [OFSet class]] && set.count == 2 && + (enumerator = [set objectEnumerator]) && + [[enumerator nextObject] longLongValue] == 123 && + [[enumerator nextObject] isEqual: @"Test"]) + + EXPECT_EXCEPTION(@"Detection of invalid set", + OFInvalidFormatException, + [[OFData dataWithItems: "\x31\x06\x02\x01\x02\x02\x01\x01" + count: 8] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated set #1", + OFTruncatedDataException, + [[OFData dataWithItems: "\x31\x01" + count: 2] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated set #2", + OFTruncatedDataException, + [[OFData dataWithItems: "\x31\x04\x02\x01\x01\x00\x00" + count: 7] objectByParsingASN1DER]) + + /* NumericString */ + TEST(@"Parsing of NumericString", + [[[[OFData dataWithItems: "\x12\x0B" "12345 67890" + count: 13] objectByParsingASN1DER] + numericStringValue] isEqual: @"12345 67890"] && + [[[[OFData dataWithItems: "\x12\x81\x80" "0000000000000000000000000" + "0000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000" + "00000000000000000000000" + count: 131] objectByParsingASN1DER] + numericStringValue] isEqual: @"000000000000000000000000000000000000" + @"000000000000000000000000000000000000" + @"000000000000000000000000000000000000" + @"00000000000000000000"]) + + EXPECT_EXCEPTION(@"Detection of invalid NumericString", + OFInvalidEncodingException, + [[OFData dataWithItems: "\x12\x02." + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range NumericString", + OFOutOfRangeException, + [[OFData dataWithItems: "\x12\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated NumericString", + OFTruncatedDataException, + [[OFData dataWithItems: "\x12\x01" + count: 2] objectByParsingASN1DER]) + + /* PrintableString */ + TEST(@"Parsing of PrintableString", + [[[[OFData dataWithItems: "\x13\x0CHello World." + count: 14] objectByParsingASN1DER] + printableStringValue] isEqual: @"Hello World."] && + [[[[OFData dataWithItems: "\x13\x81\x80 '()+,-./:=?abcdefghijklmnop" + "qrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '()" + "+,-./:=?abcdefghijklmnopqrstuvwxyzABCDEF" + "GHIJKLMNOPQRSTUVWXYZ" + count: 131] objectByParsingASN1DER] + printableStringValue] isEqual: @" '()+,-./:=?abcdefghijklmnopqrstuv" + @"wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '()" + @"+,-./:=?abcdefghijklmnopqrstuvwxyz" + @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"]) + + EXPECT_EXCEPTION(@"Detection of invalid PrintableString", + OFInvalidEncodingException, + [[OFData dataWithItems: "\x13\x02;" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range PrintableString", + OFOutOfRangeException, + [[OFData dataWithItems: "\x13\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated PrintableString", + OFTruncatedDataException, + [[OFData dataWithItems: "\x13\x01" + count: 2] objectByParsingASN1DER]) + + /* IA5String */ + TEST(@"Parsing of IA5String", + [[[[OFData dataWithItems: "\x16\x0CHello World!" + count: 14] objectByParsingASN1DER] + IA5StringValue] isEqual: @"Hello World!"] && + [[[[OFData dataWithItems: "\x16\x81\x80xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxx" + count: 131] objectByParsingASN1DER] + IA5StringValue] isEqual: @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + @"xxxxxxxx"]) + + EXPECT_EXCEPTION(@"Detection of invalid IA5String", + OFInvalidEncodingException, + [[OFData dataWithItems: "\x16\x02ä" + count: 4] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of out of range IA5String", + OFOutOfRangeException, + [[OFData dataWithItems: "\x16\x89" + "\x01\x01\x01\x01\x01\x01\x01\x01\x01" + count: 11] objectByParsingASN1DER]) + + EXPECT_EXCEPTION(@"Detection of truncated IA5String", + OFTruncatedDataException, + [[OFData dataWithItems: "\x16\x01" + count: 2] objectByParsingASN1DER]) + + objc_autoreleasePoolPop(pool); +} +@end ADDED tests/OFASN1DERRepresentationTests.m Index: tests/OFASN1DERRepresentationTests.m ================================================================== --- tests/OFASN1DERRepresentationTests.m +++ tests/OFASN1DERRepresentationTests.m @@ -0,0 +1,57 @@ +/* + * 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 "TestsAppDelegate.h" + +static OFString *module; + +@implementation TestsAppDelegate (OFASN1DERRepresentationTests) +- (void)ASN1DERRepresentationTests +{ + void *pool = objc_autoreleasePoolPush(); + OFData *data; + + module = @"OFASN1BitString"; + TEST(@"-[ASN1DERRepresentation]", + (data = [OFData dataWithItems: "\xFF\x00\xF8" count: 3]) && + [[[OFASN1BitString bitStringWithBitString: data length: 21] + ASN1DERRepresentation] isEqual: + [OFData dataWithItems: "\x03\x04\x03\xFF\x00\xF8" count: 6]] && + (data = [OFData dataWithItems: "abcdefäöü" count: 12]) && + [[[OFASN1BitString bitStringWithBitString: data length: 12 * 8] + ASN1DERRepresentation] isEqual: + [OFData dataWithItems: "\x03\x0D\x00" "abcdefäöü" count: 15]] && + (data = [OFData dataWithItems: "" count: 0]) && + [[[OFASN1BitString bitStringWithBitString: data length: 0] + ASN1DERRepresentation] isEqual: + [OFData dataWithItems: "\x03\x01\x00" count: 3]]) + + module = @"OFNumber"; + TEST(@"-[ASN1DERRepresentation] for bool", + [[[OFNumber numberWithBool: false] ASN1DERRepresentation] + isEqual: [OFData dataWithItems: "\x01\x01\x00" count: 3]] && + [[[OFNumber numberWithBool: true] ASN1DERRepresentation] + isEqual: [OFData dataWithItems: "\x01\x01\xFF" count: 3]]) + + module = @"OFNull"; + TEST(@"-[OFASN1DERRepresentation]", + [[[OFNull null] ASN1DERRepresentation] isEqual: + [OFData dataWithItems: "\x05\x00" count: 2]]) + + objc_autoreleasePoolPop(pool); +} +@end DELETED tests/OFArrayTests.m Index: tests/OFArrayTests.m ================================================================== --- tests/OFArrayTests.m +++ tests/OFArrayTests.m @@ -1,458 +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" - -#import "TestsAppDelegate.h" - -static OFString *module; -static OFString *const cArray[] = { - @"Foo", - @"Bar", - @"Baz" -}; - -@interface SimpleArray: OFArray -{ - OFMutableArray *_array; -} -@end - -@interface SimpleMutableArray: OFMutableArray -{ - OFMutableArray *_array; -} -@end - -@implementation SimpleArray -- (instancetype)init -{ - self = [super init]; - - @try { - _array = [[OFMutableArray alloc] init]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (instancetype)initWithObject: (id)object arguments: (va_list)arguments -{ - self = [super init]; - - @try { - _array = [[OFMutableArray alloc] initWithObject: object - arguments: arguments]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (instancetype)initWithObjects: (id const *)objects count: (size_t)count -{ - self = [super init]; - - @try { - _array = [[OFMutableArray alloc] initWithObjects: objects - count: count]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (void)dealloc -{ - [_array release]; - - [super dealloc]; -} - -- (id)objectAtIndex: (size_t)idx -{ - return [_array objectAtIndex: idx]; -} - -- (size_t)count -{ - return [_array count]; -} -@end - -@implementation SimpleMutableArray -+ (void)initialize -{ - if (self == [SimpleMutableArray class]) - [self inheritMethodsFromClass: [SimpleArray class]]; -} - -- (void)insertObject: (id)object atIndex: (size_t)idx -{ - [_array insertObject: object atIndex: idx]; -} - -- (void)replaceObjectAtIndex: (size_t)idx withObject: (id)object -{ - [_array replaceObjectAtIndex: idx withObject: object]; -} - -- (void)removeObjectAtIndex: (size_t)idx -{ - [_array removeObjectAtIndex: idx]; -} -@end - -@implementation TestsAppDelegate (OFArrayTests) -- (void)arrayTestsWithClass: (Class)arrayClass - mutableClass: (Class)mutableArrayClass -{ - void *pool = objc_autoreleasePoolPush(); - OFArray *array1, *array2; - OFMutableArray *mutableArray1, *mutableArray2; - OFEnumerator *enumerator; - id object; - bool ok; - size_t i; - - TEST(@"+[array]", (mutableArray1 = [mutableArrayClass array])) - - TEST(@"+[arrayWithObjects:]", - (array1 = - [arrayClass arrayWithObjects: @"Foo", @"Bar", @"Baz", nil])) - - TEST(@"+[arrayWithObjects:count:]", - (array2 = [arrayClass arrayWithObjects: cArray count: 3]) && - [array2 isEqual: array1]) - - TEST(@"-[description]", - [array1.description isEqual: @"(\n\tFoo,\n\tBar,\n\tBaz\n)"]) - - TEST(@"-[addObject:]", - R([mutableArray1 addObject: cArray[0]]) && - R([mutableArray1 addObject: cArray[2]])) - - TEST(@"-[insertObject:atIndex:]", - R([mutableArray1 insertObject: cArray[1] atIndex: 1])) - - TEST(@"-[count]", - mutableArray1.count == 3 && array1.count == 3 && array2.count == 3) - - TEST(@"-[isEqual:]", - [mutableArray1 isEqual: array1] && [array1 isEqual: array2]) - - TEST(@"-[objectAtIndex:]", - [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 1] isEqual: cArray[1]] && - [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]] && - [[array1 objectAtIndex: 0] isEqual: cArray[0]] && - [[array1 objectAtIndex: 1] isEqual: cArray[1]] && - [[array1 objectAtIndex: 2] isEqual: cArray[2]] && - [[array2 objectAtIndex: 0] isEqual: cArray[0]] && - [[array2 objectAtIndex: 1] isEqual: cArray[1]] && - [[array2 objectAtIndex: 2] isEqual: cArray[2]]) - - TEST(@"-[containsObject:]", - [array1 containsObject: cArray[1]] && - ![array1 containsObject: @"nonexistent"]) - - TEST(@"-[containsObjectIdenticalTo:]", - [array1 containsObjectIdenticalTo: cArray[1]] && - ![array1 containsObjectIdenticalTo: - [OFString stringWithString: cArray[1]]]) - - TEST(@"-[indexOfObject:]", [array1 indexOfObject: cArray[1]] == 1) - - TEST(@"-[indexOfObjectIdenticalTo:]", - [array2 indexOfObjectIdenticalTo: cArray[1]] == 1) - - TEST(@"-[objectsInRange:]", - [[array1 objectsInRange: OFMakeRange(1, 2)] isEqual: - [arrayClass arrayWithObjects: cArray[1], cArray[2], nil]]) - - TEST(@"-[replaceObject:withObject:]", - R([mutableArray1 replaceObject: cArray[1] withObject: cArray[0]]) && - [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) - - TEST(@"-[replaceObject:identicalTo:]", - R([mutableArray1 replaceObjectIdenticalTo: cArray[0] - withObject: cArray[1]]) && - [[mutableArray1 objectAtIndex: 0] isEqual: cArray[1]] && - [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) - - TEST(@"-[replaceObjectAtIndex:withObject:]", - R([mutableArray1 replaceObjectAtIndex: 0 withObject: cArray[0]]) && - [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && - [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) - - TEST(@"-[removeObject:]", - R([mutableArray1 removeObject: cArray[0]]) && - mutableArray1.count == 1) - - [mutableArray1 addObject: cArray[0]]; - - TEST(@"-[removeObjectIdenticalTo:]", - R([mutableArray1 removeObjectIdenticalTo: cArray[2]]) && - mutableArray1.count == 1) - - mutableArray2 = [[array1 mutableCopy] autorelease]; - TEST(@"-[removeObjectAtIndex:]", - R([mutableArray2 removeObjectAtIndex: 1]) && - mutableArray2.count == 2 && - [[mutableArray2 objectAtIndex: 1] isEqual: cArray[2]]) - - mutableArray2 = [[array1 mutableCopy] autorelease]; - TEST(@"-[removeObjectsInRange:]", - R([mutableArray2 removeObjectsInRange: OFMakeRange(0, 2)]) && - mutableArray2.count == 1 && - [[mutableArray2 objectAtIndex: 0] isEqual: cArray[2]]) - - mutableArray2 = [[array1 mutableCopy] autorelease]; - [mutableArray2 addObject: @"qux"]; - [mutableArray2 addObject: @"last"]; - TEST(@"-[reverse]", - R([mutableArray2 reverse]) && - [mutableArray2 isEqual: [arrayClass arrayWithObjects: - @"last", @"qux", @"Baz", @"Bar", @"Foo", nil]]) - - mutableArray2 = [[array1 mutableCopy] autorelease]; - [mutableArray2 addObject: @"qux"]; - [mutableArray2 addObject: @"last"]; - TEST(@"-[reversedArray]", - [[mutableArray2 reversedArray] isEqual: - [arrayClass arrayWithObjects: - @"last", @"qux", @"Baz", @"Bar", @"Foo", nil]]) - - mutableArray2 = [[array1 mutableCopy] autorelease]; - [mutableArray2 addObject: @"0"]; - [mutableArray2 addObject: @"z"]; - TEST(@"-[sortedArray]", - [[mutableArray2 sortedArray] isEqual: [arrayClass arrayWithObjects: - @"0", @"Bar", @"Baz", @"Foo", @"z", nil]] && - [[mutableArray2 sortedArrayUsingSelector: @selector(compare:) - options: OFArraySortDescending] - isEqual: [arrayClass arrayWithObjects: - @"z", @"Foo", @"Baz", @"Bar", @"0", nil]]) - - EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", - OFOutOfRangeException, [array1 objectAtIndex: array1.count]) - - EXPECT_EXCEPTION(@"Detect out of range in -[removeObjectsInRange:]", - OFOutOfRangeException, [mutableArray1 removeObjectsInRange: - OFMakeRange(0, mutableArray1.count + 1)]) - - TEST(@"-[componentsJoinedByString:]", - (array2 = [arrayClass arrayWithObjects: @"", @"a", @"b", @"c", - nil]) && - [[array2 componentsJoinedByString: @" "] isEqual: @" a b c"] && - (array2 = [arrayClass arrayWithObject: @"foo"]) && - [[array2 componentsJoinedByString: @" "] isEqual: @"foo"]) - - TEST(@"-[componentsJoinedByString:options]", - (array2 = [arrayClass arrayWithObjects: @"", @"foo", @"", @"", - @"bar", @"", nil]) && - [[array2 componentsJoinedByString: @" " - options: OFArraySkipEmptyComponents] - isEqual: @"foo bar"]) - - mutableArray1 = [[array1 mutableCopy] autorelease]; - ok = true; - i = 0; - - TEST(@"-[objectEnumerator]", - (enumerator = [mutableArray1 objectEnumerator])) - - while ((object = [enumerator nextObject]) != nil) { - if (![object isEqual: cArray[i]]) - ok = false; - [mutableArray1 replaceObjectAtIndex: i withObject: @""]; - i++; - } - - if (mutableArray1.count != i) - ok = false; - - TEST(@"OFEnumerator's -[nextObject]", ok) - - [mutableArray1 removeObjectAtIndex: 0]; - - EXPECT_EXCEPTION(@"Detection of mutation during enumeration", - OFEnumerationMutationException, [enumerator nextObject]) - - mutableArray1 = [[array1 mutableCopy] autorelease]; - ok = true; - i = 0; - - for (OFString *string in mutableArray1) { - if (![string isEqual: cArray[i]]) - ok = false; - [mutableArray1 replaceObjectAtIndex: i withObject: @""]; - i++; - } - - if (mutableArray1.count != i) - ok = false; - - TEST(@"Fast Enumeration", ok) - - [mutableArray1 replaceObjectAtIndex: 0 withObject: cArray[0]]; - [mutableArray1 replaceObjectAtIndex: 1 withObject: cArray[1]]; - [mutableArray1 replaceObjectAtIndex: 2 withObject: cArray[2]]; - - ok = false; - i = 0; - @try { - for (OFString *string in mutableArray1) { - (void)string; - - if (i == 0) - [mutableArray1 addObject: @""]; - - i++; - } - } @catch (OFEnumerationMutationException *e) { - ok = true; - } - - TEST(@"Detection of mutation during Fast Enumeration", ok) - - [mutableArray1 removeLastObject]; - -#ifdef OF_HAVE_BLOCKS - { - __block bool blockOK = true; - __block size_t count = 0; - OFArray *compareArray = array1; - OFMutableArray *mutableArray3; - - mutableArray1 = [[array1 mutableCopy] autorelease]; - [mutableArray1 enumerateObjectsUsingBlock: - ^ (id object_, size_t idx, bool *stop) { - count++; - if (![object_ isEqual: - [compareArray objectAtIndex: idx]]) - blockOK = false; - }]; - - if (count != compareArray.count) - blockOK = false; - - TEST(@"Enumeration using blocks", blockOK) - - blockOK = false; - mutableArray3 = mutableArray1; - @try { - [mutableArray3 enumerateObjectsUsingBlock: - ^ (id object_, size_t idx, bool *stop) { - [mutableArray3 removeObjectAtIndex: idx]; - }]; - } @catch (OFEnumerationMutationException *e) { - blockOK = true; - } @catch (OFOutOfRangeException *e) { - /* - * Out of bounds access due to enumeration not being - * detected. - */ - } - - TEST(@"Detection of mutation during enumeration using blocks", - blockOK) - } - - TEST(@"-[replaceObjectsUsingBlock:]", - R([mutableArray1 replaceObjectsUsingBlock: - ^ id (id object_, size_t idx) { - switch (idx) { - case 0: - return @"foo"; - case 1: - return @"bar"; - } - - return nil; - }]) && [mutableArray1.description isEqual: @"(\n\tfoo,\n\tbar\n)"]) - - TEST(@"-[mappedArrayUsingBlock:]", - [[mutableArray1 mappedArrayUsingBlock: - ^ id (id object_, size_t idx) { - switch (idx) { - case 0: - return @"foobar"; - case 1: - return @"qux"; - } - - return nil; - }].description isEqual: @"(\n\tfoobar,\n\tqux\n)"]) - - TEST(@"-[filteredArrayUsingBlock:]", - [[mutableArray1 filteredArrayUsingBlock: - ^ bool (id object_, size_t idx) { - return [object_ isEqual: @"foo"]; - }].description isEqual: @"(\n\tfoo\n)"]) - - TEST(@"-[foldUsingBlock:]", - [[arrayClass arrayWithObjects: [OFMutableString string], @"foo", - @"bar", @"baz", nil] foldUsingBlock: ^ id (id left, id right) { - [left appendString: right]; - return left; - }]) -#endif - - TEST(@"-[valueForKey:]", - [[[arrayClass arrayWithObjects: @"foo", @"bar", @"quxqux", nil] - valueForKey: @"length"] isEqual: - [arrayClass arrayWithObjects: [OFNumber numberWithInt: 3], - [OFNumber numberWithInt: 3], [OFNumber numberWithInt: 6], nil]] && - [[[arrayClass arrayWithObjects: @"1", @"2", nil] - valueForKey: @"@count"] isEqual: [OFNumber numberWithInt: 2]]) - - mutableArray1 = [mutableArrayClass arrayWithObjects: - [OFMutableIRI IRIWithString: @"http://foo.bar/"], - [OFMutableIRI IRIWithString: @"http://bar.qux/"], - [OFMutableIRI IRIWithString: @"http://qux.quxqux/"], nil]; - TEST(@"-[setValue:forKey:]", - R([mutableArray1 setValue: [OFNumber numberWithShort: 1234] - forKey: @"port"]) && - [mutableArray1 isEqual: [arrayClass arrayWithObjects: - [OFIRI IRIWithString: @"http://foo.bar:1234/"], - [OFIRI IRIWithString: @"http://bar.qux:1234/"], - [OFIRI IRIWithString: @"http://qux.quxqux:1234/"], nil]]) - - objc_autoreleasePoolPop(pool); -} - -- (void)arrayTests -{ - module = @"OFArray"; - [self arrayTestsWithClass: [SimpleArray class] - mutableClass: [SimpleMutableArray class]]; - - module = @"OFArray_adjacent"; - [self arrayTestsWithClass: [OFArray class] - mutableClass: [OFMutableArray class]]; -} -@end DELETED tests/OFCharacterSetTests.m Index: tests/OFCharacterSetTests.m ================================================================== --- tests/OFCharacterSetTests.m +++ tests/OFCharacterSetTests.m @@ -1,107 +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" - -#import "TestsAppDelegate.h" - -#import "OFCharacterSet.h" -#import "OFBitSetCharacterSet.h" -#import "OFRangeCharacterSet.h" - -static OFString *module; - -@interface SimpleCharacterSet: OFCharacterSet -@end - -@implementation SimpleCharacterSet -- (bool)characterIsMember: (OFUnichar)character -{ - return (character % 2 == 0); -} -@end - -@implementation TestsAppDelegate (OFCharacterSetTests) -- (void)characterSetTests -{ - void *pool = objc_autoreleasePoolPush(); - OFCharacterSet *characterSet, *invertedCharacterSet; - bool ok; - - module = @"OFCharacterSet"; - - characterSet = [[[SimpleCharacterSet alloc] init] autorelease]; - - ok = true; - for (OFUnichar c = 0; c < 65536; c++) { - if (c % 2 == 0) { - if (![characterSet characterIsMember: c]) - ok = false; - } else if ([characterSet characterIsMember: c]) - ok = false; - } - TEST(@"-[characterIsMember:]", ok); - - module = @"OFBitSetCharacterSet"; - - TEST(@"+[characterSetWithCharactersInString:]", - (characterSet = [OFCharacterSet characterSetWithCharactersInString: - @"0123456789"]) && - [characterSet isKindOfClass: [OFBitSetCharacterSet class]]) - - ok = true; - for (OFUnichar c = 0; c < 65536; c++) { - if (c >= '0' && c <= '9') { - if (![characterSet characterIsMember: c]) - ok = false; - } else if ([characterSet characterIsMember: c]) - ok = false; - } - TEST(@"-[characterIsMember:]", ok); - - module = @"OFRangeCharacterSet"; - - TEST(@"+[characterSetWithRange:]", - (characterSet = [OFCharacterSet - characterSetWithRange: OFMakeRange('0', 10)]) && - [characterSet isKindOfClass: [OFRangeCharacterSet class]]) - - ok = true; - for (OFUnichar c = 0; c < 65536; c++) { - if (c >= '0' && c <= '9') { - if (![characterSet characterIsMember: c]) - ok = false; - } else if ([characterSet characterIsMember: c]) - ok = false; - } - TEST(@"-[characterIsMember:]", ok); - - ok = true; - invertedCharacterSet = characterSet.invertedSet; - for (OFUnichar c = 0; c < 65536; c++) { - if (c >= '0' && c <= '9') { - if ([invertedCharacterSet characterIsMember: c]) - ok = false; - } else if (![invertedCharacterSet characterIsMember: c]) - ok = false; - } - TEST(@"-[invertedSet]", ok); - - TEST(@"Inverting -[invertedSet] returns original set", - invertedCharacterSet.invertedSet == characterSet) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFColorTests.m Index: tests/OFColorTests.m ================================================================== --- tests/OFColorTests.m +++ tests/OFColorTests.m @@ -1,47 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFColor"; - -@implementation TestsAppDelegate (OFColorTests) -- (void)colorTests -{ - void *pool = objc_autoreleasePoolPush(); - OFColor *color; - float red, green, blue, alpha; - - TEST(@"+[colorWithRed:green:blue:alpha:]", - (color = [OFColor colorWithRed: 63.f / 255 - green: 127.f / 255 - blue: 1 - alpha: 1])) - -#ifdef OF_OBJFW_RUNTIME - TEST(@"+[colorWithRed:green:blue:alpha:] returns tagged pointer", - object_isTaggedPointer(color)) -#endif - - TEST(@"-[getRed:green:blue:alpha:]", - R([color getRed: &red green: &green blue: &blue alpha: &alpha]) && - red == 63.f / 255 && green == 127.f / 255 && blue == 1 && - alpha == 1) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFDateTests.m Index: tests/OFDateTests.m ================================================================== --- tests/OFDateTests.m +++ tests/OFDateTests.m @@ -1,112 +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" -#import "OFStrPTime.h" - -static OFString *const module = @"OFDate"; - -@implementation TestsAppDelegate (OFDateTests) -- (void)dateTests -{ - void *pool = objc_autoreleasePoolPush(); - OFDate *date1, *date2; - - struct tm tm; - int16_t tz; - const char *dstr = "Wed, 09 Jun 2021 +0200x"; - TEST(@"OFStrPTime()", - OFStrPTime(dstr, "%a, %d %b %Y %z", &tm, &tz) == dstr + 22 && - tm.tm_wday == 3 && tm.tm_mday == 9 && tm.tm_mon == 5 && - tm.tm_year == 2021 - 1900 && tz == 2 * 60) - - TEST(@"+[dateWithTimeIntervalSince1970:]", - (date1 = [OFDate dateWithTimeIntervalSince1970: 0])) - - TEST(@"-[dateByAddingTimeInterval:]", - (date2 = [date1 dateByAddingTimeInterval: 3600 * 25 + 5.000002])) - - TEST(@"-[description]", - [date1.description isEqual: @"1970-01-01T00:00:00Z"] && - [date2.description isEqual: @"1970-01-02T01:00:05Z"]) - - TEST(@"+[dateWithDateString:format:]", - [[[OFDate dateWithDateString: @"2000-06-20T12:34:56+0200" - format: @"%Y-%m-%dT%H:%M:%S%z"] description] - isEqual: @"2000-06-20T10:34:56Z"]); - - EXPECT_EXCEPTION(@"Detection of unparsed in " - @"+[dateWithDateString:format:]", OFInvalidFormatException, - [OFDate dateWithDateString: @"2000-06-20T12:34:56+0200x" - format: @"%Y-%m-%dT%H:%M:%S%z"]) - - TEST(@"+[dateWithLocalDateString:format:]", - [[[OFDate dateWithLocalDateString: @"2000-06-20T12:34:56" - format: @"%Y-%m-%dT%H:%M:%S"] - localDateStringWithFormat: @"%Y-%m-%dT%H:%M:%S"] - isEqual: @"2000-06-20T12:34:56"]); - - TEST(@"+[dateWithLocalDateString:format:]", - [[[OFDate dateWithLocalDateString: @"2000-06-20T12:34:56-0200" - format: @"%Y-%m-%dT%H:%M:%S%z"] - description] isEqual: @"2000-06-20T14:34:56Z"]); - - EXPECT_EXCEPTION(@"Detection of unparsed in " - @"+[dateWithLocalDateString:format:] #1", OFInvalidFormatException, - [OFDate dateWithLocalDateString: @"2000-06-20T12:34:56x" - format: @"%Y-%m-%dT%H:%M:%S"]) - - EXPECT_EXCEPTION(@"Detection of unparsed in " - @"+[dateWithLocalDateString:format:] #2", OFInvalidFormatException, - [OFDate dateWithLocalDateString: @"2000-06-20T12:34:56+0200x" - format: @"%Y-%m-%dT%H:%M:%S%z"]) - - TEST(@"-[isEqual:]", - [date1 isEqual: [OFDate dateWithTimeIntervalSince1970: 0]] && - ![date1 isEqual: [OFDate dateWithTimeIntervalSince1970: 0.0000001]]) - - TEST(@"-[compare:]", [date1 compare: date2] == OFOrderedAscending) - - TEST(@"-[second]", date1.second == 0 && date2.second == 5) - - TEST(@"-[microsecond]", - date1.microsecond == 0 && date2.microsecond == 2) - - TEST(@"-[minute]", date1.minute == 0 && date2.minute == 0) - - TEST(@"-[hour]", date1.hour == 0 && date2.hour == 1) - - TEST(@"-[dayOfMonth]", date1.dayOfMonth == 1 && date2.dayOfMonth == 2) - - TEST(@"-[monthOfYear]", - date1.monthOfYear == 1 && date2.monthOfYear == 1) - - TEST(@"-[year]", date1.year == 1970 && date2.year == 1970) - - TEST(@"-[dayOfWeek]", date1.dayOfWeek == 4 && date2.dayOfWeek == 5) - - TEST(@"-[dayOfYear]", date1.dayOfYear == 1 && date2.dayOfYear == 2) - - TEST(@"-[earlierDate:]", [[date1 earlierDate: date2] isEqual: date1]) - - TEST(@"-[laterDate:]", [[date1 laterDate: date2] isEqual: date2]) - - objc_autoreleasePoolPop(pool); -} -@end 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 DELETED tests/OFINIFileTests.m Index: tests/OFINIFileTests.m ================================================================== --- tests/OFINIFileTests.m +++ tests/OFINIFileTests.m @@ -1,138 +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" - -#import "TestsAppDelegate.h" - -static OFString *module; - -@implementation TestsAppDelegate (OFINIFileTests) -- (void)INIFileTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *output = @"[tests]\r\n" - @"foo=baz\r\n" - @"foobar=baz\r\n" - @";comment\r\n" - @"new=new\r\n" - @"\r\n" - @"[foobar]\r\n" - @";foobarcomment\r\n" - @"qux=\" asd\"\r\n" - @"quxquxqux=\"hello\\\"wörld\"\r\n" - @"qux2=\"a\\f\"\r\n" - @"qux3=a\fb\r\n" - @"\r\n" - @"[types]\r\n" - @"integer=16\r\n" - @"bool=false\r\n" - @"float=0.25\r\n" - @"array1=foo\r\n" - @"array1=bar\r\n" - @"double=0.75\r\n"; - OFIRI *IRI; - OFINIFile *file; - OFINICategory *tests, *foobar, *types; - OFArray *array; -#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) - OFIRI *writeIRI; -#endif - - module = @"OFINIFile"; - - IRI = [OFIRI IRIWithString: @"embedded:testfile.ini"]; - TEST(@"+[fileWithIRI:encoding:]", - (file = [OFINIFile fileWithIRI: IRI - encoding: OFStringEncodingCodepage437])) - - tests = [file categoryForName: @"tests"]; - foobar = [file categoryForName: @"foobar"]; - types = [file categoryForName: @"types"]; - TEST(@"-[categoryForName:]", - tests != nil && foobar != nil && types != nil) - - module = @"OFINICategory"; - - TEST(@"-[stringValueForKey:]", - [[tests stringValueForKey: @"foo"] isEqual: @"bar"] && - [[foobar stringValueForKey: @"quxquxqux"] isEqual: @"hello\"wörld"]) - - TEST(@"-[setStringValue:forKey:]", - R([tests setStringValue: @"baz" forKey: @"foo"]) && - R([tests setStringValue: @"new" forKey: @"new"]) && - R([foobar setStringValue: @"a\fb" forKey: @"qux3"])) - - TEST(@"-[longLongValueForKey:defaultValue:]", - [types longLongValueForKey: @"integer" defaultValue: 2] == 0x20) - - TEST(@"-[setLongLongValue:forKey:]", - R([types setLongLongValue: 0x10 forKey: @"integer"])) - - TEST(@"-[boolValueForKey:defaultValue:]", - [types boolValueForKey: @"bool" defaultValue: false] == true) - - TEST(@"-[setBoolValue:forKey:]", - R([types setBoolValue: false forKey: @"bool"])) - - TEST(@"-[floatValueForKey:defaultValue:]", - [types floatValueForKey: @"float" defaultValue: 1] == 0.5f) - - TEST(@"-[setFloatValue:forKey:]", - R([types setFloatValue: 0.25f forKey: @"float"])) - - TEST(@"-[doubleValueForKey:defaultValue:]", - [types doubleValueForKey: @"double" defaultValue: 3] == 0.25) - - TEST(@"-[setDoubleValue:forKey:]", - R([types setDoubleValue: 0.75 forKey: @"double"])) - - array = [OFArray arrayWithObjects: @"1", @"2", nil]; - TEST(@"-[arrayValueForKey:]", - [[types arrayValueForKey: @"array1"] isEqual: array] && - [[types arrayValueForKey: @"array2"] isEqual: array] && - [[types arrayValueForKey: @"array3"] isEqual: [OFArray array]]) - - array = [OFArray arrayWithObjects: @"foo", @"bar", nil]; - TEST(@"-[setArrayValue:forKey:]", - R([types setArrayValue: array forKey: @"array1"])) - - TEST(@"-[removeValueForKey:]", - R([foobar removeValueForKey: @"quxqux "]) && - R([types removeValueForKey: @"array2"])) - - module = @"OFINIFile"; - - /* FIXME: Find a way to write files on Nintendo DS */ -#if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) - writeIRI = [OFSystemInfo temporaryDirectoryIRI]; - if (writeIRI == nil) - writeIRI = [[OFFileManager defaultManager] currentDirectoryIRI]; - writeIRI = [writeIRI IRIByAppendingPathComponent: @"objfw-tests.ini" - isDirectory: false]; - TEST(@"-[writeToFile:encoding:]", - R([file writeToIRI: writeIRI - encoding: OFStringEncodingCodepage437]) && - [[OFString stringWithContentsOfIRI: writeIRI - encoding: OFStringEncodingCodepage437] - isEqual: output]) - [[OFFileManager defaultManager] removeItemAtIRI: writeIRI]; -#else - (void)output; -#endif - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFIRITests.m Index: tests/OFIRITests.m ================================================================== --- tests/OFIRITests.m +++ tests/OFIRITests.m @@ -1,371 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFIRI"; -static OFString *IRIString = @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/" - @"pa%3Fth?que%23ry=1&f%26oo=b%3dar#frag%23ment"; - -@implementation TestsAppDelegate (OFIRITests) -- (void)IRITests -{ - void *pool = objc_autoreleasePoolPush(); - OFIRI *IRI1, *IRI2, *IRI3, *IRI4, *IRI5, *IRI6, *IRI7, *IRI8, *IRI9; - OFIRI *IRI10, *IRI11; - OFMutableIRI *mutableIRI; - - TEST(@"+[IRIWithString:]", - R(IRI1 = [OFIRI IRIWithString: IRIString]) && - R(IRI2 = [OFIRI IRIWithString: @"http://foo:80"]) && - R(IRI3 = [OFIRI IRIWithString: @"http://bar/"]) && - R(IRI4 = [OFIRI IRIWithString: @"file:///etc/passwd"]) && - R(IRI5 = [OFIRI IRIWithString: @"http://foo/bar/qux/foo%2fbar"]) && - R(IRI6 = [OFIRI IRIWithString: @"https://[12:34::56:abcd]/"]) && - R(IRI7 = [OFIRI IRIWithString: @"https://[12:34::56:abcd]:234/"]) && - R(IRI8 = [OFIRI IRIWithString: @"urn:qux:foo"]) && - R(IRI9 = [OFIRI IRIWithString: @"file:/foo?query#frag"]) && - R(IRI10 = [OFIRI IRIWithString: @"file:foo@bar/qux?query#frag"]) && - R(IRI11 = [OFIRI IRIWithString: @"http://ä/ö?ü"])) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #1", - OFInvalidFormatException, - [OFIRI IRIWithString: @"ht,tp://foo"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #2", - OFInvalidFormatException, - [OFIRI IRIWithString: @"http://f`oo"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #3", - OFInvalidFormatException, - [OFIRI IRIWithString: @"http://foo/`"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #4", - OFInvalidFormatException, - [OFIRI IRIWithString: @"http://foo/foo?`"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #5", - OFInvalidFormatException, - [OFIRI IRIWithString: @"http://foo/foo?foo#`"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #6", - OFInvalidFormatException, - [OFIRI IRIWithString: @"https://[g]/"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #7", - OFInvalidFormatException, - [OFIRI IRIWithString: @"https://[f]:/"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #8", - OFInvalidFormatException, - [OFIRI IRIWithString: @"https://[f]:f/"]) - - EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #9", - OFInvalidFormatException, - [OFIRI IRIWithString: @"foo:"]) - - TEST(@"+[IRIWithString:relativeToIRI:]", - [[[OFIRI IRIWithString: @"/foo" relativeToIRI: IRI1] string] - isEqual: @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/foo"] && - [[[OFIRI IRIWithString: @"foo/bar?q" - relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/quux"]] - string] isEqual: @"http://h/qux/foo/bar?q"] && - [[[OFIRI IRIWithString: @"foo/bar" - relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/?x"]] - string] isEqual: @"http://h/qux/foo/bar"] && - [[[OFIRI IRIWithString: @"http://foo/?q" - relativeToIRI: IRI1] string] isEqual: @"http://foo/?q"] && - [[[OFIRI IRIWithString: @"foo" - relativeToIRI: [OFIRI IRIWithString: @"http://foo/bar"]] - string] isEqual: @"http://foo/foo"] && - [[[OFIRI IRIWithString: @"foo" - relativeToIRI: [OFIRI IRIWithString: @"http://foo"]] - string] isEqual: @"http://foo/foo"]) - - EXPECT_EXCEPTION( - @"+[IRIWithString:relativeToIRI:] fails with invalid characters #1", - OFInvalidFormatException, - [OFIRI IRIWithString: @"`" relativeToIRI: IRI1]) - - EXPECT_EXCEPTION( - @"+[IRIWithString:relativeToIRI:] fails with invalid characters #2", - OFInvalidFormatException, - [OFIRI IRIWithString: @"/`" relativeToIRI: IRI1]) - - EXPECT_EXCEPTION( - @"+[IRIWithString:relativeToIRI:] fails with invalid characters #3", - OFInvalidFormatException, - [OFIRI IRIWithString: @"?`" relativeToIRI: IRI1]) - - EXPECT_EXCEPTION( - @"+[IRIWithString:relativeToIRI:] fails with invalid characters #4", - OFInvalidFormatException, - [OFIRI IRIWithString: @"#`" relativeToIRI: IRI1]) - -#ifdef OF_HAVE_FILES - TEST(@"+[fileIRIWithPath:]", - [[[OFIRI fileIRIWithPath: @"testfile.txt"] fileSystemRepresentation] - isEqual: [[OFFileManager defaultManager].currentDirectoryPath - stringByAppendingPathComponent: @"testfile.txt"]]) - -# if defined(OF_WINDOWS) || defined(OF_MSDOS) - OFIRI *tmp; - TEST(@"+[fileIRIWithPath:] for c:\\", - (tmp = [OFIRI fileIRIWithPath: @"c:\\"]) && - [tmp.string isEqual: @"file:/c:/"] && - [tmp.fileSystemRepresentation isEqual: @"c:\\"]) -# endif - -# ifdef OF_WINDOWS - TEST(@"+[fileIRIWithPath:] with UNC", - (tmp = [OFIRI fileIRIWithPath: @"\\\\foo\\bar" - isDirectory: false]) && - [tmp.host isEqual: @"foo"] && [tmp.path isEqual: @"/bar"] && - [tmp.string isEqual: @"file://foo/bar"] && - [tmp.fileSystemRepresentation isEqual: @"\\\\foo\\bar"] && - (tmp = [OFIRI fileIRIWithPath: @"\\\\test" isDirectory: true]) && - [tmp.host isEqual: @"test"] && [tmp.path isEqual: @"/"] && - [tmp.string isEqual: @"file://test/"] && - [tmp.fileSystemRepresentation isEqual: @"\\\\test"]) -# endif -#endif - - TEST(@"-[string]", - [IRI1.string isEqual: IRIString] && - [IRI2.string isEqual: @"http://foo:80"] && - [IRI3.string isEqual: @"http://bar/"] && - [IRI4.string isEqual: @"file:///etc/passwd"] && - [IRI5.string isEqual: @"http://foo/bar/qux/foo%2fbar"] && - [IRI6.string isEqual: @"https://[12:34::56:abcd]/"] && - [IRI7.string isEqual: @"https://[12:34::56:abcd]:234/"] && - [IRI8.string isEqual: @"urn:qux:foo"] && - [IRI9.string isEqual: @"file:/foo?query#frag"] && - [IRI10.string isEqual: @"file:foo@bar/qux?query#frag"] && - [IRI11.string isEqual: @"http://ä/ö?ü"]) - - TEST(@"-[scheme]", - [IRI1.scheme isEqual: @"ht+tp"] && [IRI4.scheme isEqual: @"file"] && - [IRI9.scheme isEqual: @"file"] && [IRI10.scheme isEqual: @"file"] && - [IRI11.scheme isEqual: @"http"]) - - TEST(@"-[user]", [IRI1.user isEqual: @"us:er"] && IRI4.user == nil && - IRI10.user == nil && IRI11.user == nil) - TEST(@"-[password]", - [IRI1.password isEqual: @"p@w"] && IRI4.password == nil && - IRI10.password == nil && IRI11.password == nil) - TEST(@"-[host]", [IRI1.host isEqual: @"ho:st"] && - [IRI6.host isEqual: @"12:34::56:abcd"] && - [IRI7.host isEqual: @"12:34::56:abcd"] && - IRI8.host == nil && IRI9.host == nil && IRI10.host == nil && - [IRI11.host isEqual: @"ä"]) - TEST(@"-[port]", IRI1.port.unsignedShortValue == 1234 && - [IRI4 port] == nil && IRI7.port.unsignedShortValue == 234 && - IRI8.port == nil && IRI9.port == nil && IRI10.port == nil && - IRI11.port == nil) - TEST(@"-[path]", - [IRI1.path isEqual: @"/pa?th"] && - [IRI4.path isEqual: @"/etc/passwd"] && - [IRI8.path isEqual: @"qux:foo"] && - [IRI9.path isEqual: @"/foo"] && - [IRI10.path isEqual: @"foo@bar/qux"] && - [IRI11.path isEqual: @"/ö"]) - TEST(@"-[pathComponents]", - [IRI1.pathComponents isEqual: - [OFArray arrayWithObjects: @"/", @"pa?th", nil]] && - [IRI4.pathComponents isEqual: - [OFArray arrayWithObjects: @"/", @"etc", @"passwd", nil]] && - [IRI5.pathComponents isEqual: - [OFArray arrayWithObjects: @"/", @"bar", @"qux", @"foo/bar", nil]]) - TEST(@"-[lastPathComponent]", - [[[OFIRI IRIWithString: @"http://host/foo//bar/baz"] - lastPathComponent] isEqual: @"baz"] && - [[[OFIRI IRIWithString: @"http://host/foo//bar/baz/"] - lastPathComponent] isEqual: @"baz"] && - [[[OFIRI IRIWithString: @"http://host/foo/"] - lastPathComponent] isEqual: @"foo"] && - [[[OFIRI IRIWithString: @"http://host/"] - lastPathComponent] isEqual: @"/"] && - [IRI5.lastPathComponent isEqual: @"foo/bar"]) - TEST(@"-[query]", - [IRI1.query isEqual: @"que#ry=1&f&oo=b=ar"] && IRI4.query == nil && - [IRI9.query isEqual: @"query"] && [IRI10.query isEqual: @"query"] && - [IRI11.query isEqual: @"ü"]) - TEST(@"-[queryItems]", - [IRI1.queryItems isEqual: [OFArray arrayWithObjects: - [OFPair pairWithFirstObject: @"que#ry" secondObject: @"1"], - [OFPair pairWithFirstObject: @"f&oo" secondObject: @"b=ar"], nil]]); - TEST(@"-[fragment]", - [IRI1.fragment isEqual: @"frag#ment"] && IRI4.fragment == nil && - [IRI9.fragment isEqual: @"frag"] && - [IRI10.fragment isEqual: @"frag"]) - - TEST(@"-[copy]", R(IRI4 = [[IRI1 copy] autorelease])) - - TEST(@"-[isEqual:]", [IRI1 isEqual: IRI4] && ![IRI2 isEqual: IRI3] && - [[OFIRI IRIWithString: @"HTTP://bar/"] isEqual: IRI3]) - - TEST(@"-[hash:]", IRI1.hash == IRI4.hash && IRI2.hash != IRI3.hash) - - EXPECT_EXCEPTION(@"Detection of invalid format", - OFInvalidFormatException, [OFIRI IRIWithString: @"http"]) - - TEST(@"-[IRIByAddingPercentEncodingForUnicodeCharacters]", - [IRI11.IRIByAddingPercentEncodingForUnicodeCharacters - isEqual: [OFIRI IRIWithString: @"http://%C3%A4/%C3%B6?%C3%BC"]]) - - mutableIRI = [OFMutableIRI IRIWithScheme: @"dummy"]; - - EXPECT_EXCEPTION( - @"-[setPercentEncodedScheme:] with invalid characters fails", - OFInvalidFormatException, mutableIRI.scheme = @"%20") - - TEST(@"-[setHost:]", - (mutableIRI.host = @"ho:st") && - [mutableIRI.percentEncodedHost isEqual: @"ho%3Ast"] && - (mutableIRI.host = @"12:34:ab") && - [mutableIRI.percentEncodedHost isEqual: @"[12:34:ab]"] && - (mutableIRI.host = @"12:34:aB") && - [mutableIRI.percentEncodedHost isEqual: @"[12:34:aB]"] && - (mutableIRI.host = @"12:34:g") && - [mutableIRI.percentEncodedHost isEqual: @"12%3A34%3Ag"]) - - TEST(@"-[setPercentEncodedHost:]", - (mutableIRI.percentEncodedHost = @"ho%3Ast") && - [mutableIRI.host isEqual: @"ho:st"] && - (mutableIRI.percentEncodedHost = @"[12:34]") && - [mutableIRI.host isEqual: @"12:34"] && - (mutableIRI.percentEncodedHost = @"[12::ab]") && - [mutableIRI.host isEqual: @"12::ab"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedHost:] with invalid characters fails #1", - OFInvalidFormatException, - mutableIRI.percentEncodedHost = @"/") - - EXPECT_EXCEPTION( - @"-[setPercentEncodedHost:] with invalid characters fails #2", - OFInvalidFormatException, - mutableIRI.percentEncodedHost = @"[12:34") - - EXPECT_EXCEPTION( - @"-[setPercentEncodedHost:] with invalid characters fails #3", - OFInvalidFormatException, - mutableIRI.percentEncodedHost = @"[a::g]") - - TEST(@"-[setUser:]", - (mutableIRI.user = @"us:er") && - [mutableIRI.percentEncodedUser isEqual: @"us%3Aer"]) - - TEST(@"-[setPercentEncodedUser:]", - (mutableIRI.percentEncodedUser = @"us%3Aer") && - [mutableIRI.user isEqual: @"us:er"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedUser:] with invalid characters fails", - OFInvalidFormatException, - mutableIRI.percentEncodedHost = @"/") - - TEST(@"-[setPassword:]", - (mutableIRI.password = @"pass:word") && - [mutableIRI.percentEncodedPassword isEqual: @"pass%3Aword"]) - - TEST(@"-[setPercentEncodedPassword:]", - (mutableIRI.percentEncodedPassword = @"pass%3Aword") && - [mutableIRI.password isEqual: @"pass:word"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedPassword:] with invalid characters fails", - OFInvalidFormatException, - mutableIRI.percentEncodedPassword = @"/") - - TEST(@"-[setPath:]", - (mutableIRI.path = @"pa/th@?") && - [mutableIRI.percentEncodedPath isEqual: @"pa/th@%3F"]) - - TEST(@"-[setPercentEncodedPath:]", - (mutableIRI.percentEncodedPath = @"pa/th@%3F") && - [mutableIRI.path isEqual: @"pa/th@?"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedPath:] with invalid characters fails", - OFInvalidFormatException, - mutableIRI.percentEncodedPath = @"?") - - TEST(@"-[setQuery:]", - (mutableIRI.query = @"que/ry?#") && - [mutableIRI.percentEncodedQuery isEqual: @"que/ry?%23"]) - - TEST(@"-[setPercentEncodedQuery:]", - (mutableIRI.percentEncodedQuery = @"que/ry?%23") && - [mutableIRI.query isEqual: @"que/ry?#"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedQuery:] with invalid characters fails", - OFInvalidFormatException, - mutableIRI.percentEncodedQuery = @"`") - - TEST(@"-[setQueryItems:]", - (mutableIRI.queryItems = [OFArray arrayWithObjects: - [OFPair pairWithFirstObject: @"foo&bar" secondObject: @"baz=qux"], - [OFPair pairWithFirstObject: @"f=oobar" secondObject: @"b&azqux"], - nil]) && [mutableIRI.percentEncodedQuery isEqual: - @"foo%26bar=baz%3Dqux&f%3Doobar=b%26azqux"]) - - TEST(@"-[setFragment:]", - (mutableIRI.fragment = @"frag/ment?#") && - [mutableIRI.percentEncodedFragment isEqual: @"frag/ment?%23"]) - - TEST(@"-[setPercentEncodedFragment:]", - (mutableIRI.percentEncodedFragment = @"frag/ment?%23") && - [mutableIRI.fragment isEqual: @"frag/ment?#"]) - - EXPECT_EXCEPTION( - @"-[setPercentEncodedFragment:] with invalid characters fails", - OFInvalidFormatException, - mutableIRI.percentEncodedFragment = @"`") - - TEST(@"-[IRIByAppendingPathComponent:isDirectory:]", - [[[OFIRI IRIWithString: @"file:///foo/bar"] - IRIByAppendingPathComponent: @"qux" isDirectory: false] isEqual: - [OFIRI IRIWithString: @"file:///foo/bar/qux"]] && - [[[OFIRI IRIWithString: @"file:///foo/bar/"] - IRIByAppendingPathComponent: @"qux" isDirectory: false] isEqual: - [OFIRI IRIWithString: @"file:///foo/bar/qux"]] && - [[[OFIRI IRIWithString: @"file:///foo/bar/"] - IRIByAppendingPathComponent: @"qu?x" isDirectory: false] isEqual: - [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx"]] && - [[[OFIRI IRIWithString: @"file:///foo/bar/"] - IRIByAppendingPathComponent: @"qu?x" isDirectory: true] isEqual: - [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx/"]]) - - TEST(@"-[IRIByStandardizingPath]", - [[[OFIRI IRIWithString: @"http://foo/bar/.."] - IRIByStandardizingPath] isEqual: - [OFIRI IRIWithString: @"http://foo/"]] && - [[[OFIRI IRIWithString: @"http://foo/bar/%2E%2E/../qux/"] - IRIByStandardizingPath] isEqual: - [OFIRI IRIWithString: @"http://foo/bar/qux/"]] && - [[[OFIRI IRIWithString: @"http://foo/bar/./././qux/./"] - IRIByStandardizingPath] isEqual: - [OFIRI IRIWithString: @"http://foo/bar/qux/"]] && - [[[OFIRI IRIWithString: @"http://foo/bar/../../qux"] - IRIByStandardizingPath] isEqual: - [OFIRI IRIWithString: @"http://foo/../qux"]]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFInvocationTests.m Index: tests/OFInvocationTests.m ================================================================== --- tests/OFInvocationTests.m +++ tests/OFInvocationTests.m @@ -1,95 +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 - -#if defined(HAVE_COMPLEX_H) && !defined(__STDC_NO_COMPLEX__) -# include -#endif - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFInvocation"; - -struct TestStruct { - unsigned char c; - unsigned int i; -}; - -@implementation TestsAppDelegate (OFInvocationTests) -- (struct TestStruct)invocationTestMethod1: (unsigned char)c - : (unsigned int)i - : (struct TestStruct *)ptr - : (struct TestStruct)st -{ - return st; -} - -- (void)invocationTests -{ - void *pool = objc_autoreleasePoolPush(); - SEL selector = @selector(invocationTestMethod1::::); - OFMethodSignature *sig = [self methodSignatureForSelector: selector]; - OFInvocation *invocation; - struct TestStruct st, st2, *stp = &st, *stp2; - unsigned const char c = 0xAA; - unsigned char c2; - const unsigned int i = 0x55555555; - unsigned int i2; - - memset(&st, '\xFF', sizeof(st)); - st.c = 0x55; - st.i = 0xAAAAAAAA; - - TEST(@"+[invocationWithMethodSignature:]", - (invocation = [OFInvocation invocationWithMethodSignature: sig])) - - TEST(@"-[setReturnValue]", R([invocation setReturnValue: &st])) - - TEST(@"-[getReturnValue]", R([invocation getReturnValue: &st2]) && - memcmp(&st, &st2, sizeof(st)) == 0) - - memset(&st2, '\0', sizeof(st2)); - - TEST(@"-[setArgument:atIndex:] #1", - R([invocation setArgument: &c atIndex: 2])) - - TEST(@"-[setArgument:atIndex:] #2", - R([invocation setArgument: &i atIndex: 3])) - - TEST(@"-[setArgument:atIndex:] #3", - R([invocation setArgument: &stp atIndex: 4])) - - TEST(@"-[setArgument:atIndex:] #4", - R([invocation setArgument: &st atIndex: 5])) - - TEST(@"-[getArgument:atIndex:] #1", - R([invocation getArgument: &c2 atIndex: 2]) && c == c2) - - TEST(@"-[getArgument:atIndex:] #2", - R([invocation getArgument: &i2 atIndex: 3]) && i == i2) - - TEST(@"-[getArgument:atIndex:] #3", - R([invocation getArgument: &stp2 atIndex: 4]) && stp == stp2) - - TEST(@"-[getArgument:atIndex:] #4", - R([invocation getArgument: &st2 atIndex: 5]) && - memcmp(&st, &st2, sizeof(st)) == 0) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFJSONTests.m Index: tests/OFJSONTests.m ================================================================== --- tests/OFJSONTests.m +++ tests/OFJSONTests.m @@ -1,92 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFJSON"; - -@implementation TestsAppDelegate (JSONTests) -- (void)JSONTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF," - @"null//bar\n,\"foo\",false]}"; - OFDictionary *dict = [OFDictionary dictionaryWithKeysAndObjects: - @"foo", @"b\na\r", - @"x", [OFArray arrayWithObjects: - [OFNumber numberWithFloat: .5f], - [OFNumber numberWithInt: 0xF], - [OFNull null], - @"foo", - [OFNumber numberWithBool: false], - nil], - nil]; - - TEST(@"-[objectByParsingJSON] #1", - [string.objectByParsingJSON isEqual: dict]) - - TEST(@"-[JSONRepresentation]", - [[dict JSONRepresentation] isEqual: - @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"]) - - TEST(@"OFJSONRepresentationOptionPretty", - [[dict JSONRepresentationWithOptions: - OFJSONRepresentationOptionPretty] isEqual: - @"{\n\t\"x\": [\n\t\t0.5,\n\t\t15,\n\t\tnull,\n\t\t" - @"\"foo\",\n\t\tfalse\n\t],\n\t\"foo\": \"b\\na\\r\"\n}"]) - - TEST(@"OFJSONRepresentationOptionJSON5", - [[dict JSONRepresentationWithOptions: - OFJSONRepresentationOptionJSON5] isEqual: - @"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"]) - - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #2", OFInvalidJSONException, - [@"{" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #3", OFInvalidJSONException, - [@"]" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #4", OFInvalidJSONException, - [@"bar" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #5", OFInvalidJSONException, - [@"[\"a\" \"b\"]" objectByParsingJSON]) - - TEST(@"-[objectByParsingJSON] #6", - [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" - .objectByParsingJSON isEqual: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: - [OFDictionary dictionary]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]) - - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #7", OFInvalidJSONException, - [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" - objectByParsingJSON]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFMD5HashTests.m Index: tests/OFMD5HashTests.m ================================================================== --- tests/OFMD5HashTests.m +++ tests/OFMD5HashTests.m @@ -1,59 +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 = @"OFMD5Hash"; - -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, *MD5Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (MD5 = [OFMD5Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [MD5 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (MD5Copy = [[MD5 copy] autorelease])) - - TEST(@"-[calculate]", R([MD5 calculate]) && R([MD5Copy calculate])) - - TEST(@"-[digest]", - 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]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFMatrix4x4Tests.m Index: tests/OFMatrix4x4Tests.m ================================================================== --- tests/OFMatrix4x4Tests.m +++ tests/OFMatrix4x4Tests.m @@ -1,106 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFMatrix4x4Tests"; - -@implementation TestsAppDelegate (OFMatrix4x4Tests) -- (void)matrix4x4Tests -{ - void *pool = objc_autoreleasePoolPush(); - OFMatrix4x4 *matrix, *matrix2; - OFVector4D point; - OFVector4D points[2] = {{ 1, 2, 3, 1 }, { 7, 8, 9, 2 }}; - - TEST(@"+[identityMatrix]", - memcmp([[OFMatrix4x4 identityMatrix] values], (const float [4][4]){ - { 1, 0, 0, 0 }, - { 0, 1, 0, 0 }, - { 0, 0, 1, 0 }, - { 0, 0, 0, 1 } - }, 16 * sizeof(float)) == 0) - - TEST(@"+[matrixWithValues:]", - (matrix = [OFMatrix4x4 matrixWithValues: (const float [4][4]){ - { 1, 2, 3, 4 }, - { 5, 6, 7, 8 }, - { 9, 10, 11, 12 }, - { 13, 14, 15, 16 } - }])) - - TEST(@"-[description]", - [matrix.description isEqual: @""]) - - TEST(@"-[isEqual:]", [[OFMatrix4x4 identityMatrix] isEqual: - [OFMatrix4x4 matrixWithValues: (const float [4][4]){ - { 1, 0, 0, 0 }, - { 0, 1, 0, 0 }, - { 0, 0, 1, 0 }, - { 0, 0, 0, 1 } - }]]) - - TEST(@"-[copy]", (matrix2 = [[matrix copy] autorelease]) && - [matrix2 isEqual: matrix]) - - TEST(@"-[multiplyWithMatrix:] #1", - R([matrix2 multiplyWithMatrix: [OFMatrix4x4 identityMatrix]]) && - [matrix2 isEqual: matrix]) - - matrix2 = [OFMatrix4x4 matrixWithValues: (const float [4][4]){ - { 100, 200, 300, 400 }, - { 500, 600, 700, 800 }, - { 900, 1000, 1100, 1200 }, - { 1300, 1400, 1500, 1600 } - }]; - TEST(@"-[multiplyWithMatrix:] #2", - R([matrix2 multiplyWithMatrix: matrix]) && - [matrix2 isEqual: - [OFMatrix4x4 matrixWithValues: (const float [4][4]){ - { 9000, 10000, 11000, 12000 }, - { 20200, 22800, 25400, 28000 }, - { 31400, 35600, 39800, 44000 }, - { 42600, 48400, 54200, 60000 } - }]]) - - TEST(@"-[translateWithVector:]", - (matrix2 = [OFMatrix4x4 identityMatrix]) && - R([matrix2 translateWithVector: OFMakeVector3D(1, 2, 3)]) && - R(point = - [matrix2 transformedVector: OFMakeVector4D(2, 3, 4, 1)]) && - point.x == 3 && point.y == 5 && point.z == 7 && point.w == 1) - - TEST(@"-[scaleWithVector:]", - R([matrix2 scaleWithVector: OFMakeVector3D(-1, 0.5f, 2)]) && - R(point = - [matrix2 transformedVector: OFMakeVector4D(2, 3, 4, 1)]) && - point.x == -3 && point.y == 2.5 && point.z == 14 && point.w == 1) - - TEST(@"-[transformVectors:count:]", - R([matrix transformVectors: points count: 2]) && - points[0].x == 18 && points[0].y == 46 && points[0].z == 74 && - points[0].w == 102 && points[1].x == 58 && points[1].y == 162 && - points[1].z == 266 && points[1].w == 370) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFMethodSignatureTests.m Index: tests/OFMethodSignatureTests.m ================================================================== --- tests/OFMethodSignatureTests.m +++ tests/OFMethodSignatureTests.m @@ -1,181 +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 - -#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) -# include -#endif - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFMethodSignature"; - -struct Test1Struct { - char c; - int i; - char d; -}; - -struct Test2Struct { - char c; - struct { - short s; - int i; - } st; - union { - char c; - int i; - } u; - double d; -}; - -#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) -struct Test3Struct { - char c; - complex double cd; -}; -#endif - -union Test3Union { - char c; - int i; - double d; -}; - -union Test4Union { - char c; - struct { - short x, y; - } st; - int i; - union { - float f; - double d; - } u; -}; - -@implementation TestsAppDelegate (OFMethodSignatureTests) -- (void)methodSignatureTests -{ - void *pool = objc_autoreleasePoolPush(); - OFMethodSignature *methodSignature; - - TEST(@"-[signatureWithObjCTypes:] #1", - (methodSignature = [OFMethodSignature signatureWithObjCTypes: - "i28@0:8S16*20"]) && - methodSignature.numberOfArguments == 4 && - strcmp(methodSignature.methodReturnType, "i") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 0], "@") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 1], ":") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 2], "S") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 3], "*") == 0 && - methodSignature.frameLength == 28 && - [methodSignature argumentOffsetAtIndex: 0] == 0 && - [methodSignature argumentOffsetAtIndex: 1] == 8 && - [methodSignature argumentOffsetAtIndex: 2] == 16 && - [methodSignature argumentOffsetAtIndex: 3] == 20) - - TEST(@"-[signatureWithObjCTypes:] #2", - (methodSignature = [OFMethodSignature signatureWithObjCTypes: - "{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}24@0:8" - "^{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}16"]) && - methodSignature.numberOfArguments == 3 && - strcmp(methodSignature.methodReturnType, - "{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 0], "@") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 1], ":") == 0 && - strcmp([methodSignature argumentTypeAtIndex: 2], - "^{s0=csi(u1={s2=iii{s3=(u4=ic^v*)}})}") == 0 && - methodSignature.frameLength == 24 && - [methodSignature argumentOffsetAtIndex: 0] == 0 && - [methodSignature argumentOffsetAtIndex: 1] == 8 && - [methodSignature argumentOffsetAtIndex: 2] == 16) - - EXPECT_EXCEPTION(@"-[signatureWithObjCTypes:] #3", - OFInvalidFormatException, - [OFMethodSignature signatureWithObjCTypes: "{ii"]) - - EXPECT_EXCEPTION(@"-[signatureWithObjCTypes:] #4", - OFInvalidFormatException, - [OFMethodSignature signatureWithObjCTypes: ""]) - - EXPECT_EXCEPTION(@"-[signatureWithObjCTypes:] #5", - OFInvalidFormatException, - [OFMethodSignature signatureWithObjCTypes: "0"]) - - EXPECT_EXCEPTION(@"-[signatureWithObjCTypes:] #6", - OFInvalidFormatException, - [OFMethodSignature signatureWithObjCTypes: "{{}0"]) - - TEST(@"OFSizeOfTypeEncoding() #1", - OFSizeOfTypeEncoding(@encode(struct Test1Struct)) == - sizeof(struct Test1Struct)) - - TEST(@"OFSizeOfTypeEncoding() #2", - OFSizeOfTypeEncoding(@encode(struct Test2Struct)) == - sizeof(struct Test2Struct)) - -#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) && \ - OF_GCC_VERSION >= 402 - TEST(@"OFSizeOfTypeEncoding() #3", - OFSizeOfTypeEncoding(@encode(struct Test3Struct)) == - sizeof(struct Test3Struct)) -#endif - - TEST(@"OFSizeOfTypeEncoding() #4", - OFSizeOfTypeEncoding(@encode(union Test3Union)) == - sizeof(union Test3Union)) - - TEST(@"OFSizeOfTypeEncoding() #5", - OFSizeOfTypeEncoding(@encode(union Test4Union)) == - sizeof(union Test4Union)) - - TEST(@"OFSizeOfTypeEncoding() #6", - OFSizeOfTypeEncoding(@encode(struct Test1Struct [5])) == - sizeof(struct Test1Struct [5])) - - TEST(@"OFAlignmentOfTypeEncoding() #1", - OFAlignmentOfTypeEncoding(@encode(struct Test1Struct)) == - OF_ALIGNOF(struct Test1Struct)) - - TEST(@"OFAlignmentOfTypeEncoding() #2", - OFAlignmentOfTypeEncoding(@encode(struct Test2Struct)) == - OF_ALIGNOF(struct Test2Struct)) - -#if !defined(__STDC_NO_COMPLEX__) && defined(HAVE_COMPLEX_H) && \ - OF_GCC_VERSION >= 402 - TEST(@"OFAlignmentOfTypeEncoding() #3", - OFAlignmentOfTypeEncoding(@encode(struct Test3Struct)) == - OF_ALIGNOF(struct Test3Struct)) -#endif - - TEST(@"OFAlignmentOfTypeEncoding() #4", - OFAlignmentOfTypeEncoding(@encode(union Test3Union)) == - OF_ALIGNOF(union Test3Union)) - - TEST(@"OFAlignmentOfTypeEncoding() #5", - OFAlignmentOfTypeEncoding(@encode(union Test4Union)) == - OF_ALIGNOF(union Test4Union)) - - TEST(@"OFAlignmentOfTypeEncoding() #6", - OFAlignmentOfTypeEncoding(@encode(struct Test1Struct [5])) == - OF_ALIGNOF(struct Test1Struct [5])) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFNumberTests.m Index: tests/OFNumberTests.m ================================================================== --- tests/OFNumberTests.m +++ tests/OFNumberTests.m @@ -1,92 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFNumber"; - -@implementation TestsAppDelegate (OFNumberTests) -- (void)numberTests -{ - void *pool = objc_autoreleasePoolPush(); - OFNumber *number; - - TEST(@"+[numberWithLongLong:]", - (number = [OFNumber numberWithLongLong: 123456789])) - - TEST(@"-[isEqual:]", - [number isEqual: [OFNumber numberWithLong: 123456789]]) - - TEST(@"-[hash]", number.hash == 0x82D8BC42) - - TEST(@"-[charValue]", number.charValue == 21) - - TEST(@"-[doubleValue]", number.doubleValue == 123456789.L) - - TEST(@"signed char minimum & maximum unmodified", - (number = [OFNumber numberWithChar: SCHAR_MIN]) && - number.charValue == SCHAR_MIN && - (number = [OFNumber numberWithChar: SCHAR_MAX]) && - number.charValue == SCHAR_MAX) - - TEST(@"short minimum & maximum unmodified", - (number = [OFNumber numberWithShort: SHRT_MIN]) && - number.shortValue == SHRT_MIN && - (number = [OFNumber numberWithShort: SHRT_MAX]) && - number.shortValue == SHRT_MAX) - - TEST(@"int minimum & maximum unmodified", - (number = [OFNumber numberWithInt: INT_MIN]) && - number.intValue == INT_MIN && - (number = [OFNumber numberWithInt: INT_MAX]) && - number.intValue == INT_MAX) - - TEST(@"long minimum & maximum unmodified", - (number = [OFNumber numberWithLong: LONG_MIN]) && - number.longValue == LONG_MIN && - (number = [OFNumber numberWithLong: LONG_MAX]) && - number.longValue == LONG_MAX) - - TEST(@"long long minimum & maximum unmodified", - (number = [OFNumber numberWithLongLong: LLONG_MIN]) && - number.longLongValue == LLONG_MIN && - (number = [OFNumber numberWithLongLong: LLONG_MAX]) && - number.longLongValue == LLONG_MAX) - - TEST(@"unsigned char maximum unmodified", - (number = [OFNumber numberWithUnsignedChar: UCHAR_MAX]) && - number.unsignedCharValue == UCHAR_MAX) - - TEST(@"unsigned short maximum unmodified", - (number = [OFNumber numberWithUnsignedShort: USHRT_MAX]) && - number.unsignedShortValue == USHRT_MAX) - - TEST(@"unsigned int maximum unmodified", - (number = [OFNumber numberWithUnsignedInt: UINT_MAX]) && - number.unsignedIntValue == UINT_MAX) - - TEST(@"unsigned long maximum unmodified", - (number = [OFNumber numberWithUnsignedLong: ULONG_MAX]) && - number.unsignedLongValue == ULONG_MAX) - - TEST(@"unsigned long long maximum unmodified", - (number = [OFNumber numberWithUnsignedLongLong: ULLONG_MAX]) && - number.unsignedLongLongValue == ULLONG_MAX) - - objc_autoreleasePoolPop(pool); -} -@end 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 DELETED tests/OFPluginTests.m Index: tests/OFPluginTests.m ================================================================== --- tests/OFPluginTests.m +++ tests/OFPluginTests.m @@ -1,56 +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" - -#import "TestsAppDelegate.h" - -#import "plugin/TestPlugin.h" - -#ifndef OF_IOS -static OFString *const pluginName = @"plugin/TestPlugin"; -#else -static OFString *const pluginName = @"PlugIns/TestPlugin"; -#endif - -static OFString *const module = @"OFPlugin"; - -@implementation TestsAppDelegate (OFPluginTests) -- (void)pluginTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *path; - OFPlugin *plugin; - Class (*class)(void); - TestPlugin *test; - - TEST(@"+[pathForName:]", (path = [OFPlugin pathForName: pluginName])) - - TEST(@"+[pluginWithPath:]", (plugin = [OFPlugin pluginWithPath: path])) - - TEST(@"-[addressForSymbol:]", - (class = (Class (*)(void))(uintptr_t) - [plugin addressForSymbol: @"class"])) - - test = [[class() alloc] init]; - @try { - TEST(@"TestPlugin's -[test:]", [test test: 1234] == 2468) - } @finally { - [test release]; - } - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFPropertyListTests.m Index: tests/OFPropertyListTests.m ================================================================== --- tests/OFPropertyListTests.m +++ tests/OFPropertyListTests.m @@ -1,118 +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" - -#import "TestsAppDelegate.h" - -#define PLIST(x) \ - @"" \ - @"" \ - @"\n" \ - x @"\n" \ - @"" - -static OFString *const module = @"OFPropertyList"; -static OFString *const PLIST1 = PLIST(@"Hello"); -static OFString *const PLIST2 = PLIST( - @"" - @" Hello" - @" V29ybGQh" - @" 2018-03-14T12:34:56Z" - @" " - @" " - @" 12.25" - @" -10" - @""); -static OFString *const PLIST3 = PLIST( - @"" - @" array" - @" " - @" Hello" - @" V29ybGQh" - @" 2018-03-14T12:34:56Z" - @" " - @" " - @" 12.25" - @" -10" - @" " - @" foo" - @" bar" - @""); - -@implementation TestsAppDelegate (OFPLISTParser) -- (void)propertyListTests -{ - void *pool = objc_autoreleasePoolPush(); - OFArray *array = [OFArray arrayWithObjects: - @"Hello", - [OFData dataWithItems: "World!" count: 6], - [OFDate dateWithTimeIntervalSince1970: 1521030896], - [OFNumber numberWithBool: true], - [OFNumber numberWithBool: false], - [OFNumber numberWithFloat: 12.25f], - [OFNumber numberWithInt: -10], - nil]; - - TEST(@"-[objectByParsingPropertyList:] #1", - [PLIST1.objectByParsingPropertyList isEqual: @"Hello"]) - - TEST(@"-[objectByParsingPropertyList:] #2", - [PLIST2.objectByParsingPropertyList isEqual: array]) - - TEST(@"-[objectByParsingPropertyList:] #3", - [PLIST3.objectByParsingPropertyList isEqual: - [OFDictionary dictionaryWithKeysAndObjects: - @"array", array, - @"foo", @"bar", - nil]]) - - EXPECT_EXCEPTION(@"Detecting unsupported version", - OFUnsupportedVersionException, - [[PLIST(@"") stringByReplacingOccurrencesOfString: @"1.0" - withString: @"1.1"] - objectByParsingPropertyList]) - - EXPECT_EXCEPTION( - @"-[objectByParsingPropertyList] detecting invalid format #1", - OFInvalidFormatException, - [PLIST(@"") objectByParsingPropertyList]) - - EXPECT_EXCEPTION( - @"-[objectByParsingPropertyList] detecting invalid format #2", - OFInvalidFormatException, - [PLIST(@"") objectByParsingPropertyList]) - - EXPECT_EXCEPTION( - @"-[objectByParsingPropertyList] detecting invalid format #3", - OFInvalidFormatException, - [PLIST(@"") objectByParsingPropertyList]) - - EXPECT_EXCEPTION( - @"-[objectByParsingPropertyList] detecting invalid format #4", - OFInvalidFormatException, - [PLIST(@"") - objectByParsingPropertyList]) - - EXPECT_EXCEPTION( - @"-[objectByParsingPropertyList] detecting invalid format #5", - OFInvalidFormatException, - [PLIST(@"") - objectByParsingPropertyList]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFRIPEMD160HashTests.m Index: tests/OFRIPEMD160HashTests.m ================================================================== --- tests/OFRIPEMD160HashTests.m +++ tests/OFRIPEMD160HashTests.m @@ -1,61 +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 = @"OFRIPEMD160Hash"; - -const uint8_t testFileRIPEMD160[20] = - "\x46\x02\x97\xF5\x85\xDF\xB9\x21\x00\xC8\xF9\x87\xC6\xEC\x84\x0D\xCE" - "\xE6\x08\x8B"; - -@implementation TestsAppDelegate (OFRIPEMD160HashTests) -- (void)RIPEMD160HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFRIPEMD160Hash *RIPEMD160, *RIPEMD160Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (RIPEMD160 = [OFRIPEMD160Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [RIPEMD160 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (RIPEMD160Copy = [[RIPEMD160 copy] autorelease])) - - TEST(@"-[calculate]", - R([RIPEMD160 calculate]) && R([RIPEMD160Copy calculate])) - - TEST(@"-[digest]", - memcmp(RIPEMD160.digest, testFileRIPEMD160, 20) == 0 && - memcmp(RIPEMD160Copy.digest, testFileRIPEMD160, 20) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length]", OFHashAlreadyCalculatedException, - [RIPEMD160 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSHA1HashTests.m Index: tests/OFSHA1HashTests.m ================================================================== --- tests/OFSHA1HashTests.m +++ tests/OFSHA1HashTests.m @@ -1,60 +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 = @"OFSHA1Hash"; - -const uint8_t testFileSHA1[20] = - "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5" - "\x94\xE7\x17"; - -@implementation TestsAppDelegate (SHA1HashTests) -- (void)SHA1HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA1Hash *SHA1, *SHA1Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (SHA1 = [OFSHA1Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [SHA1 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (SHA1Copy = [[SHA1 copy] autorelease])) - - TEST(@"-[calculate]", R([SHA1 calculate]) && R([SHA1Copy calculate])) - - TEST(@"-[digest]", - memcmp(SHA1.digest, testFileSHA1, 20) == 0 && - memcmp(SHA1Copy.digest, testFileSHA1, 20) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, - [SHA1 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSHA224HashTests.m Index: tests/OFSHA224HashTests.m ================================================================== --- tests/OFSHA224HashTests.m +++ tests/OFSHA224HashTests.m @@ -1,61 +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 = @"OFSHA224Hash"; - -const uint8_t testFileSHA224[28] = - "\x27\x69\xD8\x04\x2D\x0F\xCA\x84\x6C\xF1\x62\x44\xBA\x0C\xBD\x46\x64" - "\x5F\x4F\x20\x02\x4D\x15\xED\x1C\x61\x1F\xF7"; - -@implementation TestsAppDelegate (SHA224HashTests) -- (void)SHA224HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA224Hash *SHA224, *SHA224Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (SHA224 = [OFSHA224Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [SHA224 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (SHA224Copy = [[SHA224 copy] autorelease])) - - TEST(@"-[calculate]", - R([SHA224 calculate]) && R([SHA224Copy calculate])) - - TEST(@"-[digest]", - memcmp(SHA224.digest, testFileSHA224, 28) == 0 && - memcmp(SHA224Copy.digest, testFileSHA224, 28) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, - [SHA224 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSHA256HashTests.m Index: tests/OFSHA256HashTests.m ================================================================== --- tests/OFSHA256HashTests.m +++ tests/OFSHA256HashTests.m @@ -1,61 +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 = @"OFSHA256Hash"; - -const uint8_t testFileSHA256[32] = - "\x1A\x02\xD6\x46\xF5\xA6\xBA\xAA\xFF\x7F\xD5\x87\xBA\xC3\xF6\xC6\xB5" - "\x67\x93\x8F\x0F\x44\x90\xB8\xF5\x35\x89\xF0\x5A\x23\x7F\x69"; - -@implementation TestsAppDelegate (SHA256HashTests) -- (void)SHA256HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA256Hash *SHA256, *SHA256Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (SHA256 = [OFSHA256Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[64]; - size_t length = [file readIntoBuffer: buffer length: 64]; - [SHA256 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (SHA256Copy = [[SHA256 copy] autorelease])) - - TEST(@"-[calculate]", - R([SHA256 calculate]) && R([SHA256Copy calculate])) - - TEST(@"-[digest]", - memcmp(SHA256.digest, testFileSHA256, 32) == 0 && - memcmp(SHA256Copy.digest, testFileSHA256, 32) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, - [SHA256 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSHA384HashTests.m Index: tests/OFSHA384HashTests.m ================================================================== --- tests/OFSHA384HashTests.m +++ tests/OFSHA384HashTests.m @@ -1,62 +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 = @"OFSHA384Hash"; - -const uint8_t testFileSHA384[48] = - "\x7E\xDE\x62\xE2\x10\xA5\x1E\x18\x8A\x11\x7F\x78\xD7\xC7\x55\xB6\x43" - "\x94\x1B\xD2\x78\x5C\xCF\xF3\x8A\xB8\x98\x22\xC7\x0E\xFE\xF1\xEC\x53" - "\xE9\x1A\xB3\x51\x70\x8C\x1F\x3F\x56\x12\x44\x01\x91\x54"; - -@implementation TestsAppDelegate (SHA384HashTests) -- (void)SHA384HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA384Hash *SHA384, *SHA384Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (SHA384 = [OFSHA384Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[128]; - size_t length = [file readIntoBuffer: buffer length: 128]; - [SHA384 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (SHA384Copy = [[SHA384 copy] autorelease])) - - TEST(@"-[calculate]", - R([SHA384 calculate]) && R([SHA384Copy calculate])) - - TEST(@"-[digest]", - memcmp(SHA384.digest, testFileSHA384, 48) == 0 && - memcmp(SHA384Copy.digest, testFileSHA384, 48) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, - [SHA384 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSHA512HashTests.m Index: tests/OFSHA512HashTests.m ================================================================== --- tests/OFSHA512HashTests.m +++ tests/OFSHA512HashTests.m @@ -1,63 +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 = @"OFSHA512Hash"; - -const uint8_t testFileSHA512[64] = - "\x8F\x36\x6E\x3C\x19\x4B\xBB\xC7\x82\xAA\xCD\x7D\x55\xA2\xD3\x29\x29" - "\x97\x6A\x3F\xEB\x9B\xB2\xCB\x75\xC9\xEC\xC8\x10\x07\xD6\x07\x31\x4A" - "\xB1\x30\x97\x82\x58\xA5\x1F\x71\x42\xE6\x56\x07\x99\x57\xB2\xB8\x3B" - "\xA1\x8A\x41\x64\x33\x69\x21\x8C\x2A\x44\x6D\xF2\xA0"; - -@implementation TestsAppDelegate (SHA512HashTests) -- (void)SHA512HashTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSHA512Hash *SHA512, *SHA512Copy; - OFIRI *IRI = [OFIRI IRIWithString: @"embedded:testfile.bin"]; - OFStream *file = [OFIRIHandler openItemAtIRI: IRI mode: @"r"]; - - TEST(@"+[hashWithAllowsSwappableMemory:]", - (SHA512 = [OFSHA512Hash hashWithAllowsSwappableMemory: true])) - - while (!file.atEndOfStream) { - char buffer[128]; - size_t length = [file readIntoBuffer: buffer length: 128]; - [SHA512 updateWithBuffer: buffer length: length]; - } - [file close]; - - TEST(@"-[copy]", (SHA512Copy = [[SHA512 copy] autorelease])) - - TEST(@"-[calculate]", - R([SHA512 calculate]) && R([SHA512Copy calculate])) - - TEST(@"-[digest]", - memcmp(SHA512.digest, testFileSHA512, 64) == 0 && - memcmp(SHA512Copy.digest, testFileSHA512, 64) == 0) - - EXPECT_EXCEPTION(@"Detect invalid call of " - @"-[updateWithBuffer:length:]", OFHashAlreadyCalculatedException, - [SHA512 updateWithBuffer: "" length: 1]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFScryptTests.m Index: tests/OFScryptTests.m ================================================================== --- tests/OFScryptTests.m +++ tests/OFScryptTests.m @@ -1,223 +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 = @"OFScrypt"; -/* Test vectors form RFC 7914 */ -static const unsigned char salsa20Input[64] = { - 0x7E, 0x87, 0x9A, 0x21, 0x4F, 0x3E, 0xC9, 0x86, 0x7C, 0xA9, 0x40, 0xE6, - 0x41, 0x71, 0x8F, 0x26, 0xBA, 0xEE, 0x55, 0x5B, 0x8C, 0x61, 0xC1, 0xB5, - 0x0D, 0xF8, 0x46, 0x11, 0x6D, 0xCD, 0x3B, 0x1D, 0xEE, 0x24, 0xF3, 0x19, - 0xDF, 0x9B, 0x3D, 0x85, 0x14, 0x12, 0x1E, 0x4B, 0x5A, 0xC5, 0xAA, 0x32, - 0x76, 0x02, 0x1D, 0x29, 0x09, 0xC7, 0x48, 0x29, 0xED, 0xEB, 0xC6, 0x8D, - 0xB8, 0xB8, 0xC2, 0x5E -}; -static const unsigned char salsa20Output[64] = { - 0xA4, 0x1F, 0x85, 0x9C, 0x66, 0x08, 0xCC, 0x99, 0x3B, 0x81, 0xCA, 0xCB, - 0x02, 0x0C, 0xEF, 0x05, 0x04, 0x4B, 0x21, 0x81, 0xA2, 0xFD, 0x33, 0x7D, - 0xFD, 0x7B, 0x1C, 0x63, 0x96, 0x68, 0x2F, 0x29, 0xB4, 0x39, 0x31, 0x68, - 0xE3, 0xC9, 0xE6, 0xBC, 0xFE, 0x6B, 0xC5, 0xB7, 0xA0, 0x6D, 0x96, 0xBA, - 0xE4, 0x24, 0xCC, 0x10, 0x2C, 0x91, 0x74, 0x5C, 0x24, 0xAD, 0x67, 0x3D, - 0xC7, 0x61, 0x8F, 0x81 -}; -static const union { - unsigned char uc[128]; - uint32_t u32[32]; -} blockMixInput = { .uc = { - 0xF7, 0xCE, 0x0B, 0x65, 0x3D, 0x2D, 0x72, 0xA4, 0x10, 0x8C, 0xF5, 0xAB, - 0xE9, 0x12, 0xFF, 0xDD, 0x77, 0x76, 0x16, 0xDB, 0xBB, 0x27, 0xA7, 0x0E, - 0x82, 0x04, 0xF3, 0xAE, 0x2D, 0x0F, 0x6F, 0xAD, 0x89, 0xF6, 0x8F, 0x48, - 0x11, 0xD1, 0xE8, 0x7B, 0xCC, 0x3B, 0xD7, 0x40, 0x0A, 0x9F, 0xFD, 0x29, - 0x09, 0x4F, 0x01, 0x84, 0x63, 0x95, 0x74, 0xF3, 0x9A, 0xE5, 0xA1, 0x31, - 0x52, 0x17, 0xBC, 0xD7, - 0x89, 0x49, 0x91, 0x44, 0x72, 0x13, 0xBB, 0x22, 0x6C, 0x25, 0xB5, 0x4D, - 0xA8, 0x63, 0x70, 0xFB, 0xCD, 0x98, 0x43, 0x80, 0x37, 0x46, 0x66, 0xBB, - 0x8F, 0xFC, 0xB5, 0xBF, 0x40, 0xC2, 0x54, 0xB0, 0x67, 0xD2, 0x7C, 0x51, - 0xCE, 0x4A, 0xD5, 0xFE, 0xD8, 0x29, 0xC9, 0x0B, 0x50, 0x5A, 0x57, 0x1B, - 0x7F, 0x4D, 0x1C, 0xAD, 0x6A, 0x52, 0x3C, 0xDA, 0x77, 0x0E, 0x67, 0xBC, - 0xEA, 0xAF, 0x7E, 0x89 -}}; -static const unsigned char blockMixOutput[128] = { - 0xA4, 0x1F, 0x85, 0x9C, 0x66, 0x08, 0xCC, 0x99, 0x3B, 0x81, 0xCA, 0xCB, - 0x02, 0x0C, 0xEF, 0x05, 0x04, 0x4B, 0x21, 0x81, 0xA2, 0xFD, 0x33, 0x7D, - 0xFD, 0x7B, 0x1C, 0x63, 0x96, 0x68, 0x2F, 0x29, 0xB4, 0x39, 0x31, 0x68, - 0xE3, 0xC9, 0xE6, 0xBC, 0xFE, 0x6B, 0xC5, 0xB7, 0xA0, 0x6D, 0x96, 0xBA, - 0xE4, 0x24, 0xCC, 0x10, 0x2C, 0x91, 0x74, 0x5C, 0x24, 0xAD, 0x67, 0x3D, - 0xC7, 0x61, 0x8F, 0x81, - 0x20, 0xED, 0xC9, 0x75, 0x32, 0x38, 0x81, 0xA8, 0x05, 0x40, 0xF6, 0x4C, - 0x16, 0x2D, 0xCD, 0x3C, 0x21, 0x07, 0x7C, 0xFE, 0x5F, 0x8D, 0x5F, 0xE2, - 0xB1, 0xA4, 0x16, 0x8F, 0x95, 0x36, 0x78, 0xB7, 0x7D, 0x3B, 0x3D, 0x80, - 0x3B, 0x60, 0xE4, 0xAB, 0x92, 0x09, 0x96, 0xE5, 0x9B, 0x4D, 0x53, 0xB6, - 0x5D, 0x2A, 0x22, 0x58, 0x77, 0xD5, 0xED, 0xF5, 0x84, 0x2C, 0xB9, 0xF1, - 0x4E, 0xEF, 0xE4, 0x25 -}; -static const unsigned char ROMixInput[128] = { - 0xF7, 0xCE, 0x0B, 0x65, 0x3D, 0x2D, 0x72, 0xA4, 0x10, 0x8C, 0xF5, 0xAB, - 0xE9, 0x12, 0xFF, 0xDD, 0x77, 0x76, 0x16, 0xDB, 0xBB, 0x27, 0xA7, 0x0E, - 0x82, 0x04, 0xF3, 0xAE, 0x2D, 0x0F, 0x6F, 0xAD, 0x89, 0xF6, 0x8F, 0x48, - 0x11, 0xD1, 0xE8, 0x7B, 0xCC, 0x3B, 0xD7, 0x40, 0x0A, 0x9F, 0xFD, 0x29, - 0x09, 0x4F, 0x01, 0x84, 0x63, 0x95, 0x74, 0xF3, 0x9A, 0xE5, 0xA1, 0x31, - 0x52, 0x17, 0xBC, 0xD7, 0x89, 0x49, 0x91, 0x44, 0x72, 0x13, 0xBB, 0x22, - 0x6C, 0x25, 0xB5, 0x4D, 0xA8, 0x63, 0x70, 0xFB, 0xCD, 0x98, 0x43, 0x80, - 0x37, 0x46, 0x66, 0xBB, 0x8F, 0xFC, 0xB5, 0xBF, 0x40, 0xC2, 0x54, 0xB0, - 0x67, 0xD2, 0x7C, 0x51, 0xCE, 0x4A, 0xD5, 0xFE, 0xD8, 0x29, 0xC9, 0x0B, - 0x50, 0x5A, 0x57, 0x1B, 0x7F, 0x4D, 0x1C, 0xAD, 0x6A, 0x52, 0x3C, 0xDA, - 0x77, 0x0E, 0x67, 0xBC, 0xEA, 0xAF, 0x7E, 0x89 -}; -static const unsigned char ROMixOutput[128] = { - 0x79, 0xCC, 0xC1, 0x93, 0x62, 0x9D, 0xEB, 0xCA, 0x04, 0x7F, 0x0B, 0x70, - 0x60, 0x4B, 0xF6, 0xB6, 0x2C, 0xE3, 0xDD, 0x4A, 0x96, 0x26, 0xE3, 0x55, - 0xFA, 0xFC, 0x61, 0x98, 0xE6, 0xEA, 0x2B, 0x46, 0xD5, 0x84, 0x13, 0x67, - 0x3B, 0x99, 0xB0, 0x29, 0xD6, 0x65, 0xC3, 0x57, 0x60, 0x1F, 0xB4, 0x26, - 0xA0, 0xB2, 0xF4, 0xBB, 0xA2, 0x00, 0xEE, 0x9F, 0x0A, 0x43, 0xD1, 0x9B, - 0x57, 0x1A, 0x9C, 0x71, 0xEF, 0x11, 0x42, 0xE6, 0x5D, 0x5A, 0x26, 0x6F, - 0xDD, 0xCA, 0x83, 0x2C, 0xE5, 0x9F, 0xAA, 0x7C, 0xAC, 0x0B, 0x9C, 0xF1, - 0xBE, 0x2B, 0xFF, 0xCA, 0x30, 0x0D, 0x01, 0xEE, 0x38, 0x76, 0x19, 0xC4, - 0xAE, 0x12, 0xFD, 0x44, 0x38, 0xF2, 0x03, 0xA0, 0xE4, 0xE1, 0xC4, 0x7E, - 0xC3, 0x14, 0x86, 0x1F, 0x4E, 0x90, 0x87, 0xCB, 0x33, 0x39, 0x6A, 0x68, - 0x73, 0xE8, 0xF9, 0xD2, 0x53, 0x9A, 0x4B, 0x8E -}; -static const unsigned char testVector1[64] = { - 0x77, 0xD6, 0x57, 0x62, 0x38, 0x65, 0x7B, 0x20, 0x3B, 0x19, 0xCA, 0x42, - 0xC1, 0x8A, 0x04, 0x97, 0xF1, 0x6B, 0x48, 0x44, 0xE3, 0x07, 0x4A, 0xE8, - 0xDF, 0xDF, 0xFA, 0x3F, 0xED, 0xE2, 0x14, 0x42, 0xFC, 0xD0, 0x06, 0x9D, - 0xED, 0x09, 0x48, 0xF8, 0x32, 0x6A, 0x75, 0x3A, 0x0F, 0xC8, 0x1F, 0x17, - 0xE8, 0xD3, 0xE0, 0xFB, 0x2E, 0x0D, 0x36, 0x28, 0xCF, 0x35, 0xE2, 0x0C, - 0x38, 0xD1, 0x89, 0x06 -}; -static const unsigned char testVector2[64] = { - 0xFD, 0xBA, 0xBE, 0x1C, 0x9D, 0x34, 0x72, 0x00, 0x78, 0x56, 0xE7, 0x19, - 0x0D, 0x01, 0xE9, 0xFE, 0x7C, 0x6A, 0xD7, 0xCB, 0xC8, 0x23, 0x78, 0x30, - 0xE7, 0x73, 0x76, 0x63, 0x4B, 0x37, 0x31, 0x62, 0x2E, 0xAF, 0x30, 0xD9, - 0x2E, 0x22, 0xA3, 0x88, 0x6F, 0xF1, 0x09, 0x27, 0x9D, 0x98, 0x30, 0xDA, - 0xC7, 0x27, 0xAF, 0xB9, 0x4A, 0x83, 0xEE, 0x6D, 0x83, 0x60, 0xCB, 0xDF, - 0xA2, 0xCC, 0x06, 0x40 -}; -/* The third test vector is too expensive for m68k. */ -#ifndef OF_M68K -static const unsigned char testVector3[64] = { - 0x70, 0x23, 0xBD, 0xCB, 0x3A, 0xFD, 0x73, 0x48, 0x46, 0x1C, 0x06, 0xCD, - 0x81, 0xFD, 0x38, 0xEB, 0xFD, 0xA8, 0xFB, 0xBA, 0x90, 0x4F, 0x8E, 0x3E, - 0xA9, 0xB5, 0x43, 0xF6, 0x54, 0x5D, 0xA1, 0xF2, 0xD5, 0x43, 0x29, 0x55, - 0x61, 0x3F, 0x0F, 0xCF, 0x62, 0xD4, 0x97, 0x05, 0x24, 0x2A, 0x9A, 0xF9, - 0xE6, 0x1E, 0x85, 0xDC, 0x0D, 0x65, 0x1E, 0x40, 0xDF, 0xCF, 0x01, 0x7B, - 0x45, 0x57, 0x58, 0x87 -}; -#endif -/* The forth test vector is too expensive to include it in the tests. */ -#if 0 -static const unsigned char testVector4[64] = { - 0x21, 0x01, 0xCB, 0x9B, 0x6A, 0x51, 0x1A, 0xAE, 0xAD, 0xDB, 0xBE, 0x09, - 0xCF, 0x70, 0xF8, 0x81, 0xEC, 0x56, 0x8D, 0x57, 0x4A, 0x2F, 0xFD, 0x4D, - 0xAB, 0xE5, 0xEE, 0x98, 0x20, 0xAD, 0xAA, 0x47, 0x8E, 0x56, 0xFD, 0x8F, - 0x4B, 0xA5, 0xD0, 0x9F, 0xFA, 0x1C, 0x6D, 0x92, 0x7C, 0x40, 0xF4, 0xC3, - 0x37, 0x30, 0x40, 0x49, 0xE8, 0xA9, 0x52, 0xFB, 0xCB, 0xF4, 0x5C, 0x6F, - 0xA7, 0x7A, 0x41, 0xA4 -}; -#endif - -@implementation TestsAppDelegate (OFScryptTests) -- (void)scryptTests -{ - void *pool = objc_autoreleasePoolPush(); - uint32_t salsa20Buffer[16]; - uint32_t blockMixBuffer[32]; - uint32_t ROMixBuffer[32], ROMixTmp[17 * 32]; - unsigned char output[64]; - - TEST(@"Salsa20/8 Core", - R(memcpy(salsa20Buffer, salsa20Input, 64)) && - R(OFSalsa20_8Core(salsa20Buffer)) && - memcmp(salsa20Buffer, salsa20Output, 64) == 0) - - TEST(@"Block mix", - R(OFScryptBlockMix(blockMixBuffer, blockMixInput.u32, 1)) && - memcmp(blockMixBuffer, blockMixOutput, 128) == 0) - - TEST(@"ROMix", - R(memcpy(ROMixBuffer, ROMixInput, 128)) && - R(OFScryptROMix(ROMixBuffer, 1, 16, ROMixTmp)) && - memcmp(ROMixBuffer, ROMixOutput, 128) == 0) - - TEST(@"scrypt test vector #1", - R(OFScrypt((OFScryptParameters){ - .blockSize = 1, - .costFactor = 16, - .parallelization = 1, - .salt = (unsigned char *)"", - .saltLength = 0, - .password = "", - .passwordLength = 0, - .key = output, - .keyLength = 64, - .allowsSwappableMemory = true - })) && memcmp(output, testVector1, 64) == 0) - - TEST(@"scrypt test vector #2", - R(OFScrypt((OFScryptParameters){ - .blockSize = 8, - .costFactor = 1024, - .parallelization = 16, - .salt = (unsigned char *)"NaCl", - .saltLength = 4, - .password = "password", - .passwordLength = 8, - .key = output, - .keyLength = 64, - .allowsSwappableMemory = true - })) && memcmp(output, testVector2, 64) == 0) - - /* The third test vector is too expensive for m68k. */ -#ifndef OF_M68K - TEST(@"scrypt test vector #3", - R(OFScrypt((OFScryptParameters){ - .blockSize = 8, - .costFactor = 16384, - .parallelization = 1, - .salt = (unsigned char *)"SodiumChloride", - .saltLength = 14, - .password = "pleaseletmein", - .passwordLength = 13, - .key = output, - .keyLength = 64, - .allowsSwappableMemory = true - })) && memcmp(output, testVector3, 64) == 0) -#endif - - /* The forth test vector is too expensive to include it in the tests. */ -#if 0 - TEST(@"scrypt test vector #4", - R(OFScrypt((OFScryptParameters){ - .blockSize = 8, - .costFactor = 1048576, - .parallelization = 1, - .salt = (unsigned char *)"SodiumChloride", - .saltLength = 14, - .password = "pleaseletmein", - .passwordLength = 13, - .key = output, - .keyLength = 64, - .allowsSwappableMemory = true - })) && memcmp(output, testVector4, 64) == 0) -#endif - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSocketTests.m Index: tests/OFSocketTests.m ================================================================== --- tests/OFSocketTests.m +++ tests/OFSocketTests.m @@ -1,217 +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" - -#import "TestsAppDelegate.h" - -#define COMPARE_V6(a, a0, a1, a2, a3, a4, a5, a6, a7) \ - (a.sockaddr.in6.sin6_addr.s6_addr[0] == (a0 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[1] == (a0 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[2] == (a1 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[3] == (a1 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[4] == (a2 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[5] == (a2 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[6] == (a3 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[7] == (a3 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[8] == (a4 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[9] == (a4 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[10] == (a5 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[11] == (a5 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[12] == (a6 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[13] == (a6 & 0xFF) && \ - a.sockaddr.in6.sin6_addr.s6_addr[14] == (a7 >> 8) && \ - a.sockaddr.in6.sin6_addr.s6_addr[15] == (a7 & 0xFF)) -#define SET_V6(a, a0, a1, a2, a3, a4, a5, a6, a7) \ - a.sockaddr.in6.sin6_addr.s6_addr[0] = a0 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[1] = a0 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[2] = a1 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[3] = a1 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[4] = a2 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[5] = a2 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[6] = a3 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[7] = a3 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[8] = a4 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[9] = a4 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[10] = a5 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[11] = a5 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[12] = a6 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[13] = a6 & 0xFF; \ - a.sockaddr.in6.sin6_addr.s6_addr[14] = a7 >> 8; \ - a.sockaddr.in6.sin6_addr.s6_addr[15] = a7 & 0xFF; - -static OFString *const module = @"OFSocket"; - -@implementation TestsAppDelegate (OFSocketTests) -- (void)socketTests -{ - void *pool = objc_autoreleasePoolPush(); - OFSocketAddress addr; - - TEST(@"Parsing an IPv4", - R(addr = OFSocketAddressParseIP(@"127.0.0.1", 1234)) && - OFFromBigEndian32(addr.sockaddr.in.sin_addr.s_addr) == 0x7F000001 && - OFFromBigEndian16(addr.sockaddr.in.sin_port) == 1234) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #1", OFInvalidFormatException, - OFSocketAddressParseIP(@"127.0.0.0.1", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #2", OFInvalidFormatException, - OFSocketAddressParseIP(@"127.0.0.256", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #3", OFInvalidFormatException, - OFSocketAddressParseIP(@"127.0.0. 1", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #4", OFInvalidFormatException, - OFSocketAddressParseIP(@" 127.0.0.1", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #5", OFInvalidFormatException, - OFSocketAddressParseIP(@"127.0.a.1", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv4 #6", OFInvalidFormatException, - OFSocketAddressParseIP(@"127.0..1", 1234)) - - TEST(@"Port of an IPv4 address", OFSocketAddressIPPort(&addr) == 1234) - - TEST(@"Converting an IPv4 to a string", - [OFSocketAddressString(&addr) isEqual: @"127.0.0.1"]) - - TEST(@"Parsing an IPv6 #1", - R(addr = OFSocketAddressParseIP( - @"1122:3344:5566:7788:99aa:bbCc:ddee:ff00", 1234)) && - COMPARE_V6(addr, - 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #2", - R(addr = OFSocketAddressParseIP(@"::", 1234)) && - COMPARE_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #3", - R(addr = OFSocketAddressParseIP(@"aaAa::bBbb", 1234)) && - COMPARE_V6(addr, 0xAAAA, 0, 0, 0, 0, 0, 0, 0xBBBB) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #4", - R(addr = OFSocketAddressParseIP(@"aaAa::", 1234)) && - COMPARE_V6(addr, 0xAAAA, 0, 0, 0, 0, 0, 0, 0) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #5", - R(addr = OFSocketAddressParseIP(@"::aaAa", 1234)) && - COMPARE_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0xAAAA) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #6", - R(addr = OFSocketAddressParseIP(@"fd00::1%123", 1234)) && - COMPARE_V6(addr, 0xFD00, 0, 0, 0, 0, 0, 0, 1) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234 && - addr.sockaddr.in6.sin6_scope_id == 123) - - TEST(@"Parsing an IPv6 #7", - R(addr = OFSocketAddressParseIP(@"::ffff:127.0.0.1", 1234)) && - COMPARE_V6(addr, 0, 0, 0, 0, 0, 0xFFFF, 0x7F00, 1) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - TEST(@"Parsing an IPv6 #8", - R(addr = OFSocketAddressParseIP(@"64:ff9b::127.0.0.1", 1234)) && - COMPARE_V6(addr, 0x64, 0xFF9B, 0, 0, 0, 0, 0x7F00, 1) && - OFFromBigEndian16(addr.sockaddr.in6.sin6_port) == 1234) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #1", OFInvalidFormatException, - OFSocketAddressParseIP(@"1:::2", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #2", OFInvalidFormatException, - OFSocketAddressParseIP(@"1: ::2", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #3", OFInvalidFormatException, - OFSocketAddressParseIP(@"1:: :2", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #4", OFInvalidFormatException, - OFSocketAddressParseIP(@"1::2::3", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #5", OFInvalidFormatException, - OFSocketAddressParseIP(@"10000::1", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #6", OFInvalidFormatException, - OFSocketAddressParseIP(@"::10000", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #7", OFInvalidFormatException, - OFSocketAddressParseIP(@"::1::", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #8", OFInvalidFormatException, - OFSocketAddressParseIP(@"1:2:3:4:5:6:7:", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #9", OFInvalidFormatException, - OFSocketAddressParseIP(@"1:2:3:4:5:6:7::", 1234)) - - EXPECT_EXCEPTION(@"Refusing invalid IPv6 #10", OFInvalidFormatException, - OFSocketAddressParseIP(@"1:2", 1234)) - - TEST(@"Port of an IPv6 address", OFSocketAddressIPPort(&addr) == 1234) - - addr.sockaddr.in6.sin6_scope_id = 0; - - SET_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0) - TEST(@"Converting an IPv6 to a string #1", - [OFSocketAddressString(&addr) isEqual: @"::"]) - - SET_V6(addr, 0, 0, 0, 0, 0, 0, 0, 1) - TEST(@"Converting an IPv6 to a string #2", - [OFSocketAddressString(&addr) isEqual: @"::1"]) - - SET_V6(addr, 1, 0, 0, 0, 0, 0, 0, 0) - TEST(@"Converting an IPv6 to a string #3", - [OFSocketAddressString(&addr) isEqual: @"1::"]) - - SET_V6(addr, - 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) - TEST(@"Converting an IPv6 to a string #4", - [OFSocketAddressString(&addr) - isEqual: @"1122:3344:5566:7788:99aa:bbcc:ddee:ff00"]) - - SET_V6(addr, 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0) - TEST(@"Converting an IPv6 to a string #5", - [OFSocketAddressString(&addr) - isEqual: @"1122:3344:5566:7788:99aa:bbcc:ddee:0"]) - - SET_V6(addr, 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0) - TEST(@"Converting an IPv6 to a string #6", - [OFSocketAddressString(&addr) - isEqual: @"1122:3344:5566:7788:99aa:bbcc::"]) - - SET_V6(addr, 0, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) - TEST(@"Converting an IPv6 to a string #7", - [OFSocketAddressString(&addr) - isEqual: @"0:3344:5566:7788:99aa:bbcc:ddee:ff00"]) - - SET_V6(addr, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) - TEST(@"Converting an IPv6 to a string #8", - [OFSocketAddressString(&addr) - isEqual: @"::5566:7788:99aa:bbcc:ddee:ff00"]) - - SET_V6(addr, 0, 0, 0x5566, 0, 0, 0, 0xDDEE, 0xFF00) - TEST(@"Converting an IPv6 to a string #9", - [OFSocketAddressString(&addr) isEqual: @"0:0:5566::ddee:ff00"]) - - SET_V6(addr, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0) - TEST(@"Converting an IPv6 to a string #10", - [OFSocketAddressString(&addr) - isEqual: @"::5566:7788:99aa:bbcc:0:0"]) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFSubprocessTests.m Index: tests/OFSubprocessTests.m ================================================================== --- tests/OFSubprocessTests.m +++ tests/OFSubprocessTests.m @@ -1,57 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFSubprocess"; - -@implementation TestsAppDelegate (OFSubprocessTests) -- (void)subprocessTests -{ - void *pool = objc_autoreleasePoolPush(); -#ifdef OF_HAVE_FILES - OFString *program = [@"subprocess" stringByAppendingPathComponent: - @"subprocess" @PROG_SUFFIX]; -#else - OFString *program = @"subprocess/subprocess" @PROG_SUFFIX; -#endif - OFArray *arguments = [OFArray arrayWithObjects: @"tést", @"123", nil]; - OFMutableDictionary *environment = - [[[OFApplication environment] mutableCopy] autorelease]; - OFSubprocess *subprocess; - - [environment setObject: @"yés" forKey: @"tëst"]; - - TEST(@"+[subprocessWithProgram:programName:arguments:environment]", - (subprocess = - [OFSubprocess subprocessWithProgram: program - programName: program - arguments: arguments - environment: environment])) - - TEST(@"Standard input", R([subprocess writeLine: @"Hellö world!"])) - - TEST(@"Standard output", - [[subprocess readLine] isEqual: @"HELLÖ WORLD!"]) - - TEST(@"-[closeForWriting]", R([subprocess closeForWriting])) - - TEST(@"-[waitForTermination]", [subprocess waitForTermination] == 0) - - objc_autoreleasePoolPop(pool); -} -@end DELETED tests/OFThreadTests.m Index: tests/OFThreadTests.m ================================================================== --- tests/OFThreadTests.m +++ tests/OFThreadTests.m @@ -1,53 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFThread"; - -@interface TestThread: OFThread -@end - -@implementation TestThread -- (id)main -{ - [[OFThread threadDictionary] setObject: @"bar" forKey: @"foo"]; - OFEnsure([[[OFThread threadDictionary] - objectForKey: @"foo"] isEqual: @"bar"]); - - return @"success"; -} -@end - -@implementation TestsAppDelegate (OFThreadTests) -- (void)threadTests -{ - void *pool = objc_autoreleasePoolPush(); - TestThread *thread; - - TEST(@"+[thread]", (thread = [TestThread thread])) - - TEST(@"-[start]", R([thread start])) - - TEST(@"-[join]", [[thread join] isEqual: @"success"]) - - TEST(@"-[threadDictionary]", - [[OFThread threadDictionary] objectForKey: @"foo"] == nil) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -57,26 +57,22 @@ - (void)outputTesting: (OFString *)test inModule: (OFString *)module; - (void)outputSuccess: (OFString *)test inModule: (OFString *)module; - (void)outputFailure: (OFString *)test inModule: (OFString *)module; @end -@interface TestsAppDelegate (OFArrayTests) -- (void)arrayTests; +@interface TestsAppDelegate (OFASN1DERParsingTests) +- (void)ASN1DERParsingTests; +@end + +@interface TestsAppDelegate (OFASN1DERRepresentationTests) +- (void)ASN1DERRepresentationTests; @end @interface TestsAppDelegate (OFBlockTests) - (void)blockTests; @end -@interface TestsAppDelegate (OFCharacterSetTests) -- (void)characterSetTests; -@end - -@interface TestsAppDelegate (OFColorTests) -- (void)colorTests; -@end - @interface TestsAppDelegate (OFDDPSocketTests) - (void)DDPSocketTests; @end @interface TestsAppDelegate (OFDNSResolverTests) @@ -85,14 +81,10 @@ @interface TestsAppDelegate (OFDataTests) - (void)dataTests; @end -@interface TestsAppDelegate (OFDateTests) -- (void)dateTests; -@end - @interface TestsAppDelegate (OFDictionaryTests) - (void)dictionaryTests; @end @interface TestsAppDelegate (ForwardingTests) @@ -109,34 +101,14 @@ @interface TestsAppDelegate (OFHTTPCookieManagerTests) - (void)HTTPCookieManagerTests; @end -@interface TestsAppDelegate (OFINIFileTests) -- (void)INIFileTests; -@end - -@interface TestsAppDelegate (OFIRITests) -- (void)IRITests; -@end - @interface TestsAppDelegate (OFIPXSocketTests) - (void)IPXSocketTests; @end -@interface TestsAppDelegate (OFInvocationTests) -- (void)invocationTests; -@end - -@interface TestsAppDelegate (OFJSONTests) -- (void)JSONTests; -@end - -@interface TestsAppDelegate (OFHMACTests) -- (void)HMACTests; -@end - @interface TestsAppDelegate (OFKernelEventObserverTests) - (void)kernelEventObserverTests; @end @interface TestsAppDelegate (OFListTests) @@ -145,86 +117,30 @@ @interface TestsAppDelegate (OFLocaleTests) - (void)localeTests; @end -@interface TestsAppDelegate (OFMD5HashTests) -- (void)MD5HashTests; -@end - -@interface TestsAppDelegate (OFMatrix4x4Tests) -- (void)matrix4x4Tests; -@end - @interface TestsAppDelegate (OFMemoryStreamTests) - (void)memoryStreamTests; @end -@interface TestsAppDelegate (OFMethodSignatureTests) -- (void)methodSignatureTests; -@end - @interface TestsAppDelegate (OFNotificationCenterTests) - (void)notificationCenterTests; @end -@interface TestsAppDelegate (OFNumberTests) -- (void)numberTests; -@end - @interface TestsAppDelegate (OFObjectTests) - (void)objectTests; @end -@interface TestsAppDelegate (OFPBKDF2Tests) -- (void)PBKDF2Tests; -@end - -@interface TestsAppDelegate (OFPropertyListTests) -- (void)propertyListTests; -@end - -@interface TestsAppDelegate (OFPluginTests) -- (void)pluginTests; -@end - @interface TestsAppDelegate (RuntimeTests) - (void)runtimeTests; @end @interface TestsAppDelegate (RuntimeARCTests) - (void)runtimeARCTests; @end -@interface TestsAppDelegate (OFRIPEMD160HashTests) -- (void)RIPEMD160HashTests; -@end - -@interface TestsAppDelegate (OFScryptTests) -- (void)scryptTests; -@end - -@interface TestsAppDelegate (OFSHA1HashTests) -- (void)SHA1HashTests; -@end - -@interface TestsAppDelegate (OFSHA224HashTests) -- (void)SHA224HashTests; -@end - -@interface TestsAppDelegate (OFSHA256HashTests) -- (void)SHA256HashTests; -@end - -@interface TestsAppDelegate (OFSHA384HashTests) -- (void)SHA384HashTests; -@end - -@interface TestsAppDelegate (OFSHA512HashTests) -- (void)SHA512HashTests; -@end - @interface TestsAppDelegate (OFSPXSocketTests) - (void)SPXSocketTests; @end @interface TestsAppDelegate (OFSPXStreamSocketTests) @@ -237,34 +153,22 @@ @interface TestsAppDelegate (OFSystemInfoTests) - (void)systemInfoTests; @end -@interface TestsAppDelegate (OFSocketTests) -- (void)socketTests; -@end - @interface TestsAppDelegate (OFStreamTests) - (void)streamTests; @end @interface TestsAppDelegate (OFStringTests) - (void)stringTests; @end -@interface TestsAppDelegate (OFSubprocessTests) -- (void)subprocessTests; -@end - @interface TestsAppDelegate (OFTCPSocketTests) - (void)TCPSocketTests; @end -@interface TestsAppDelegate (OFThreadTests) -- (void)threadTests; -@end - @interface TestsAppDelegate (OFUDPSocketTests) - (void)UDPSocketTests; @end @interface TestsAppDelegate (OFUNIXDatagramSocketTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -373,45 +373,24 @@ [self runtimeTests]; #ifdef COMPILER_SUPPORTS_ARC [self runtimeARCTests]; #endif [self objectTests]; - [self methodSignatureTests]; - [self invocationTests]; [self forwardingTests]; #ifdef OF_HAVE_BLOCKS [self blockTests]; #endif [self stringTests]; - [self characterSetTests]; [self dataTests]; - [self arrayTests]; [self dictionaryTests]; [self listTests]; [self setTests]; - [self dateTests]; [self valueTests]; - [self numberTests]; - [self colorTests]; [self streamTests]; [self memoryStreamTests]; [self notificationCenterTests]; - [self MD5HashTests]; - [self RIPEMD160HashTests]; - [self SHA1HashTests]; - [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 - [self socketTests]; [self TCPSocketTests]; [self UDPSocketTests]; # ifdef OF_HAVE_UNIX_SOCKETS [self UNIXDatagramSocketTests]; [self UNIXStreamSocketTests]; @@ -424,14 +403,10 @@ # ifdef OF_HAVE_APPLETALK [self DDPSocketTests]; # endif [self kernelEventObserverTests]; #endif -#ifdef OF_HAVE_THREADS - [self threadTests]; -#endif - [self IRITests]; #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_THREADS) [self HTTPClientTests]; #endif #ifdef OF_HAVE_SOCKETS [self HTTPCookieTests]; @@ -438,20 +413,13 @@ [self HTTPCookieManagerTests]; #endif [self XMLParserTests]; [self XMLNodeTests]; [self XMLElementBuilderTests]; - [self JSONTests]; - [self propertyListTests]; - [self matrix4x4Tests]; - -#ifdef OF_HAVE_PLUGINS - [self pluginTests]; -#endif -#ifdef OF_HAVE_SUBPROCESSES - [self subprocessTests]; -#endif + [self ASN1DERParsingTests]; + [self ASN1DERRepresentationTests]; + #ifdef OF_WINDOWS [self windowsRegistryKeyTests]; #endif #ifdef OF_HAVE_SOCKETS DELETED tests/plugin/Info.plist.in Index: tests/plugin/Info.plist.in ================================================================== --- tests/plugin/Info.plist.in +++ tests/plugin/Info.plist.in @@ -1,22 +0,0 @@ - - - - - CFBundleExecutable - TestPlugin - CFBundleName - TestPlugin - CFBundleIdentifier - im.nil.objfw.tests.plugin - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - BNDL - CFBundleVersion - @BUNDLE_VERSION@ - CFBundleShortVersionString - @BUNDLE_SHORT_VERSION@ - MinimumOSVersion - 9.0 - - DELETED tests/plugin/Makefile Index: tests/plugin/Makefile ================================================================== --- tests/plugin/Makefile +++ tests/plugin/Makefile @@ -1,11 +0,0 @@ -DISTCLEAN = Info.plist - -PLUGIN_NOINST = TestPlugin${PLUGIN_SUFFIX} -SRCS = TestPlugin.m - -include ../../buildsys.mk -include ../../extra.mk - -CPPFLAGS += -I../.. -I../../src -I../../src/runtime -LIBS := ${TESTPLUGIN_LIBS} ${LIBS} -LD = ${OBJC} DELETED tests/plugin/TestPlugin.h Index: tests/plugin/TestPlugin.h ================================================================== --- tests/plugin/TestPlugin.h +++ tests/plugin/TestPlugin.h @@ -1,20 +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. - */ - -#import "OFObject.h" - -@interface TestPlugin: OFObject -- (int)test: (int)num; -@end DELETED tests/plugin/TestPlugin.m Index: tests/plugin/TestPlugin.m ================================================================== --- tests/plugin/TestPlugin.m +++ tests/plugin/TestPlugin.m @@ -1,51 +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" - -#import "TestPlugin.h" - -#ifdef OF_OBJFW_RUNTIME -# import "runtime/private.h" - -OF_DESTRUCTOR() -{ - Class class = objc_getClass("TestPlugin"); - - if (class == Nil) - /* - * musl has broken dlclose(): Instead of calling the destructor - * on dlclose(), they call it on exit(). This of course means - * that our tests might have already called objc_deinit() and - * the class is already gone. - */ - return; - - objc_unregisterClass(class); -} -#endif - -@implementation TestPlugin -- (int)test: (int)num -{ - return num * 2; -} -@end - -Class -class(void) -{ - return [TestPlugin class]; -} DELETED tests/subprocess/Makefile Index: tests/subprocess/Makefile ================================================================== --- tests/subprocess/Makefile +++ tests/subprocess/Makefile @@ -1,9 +0,0 @@ -PROG_NOINST = subprocess${PROG_SUFFIX} -SRCS = Subprocess.m - -include ../../buildsys.mk -include ../../extra.mk - -CPPFLAGS += -I../../src -I../../src/exceptions -I../../src/runtime -I../.. -LIBS := -L../../src -lobjfw -L../../src/runtime ${RUNTIME_LIBS} ${LIBS} -LD = ${OBJC} DELETED tests/subprocess/Subprocess.m Index: tests/subprocess/Subprocess.m ================================================================== --- tests/subprocess/Subprocess.m +++ tests/subprocess/Subprocess.m @@ -1,43 +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" - -#import "ObjFW.h" - -@interface Subprocess: OFObject -@end - -OF_APPLICATION_DELEGATE(Subprocess) - -@implementation Subprocess -- (void)applicationDidFinishLaunching: (OFNotification *)notification -{ - OFString *line; - - if (![[OFApplication arguments] isEqual: - [OFArray arrayWithObjects: @"tést", @"123", nil]]) - [OFApplication terminateWithStatus: 1]; - - if (![[[OFApplication environment] objectForKey: @"tëst"] - isEqual: @"yés"]) - [OFApplication terminateWithStatus: 2]; - - while ((line = [OFStdIn readLine]) != nil) - [OFStdOut writeLine: line.uppercaseString]; - - [OFApplication terminate]; -} -@end DELETED tests/testfile.bin Index: tests/testfile.bin ================================================================== --- tests/testfile.bin +++ tests/testfile.bin cannot compute difference between binary files DELETED tests/testfile.ini Index: tests/testfile.ini ================================================================== --- tests/testfile.ini +++ tests/testfile.ini @@ -1,21 +0,0 @@ -[tests] -foo = bar -foobar=baz -;comment - -[foobar] -;foobarcomment -qux=" asd" -"quxqux " = asd -quxquxqux="hello\"w”rld" -qux2="a\f" - -[types] -integer = 0x20 -bool = true -float = 0.5 -array1 = 1 -array2 = 1 -double = 0.25 -array1 = 2 -array2 = 2