diff --git a/FTPClient/entry/src/ohosTest/ets/test/FTPClient.test.ets b/FTPClient/entry/src/ohosTest/ets/test/FTPClient.test.ets index 934926659ca327d013410afc14f8679ed4f7be89..d870d6bc3a6627133e14de9b36420fc0c714f7aa 100644 --- a/FTPClient/entry/src/ohosTest/ets/test/FTPClient.test.ets +++ b/FTPClient/entry/src/ohosTest/ets/test/FTPClient.test.ets @@ -19,10 +19,13 @@ export default function FTPClientTest() { let password = '123456'; let host = '1.15.87.213'; let secureOptions: socket.TLSConnectOptions = undefined - let remoteRoot = undefined + let remoteRoot = "/" let currentFileList: FileInfo[] = []; let localUploadFilePath = undefined; let localUploadFileDir = undefined; + let localUploadEmptyFileDir = undefined; + let localUploadDeleteAllDir = undefined; + let localUploadDeleteSelfBufDir = undefined; let localDownloadFilePath = undefined; let localDownloadFileDir = undefined; let selectFilePath = undefined; @@ -34,7 +37,7 @@ export default function FTPClientTest() { let inputValue = 'click me' let operationType = '' - let option= { + let option = { ALPNProtocols: ["spdy/1", "http/1.1"], address: { address: '1.15.87.213', @@ -53,14 +56,18 @@ export default function FTPClientTest() { } } - beforeAll(function () { + beforeAll(function (done) { let context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext() ftpUtil = new NoTlsUtil(context) ftpUtil.setTag(); - + loginServer(done) + createSingleFile() + createFileDir() + createEmptyFileDirDeleteSelfBuf() + createEmptyFileDirDeleteAll() }) - async function loginServer() { + async function loginServer(done) { message = '初始化参数,准备登录' var loginInfo: AccessOptions = null if (secure) { @@ -114,85 +121,262 @@ export default function FTPClientTest() { onLoginStart(info) { }, onLoginSuccess(result) { + createEmptyFileDir() + uploadEmptyDir(done) }, onLoginErr(err: Error) { expect(0).assertEqual(1) - throw new Error("throw error:"+err) + done() } }) } } + function createSingleFile() { + try { + localUploadFilePath = AbilityDelegatorRegistry.getAbilityDelegator() + .getAppContext() + .cacheDir + '/' + 'clientToServer.txt' + let file = fs.openSync(localUploadFilePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) + let str = '' + for (let i = 0; i < 1024; i++) { + str += "客户端发送到服务端的信息,请查收\r\n" + } + fs.writeSync(file.fd, str) + fs.fsyncSync(file.fd) + fs.closeSync(file) + } catch (err) { + localUploadFilePath = undefined + } + } - describe('FTPClientTest', function () { - - it('loginserver', 0, async function () { - message = '初始化参数,准备登录' - var loginInfo: AccessOptions = null - if (secure) { - let context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext() - let keyData = await context.resourceManager.getRawFileContent('client_rsa_private.pem.unsecure') - let key = ''; - for (let i = 0; i < keyData.length; i++) { - let todo = keyData[i] - let item = String.fromCharCode(todo); - key += item; - } - option.secureOptions.key = key; - - let certData = await context.resourceManager.getRawFileContent('client.pem') - let cert = ''; - for (let i = 0; i < certData.length; i++) { - let todo = certData[i] - let item = String.fromCharCode(todo); - cert += item; - } - option.secureOptions.cert = cert; - - - let caData = await context.resourceManager.getRawFileContent('ca.pem') - let ca = ''; - for (let i = 0; i < caData.length; i++) { - let todo = caData[i] - let item = String.fromCharCode(todo); - ca += item; - } - option.secureOptions.ca[0] = ca; - - loginInfo = { - host: '1.15.87.213', - user: 'zhaodongyang', - password: '123456', - secure: 'implicit', - secureOptions: option - } - } else { - loginInfo = { - host: '1.15.87.213', - user: 'zhaodongyang', - password: '123456', - secure: false, - secureOptions: undefined - } + function createFileDir() { + try { + localUploadFileDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/client' + let localPath1 = localUploadFileDir + '/' + 'test1.txt' + let localPath2 = localUploadFileDir + '/' + 'test2.txt' + let localPath3 = localUploadFileDir + '/' + 'test3.txt' + + fs.mkdirSync(localUploadFileDir) + let file = fs.openSync(localPath1, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) + let str = '' + for (let i = 0; i < 1024; i++) { + str += "客户端发送到服务端的信息,请查收\r\n" } + fs.writeSync(file.fd, str) + fs.fsyncSync(file.fd) + fs.closeSync(file) + + let file1 = fs.openSync(localPath2, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) + let str1 = '789456123abcd' + fs.writeSync(file1.fd, str1) + fs.fsyncSync(file1.fd) + fs.closeSync(file1) + + + let file3 = fs.openSync(localPath3, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) + let str3 = '111111111111111111111111111111111111111111111111111111' + fs.writeSync(file3.fd, str3) + fs.fsyncSync(file3.fd) + fs.closeSync(file3) + + } catch (err) { + localUploadFileDir = undefined + } + } + + + function createEmptyFileDir() { + try { + localUploadEmptyFileDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/testempty' + fs.mkdirSync(localUploadEmptyFileDir) + } catch (err) { + localUploadEmptyFileDir = undefined + } + } + function createEmptyFileDirDeleteAll() { + try { + localUploadDeleteAllDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/deleteAll' + fs.mkdirSync(localUploadDeleteAllDir) + } catch (err) { + localUploadDeleteAllDir = undefined + } + } + + function createEmptyFileDirDeleteSelfBuf() { + try { + localUploadDeleteSelfBufDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/deleteSelfBuf' + fs.mkdirSync(localUploadDeleteSelfBufDir) + } catch (err) { + localUploadDeleteSelfBufDir = undefined + } + } + + function uploadEmptyDir(done) { if (ftpUtil) { - ftpUtil.doLogin(loginInfo, { - onLoginStart(info) { + if (!ftpUtil.getLogin()) { + operationType = '' + return + } + if (!localUploadEmptyFileDir || localUploadEmptyFileDir.length < 1) { + operationType = '' + return + } + remoteRoot="/" + if (!remoteRoot || remoteRoot.length < 1) { + operationType = '' + return + } + inputValue = "testempty" + if (!inputValue || inputValue.length < 1) { + operationType = '' + return + } + let regex = new RegExp(`^(?!_)(?!.*?_$)[a-zA-Z0-9_u4e00-u9fa5]+$`); //正则表达式 + if (!regex.test(inputValue)) { + operationType = '' + return + } + ftpUtil.uploadDir(localUploadEmptyFileDir, inputValue, { + uploadDirErr(err: Error) { + done() }, - onLoginSuccess(result) { - expect(0).assertEqual(0) + uploadDirStart(info) { }, - onLoginErr(err: Error) { - expect(0).assertEqual(1) + uploadDirSuccess(msg) { + uploadDeleteAllDir(done) + }, + uploadDirProgress(currentSize: number, totalSize: number) { } }) } + } - }) + function uploadDeleteAllDir(done) { + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + operationType = '' + return + } + if (!localUploadDeleteAllDir || localUploadDeleteAllDir.length < 1) { + operationType = '' + return + } + remoteRoot="/" + if (!remoteRoot || remoteRoot.length < 1) { + operationType = '' + return + } + inputValue = "deleteAll" + if (!inputValue || inputValue.length < 1) { + operationType = '' + return + } + let regex = new RegExp(`^(?!_)(?!.*?_$)[a-zA-Z0-9_u4e00-u9fa5]+$`); //正则表达式 + if (!regex.test(inputValue)) { + operationType = '' + return + } + ftpUtil.uploadDir(localUploadDeleteAllDir, inputValue, { + uploadDirErr(err: Error) { + done() + }, + uploadDirStart(info) { + }, + uploadDirSuccess(msg) { + uploadDeleteSelfBufDir(done) + }, + uploadDirProgress(currentSize: number, totalSize: number) { + } + }) + } + } - it('getCurrentDirectoryAndFileList', 0, function () { - loginServer(); + function uploadDeleteSelfBufDir(done) { + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + operationType = '' + return + } + if (!localUploadDeleteSelfBufDir || localUploadDeleteSelfBufDir.length < 1) { + operationType = '' + return + } + remoteRoot="/" + if (!remoteRoot || remoteRoot.length < 1) { + operationType = '' + return + } + inputValue = "deleteSelfBuf" + if (!inputValue || inputValue.length < 1) { + operationType = '' + return + } + let regex = new RegExp(`^(?!_)(?!.*?_$)[a-zA-Z0-9_u4e00-u9fa5]+$`); //正则表达式 + if (!regex.test(inputValue)) { + operationType = '' + return + } + ftpUtil.uploadDir(localUploadDeleteSelfBufDir, inputValue, { + uploadDirErr(err: Error) { + done() + }, + uploadDirStart(info) { + }, + uploadDirSuccess(msg) { + getCurrentDirectoryAndFileList(done) + }, + uploadDirProgress(currentSize: number, totalSize: number) { + } + }) + } + } + + + function getCurrentDirectoryAndFileList(done) { + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + return; + } + ftpUtil.getCurrentDirectory({ + currentDirectoryErr(err: Error) { + done() + }, + currentDirectoryStart(info) { + }, + currentDirectorySuccess(msg) { + remoteRoot = msg + let listName = ''; + if (remoteRoot == '' || remoteRoot == '\\' || remoteRoot == '/') { + listName = '' + } else { + listName = msg + } + ftpUtil.getList(listName, { + getListErr(err: Error) { + currentFileList = [] + done() + }, + getListStart(info) { + }, + getListSuccess(result: FileInfo[]) { + if (!result) { + currentFileList = [] + } else { + currentFileList = result; + } + } + }) + + } + }) + } + } + + describe('FTPClientTest', function () { + + it('getCurrentDirectoryAndFileList', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return; @@ -200,6 +384,7 @@ export default function FTPClientTest() { ftpUtil.getCurrentDirectory({ currentDirectoryErr(err: Error) { expect(0).assertEqual(1) + done() }, currentDirectoryStart(info) { }, @@ -215,17 +400,18 @@ export default function FTPClientTest() { getListErr(err: Error) { currentFileList = [] expect(0).assertEqual(1) + done() }, getListStart(info) { }, getListSuccess(result: FileInfo[]) { - expect(0).assertEqual(0) if (!result) { currentFileList = [] } else { currentFileList = result; } - + expect(0).assertEqual(0) + done() } }) @@ -234,27 +420,7 @@ export default function FTPClientTest() { } }) - - it('createSingleFile', 0, function () { - loginServer(); - try { - localUploadFilePath = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/' + 'clientToServer.txt' - let file = fs.openSync(localUploadFilePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) - let str = '' - for (let i = 0; i < 1024; i++) { - str += "客户端发送到服务端的信息,请查收\r\n" - } - fs.writeSync(file.fd, str) - fs.fsyncSync(file.fd) - fs.closeSync(file) - } catch (err) { - localUploadFilePath = undefined - expect(0).assertEqual(1) - } - }) - - it('uploadSingleFile', 0, function () { - loginServer(); + it('uploadSingleFile', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { operationType = '' @@ -268,18 +434,21 @@ export default function FTPClientTest() { operationType = '' return } + inputValue = "clientToServer.txt" if (!inputValue || inputValue.length < 1) { operationType = '' return } ftpUtil.uploadSingleFile(localUploadFilePath, inputValue, { uploadErr(err: Error) { - throw new Error("throw error:"+err) expect(0).assertEqual(1) + done() }, uploadStart(info) { }, uploadSuccess(msg: FTPResponse) { + expect(0).assertEqual(0) + done() }, uploadProgress(currentSize: number, totalSize: number) { } @@ -287,47 +456,7 @@ export default function FTPClientTest() { } }) - - it('createFileDir', 0, function () { - loginServer(); - try { - localUploadFileDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/client' - let localPath1 = localUploadFileDir + '/' + 'test1.txt' - let localPath2 = localUploadFileDir + '/' + 'test2.txt' - let localPath3 = localUploadFileDir + '/' + 'test3.txt' - - fs.mkdirSync(localUploadFileDir) - let file = fs.openSync(localPath1, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) - let str = '' - for (let i = 0; i < 1024; i++) { - str += "客户端发送到服务端的信息,请查收\r\n" - } - fs.writeSync(file.fd, str) - fs.fsyncSync(file.fd) - fs.closeSync(file) - - let file1 = fs.openSync(localPath2, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) - let str1 = '789456123abcd' - fs.writeSync(file1.fd, str1) - fs.fsyncSync(file1.fd) - fs.closeSync(file1) - - - let file3 = fs.openSync(localPath3, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) - let str3 = '111111111111111111111111111111111111111111111111111111' - fs.writeSync(file3.fd, str3) - fs.fsyncSync(file3.fd) - fs.closeSync(file3) - - - } catch (err) { - localUploadFileDir = undefined - } - }) - - - it('uploadDir', 0, function () { - loginServer(); + it('uploadDir', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { operationType = '' @@ -341,6 +470,7 @@ export default function FTPClientTest() { operationType = '' return } + inputValue = "client" if (!inputValue || inputValue.length < 1) { operationType = '' return @@ -353,10 +483,13 @@ export default function FTPClientTest() { ftpUtil.uploadDir(localUploadFileDir, inputValue, { uploadDirErr(err: Error) { expect(0).assertEqual(1) + done() }, uploadDirStart(info) { }, uploadDirSuccess(msg) { + expect(0).assertEqual(0) + done() }, uploadDirProgress(currentSize: number, totalSize: number) { } @@ -364,11 +497,10 @@ export default function FTPClientTest() { } }) - it('downloadSingleFile', 0, function () { - loginServer(); - - let localPath = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/' + 'downloadfromServer.txt' - + it('downloadSingleFile', 0, function (done) { + let localPath = AbilityDelegatorRegistry.getAbilityDelegator() + .getAppContext() + .cacheDir + '/' + 'downloadfromServer.txt' let file = fs.openSync(localPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) if (ftpUtil) { if (!ftpUtil.getLogin()) { @@ -377,12 +509,14 @@ export default function FTPClientTest() { if (!remoteRoot || remoteRoot.length < 1) { return } + selectFilePath = "clientToServer.txt"; if (!selectFilePath || selectFilePath.length < 1) { return } ftpUtil.downloadSingleFile(localPath, selectFilePath, { downloadErr(err: Error) { expect(0).assertEqual(1) + done() }, downloadStart(info) { }, @@ -400,6 +534,8 @@ export default function FTPClientTest() { } fs.fsyncSync(file.fd); fs.close(file) + expect(0).assertEqual(0) + done() }, downloadProgress(currentSize: number, totalSize: number) { } @@ -407,36 +543,40 @@ export default function FTPClientTest() { } }) - it('downloadDir', 0, function () { - loginServer(); - let localDir = "/data/storage/el2/base/haps/entry/cache" + '/server' - - if (ftpUtil) { - if (!ftpUtil.getLogin()) { - return - } - if (!remoteRoot || remoteRoot.length < 1) { - return - } - if (!selectDirPath || selectDirPath.length < 1) { - return - } - ftpUtil.downloadDir(localDir, selectDirPath, { - downloadDirErr(err: Error) { - expect(0).assertEqual(1) - }, - downloadDirStart(info) { - }, - downloadDirSuccess(msg) { - }, - downloadDirProgress(currentSize: number, totalSize: number) { + it('downloadDir', 0, function (done) { + setTimeout(()=>{ + let localDir = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().cacheDir + '/server' + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + return } - }) - } + remoteRoot="/" + if (!remoteRoot || remoteRoot.length < 1) { + return + } + selectDirPath = "client" + if (!selectDirPath || selectDirPath.length < 1) { + return + } + ftpUtil.downloadDir(localDir, selectDirPath, { + downloadDirErr(err: Error) { + expect(0).assertEqual(1) + done() + }, + downloadDirStart(info) { + }, + downloadDirSuccess(msg) { + expect(0).assertEqual(0) + done() + }, + downloadDirProgress(currentSize: number, totalSize: number) { + } + }) + } + },1200) }) - it('getFileSize', 0, function () { - loginServer(); + it('getFileSize', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return @@ -444,23 +584,26 @@ export default function FTPClientTest() { if (!remoteRoot || remoteRoot.length < 1) { return } + selectFilePath = "clientToServer.txt"; if (!selectFilePath || selectFilePath.length < 1) { return } ftpUtil.getFileSize(selectFilePath, { getSizeErr(err: Error) { expect(0).assertEqual(1) + done() }, getSizeStart(info) { }, getSizeSuccess(result: number) { + expect(0).assertEqual(0) + done() } }) } }) - it('getServerFeatures', 0, function () { - loginServer(); + it('getServerFeatures', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return @@ -468,17 +611,19 @@ export default function FTPClientTest() { ftpUtil.getServerFeatures({ featuresErr(err: Error) { expect(0).assertEqual(1) + done() }, featuresStart(info) { }, featuresSuccess(msg: Map) { + expect(0).assertEqual(0) + done() } }) } }) - it('getLastModifyTime', 0, function () { - loginServer(); + it('getLastModifyTime', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return @@ -486,51 +631,71 @@ export default function FTPClientTest() { if (!remoteRoot || remoteRoot.length < 1) { return } + selectFilePath = "clientToServer.txt"; + if (!selectFilePath || selectFilePath.length < 1) { return } ftpUtil.getLastModify(selectFilePath, { lastModifyErr(err: Error) { expect(0).assertEqual(1) + done() }, lastModifyStart(info) { }, lastModifySuccess(msg: Date) { + expect(0).assertEqual(0) + done() } }) } }) - it('deleteSingleFile', 0, function () { - loginServer(); - - if (ftpUtil) { - if (!ftpUtil.getLogin()) { - return - } - if (!remoteRoot || remoteRoot.length < 1) { - return - } - if (!selectFilePath || selectFilePath.length < 1) { - return - } - ftpUtil.deleteFile(selectFilePath, { - deleteFileErr(err: Error) { - expect(0).assertEqual(1) - }, - deleteFileStart(info) { - }, - deleteFileSuccess(msg: FTPResponse) { + it('renameFile', 0, function (done) { + setTimeout(()=>{ + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + operationType = '' + return + } + if (!remoteRoot || remoteRoot.length < 1) { + operationType = '' + return } - }) - } - }) + inputValue = "clientToServerNew.txt" + if (!inputValue || inputValue.length < 1) { + operationType = '' + return + } + selectFilePath = "clientToServer.txt"; + + if (!selectFilePath || selectFilePath.length < 1) { + return + } + + ftpUtil.renameFile(inputValue, selectFilePath, { + renameFileErr(err: Error) { + operationType = '' + expect(0).assertEqual(1) + done() + }, + renameFileStart(info) { + operationType = '' + }, + renameFileSuccess(result: FTPResponse) { + operationType = '' + expect(0).assertEqual(0) + done() + } + }) + } + },600) + }) - it('cdToParentDirectory', 0, function () { - loginServer(); + it('cdToParentDirectory', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return @@ -541,19 +706,20 @@ export default function FTPClientTest() { ftpUtil.cdToParentDirectory({ cdToParentDirectoryErr(err: Error) { expect(0).assertEqual(1) + done() }, cdToParentDirectoryStart(info) { }, cdToParentDirectorySuccess(res: FTPResponse) { + expect(0).assertEqual(0) + done() } }) } }) - it('ensureRemotePath', 0, function () { - loginServer(); - + it('ensureRemotePath', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { operationType = '' @@ -563,6 +729,7 @@ export default function FTPClientTest() { operationType = '' return } + inputValue = "client" if (!inputValue || inputValue.length < 1) { operationType = '' return @@ -574,29 +741,32 @@ export default function FTPClientTest() { } ftpUtil.ensureRemotePath(inputValue, { ensureRemotePathErr(err: Error) { - expect(0).assertEqual(1) operationType = '' + expect(0).assertEqual(1) + done() }, ensureRemotePathStart(info) { operationType = '' }, ensureRemotePathSuccess(result) { operationType = '' + expect(0).assertEqual(0) + done() } }) } }) - it('deleteEmptyDirectory', 0, function () { - loginServer(); - + it('deleteEmptyDirectory', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return } + remoteRoot="/" if (!remoteRoot || remoteRoot.length < 1) { return } + selectDirPath = "/testempty" if (!selectDirPath || selectDirPath.length < 1) { return } @@ -604,26 +774,61 @@ export default function FTPClientTest() { ftpUtil.deleteEmptyDirectory(selectDirPath, { deleteEmptyDirectoryErr(err: Error) { expect(0).assertEqual(1) + done() }, deleteEmptyDirectoryStart(info) { }, deleteEmptyDirectorySuccess(result: FTPResponse) { + expect(0).assertEqual(0) + done() } }) } }) + it('deleteSingleFile', 0, function (done) { + setTimeout(() => { + if (ftpUtil) { + if (!ftpUtil.getLogin()) { + return + } + remoteRoot="/" + if (!remoteRoot || remoteRoot.length < 1) { + return + } + selectFilePath = "/clientToServerNew.txt"; + + if (!selectFilePath || selectFilePath.length < 1) { + return + } + ftpUtil.deleteFile(selectFilePath, { + deleteFileErr(err: Error) { + expect(0).assertEqual(1) + done() + }, + deleteFileStart(info) { + }, + deleteFileSuccess(msg: FTPResponse) { + expect(0).assertEqual(0) + done() + } + }) + } + }, 10000) + }) - it('deleteAll', 0, function () { - loginServer(); + + it('deleteAll', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return } + remoteRoot="/" if (!remoteRoot || remoteRoot.length < 1) { return } + selectDirPath="/deleteAll" if (!selectDirPath || selectDirPath.length < 1) { return } @@ -631,83 +836,66 @@ export default function FTPClientTest() { ftpUtil.deleteAll(selectDirPath, { deleteAllErr(err: Error) { expect(0).assertEqual(1) + done() }, deleteAllStart(info) { }, deleteAllSuccess(result) { + expect(0).assertEqual(0) + done() } }) } }) - it('deleteAllButSelf', 0, function () { - loginServer(); - + it('deleteAllButSelf', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return } + remoteRoot="/" if (!remoteRoot || remoteRoot.length < 1) { return } - ftpUtil.deleteAllButSelf({ - deleteAllButSelfErr(err: Error) { + remoteChildPath="/deleteSelfBuf" + ftpUtil.setWorkingDirectory(remoteChildPath, { + setWorkingDirectoryErr(err: Error) { expect(0).assertEqual(1) + done() }, - deleteAllButSelfStart(info) { + setWorkingDirectoryStart(info) { }, - deleteAllButSelfSuccess(result) { + setWorkingDirectorySuccess(result: FTPResponse) { + ftpUtil.deleteAllButSelf({ + deleteAllButSelfErr(err: Error) { + expect(0).assertEqual(1) + done() + }, + deleteAllButSelfStart(info) { + }, + deleteAllButSelfSuccess(result) { + expect(0).assertEqual(0) + done() + } + }) } }) } }) - it('renameFile', 0, function () { - loginServer(); - - if (ftpUtil) { - if (!ftpUtil.getLogin()) { - operationType = '' - return - } - if (!remoteRoot || remoteRoot.length < 1) { - operationType = '' - return - } - if (!inputValue || inputValue.length < 1) { - operationType = '' - return - } - if (!selectFilePath || selectFilePath.length < 1) { - return - } - - ftpUtil.renameFile(inputValue, selectFilePath, { - renameFileErr(err: Error) { - expect(0).assertEqual(1) - operationType = '' - }, - renameFileStart(info) { - operationType = '' - }, - renameFileSuccess(result: FTPResponse) { - operationType = '' - } - }) - } - }) - it('setWorkingDirectory', 0, function () { - loginServer(); + it('setWorkingDirectory', 0, function (done) { if (ftpUtil) { if (!ftpUtil.getLogin()) { return } + remoteRoot="/" if (!remoteRoot || remoteRoot.length < 1) { return } + remoteChildPath="/client" if (!remoteChildPath || remoteChildPath.length < 1) { return } @@ -715,10 +903,13 @@ export default function FTPClientTest() { ftpUtil.setWorkingDirectory(remoteChildPath, { setWorkingDirectoryErr(err: Error) { expect(0).assertEqual(1) + done() }, setWorkingDirectoryStart(info) { }, setWorkingDirectorySuccess(result: FTPResponse) { + expect(0).assertEqual(0) + done() } }) } diff --git a/POP3Client/entry/src/ohosTest/ets/test/POP3.test.ets b/POP3Client/entry/src/ohosTest/ets/test/POP3.test.ets index 077034f7f46eebdc0c0a3994bbeb1827e07e12f0..b22ec8809d56d7657d4c9097de1b6a21982f4a80 100644 --- a/POP3Client/entry/src/ohosTest/ets/test/POP3.test.ets +++ b/POP3Client/entry/src/ohosTest/ets/test/POP3.test.ets @@ -25,7 +25,6 @@ export default function POP3Test() { describe('POP3Test', function () { - async function login() { globalThis.client = undefined; let jumpOption = undefined; @@ -98,7 +97,7 @@ export default function POP3Test() { sendList() }) - async function sendList(){ + async function sendList() { try { if (client) { let result = await client.LIST() @@ -131,42 +130,41 @@ export default function POP3Test() { } - it('sendSTAT', 0, function () { - setTimeout(async ()=>{ - isListShow = false - msgList = [] - listData = [] - selectMsgNum = -1; - try { - if (client) { - let result = await client.STAT() - if (result && result.toString() && result.toString().length > 0) { - expect(0).assertEqual(0) - let arr = result.toString().split(' ') - if (arr && arr.length >= 1) { - let totalMailSize = parseInt(arr[0]); - if (totalMailSize > 0) { - isListShow = true; - listData.push(`获取到STAT命令的结果:${'\r\n'}${result}`) - return - } + it('sendSTAT', 0, async function (done) { + isListShow = false + msgList = [] + listData = [] + selectMsgNum = -1; + try { + if (client) { + let result = await client.STAT() + if (result && result.toString() && result.toString().length > 0) { + let arr = result.toString().split(' ') + if (arr && arr.length >= 1) { + let totalMailSize = parseInt(arr[0]); + if (totalMailSize > 0) { + isListShow = true; + listData.push(`获取到STAT命令的结果:${'\r\n'}${result}`) + return } } - listData.push(`获取到STAT命令的结果:${'\r\n'}获取到的结果为空`) - } else { - expect(0).assertEqual(1) - listData.push(`账号未登录,请需重新登录`) } - } catch (err) { + listData.push(`获取到STAT命令的结果:${'\r\n'}获取到的结果为空`) + done() + } else { expect(0).assertEqual(1) - console - listData.push(`获取STAT命令结果失败:${'\r\n'}${err.message}`) + listData.push(`账号未登录,请需重新登录`) + done() } - },200) + } catch (err) { + expect(0).assertEqual(1) + listData.push(`获取STAT命令结果失败:${'\r\n'}${err.message}`) + done() + } }) - it('sendNOOP', 0, function () { - setTimeout(async ()=>{ + it('sendNOOP', 0, function (done) { + setTimeout(async () => { msgList = [] listData = [] selectMsgNum = -1; @@ -174,20 +172,23 @@ export default function POP3Test() { if (client) { let result = await client.NOOP() listData.push(`获取到NOOP命令的结果:${'\r\n'}${JSON.stringify(result)}`) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取NOOP命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } - },200) + }, 200) }) - it('sendUIDL', 0, function () { - setTimeout(async ()=>{ + it('sendUIDL', 0, function (done) { + setTimeout(async () => { listData = [] try { selectMsgNum = 1; @@ -198,22 +199,24 @@ export default function POP3Test() { if (client) { let result = await client.UIDL(selectMsgNum + '') listData.push(`获取到UIDL命令的结果:${'\r\n'}${result}`) - expect(0).assertEqual(0) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取UIDL命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } selectMsgNum = -1; }) }) - it('sendLAST', 0, function () { - setTimeout(async ()=>{ + it('sendLAST', 0, function (done) { + setTimeout(async () => { msgList = [] listData = [] selectMsgNum = -1; @@ -221,21 +224,23 @@ export default function POP3Test() { if (client) { let result = await client.LAST() listData.push(`获取到LAST命令的结果:${'\r\n'}${result}`) - expect(0).assertEqual(0) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取LAST命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } - },200) + }, 200) }) - it('sendRSET', 0, function () { - setTimeout(async ()=>{ + it('sendRSET', 0, function (done) { + setTimeout(async () => { listData = [] try { selectMsgNum = 3 @@ -247,22 +252,24 @@ export default function POP3Test() { listData.push(`发送DELE命令删除的结果:${'\r\n'}${delResult}`) let result = await client.RSET() // 用于撤销DELE命令 所以不需要等DELE命令返回 listData.push(`发送RSET命令取消删除操作的结果:${'\r\n'}${result}`) - expect(0).assertEqual(0) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取RSET命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } selectMsgNum = -1; - },200) + }, 200) }) - it('sendRETR', 0, function () { - setTimeout(async ()=>{ + it('sendRETR', 0, function (done) { + setTimeout(async () => { listData = [] try { selectMsgNum = 4 @@ -281,49 +288,56 @@ export default function POP3Test() { listData.push(`ETR命令的结果显示:${'\r\n'}${result.toString()}`) } expect(0).assertEqual(0) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取RETR命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } selectMsgNum = -1; - },200) + }, 200) }) - it('sendDELE', 0,function () { - setTimeout(async ()=>{ + it('sendDELE', 0, function (done) { + setTimeout(async () => { listData = [] try { selectMsgNum = 4 if (selectMsgNum === -1) { expect(0).assertEqual(1) + done() return } if (client) { let result = await client.DELE(selectMsgNum + '') listData.push(`获取到DELE命令的结果:${'\r\n'}${result}`) expect(0).assertEqual(0) + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取DELE命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } selectMsgNum = -1; - },500) + }, 500) }) - it('sendQUIT', 0, function () { - setTimeout(async ()=>{ + it('sendQUIT', 0, function (done) { + setTimeout(async () => { msgList = [] listData = [] selectMsgNum = -1; @@ -333,17 +347,19 @@ export default function POP3Test() { const [quitInfo] = await client.QUIT(); listData.push(`获取到QUIT命令的结果:${'\r\n'}${quitInfo}`) - + done() } else { listData.push(`账号未登录,请需重新登录`) expect(0).assertEqual(1) + done() } } catch (err) { listData.push(`获取RETR命令结果失败:${'\r\n'}${err.message}`) expect(0).assertEqual(1) + done() } - },800) + }, 800) }) }) diff --git a/SMTPClient/entry/src/ohosTest/ets/test/SMTP.test.ets b/SMTPClient/entry/src/ohosTest/ets/test/SMTP.test.ets index 74d436df056be9a835ec09e0a4097966ade914c4..8adcbfef46f13d695679886c27af5af81164f943 100644 --- a/SMTPClient/entry/src/ohosTest/ets/test/SMTP.test.ets +++ b/SMTPClient/entry/src/ohosTest/ets/test/SMTP.test.ets @@ -20,13 +20,14 @@ export default function SMTPTest() { let attachment: Array = [] - beforeAll(function () { + beforeAll(function (done) { // Presets an action, which is performed only once before all test cases of the test suite start. // This API supports only one parameter: preset action function. - loginmail() + loginmail(done) + createAttachment() }) - async function loginmail() { + async function loginmail(done) { try { if (!globalThis.client) { let client: SMTPClient = undefined; @@ -93,18 +94,12 @@ export default function SMTPTest() { if (globalThis.client) { if (!globalThis.client.isLogin()) { globalThis.client.login((err, result) => { - if (!err && result == true) { - expect(0).assertContain(0) - } else { - expect(0).assertContain(1) - return - } + done() }) } } } catch (err) { - expect(0).assertContain(1) - return + done() } } @@ -198,100 +193,13 @@ export default function SMTPTest() { } } catch (err) { - console.log("SMTP------------>createAttachment------>error:" + err) expect(0).assertEqual(1) } } describe('SMTPTest', function () { - - - it('login', 0, async function () { - // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - try { - if (!globalThis.client) { - let client: SMTPClient = undefined; - if (secure) { - let option: socket.TLSConnectOptions = { - ALPNProtocols: ["spdy/1", "http/1.1"], - address: { - address: 'smtp.qq.com', - port: 465, - family: 1 - }, - secureOptions: { - key: '', - cert: '', - ca: [''], - useRemoteCipherPrefer: true, - } - } - let context: Context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext() - let ca0Data = await context.resourceManager.getRawFileContent('QQMailMiddle.pem') - let ca0 = ''; - for (let i = 0; i < ca0Data.length; i++) { - let todo = ca0Data[i] - let item = String.fromCharCode(todo); - ca0 += item; - } - // @ts-ignore - option.secureOptions.ca[0] = ca0; - - let ca1Data = await context.resourceManager.getRawFileContent('QQMailRoot.pem') - let ca1 = ''; - for (let i = 0; i < ca1Data.length; i++) { - let todo = ca1Data[i] - let item = String.fromCharCode(todo); - ca1 += item; - } - // @ts-ignore - option.secureOptions.ca[1] = ca1; - - client = new SMTPClient({ - user: '479868299@qq.com', - password: 'bfxeualuaxambged', - host: 'smtp.qq.com', - port: 465, - timeout: 30000, - authentication: [AUTH_METHODS.LOGIN], - ssl: option, - tls: undefined - }); - } else { - client = new SMTPClient({ - user: '479868299@qq.com', - password: 'bfxeualuaxambged', - host: 'smtp.qq.com', - port: 25, - timeout: 30000, - authentication: [AUTH_METHODS.LOGIN], - ssl: false, - tls: undefined - }); - } - globalThis.client = client - } - if (globalThis.client) { - if (!globalThis.client.isLogin()) { - globalThis.client.login((err, result) => { - if (!err && result == true) { - expect(0).assertContain(0) - } else { - expect(0).assertContain(1) - } - }) - } - } - } catch (err) { - console.log("SMPT----------->LOGIN------->ERROR:" + err) - expect(0).assertContain(1) - } - }) - - it('sendMail', 0, async function () { - setTimeout(async ()=>{ - createAttachment() + it('sendMail', 0, async function (done) { try { let msg: Message | MessageHeaders = { text: '鸿蒙客户端发送的邮件内容,哈哈哈哈哈哈哈', @@ -303,12 +211,11 @@ export default function SMTPTest() { attachment: attachment } const message = await globalThis.client.sendAsync(msg) + done() } catch (err) { - console.log("SMTP---------------->sendmail-------------->error:" + err) expect(0).assertContain(1) + done() } - },300) - }) })