/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* global cordova, FileTransfer, FileTransferError, FileUploadOptions, LocalFileSystem */
exports.defineAutoTests = function () {
'use strict';
// constants
const ONE_SECOND = 1000; // in milliseconds
const GRACE_TIME_DELTA = 600; // in milliseconds
const DEFAULT_FILESYSTEM_SIZE = 1024 * 50; // filesystem size in bytes
const UNKNOWN_HOST = 'http://foobar.apache.org';
const DOWNLOAD_TIMEOUT = 15 * ONE_SECOND;
const UPLOAD_TIMEOUT = 15 * ONE_SECOND;
const ABORT_DELAY = 100; // for abort() tests
const LATIN1_SYMBOLS = '¥§©ÆÖÑøøø¼';
const DATA_URI_PREFIX = 'data:image/png;base64,';
const DATA_URI_CONTENT =
'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
const DATA_URI_CONTENT_LENGTH = 85; // bytes. (This is the raw file size: used https://en.wikipedia.org/wiki/File:Red-dot-5px.png from https://en.wikipedia.org/wiki/Data_URI_scheme)
const RETRY_COUNT = 100; // retry some flaky tests (yes, THIS many times, due to Heroku server instability)
const RETRY_INTERVAL = 100;
// upload test server address
// NOTE:
// more info at https://github.com/apache/cordova-labs/tree/cordova-filetransfer
// Will get it from the config
// you can specify it as a 'FILETRANSFER_SERVER_ADDRESS' variable upon test plugin installation
// or change the default value in plugin.xml
let SERVER = '';
let SERVER_WITH_CREDENTIALS = '';
// flags
const isBrowser = cordova.platformId === 'browser';
const isIE = isBrowser && navigator.userAgent.indexOf('Trident') >= 0;
const isIos = cordova.platformId === 'ios';
const isIot = cordova.platformId === 'android' && navigator.userAgent.indexOf('iot') >= 0;
// tests
describe('FileTransferError', function () {
it('should exist', function () {
expect(FileTransferError).toBeDefined();
});
it('should be constructable', function () {
const transferError = new FileTransferError();
expect(transferError).toBeDefined();
});
it('filetransfer.spec.3 should expose proper constants', function () {
expect(FileTransferError.FILE_NOT_FOUND_ERR).toBeDefined();
expect(FileTransferError.INVALID_URL_ERR).toBeDefined();
expect(FileTransferError.CONNECTION_ERR).toBeDefined();
expect(FileTransferError.ABORT_ERR).toBeDefined();
expect(FileTransferError.NOT_MODIFIED_ERR).toBeDefined();
expect(FileTransferError.FILE_NOT_FOUND_ERR).toBe(1);
expect(FileTransferError.INVALID_URL_ERR).toBe(2);
expect(FileTransferError.CONNECTION_ERR).toBe(3);
expect(FileTransferError.ABORT_ERR).toBe(4);
expect(FileTransferError.NOT_MODIFIED_ERR).toBe(5);
});
});
describe('FileUploadOptions', function () {
it('should exist', function () {
expect(FileUploadOptions).toBeDefined();
});
it('should be constructable', function () {
const transferOptions = new FileUploadOptions();
expect(transferOptions).toBeDefined();
});
});
describe('FileTransfer', function () {
this.persistentRoot = null;
this.tempRoot = null;
// named callbacks
const unexpectedCallbacks = {
httpFail: function () {},
httpWin: function () {},
fileSystemFail: function () {},
fileSystemWin: function () {},
fileOperationFail: function () {},
fileOperationWin: function () {}
};
const expectedCallbacks = {
unsupportedOperation: function (response) {
console.log('spec called unsupported functionality; response:', response);
}
};
// helpers
const deleteFile = function (fileSystem, name, done) {
fileSystem.getFile(
name,
null,
function (fileEntry) {
fileEntry.remove(
function () {
done();
},
function () {
throw new Error("failed to delete: '" + name + "'");
}
);
},
function () {
done();
}
);
};
const writeFile = function (fileSystem, name, content, success, done) {
const fileOperationFail = function () {
unexpectedCallbacks.fileOperationFail();
done();
};
fileSystem.getFile(
name,
{ create: true },
function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.onwrite = function () {
success(fileEntry);
};
writer.onabort = function (evt) {
throw new Error("aborted creating test file '" + name + "': " + evt);
};
writer.error = function (evt) {
throw new Error("aborted creating test file '" + name + "': " + evt);
};
if (cordova.platformId === 'browser') {
const blob = new Blob([content + '\n'], { type: 'text/plain' });
writer.write(blob);
} else {
writer.write(content + '\n');
}
}, fileOperationFail);
},
function () {
throw new Error("could not create test file '" + name + "'");
}
);
};
const defaultOnProgressHandler = function (event) {
if (event.lengthComputable) {
expect(event.loaded).toBeGreaterThan(1);
expect(event.total).toBeGreaterThan(0);
expect(event.total).not.toBeLessThan(event.loaded);
expect(event.lengthComputable).toBe(true, 'lengthComputable');
} else {
// In IE, when lengthComputable === false, event.total somehow is equal to 2^64
if (isIE) {
expect(event.total).toBe(Math.pow(2, 64));
} else {
// iOS returns -1, and other platforms return 0
expect(event.total).toBeLessThan(1);
}
}
};
const getMalformedUrl = function () {
if (cordova.platformId === 'android') {
// bad protocol causes a MalformedUrlException on Android
return 'httpssss://example.com';
} else {
// iOS doesn't care about protocol, space in hostname causes error
return 'httpssss://exa mple.com';
}
};
const setServerAddress = function (address) {
SERVER = address;
SERVER_WITH_CREDENTIALS = SERVER.replace('http://', 'http://cordova_user:cordova_password@');
};
// NOTE:
// there are several beforeEach calls, one per async call; since calling done()
// signifies a completed async call, each async call needs its own done(), and
// therefore its own beforeEach
beforeEach(function (done) {
const specContext = this;
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
DEFAULT_FILESYSTEM_SIZE,
function (fileSystem) {
specContext.persistentRoot = fileSystem.root;
done();
},
function () {
throw new Error('Failed to initialize persistent file system.');
}
);
});
beforeEach(function (done) {
const specContext = this;
window.requestFileSystem(
LocalFileSystem.TEMPORARY,
DEFAULT_FILESYSTEM_SIZE,
function (fileSystem) {
specContext.tempRoot = fileSystem.root;
done();
},
function () {
throw new Error('Failed to initialize temporary file system.');
}
);
});
// spy on all named callbacks
beforeEach(function () {
// ignore the actual implementations of the unexpected callbacks
for (const callback in unexpectedCallbacks) {
if (Object.prototype.hasOwnProperty.call(unexpectedCallbacks, callback)) {
spyOn(unexpectedCallbacks, callback);
}
}
// but run the implementations of the expected callbacks
for (const callback in expectedCallbacks) {
if (Object.prototype.hasOwnProperty.call(expectedCallbacks, callback)) {
spyOn(expectedCallbacks, callback).and.callThrough();
}
}
});
// at the end, check that none of the unexpected callbacks got called,
// and act on the expected callbacks
afterEach(function () {
for (const callback in unexpectedCallbacks) {
if (Object.prototype.hasOwnProperty.call(unexpectedCallbacks, callback)) {
expect(unexpectedCallbacks[callback]).not.toHaveBeenCalled();
}
}
if (expectedCallbacks.unsupportedOperation.calls.any()) {
pending();
}
});
it('util spec: get file transfer server url', function () {
try {
// attempt to synchronously load medic config
const xhr = new XMLHttpRequest();
xhr.open('GET', '../fileTransferOpts.json', false);
xhr.send(null);
const parsedCfg = JSON.parse(xhr.responseText);
if (parsedCfg.serverAddress) {
setServerAddress(parsedCfg.serverAddress);
}
} catch (ex) {
console.error('Unable to load file transfer server url: ' + ex);
console.error(
'Note: if you are testing this on cordova-ios with cordova-plugin-wkwebview-engine, that may be why you are getting this error. See https://issues.apache.org/jira/browse/CB-10144.'
);
fail(ex);
}
});
it('should initialise correctly', function () {
expect(this.persistentRoot).toBeDefined();
expect(this.tempRoot).toBeDefined();
});
it('should exist', function () {
expect(FileTransfer).toBeDefined();
});
it('filetransfer.spec.1 should be constructable', function () {
const transfer = new FileTransfer();
expect(transfer).toBeDefined();
});
it('filetransfer.spec.2 should expose proper functions', function () {
const transfer = new FileTransfer();
expect(transfer.upload).toBeDefined();
expect(transfer.download).toBeDefined();
expect(transfer.upload).toEqual(jasmine.any(Function));
expect(transfer.download).toEqual(jasmine.any(Function));
});
describe('methods', function () {
this.transfer = null;
this.root = null;
this.fileName = null;
this.localFilePath = null;
beforeEach(function () {
this.transfer = new FileTransfer();
// assign onprogress handler
this.transfer.onprogress = defaultOnProgressHandler;
// spy on the onprogress handler, but still call through to it
spyOn(this.transfer, 'onprogress').and.callThrough();
this.root = this.persistentRoot;
this.fileName = 'testFile.txt';
this.localFilePath = this.root.toURL() + this.fileName;
});
// NOTE:
// if download tests are failing, check the
// URL white list for the following URLs:
// - 'httpssss://example.com'
// - 'apache.org', with subdomains="true"
// - 'cordova-filetransfer.jitsu.com'
describe('download', function () {
// helpers
const verifyDownload = function (fileEntry, specContext) {
expect(fileEntry.name).toBe(specContext.fileName);
};
// delete the downloaded file
afterEach(function (done) {
deleteFile(this.root, this.fileName, done);
});
it('ensures that test file does not exist', function (done) {
deleteFile(this.root, this.fileName, done);
});
it(
'filetransfer.spec.4 should download a file',
function (done) {
const fileURL = SERVER + '/robots.txt';
const specContext = this;
const fileWin = function (blob) {
if (specContext.transfer.onprogress.calls.any()) {
const lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0];
expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size);
} else {
console.log('no progress events were emitted');
}
done();
};
const fileSystemFail = function () {
unexpectedCallbacks.fileSystemFail();
done();
};
const downloadFail = function () {
unexpectedCallbacks.httpFail();
done();
};
const downloadWin = function (entry) {
verifyDownload(entry, specContext);
// verify the FileEntry representing this file
entry.file(fileWin, fileSystemFail);
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT * 10
); // to give Heroku server some time to wake up
it(
'filetransfer.spec.4.1 should download a file using target name with space',
function (done) {
const fileURL = SERVER + '/robots.txt';
this.fileName = 'test file.txt';
this.localFilePath = this.root.toURL() + this.fileName;
const specContext = this;
const fileWin = function (blob) {
if (specContext.transfer.onprogress.calls.any()) {
const lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0];
expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size);
} else {
console.log('no progress events were emitted');
}
done();
};
const fileSystemFail = function () {
unexpectedCallbacks.fileSystemFail();
done();
};
const downloadFail = function () {
unexpectedCallbacks.httpFail();
done();
};
const downloadWin = function (entry) {
verifyDownload(entry, specContext);
// verify the FileEntry representing this file
entry.file(fileWin, fileSystemFail);
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.5 should download a file using http basic auth',
function (done) {
const fileURL = SERVER_WITH_CREDENTIALS + '/download_basic_auth';
const specContext = this;
const downloadWin = function (entry) {
verifyDownload(entry, specContext);
done();
};
const downloadFail = function () {
unexpectedCallbacks.httpFail();
done();
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.6 should get 401 status on http basic auth failure',
function (done) {
// NOTE:
// using server without credentials
const fileURL = SERVER + '/download_basic_auth';
const downloadFail = function (error) {
expect(error.http_status).toBe(401);
expect(error.http_status).not.toBe(404, 'Ensure ' + fileURL + ' is in the white list');
done();
};
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail, null, {
headers: {
'If-Modified-Since': 'Thu, 19 Mar 2015 00:00:00 GMT'
}
});
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.7 should download a file using file:// (when hosted from file://)',
function (done) {
const fileURL = window.location.protocol + '//' + window.location.pathname.replace(/ /g, '%20');
const specContext = this;
if (!/^file:/.exec(fileURL)) {
done();
return;
}
const downloadWin = function (entry) {
verifyDownload(entry, specContext);
done();
};
const downloadFail = function () {
unexpectedCallbacks.httpFail();
done();
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.8 should download a file using https://',
function (done) {
const fileURL = 'https://www.apache.org/licenses/';
const specContext = this;
const downloadFail = function () {
unexpectedCallbacks.httpFail();
done();
};
const fileOperationFail = function () {
unexpectedCallbacks.fileOperationFail();
done();
};
const fileSystemFail = function () {
unexpectedCallbacks.fileSystemFail();
done();
};
const fileWin = function (file) {
const reader = new FileReader();
reader.onerror = fileOperationFail;
reader.onload = function () {
expect(reader.result).toMatch(/The Apache Software Foundation/);
done();
};
reader.readAsText(file);
};
const downloadWin = function (entry) {
verifyDownload(entry, specContext);
entry.file(fileWin, fileSystemFail);
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.11 should call the error callback on abort()',
function (done) {
let fileURL = 'https://cordova.apache.org/static/downloads/BlueZedEx.mp3';
fileURL = fileURL + '?q=' + new Date().getTime();
const specContext = this;
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, done);
setTimeout(function () {
specContext.transfer.abort();
}, ABORT_DELAY);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.9 should not leave partial file due to abort',
function (done) {
const fileURL = 'https://cordova.apache.org/static/downloads/logos.zip';
const specContext = this;
const fileSystemWin = function () {
unexpectedCallbacks.fileSystemWin();
done();
};
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
const downloadFail = function (error) {
const result = !!(error.code === FileTransferError.ABORT_ERR || error.code === FileTransferError.CONNECTION_ERR);
if (!result) {
fail(
'Expected ' +
error.code +
' to be ' +
FileTransferError.ABORT_ERR +
' or ' +
FileTransferError.CONNECTION_ERR
);
}
expect(specContext.transfer.onprogress).toHaveBeenCalled();
// check that there is no file
specContext.root.getFile(specContext.fileName, null, fileSystemWin, done);
};
// abort at the first onprogress event
specContext.transfer.onprogress = function (event) {
if (event.loaded > 0) {
specContext.transfer.abort();
}
};
spyOn(specContext.transfer, 'onprogress').and.callThrough();
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.10 should be stopped by abort()',
function (done) {
let fileURL = 'https://cordova.apache.org/static/downloads/BlueZedEx.mp3';
fileURL = fileURL + '?q=' + new Date().getTime();
const specContext = this;
expect(specContext.transfer.abort).not.toThrow(); // should be a no-op.
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
const downloadFail = function (error) {
expect(error.code).toBe(FileTransferError.ABORT_ERR);
// delay calling done() to wait for the bogus abort()
setTimeout(done, GRACE_TIME_DELTA * 2);
};
specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
setTimeout(function () {
specContext.transfer.abort();
}, ABORT_DELAY);
// call abort() again, after a time greater than the grace period
setTimeout(function () {
expect(specContext.transfer.abort).not.toThrow();
}, GRACE_TIME_DELTA);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.12 should get http status on failure',
function (done) {
const fileURL = SERVER + '/404';
const downloadFail = function (error) {
expect(error.http_status).not.toBe(401, 'Ensure ' + fileURL + ' is in the white list');
expect(error.http_status).toBe(404);
expect(error.code).toBe(FileTransferError.FILE_NOT_FOUND_ERR);
done();
};
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it(
'filetransfer.spec.13 should get http body on failure',
function (done) {
const fileURL = SERVER + '/404';
const downloadFail = function (error) {
expect(error.http_status).not.toBe(401, 'Ensure ' + fileURL + ' is in the white list');
expect(error.http_status).toBe(404);
expect(error.body).toBeDefined();
expect(error.body).toMatch('You requested a 404');
done();
};
const downloadWin = function () {
unexpectedCallbacks.httpWin();
done();
};
this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
},
DOWNLOAD_TIMEOUT
);
it('filetransfer.spec.14 should handle malformed urls', function (done) {
const fileURL = getMalformedUrl();
const downloadFail = function (error) {
// Note: Android needs the bad protocol to be added to the access list
//