From 4a8042a473e5fc02862007f629a737d62ac23365 Mon Sep 17 00:00:00 2001 From: Max Lohrmann Date: Sat, 20 May 2017 00:02:39 +0200 Subject: Some changes to PR #2795 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adjusted code style * Added unit tests * Rewrote a function to be easier to understand * Added support for MySQL X’val’ style hex literal --- UnitTests/SPDataAdditionsTests.m | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'UnitTests') diff --git a/UnitTests/SPDataAdditionsTests.m b/UnitTests/SPDataAdditionsTests.m index 4769e499..0491d4aa 100644 --- a/UnitTests/SPDataAdditionsTests.m +++ b/UnitTests/SPDataAdditionsTests.m @@ -41,6 +41,8 @@ - (void)testDataDecryptedWithPassword; - (void)testDataDecryptedWithKey; - (void)testEnumerateLinesBreakingAt_withBlock; +- (void)testDataWithHexString; +- (void)testDataWithHexStringMySQL; @end @@ -384,4 +386,78 @@ } } +- (void)testDataWithHexString +{ + //nil + { + XCTAssertNil([NSData dataWithHexString:nil], @"nil input"); + } + //empty + { + XCTAssertTrue([[NSData dataWithHexString:@""] length] == 0, @"empty input"); + } + //single byte 0 + { + const char single[] = {0}; + XCTAssertEqualObjects([NSData dataWithHexString:@"0"], [NSData dataWithBytes:single length:1], @"single '0'" ); + } + //empty, with 0x + { + XCTAssertEqualObjects([NSData dataWithHexString:@" 0x "], [NSData data], @"empty input after trimming"); + } + //one lower nibble + { + const char single[] = { 0xf }; + XCTAssertEqualObjects([NSData dataWithHexString:@"0xf"], [NSData dataWithBytes:single length:1], @"0x0F"); + } + //full char, uppercase + { + const char single[] = { 0xcf }; + XCTAssertEqualObjects([NSData dataWithHexString:@"CF"], [NSData dataWithBytes:single length:1], @"0xCF"); + } + //regular input + { + NSString *inp = @"0x de AD Be eF\t0102 0304"; + const char exp[] = {0xde,0xad,0xbe,0xef,0x01,0x02,0x03,0x04}; + XCTAssertEqualObjects([NSData dataWithHexString:inp], [NSData dataWithBytes:exp length:sizeof(exp)], @"regular input"); + } + //invalid input + { + XCTAssertNil([NSData dataWithHexString:@"0xaG"], @"invalid char in input"); + } +} + +- (void)testDataWithHexStringMySQL +{ + //empty + { + XCTAssertEqualObjects([NSData dataWithHexString:@"x''"], [NSData data], @"empty mysql hex literal"); + } + //empty, whitespace around, capital x + { + XCTAssertEqualObjects([NSData dataWithHexString:@" X''\t "], [NSData data], @"empty mysql hex literal (2)"); + } + //nonempty valid, case-insensitive + { + const char exp[] = {0xde,0xad,0xbe,0xef}; + XCTAssertEqualObjects([NSData dataWithHexString:@"X'deADBeeF'"], [NSData dataWithBytes:exp length:sizeof(exp)], @"regular input"); + } + //bad: uneven + { + XCTAssertNil([NSData dataWithHexString:@"X'aFF'"],@"uneven length in mysql hex literal"); + } + //bad: whitespace inside literal + { + XCTAssertNil([NSData dataWithHexString:@"x'0A ff'"], @"whitespace inside mysql hex literal"); + } + //bad: non-whitespace after literal + { + XCTAssertNil([NSData dataWithHexString:@"X'1234' ."], @"garbage at end"); + } + //bad: non hex char in literal + { + XCTAssertNil([NSData dataWithHexString:@"x'01äß'"], @"non-hex char in literal"); + } +} + @end -- cgit v1.2.3