Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit af8cf8c

Browse files
committed
Guard against null values for paymentDiscount
1 parent d8b6533 commit af8cf8c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

packages/in_app_purchase/in_app_purchase_ios/example/ios/RunnerTests/TranslatorTests.m

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,74 @@ - (void)testSKPaymentDiscountFromMap {
200200
}
201201
}
202202

203+
- (void)testSKPaymentDiscountFromMapMissingIdentifier {
204+
if (@available(iOS 12.2, *)) {
205+
NSDictionary* discountMap = @{
206+
@"keyIdentifier" : @"payment_discount_key_identifier",
207+
@"nonce" : @"d18981e0-9003-4365-98a2-4b90e3b62c52",
208+
@"signature" : @"this is a encrypted signature",
209+
@"timestamp" : @([NSDate date].timeIntervalSince1970),
210+
};
211+
212+
XCTAssertThrows(
213+
[FIAObjectTranslator getSKPaymentDiscountFromMap:discountMap]);
214+
}
215+
}
216+
217+
- (void)testSKPaymentDiscountFromMapMissingKeyIdentifier {
218+
if (@available(iOS 12.2, *)) {
219+
NSDictionary* discountMap = @{
220+
@"identifier" : @"payment_discount_identifier",
221+
@"nonce" : @"d18981e0-9003-4365-98a2-4b90e3b62c52",
222+
@"signature" : @"this is a encrypted signature",
223+
@"timestamp" : @([NSDate date].timeIntervalSince1970),
224+
};
225+
226+
XCTAssertThrows(
227+
[FIAObjectTranslator getSKPaymentDiscountFromMap:discountMap]);
228+
}
229+
}
230+
231+
- (void)testSKPaymentDiscountFromMapMissingNonce {
232+
if (@available(iOS 12.2, *)) {
233+
NSDictionary* discountMap = @{
234+
@"identifier" : @"payment_discount_identifier",
235+
@"keyIdentifier" : @"payment_discount_key_identifier",
236+
@"signature" : @"this is a encrypted signature",
237+
@"timestamp" : @([NSDate date].timeIntervalSince1970),
238+
};
239+
240+
XCTAssertThrows(
241+
[FIAObjectTranslator getSKPaymentDiscountFromMap:discountMap]);
242+
}
243+
}
244+
245+
- (void)testSKPaymentDiscountFromMapMissingSignature {
246+
if (@available(iOS 12.2, *)) {
247+
NSDictionary* discountMap = @{
248+
@"identifier" : @"payment_discount_identifier",
249+
@"keyIdentifier" : @"payment_discount_key_identifier",
250+
@"nonce" : @"d18981e0-9003-4365-98a2-4b90e3b62c52",
251+
@"timestamp" : @([NSDate date].timeIntervalSince1970),
252+
};
253+
254+
XCTAssertThrows(
255+
[FIAObjectTranslator getSKPaymentDiscountFromMap:discountMap]);
256+
}
257+
}
258+
259+
- (void)testSKPaymentDiscountFromMapMissingTimestamp {
260+
if (@available(iOS 12.2, *)) {
261+
NSDictionary* discountMap = @{
262+
@"identifier" : @"payment_discount_identifier",
263+
@"keyIdentifier" : @"payment_discount_key_identifier",
264+
@"nonce" : @"d18981e0-9003-4365-98a2-4b90e3b62c52",
265+
@"signature" : @"this is a encrypted signature",
266+
};
267+
268+
XCTAssertThrows(
269+
[FIAObjectTranslator getSKPaymentDiscountFromMap:discountMap]);
270+
}
271+
}
272+
203273
@end

packages/in_app_purchase/in_app_purchase_ios/ios/Classes/FIAObjectTranslator.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,15 @@ + (NSDictionary *)getMapFromSKStorefront:(SKStorefront *)storefront
209209
}
210210

211211
+ (SKPaymentDiscount *)getSKPaymentDiscountFromMap:(NSDictionary *)map {
212-
if (!map) {
212+
if (!map || map.count <= 0) {
213213
return nil;
214214
}
215+
216+
NSAssert(map[@"identifier"], @"When specifying a payment discount the 'identifier' field is mandatory.");
217+
NSAssert(map[@"keyIdentifier"], @"When specifying a payment discount the 'keyIdentifier' field is mandatory.");
218+
NSAssert(map[@"nonce"], @"When specifying a payment discount the 'nonce' field is mandatory.");
219+
NSAssert(map[@"signature"], @"When specifying a payment discount the 'signature' field is mandatory.");
220+
NSAssert(map[@"timestamp"], @"When specifying a payment discount the 'timestamp' field is mandatory.");
215221

216222
NSString *identifier = map[@"identifier"];
217223
NSString *keyIdentifier = map[@"keyIdentifier"];

0 commit comments

Comments
 (0)