From 32c3552c3399740c8cbc4e00ca5714fdfa1842a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Isnard?= Date: Tue, 16 Apr 2024 15:32:03 +0200 Subject: [PATCH] BREAKING: Convert prehashPassword from exception to errors Required for compat with Swift, which doesn't have exceptions --- Tanker/Headers/TKRTanker.h | 2 +- Tanker/Sources/TKRTanker.m | 9 ++++++--- Tanker/Tests/UnitTests.swift | 11 +++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Tanker/Headers/TKRTanker.h b/Tanker/Headers/TKRTanker.h index cb5d0b6..42e2655 100644 --- a/Tanker/Headers/TKRTanker.h +++ b/Tanker/Headers/TKRTanker.h @@ -39,7 +39,7 @@ + (nonnull NSString*)nativeVersionString; -+ (nonnull NSString*)prehashPassword:(nonnull NSString*)password; ++ (nullable NSString*)prehashPassword:(nonnull NSString*)password err:(NSError**)errResult; + (void)connectLogHandler:(nonnull TKRLogHandler)handler; diff --git a/Tanker/Sources/TKRTanker.m b/Tanker/Sources/TKRTanker.m index 05c557a..e6a8d8c 100644 --- a/Tanker/Sources/TKRTanker.m +++ b/Tanker/Sources/TKRTanker.m @@ -218,10 +218,13 @@ + (nonnull NSString*)nativeVersionString return [NSString stringWithCString:tanker_version_string() encoding:NSUTF8StringEncoding]; } -+ (nonnull NSString*)prehashPassword:(nonnull NSString*)password ++ (nullable NSString*)prehashPassword:(nonnull NSString*)password err:(NSError**)errResult { - if (!password.length) - [NSException raise:NSInvalidArgumentException format:@"cannot hash empty password"]; + if (!password.length) { + if (errResult != nil) + *errResult = TKR_createNSError(TKRErrorInvalidArgument, @"cannot hash empty password"); + return nil; + } char const* c_password = [password cStringUsingEncoding:NSUTF8StringEncoding]; tanker_expected_t* expected_chashed = tanker_prehash_password(c_password); diff --git a/Tanker/Tests/UnitTests.swift b/Tanker/Tests/UnitTests.swift index 61f848b..cf857df 100644 --- a/Tanker/Tests/UnitTests.swift +++ b/Tanker/Tests/UnitTests.swift @@ -12,18 +12,21 @@ class UnitTests: QuickSpec { describe("prehashPassword") { it("should fail to hash an empty password") { expect { - TKRTanker.prehashPassword("") - }.to(raiseException(named: "NSInvalidArgumentException")) + try TKRTanker.prehashPassword("") + }.to(throwError { (error: NSError) in + expect(error.domain).to(equal(TKRErrorDomain)); + expect(error.code) == TKRError.invalidArgument.rawValue; + }) } it("should hash a test vector 1") { - let hashed = TKRTanker.prehashPassword("super secretive password"); + let hashed = try! TKRTanker.prehashPassword("super secretive password"); let expected = "UYNRgDLSClFWKsJ7dl9uPJjhpIoEzadksv/Mf44gSHI="; expect(hashed) == expected; } it("should hash a test vector 2") { - let hashed = TKRTanker.prehashPassword("test éå 한국어 😃"); + let hashed = try! TKRTanker.prehashPassword("test éå 한국어 😃"); let expected = "Pkn/pjub2uwkBDpt2HUieWOXP5xLn0Zlen16ID4C7jI="; expect(hashed) == expected; }