duplicateProtocols.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "test.h"
  2. // This test assumes
  3. // - that on launch we don't have NSCoding, NSSecureCoding, or NSDictionary, ie, libSystem doesn't contain those
  4. // - that after dlopening CF, we get NSDictionary and it conforms to NSSecureCoding which conforms to NSCoding
  5. // - that our NSCoding will be used if we ask either NSSecureCoding or NSDictionary if they conform to our test protocol
  6. @protocol NewNSCodingSuperProto
  7. @end
  8. @protocol NSCoding <NewNSCodingSuperProto>
  9. @end
  10. int main()
  11. {
  12. // Before we dlopen, make sure we are using our NSCoding, not the shared cache version
  13. Protocol* codingSuperProto = objc_getProtocol("NewNSCodingSuperProto");
  14. Protocol* codingProto = objc_getProtocol("NSCoding");
  15. if (@protocol(NewNSCodingSuperProto) != codingSuperProto) fail("Protocol mismatch");
  16. if (@protocol(NSCoding) != codingProto) fail("Protocol mismatch");
  17. if (!protocol_conformsToProtocol(codingProto, codingSuperProto)) fail("Our NSCoding should conform to NewNSCodingSuperProto");
  18. // Also make sure we don't yet have an NSSecureCoding or NSDictionary
  19. if (objc_getProtocol("NSSecureCoding")) fail("Test assumes we don't have NSSecureCoding yet");
  20. if (objc_getClass("NSDictionary")) fail("Test assumes we don't have NSDictionary yet");
  21. void *dl = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
  22. if (!dl) fail("couldn't open CoreFoundation");
  23. // We should now have NSSecureCoding and NSDictionary
  24. Protocol* secureCodingProto = objc_getProtocol("NSSecureCoding");
  25. id dictionaryClass = objc_getClass("NSDictionary");
  26. if (!secureCodingProto) fail("Should have got NSSecureCoding from CoreFoundation");
  27. if (!dictionaryClass) fail("Should have got NSDictionary from CoreFoundation");
  28. // Now make sure that NSDictionary and NSSecureCoding find our new protocols
  29. if (!protocol_conformsToProtocol(secureCodingProto, codingProto)) fail("NSSecureCoding should conform to our NSCoding");
  30. if (!protocol_conformsToProtocol(secureCodingProto, codingSuperProto)) fail("NSSecureCoding should conform to our NewNSCodingSuperProto");
  31. if (!class_conformsToProtocol(dictionaryClass, codingProto)) fail("NSDictionary should conform to our NSCoding");
  32. if (!class_conformsToProtocol(dictionaryClass, codingSuperProto)) fail("NSDictionary should conform to our NewNSCodingSuperProto");
  33. }