Exemplo n.º 1
0
def _CopyAndSignLibFile(src_lib, target_parent_dir, signing_identity):
    """Copies the library to the directory and signs the file with identity."""
    file_name = os.path.basename(src_lib)
    target_path = os.path.join(target_parent_dir, file_name)
    if os.path.exists(target_path):
        os.remove(target_path)
    shutil.copy(src_lib, target_path)
    bundle_util.CodesignBundle(target_path, identity=signing_identity)
Exemplo n.º 2
0
def _CopyAndSignFramework(src_framework, target_parent_dir, signing_identity):
    """Copies the framework to the directory and signs the file with identity."""
    file_name = os.path.basename(src_framework)
    target_path = os.path.join(target_parent_dir, file_name)
    if os.path.exists(target_path):
        shutil.rmtree(target_path)
    shutil.copytree(src_framework, target_path)
    bundle_util.CodesignBundle(target_path, identity=signing_identity)
Exemplo n.º 3
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
        app_under_test_frameworks_dir = os.path.join(self._app_under_test_dir,
                                                     'Frameworks')
        if not os.path.exists(app_under_test_frameworks_dir):
            os.mkdir(app_under_test_frameworks_dir)
        xctest_framework = os.path.join(app_under_test_frameworks_dir,
                                        'XCTest.framework')
        if not os.path.exists(xctest_framework):
            shutil.copytree(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                xctest_framework)
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            insert_libs_framework = os.path.join(
                app_under_test_frameworks_dir, 'IDEBundleInjection.framework')
            if not os.path.exists(insert_libs_framework):
                shutil.copytree(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'IDEBundleInjection.framework'), insert_libs_framework)
        else:
            insert_libs_framework = os.path.join(
                app_under_test_frameworks_dir, 'libXCTestBundleInject.dylib')
            if not os.path.exists(insert_libs_framework):
                shutil.copyfile(
                    os.path.join(
                        platform_path,
                        'Developer/usr/lib/libXCTestBundleInject.dylib'),
                    insert_libs_framework)

        if self._on_device:
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            bundle_util.CodesignBundle(
                xctest_framework, identity=app_under_test_signing_identity)
            bundle_util.CodesignBundle(
                insert_libs_framework,
                identity=app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            dyld_insert_libs = (
                '__PLATFORMS__/%s.platform/Developer/Library/'
                'PrivateFrameworks/IDEBundleInjection.framework/'
                'IDEBundleInjection' % platform_name)
        else:
            dyld_insert_libs = ('__PLATFORMS__/%s.platform/Developer/usr/lib/'
                                'libXCTestBundleInject.dylib' % platform_name)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name,
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/Library/'
            'Frameworks' % platform_name,
        }
        self._xctestrun_dict = {
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }
Exemplo n.º 4
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_library_path = os.path.join(
            xcode_info_util.GetSdkPlatformPath(self._sdk), 'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)

        runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                 'Frameworks')
        os.mkdir(runner_app_frameworks_dir)
        xctest_framework = os.path.join(runner_app_frameworks_dir,
                                        'XCTest.framework')
        shutil.copytree(
            os.path.join(platform_library_path, 'Frameworks/XCTest.framework'),
            xctest_framework)
        if xcode_info_util.GetXcodeVersionNumber() >= 900:
            xct_automation_framework = os.path.join(
                runner_app_frameworks_dir, 'XCTAutomationSupport.framework')
            shutil.copytree(
                os.path.join(
                    platform_library_path,
                    'PrivateFrameworks/XCTAutomationSupport.framework'),
                xct_automation_framework)

        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)
            bundle_util.CodesignBundle(xctest_framework,
                                       identity=test_bundle_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 900:
                bundle_util.CodesignBundle(
                    xct_automation_framework,
                    identity=test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name
        }
        self._xctestrun_dict = {
            'IsUITestBundle': True,
            'SystemAttachmentLifetime': 'keepNever',
            'TestBundlePath': self._test_bundle_dir,
            'TestHostPath': uitest_runner_app,
            'UITargetAppPath': self._app_under_test_dir,
            'UserAttachmentLifetime': 'keepNever',
            'TestingEnvironmentVariables': test_envs
        }
Exemplo n.º 5
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        if self._on_device:
            platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
            app_under_test_frameworks_dir = os.path.join(
                self._app_under_test_dir, 'Frameworks')
            if not os.path.exists(app_under_test_frameworks_dir):
                os.mkdir(app_under_test_frameworks_dir)
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            _CopyAndSignFramework(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                app_under_test_frameworks_dir, app_under_test_signing_identity)
            xcode_version_num = xcode_info_util.GetXcodeVersionNumber()
            if xcode_version_num < 1000:
                bundle_injection_lib = os.path.join(
                    platform_path, 'Developer/Library/PrivateFrameworks/'
                    'IDEBundleInjection.framework')
                _CopyAndSignFramework(bundle_injection_lib,
                                      app_under_test_frameworks_dir,
                                      app_under_test_signing_identity)
            else:
                bundle_injection_lib = os.path.join(
                    platform_path,
                    'Developer/usr/lib/libXCTestBundleInject.dylib')
                _CopyAndSignLibFile(bundle_injection_lib,
                                    app_under_test_frameworks_dir,
                                    app_under_test_signing_identity)
            if xcode_version_num >= 1100:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTAutomationSupport.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            dyld_insert_libs = (
                '%s/Library/PrivateFrameworks/'
                'IDEBundleInjection.framework/IDEBundleInjection' %
                developer_path)
        else:
            dyld_insert_libs = ('%s/usr/lib/libXCTestBundleInject.dylib' %
                                developer_path)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib:' % developer_path
        }

        # Fixes failures for test targets that depend on Swift libraries when running with Xcode 11
        # on pre-iOS 12.2 simulators.
        # Example failure message this resolves: "The bundle couldn’t be loaded because it is damaged
        # or missing necessary resources."
        swift5FallbackLibsDir = xcode_info_util.GetSwift5FallbackLibsDir()
        if swift5FallbackLibsDir:
            test_envs["DYLD_FALLBACK_LIBRARY_PATH"] = swift5FallbackLibsDir

        self._xctestrun_dict = {
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }
Exemplo n.º 6
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
        platform_library_path = os.path.join(platform_path,
                                             'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)
        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)

            runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                     'Frameworks')
            os.mkdir(runner_app_frameworks_dir)
            _CopyAndSignFramework(
                os.path.join(platform_library_path,
                             'Frameworks/XCTest.framework'),
                runner_app_frameworks_dir, test_bundle_signing_identity)
            xcode_version_num = xcode_info_util.GetXcodeVersionNumber()
            if xcode_version_num >= 900:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_library_path,
                        'PrivateFrameworks/XCTAutomationSupport.framework'),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            if xcode_version_num >= 1100:
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib' % developer_path
        }

        # Fixes failures for UI test targets that depend on Swift libraries when running with Xcode 11
        # on pre-iOS 12.2 simulators.
        # Example failure message this resolves: "The bundle couldn’t be loaded because it is damaged
        # or missing necessary resources."
        swift5FallbackLibsDir = xcode_info_util.GetSwift5FallbackLibsDir()
        if swift5FallbackLibsDir:
            test_envs["DYLD_FALLBACK_LIBRARY_PATH"] = swift5FallbackLibsDir

        self._xctestrun_dict = {
            'IsUITestBundle':
            True,
            'SystemAttachmentLifetime':
            'keepAlways',
            'TestBundlePath':
            self._test_bundle_dir,
            'TestHostPath':
            uitest_runner_app,
            'UITargetAppPath':
            self._app_under_test_dir,
            'UserAttachmentLifetime':
            'keepAlways',
            'TestingEnvironmentVariables':
            test_envs,
            'DependentProductPaths':
            [self._app_under_test_dir, self._test_bundle_dir],
        }
Exemplo n.º 7
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        if self._on_device:
            platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
            app_under_test_frameworks_dir = os.path.join(
                self._app_under_test_dir, 'Frameworks')
            if not os.path.exists(app_under_test_frameworks_dir):
                os.mkdir(app_under_test_frameworks_dir)
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            _CopyAndSignFramework(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                app_under_test_frameworks_dir, app_under_test_signing_identity)
            bundle_injection_lib = os.path.join(
                platform_path, 'Developer/usr/lib/libXCTestBundleInject.dylib')
            _CopyAndSignLibFile(bundle_injection_lib,
                                app_under_test_frameworks_dir,
                                app_under_test_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 1100:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTAutomationSupport.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 1300:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCUIAutomation.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTestCore.framework'), app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCUnit.framework'), app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name

        if self._on_device:
            dyld_insert_libs = '__TESTHOST__/Frameworks/libXCTestBundleInject.dylib'
        else:
            dyld_insert_libs = ('%s/usr/lib/libXCTestBundleInject.dylib' %
                                developer_path)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib:' % developer_path
        }
        self._xctestrun_dict = {
            'ProductModuleName': self._test_name,
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }