Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
throw new Error(`The keychains backup archive does not exist. ` +
`Are you sure it was created before?`);
}
if (_.isString(excludePatterns)) {
excludePatterns = excludePatterns.split(',').map((x) => x.trim());
}
const {state} = await this.stat();
const isServerRunning = state === 'Booted';
let plistPath;
if (isServerRunning) {
plistPath = path.resolve(await this.getLaunchDaemonsRoot(), 'com.apple.securityd.plist');
if (!await fs.exists(plistPath)) {
throw new Error(`Cannot clear keychains because '${plistPath}' does not exist`);
}
await simctl.spawn(this.udid, ['launchctl', 'unload', plistPath]);
}
try {
await fs.rimraf(this.keychainPath);
await mkdirp(this.keychainPath);
const unzipArgs = [
'-o', this._keychainsBackupPath,
...(_.flatMap(excludePatterns.map((x) => ['-x', x]))),
'-d', '/'
];
log.debug(`Restoring keychains with 'unzip ${unzipArgs.join(' ')}' command`);
await exec('unzip', unzipArgs);
await fs.unlink(this._keychainsBackupPath);
this._keychainsBackupPath = null;
} finally {
if (isServerRunning && plistPath) {
await simctl.spawn(this.udid, ['launchctl', 'load', plistPath]);
async clearKeychains () {
const plistPath = path.resolve(await this.getLaunchDaemonsRoot(), 'com.apple.securityd.plist');
if (!await fs.exists(plistPath)) {
throw new Error(`Cannot clear keychains because '${plistPath}' does not exist`);
}
await simctl.spawn(this.udid, ['launchctl', 'unload', plistPath]);
try {
if (await fs.exists(this.keychainPath)) {
await fs.rimraf(this.keychainPath);
await mkdirp(this.keychainPath);
}
} finally {
await simctl.spawn(this.udid, ['launchctl', 'load', plistPath]);
}
}
async shake () {
log.info(`Performing shake gesture on ${this.udid} Simulator`);
await spawn(this.udid, [
'notifyutil',
'-p', 'com.apple.UIKit.SimulatorShake'
]);
}
async isBiometricEnrolled () {
const {stdout} = await spawn(this.udid, [
'notifyutil',
'-g', ENROLLMENT_NOTIFICATION_RECEIVER
]);
const match = (new RegExp(`${_.escapeRegExp(ENROLLMENT_NOTIFICATION_RECEIVER)}\\s+([01])`))
.exec(stdout);
if (!match) {
throw new Error(`Cannot parse biometric enrollment state from '${stdout}'`);
}
log.info(`Current biometric enrolled state for ${this.udid} Simulator: ${match[1]}`);
return match[1] === '1';
}
async enrollBiometric (isEnabled = true) {
log.debug(`Setting biometric enrolled state for ${this.udid} Simulator to '${isEnabled ? 'enabled' : 'disabled'}'`);
await spawn(this.udid, [
'notifyutil',
'-s', ENROLLMENT_NOTIFICATION_RECEIVER, isEnabled ? '1' : '0'
]);
await spawn(this.udid, [
'notifyutil',
'-p', ENROLLMENT_NOTIFICATION_RECEIVER
]);
if (await this.isBiometricEnrolled() !== isEnabled) {
throw new Error(`Cannot set biometric enrolled state for ${this.udid} Simulator to '${isEnabled ? 'enabled' : 'disabled'}'`);
}
}
async sendBiometricMatch (shouldMatch = true, biometricName = 'touchId') {
const domainComponent = toBiometricDomainComponent(biometricName);
const domain = `com.apple.BiometricKit_Sim.${domainComponent}.${shouldMatch ? '' : 'no'}match`;
await spawn(this.udid, [
'notifyutil',
'-p', domain
]);
log.info(`Sent notification ${domain} to ${shouldMatch ? 'match' : 'not match'} ${biometricName} biometric ` +
`for ${this.udid} Simulator`);
}