GIDTokenTest.m 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h"
  16. #import "GoogleSignIn/Sources/GIDToken_Private.h"
  17. static NSString * const tokenString = @"tokenString";
  18. static NSString * const tokenString2 = @"tokenString2";
  19. @interface GIDTokenTest : XCTestCase {
  20. NSDate *_date;
  21. }
  22. @end
  23. @implementation GIDTokenTest
  24. - (void)setUp {
  25. [super setUp];
  26. _date = [[NSDate alloc]initWithTimeIntervalSince1970:1000];
  27. }
  28. - (void)testInitializer {
  29. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  30. XCTAssertEqualObjects(token.tokenString, tokenString);
  31. XCTAssertEqualObjects(token.expirationDate, _date);
  32. }
  33. - (void)testTokensWithSameTokenStringAndExpirationDateAreEqual {
  34. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  35. GIDToken *token2 = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  36. XCTAssertEqualObjects(token, token2);
  37. }
  38. - (void)testEqualTokensHaveTheSameHash {
  39. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  40. GIDToken *token2 = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  41. XCTAssertEqualObjects(token, token2);
  42. XCTAssertEqual(token.hash, token2.hash);
  43. }
  44. - (void)testTokensWithDifferentTokenStringsAreNotEqual {
  45. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  46. GIDToken *token2 = [[GIDToken alloc]initWithTokenString:tokenString2 expirationDate:_date];
  47. XCTAssertNotEqualObjects(token, token2);
  48. }
  49. - (void)testTokensWithSameTokenStringAndNoExpirationDateAreEqual {
  50. GIDToken *refreshToken = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:nil];
  51. GIDToken *refreshToken2 = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:nil];
  52. XCTAssertEqualObjects(refreshToken, refreshToken2);
  53. }
  54. - (void)testTokensWithSameTokenStringAndDifferentExpirationDateAreNotEqual {
  55. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  56. NSDate *date2 = [[NSDate alloc]initWithTimeIntervalSince1970:2000];
  57. GIDToken *token2 = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:date2];
  58. XCTAssertNotEqualObjects(token, token2);
  59. }
  60. - (void)testTokensWithSameTokenStringAndOneHasNoExpirationDateAreNotEqual {
  61. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  62. GIDToken *token2 = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:nil];
  63. XCTAssertNotEqualObjects(token, token2);
  64. }
  65. - (void)testCoding {
  66. if (@available(iOS 11, macOS 10.13, *)) {
  67. GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date];
  68. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:token requiringSecureCoding:YES error:nil];
  69. GIDToken *newToken = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDToken class]
  70. fromData:data
  71. error:nil];
  72. XCTAssertEqualObjects(token, newToken);
  73. XCTAssertTrue([GIDToken supportsSecureCoding]);
  74. } else {
  75. XCTSkip(@"Required API is not available for this test.");
  76. }
  77. }
  78. @end