Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Skip signature checks of server packages #759

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 52 additions & 75 deletions lib/uiautomator2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import {
TEST_APK_PATH as testApkPath,
version as serverVersion
} from 'appium-uiautomator2-server';
import {
util, logger, tempDir, fs, timing
} from 'appium/support';
import { util, logger, timing } from 'appium/support';
import B from 'bluebird';
import {isWriteable, signApp} from './helpers';
import axios from 'axios';
import path from 'path';

const REQD_PARAMS = ['adb', 'tmpDir', 'host', 'systemPort', 'devicePort', 'disableWindowAnimation'];
const SERVER_LAUNCH_TIMEOUT = 30000;
Expand Down Expand Up @@ -78,30 +74,18 @@ class UiAutomator2Server {
this.jwproxy.didInstrumentationExit = false;
}

async prepareServerPackage(appPath, appId, tmpRoot) {
/**
* @param {string} appPath
* @param {string} appId
* @returns {Promise<{installState: import('appium-adb').InstallState, appPath: string; appId: string}>}
*/
async prepareServerPackage(appPath, appId) {
const resultInfo = {
wasSigned: false,
installState: this.adb.APP_INSTALL_STATE.NOT_INSTALLED,
appPath,
appId,
};

if (await this.adb.checkApkCert(resultInfo.appPath, appId)) {
resultInfo.wasSigned = true;
} else {
if (!await isWriteable(appPath)) {
this.log.warn(
`Server package at '${appPath}' is not writeable. ` +
`Will copy it into the temporary location at '${tmpRoot}' as a workaround. ` +
`Consider making this file writeable manually in order to improve the performance of session startup.`
);
const dstPath = path.resolve(tmpRoot, path.basename(appPath));
await fs.copyFile(appPath, dstPath);
resultInfo.appPath = dstPath;
}
await signApp(this.adb, resultInfo.appPath);
}

if (appId === SERVER_TEST_PACKAGE_ID && await this.adb.isAppInstalled(appId)) {
// There is no point in getting the state for the test server,
// since it does not contain any version info
Expand All @@ -119,59 +103,52 @@ class UiAutomator2Server {
* @param {number} installTimeout - Installation timeout
*/
async installServerApk (installTimeout = SERVER_INSTALL_RETRIES * 1000) {
const tmpRoot = await tempDir.openDir();
try {
const packagesInfo = await B.all(
[
{
appPath: apkPath,
appId: SERVER_PACKAGE_ID,
}, {
appPath: testApkPath,
appId: SERVER_TEST_PACKAGE_ID,
},
].map(({appPath, appId}) => this.prepareServerPackage(appPath, appId, tmpRoot))
);
const packagesInfo = await B.all(
[
{
appPath: apkPath,
appId: SERVER_PACKAGE_ID,
}, {
appPath: testApkPath,
appId: SERVER_TEST_PACKAGE_ID,
},
].map(({appPath, appId}) => this.prepareServerPackage(appPath, appId))
);

this.log.debug(`Server packages status: ${JSON.stringify(packagesInfo)}`);
// We want to enforce uninstall in case the current server package has not been signed properly
// or if any of server packages is not installed, while the other does
const shouldUninstallServerPackages = packagesInfo.some(({wasSigned}) => !wasSigned)
|| (packagesInfo.some(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED)
&& !packagesInfo.every(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED));
// Install must always follow uninstall. Also, perform the install if
// any of server packages is not installed or is outdated
const shouldInstallServerPackages = shouldUninstallServerPackages || packagesInfo.some(({installState}) => [
this.adb.APP_INSTALL_STATE.NOT_INSTALLED,
this.adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
].includes(installState));
this.log.info(`Server packages are ${shouldInstallServerPackages ? '' : 'not '}going to be (re)installed`);
if (shouldInstallServerPackages && shouldUninstallServerPackages) {
this.log.info('Full packages reinstall is going to be performed');
}
if (shouldUninstallServerPackages) {
const silentUninstallPkg = async (pkgId) => {
try {
await this.adb.uninstallApk(pkgId);
} catch (err) {
this.log.info(`Cannot uninstall '${pkgId}': ${err.message}`);
}
};
await B.all(packagesInfo.map(({appId}) => silentUninstallPkg(appId)));
}
if (shouldInstallServerPackages) {
const installPkg = async (pkgPath) => {
await this.adb.install(pkgPath, {
noIncremental: true,
replace: true,
timeout: installTimeout,
timeoutCapName: 'uiautomator2ServerInstallTimeout'
});
};
await B.all(packagesInfo.map(({appPath}) => installPkg(appPath)));
}
} finally {
await fs.rimraf(tmpRoot);
this.log.debug(`Server packages status: ${JSON.stringify(packagesInfo)}`);
// Enforce server packages reinstall if any of the packages is not installed, while the other is
const shouldUninstallServerPackages = (packagesInfo.some(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED)
&& !packagesInfo.every(({installState}) => installState === this.adb.APP_INSTALL_STATE.NOT_INSTALLED));
// Install must always follow uninstall. Also, perform the install if
// any of server packages is not installed or is outdated
const shouldInstallServerPackages = shouldUninstallServerPackages || packagesInfo.some(({installState}) => [
this.adb.APP_INSTALL_STATE.NOT_INSTALLED,
this.adb.APP_INSTALL_STATE.OLDER_VERSION_INSTALLED,
].includes(installState));
this.log.info(`Server packages are ${shouldInstallServerPackages ? '' : 'not '}going to be (re)installed`);
if (shouldInstallServerPackages && shouldUninstallServerPackages) {
this.log.info('Full packages reinstall is going to be performed');
}
if (shouldUninstallServerPackages) {
const silentUninstallPkg = async (pkgId) => {
try {
await this.adb.uninstallApk(pkgId);
} catch (err) {
this.log.info(`Cannot uninstall '${pkgId}': ${err.message}`);
}
};
await B.all(packagesInfo.map(({appId}) => silentUninstallPkg(appId)));
}
if (shouldInstallServerPackages) {
const installPkg = async (pkgPath) => {
await this.adb.install(pkgPath, {
noIncremental: true,
replace: true,
timeout: installTimeout,
timeoutCapName: 'uiautomator2ServerInstallTimeout'
});
};
await B.all(packagesInfo.map(({appPath}) => installPkg(appPath)));
}

await this.verifyServicesAvailability();
Expand Down
Loading