Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -884,5 +884,11 @@ /** * An OFException indicating that unlocking a mutex failed. */ @interface OFMutexUnlockFailedException: OFException {} @end + +/** + * An OFException indicating that the hash has already been calculated. + */ +@interface OFHashAlreadyCalculatedException: OFException {} +@end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -1263,8 +1263,22 @@ return string; string = [[OFString alloc] initWithFormat: @"A mutex could not be unlocked in class %s", [class_ className]]; + return string; +} +@end + +@implementation OFHashAlreadyCalculatedException +- (OFString*)string +{ + if (string != nil) + return string; + + string = [[OFString alloc] initWithFormat: + @"The hash has already been calculated in class %s and thus no new " + @"data can be added", [class_ className]]; + return string; } @end Index: src/OFHashes.m ================================================================== --- src/OFHashes.m +++ src/OFHashes.m @@ -143,12 +143,11 @@ if (size == 0) return self; if (calculated) - /* FIXME: Maybe a new exception would be better */ - @throw [OFInvalidArgumentException newWithClass: isa]; + @throw [OFHashAlreadyCalculatedException newWithClass: isa]; /* Update bitcount */ t = bits[0]; if ((bits[0] = t + ((uint32_t)size << 3)) < t) /* Carry from low to high */ @@ -382,12 +381,11 @@ { if (size == 0) return self; if (calculated) - /* FIXME: Maybe a new exception would be better */ - @throw [OFInvalidArgumentException newWithClass: isa]; + @throw [OFHashAlreadyCalculatedException newWithClass: isa]; sha1_update(state, &count, buffer, buf, size); return self; } Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -45,11 +45,10 @@ forTLSKey: (OFTLSKey*)key { id old = of_tlskey_get(key->key); if (!of_tlskey_set(key->key, [obj retain])) - /* FIXME: Maybe another exception would be better */ @throw [OFInvalidArgumentException newWithClass: self selector: _cmd]; [old release]; Index: tests/OFHashes.m ================================================================== --- tests/OFHashes.m +++ tests/OFHashes.m @@ -54,8 +54,15 @@ [f close]; TEST(@"-[digest]", !memcmp([md5 digest], testfile_md5, OF_MD5_DIGEST_SIZE) && !memcmp([sha1 digest], testfile_sha1, OF_MD5_DIGEST_SIZE)) + + EXPECT_EXCEPTION(@"Detect invalid call of -[updateWithBuffer] #1", + OFHashAlreadyCalculatedException, [md5 updateWithBuffer: "" + ofSize: 1]) + EXPECT_EXCEPTION(@"Detect invalid call of -[updateWithBuffer] #2", + OFHashAlreadyCalculatedException, [sha1 updateWithBuffer: "" + ofSize: 1]) [pool release]; }