Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should pull a folder', async function () {
const stringData = `random string data ${Math.random()}`;
const base64Data = Buffer.from(stringData).toString('base64');
// send the files, then pull the whole folder
const remoteDir = getRandomDir();
await driver.pushFile(`${remoteDir}/remote0.txt`, base64Data);
await driver.pushFile(`${remoteDir}/remote1.txt`, base64Data);
const data = await driver.pullFolder(remoteDir);
const tmpRoot = await tempDir.openDir();
try {
const zipPath = path.resolve(tmpRoot, 'data.zip');
await fs.writeFile(zipPath, Buffer.from(data, 'base64'));
const extractedDataPath = path.resolve(tmpRoot, 'extracted_data');
await fs.mkdir(extractedDataPath);
await zip.extractAllTo(zipPath, extractedDataPath);
const itemsCount = (await fs.readdir(extractedDataPath)).length;
itemsCount.should.eql(2);
} finally {
await fs.rimraf(tmpRoot);
}
});
});
it('should pull a folder from filesystem as a base64 zip, extract the zip and have same contents as in filesystem', async function () {
// Create a temporary directory with one file in it
const tempPath = await tempDir.openDir();
await fs.writeFile(path.resolve(tempPath, 'a.txt'), 'Hello World!');
const getSimPathStub = sinon.stub(driver, 'getSimFileFullPath').returns(tempPath);
// Zip the directory to base64 and write it to 'zip.zip'
const zippedData = await driver.pullFolder('/does/not/matter');
const zippedFilepath = path.resolve(tempPath, 'zip.zip');
await fs.writeFile(zippedFilepath, zippedData, {encoding: 'base64'});
// Unzip it and check it matches original file contents
const unzippedDir = path.resolve(tempPath, 'unzipped');
await zip.extractAllTo(zippedFilepath, unzippedDir);
await fs.readFile(path.resolve(unzippedDir, 'a.txt'), {encoding: 'utf8'}).should.eventually.equal('Hello World!');
getSimPathStub.restore();
});
} catch (e) {
log.debug(`customSSLCert requires openssl to be available on path`);
log.errorAndThrow(`Command 'openssl' not found`);
}
// Check that sqlite3 is installed on the path
try {
await fs.which('sqlite3');
} catch (e) {
log.debug(`customSSLCert requires sqlite3 to be available on path`);
log.errorAndThrow(`Command 'sqlite3' not found`);
}
// Create a temporary file to store PEM text
// (a temp file is necessary to run `openssl` shell commands, can't be done in memory)
let tempFileName = path.resolve(await tempDir.openDir(), 'temp-ssl-cert.pem');
let pathToKeychain = new Simulator(udid).getDir();
await fs.writeFile(tempFileName, pemText);
try {
await fs.stat(pathToKeychain);
} catch (e) {
log.debug(`Could not install SSL certificate. No simulator with udid '${udid}'`);
log.errorAndThrow(e);
}
// Do the certificate installation
let certificate = new Certificate(tempFileName);
log.debug(`Installing certificate to ${pathToKeychain}`);
await certificate.add(pathToKeychain);
// Remove the temporary file
await fs.unlink(tempFileName);
return await APKS_CACHE_GUARD.acquire(apks, async () => {
// It might be that the original file has been replaced,
// so we need to keep the hash sums instead of the actual file paths
// as caching keys
const apksHash = await fs.hash(apks);
log.debug(`Calculated '${apks}' hash: ${apksHash}`);
if (APKS_CACHE.has(apksHash)) {
const resultPath = path.resolve(APKS_CACHE.get(apksHash), ...dstPath);
if (await fs.exists(resultPath)) {
return resultPath;
}
APKS_CACHE.del(apksHash);
}
const tmpRoot = await tempDir.openDir();
log.debug(`Unpacking application bundle at '${apks}' to '${tmpRoot}'`);
await unzipFile(apks, tmpRoot);
const resultPath = path.resolve(tmpRoot, ...dstPath);
if (!await fs.exists(resultPath)) {
throw new Error(`${dstPath.join(path.sep)} cannot be found in '${apks}' bundle. ` +
`Does the archive contain a valid application bundle?`);
}
APKS_CACHE.set(apksHash, tmpRoot);
return resultPath;
});
}
async function pullFolderFromRealDevice (service, relativePath) {
const tmpFolder = await tempDir.openDir();
try {
const folderPath = path.join(tmpFolder, relativePath);
await mkdirp(folderPath);
const promises = [];
await service.walkDir(relativePath, true, async (itemPath, isDir) => {
const pathOnServer = path.join(tmpFolder, itemPath);
if (isDir) {
await fs.mkdir(pathOnServer);
} else {
const readStream = await service.createReadStream(itemPath, {autoDestroy: true });
const writeStream = fs.createWriteStream(pathOnServer, {autoClose: true});
promises.push(new B((resolve) => writeStream.on('close', resolve)));
readStream.pipe(writeStream);
}
});
try {
apksUtilsMethods.installApks = async function installApks (apks, options = {}) {
options = _.cloneDeep(options);
_.defaults(options, {
timeout: this.adbExecTimeout === DEFAULT_ADB_EXEC_TIMEOUT ? APKS_INSTALL_TIMEOUT : this.adbExecTimeout,
timeoutCapName: 'androidInstallTimeout',
});
Object.assign(options, {replace: true});
const tmpRoot = await tempDir.openDir();
try {
const specPath = await this.getDeviceSpec(path.resolve(tmpRoot, 'deviceSpec.json'));
const args = [
'extract-apks',
'--apks', apks,
'--output-dir', tmpRoot,
'--device-spec', specPath,
];
log.debug(`Extracting the apk files from '${apks}'`);
await this.execBundletool(args, `Cannot extract the application bundle at '${apks}'`);
const installArgs = buildInstallArgs(await this.getApiLevel(), options);
const apkPathsToInstall = (await fs.readdir(tmpRoot))
.filter((name) => name.endsWith(APK_EXTENSION))
.map((name) => path.resolve(tmpRoot, name));
log.debug('Got the following apk files to install: ' +
JSON.stringify(apkPathsToInstall.map((x) => path.basename(x))));
async unzipDriver (src, dst) {
const tmpRoot = await tempDir.openDir();
try {
await zip.extractAllTo(src, tmpRoot);
const chromedriverPath = await fs.walkDir(tmpRoot, true, (itemPath, isDirectory) =>
!isDirectory && _.toLower(path.parse(itemPath).name) === 'chromedriver');
if (!chromedriverPath) {
throw new Error('The archive was unzipped properly, but we could not find any chromedriver executable');
}
log.debug(`Moving the extracted '${path.basename(chromedriverPath)}' to '${dst}'`);
await fs.mv(chromedriverPath, dst, {
mkdirp: true
});
} finally {
await fs.rimraf(tmpRoot);
}
}
}
if (_.isEmpty(this.mapping)) {
throw new Error('Cannot retrieve chromedrivers mapping from Google storage');
}
const driversToSync = this.selectMatchingDrivers(await getOsInfo(), opts);
if (_.isEmpty(driversToSync)) {
log.debug(`There are no drivers to sync. Exiting`);
return [];
}
log.debug(`Got ${driversToSync.length} driver${driversToSync.length === 1 ? '' : 's'} to sync: ` +
JSON.stringify(driversToSync, null, 2));
const synchronizedDrivers = [];
const promises = [];
const archivesRoot = await tempDir.openDir();
try {
for (const [idx, driverKey] of driversToSync.entries()) {
promises.push((async () => {
if (await this.retrieveDriver(idx, driverKey, archivesRoot, !_.isEmpty(opts))) {
synchronizedDrivers.push(driverKey);
}
})());
if (promises.length % MAX_PARALLEL_DOWNLOADS === 0) {
await B.all(promises);
}
}
await B.all(promises);
} finally {
await fs.rimraf(archivesRoot);
}