aboutsummaryrefslogtreecommitdiffstats
path: root/UnitTests
diff options
context:
space:
mode:
authorMax Lohrmann <dmoagx@users.noreply.github.com>2017-05-20 00:02:39 +0200
committerMax Lohrmann <dmoagx@users.noreply.github.com>2017-05-20 00:02:39 +0200
commit4a8042a473e5fc02862007f629a737d62ac23365 (patch)
tree98855f4e19b7d4d9eae2443a3e9e42212e6cbe1f /UnitTests
parent0116bcecf0b1ebb66be0179a1a8e73670814dee1 (diff)
downloadsequelpro-4a8042a473e5fc02862007f629a737d62ac23365.tar.gz
sequelpro-4a8042a473e5fc02862007f629a737d62ac23365.tar.bz2
sequelpro-4a8042a473e5fc02862007f629a737d62ac23365.zip
Some changes to PR #2795
* Adjusted code style * Added unit tests * Rewrote a function to be easier to understand * Added support for MySQL X’val’ style hex literal
Diffstat (limited to 'UnitTests')
-rw-r--r--UnitTests/SPDataAdditionsTests.m76
1 files changed, 76 insertions, 0 deletions
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