New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-25727] iOS/Android: Fix Base64 behavior and accepted parameter types #9773
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already have a manual example in the JIRA ticket, please add it to our Base64 unit tests.
Should be pretty easy to add a few tests for improperly padded encoded strings and verify we handle them and decode them as expected.
iphone/Classes/UtilsModule.m
Outdated
[str appendString:@"="]; | ||
} | ||
|
||
if ([str rangeOfString:@"="].length > 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't you want to check the string before adding padding?
iphone/Classes/UtilsModule.m
Outdated
NSMutableString *str = [[NSMutableString alloc] initWithString:[[self convertToString:args] | ||
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; | ||
// Fill padding for backwards compatibility | ||
while ([str length] % 4 != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is right. Base64 encoded strings will only have one or two '=' characters as padding, which suggest modulo 3, not 4.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RFC sounds helpful:
(1) the final quantum of encoding input is an integral multiple of 24
bits; here, the final unit of encoded output will be an integral
multiple of 4 characters with no "=" padding,
(2) the final quantum of encoding input is exactly 8 bits; here, the
final unit of encoded output will be two characters followed by two
"=" padding characters, or
(3) the final quantum of encoding input is exactly 16 bits; here, the
final unit of encoded output will be three characters followed by one
"=" padding character.
Some possible unit test examples:
|
@sgtcoolguy Fixed the iOS padding for all cases, verified the unit-tests and fixed a few more of them (all iOS and Android ones). iOS now accepts the "TiFile" parameter as well and Android does for the decoding as well (only worked for encoding on Android). Test-case is also listed on JIRA. |
39983a2
to
70b1756
Compare
Generated by 🚫 dangerJS |
# Conflicts: # android/modules/utils/src/java/ti/modules/titanium/utils/UtilsModule.java
apidoc/Titanium/Utils/Utils.yml
Outdated
|
||
- name: base64encode | ||
summary: Returns the specified data encoded to Base64. | ||
description: | | ||
Starting from Titanium 3.3.0, `obj` can be a [File](Titanium.Filesystem.File) object | ||
on Android. | ||
The parameter can be a String, Ti.Blob or Ti.File (iOS and Android only). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May still want to retain note that Ti.Filesystem/File is supported on Android 3.3+, iOS 7.0.3+ (or whatever version this PR and cherry-picks gets into).
}); | ||
|
||
// FIXME Windows gives: 'base64decode: attempt to decode a value not in base64 char set' | ||
it.windowsBroken('#base64decode(Ti.Blob)', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for #base64decode(Ti.Filesystem.File)
should(test.getText()).be.eql('dGVzdA=='); | ||
}); | ||
|
||
it('#base64encode(Ti.Blob)', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test for #base64encode(Ti.Filesystem.File)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already in https://github.com/appcelerator/titanium_mobile/pull/9773/files#diff-6384f93cac05d6aea1061d799b0fe88aR67. Other test is added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I'd add tests for base64encode/decode with a Ti.Filesystem.File argument as well for completeness (we already have Ti.Blob test variants, so just pass the file in rather than convert the file to a Ti.Blob first)
FR Passed on 7_0_X branch |
JIRA: https://jira.appcelerator.org/browse/TIMOB-25727