GULRuntimeStateHelperTests.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 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 <objc/runtime.h>
  16. #import <GoogleUtilities/GULRuntimeStateHelper.h>
  17. @interface GULRuntimeStateHelperTestHelperClass : NSObject
  18. @end
  19. @implementation GULRuntimeStateHelperTestHelperClass
  20. @end
  21. @interface GULRuntimeStateHelperTests : XCTestCase
  22. @end
  23. @implementation GULRuntimeStateHelperTests
  24. - (void)testCaptureRuntimeState {
  25. NSUInteger snapshot1 = 0;
  26. XCTAssertNoThrow(snapshot1 = [GULRuntimeStateHelper captureRuntimeState]);
  27. }
  28. - (void)testDiffBetweenFirstSnapshotSecondSnapshot {
  29. NSUInteger snapshot1 = [GULRuntimeStateHelper captureRuntimeState];
  30. NSString *newClassName = [NSStringFromClass([self class]) stringByAppendingString:@"_gen"];
  31. Class newSubclass = objc_allocateClassPair([self class], [newClassName UTF8String], 0);
  32. objc_registerClassPair(newSubclass);
  33. Method dummyMethod = class_getInstanceMethod([self class], @selector(dummyMethod));
  34. IMP originalIMP = method_getImplementation(dummyMethod);
  35. NSString *originalIMPString = [NSString stringWithFormat:@"%p", originalIMP];
  36. IMP newIMP = imp_implementationWithBlock((NSString *)^(id _self) {
  37. return @"Goodbye!";
  38. });
  39. method_setImplementation(dummyMethod, newIMP);
  40. NSUInteger snapshot2 = [GULRuntimeStateHelper captureRuntimeState];
  41. GULRuntimeDiff *diff = [GULRuntimeStateHelper diffBetween:snapshot1 secondSnapshot:snapshot2];
  42. BOOL found = NO;
  43. for (NSString *class in diff.addedClasses) {
  44. if ([class isEqualToString:newClassName]) {
  45. found = YES;
  46. break;
  47. }
  48. }
  49. XCTAssertTrue(found, @"The generated class above should be found in the list of added classes");
  50. found = NO;
  51. for (GULRuntimeClassDiff *classDiff in diff.classDiffs) {
  52. for (NSString *modifiedIMP in classDiff.modifiedImps) {
  53. if ([modifiedIMP containsString:originalIMPString]) {
  54. found = YES;
  55. break;
  56. }
  57. }
  58. }
  59. XCTAssertTrue(found, @"One of the classdiffs should contain the address of the original IMP of "
  60. "the method that was modified above");
  61. }
  62. #pragma mark - Helper methods
  63. /** Exists to just be swizzled, to test detection capability.
  64. *
  65. * @return The string "Hello!".
  66. */
  67. - (NSString *)dummyMethod {
  68. return @"Hello!";
  69. }
  70. @end