diff --git a/storage/storagefileiov9jstest/src/main/js/test/List.test.js b/storage/storagefileiov9jstest/src/main/js/test/List.test.js index fe3fc11330093e40ca9cf29b6ecfb39c22c49787..ac3fbd15bd511a071ef99ea2828c6c40a6d7b57e 100644 --- a/storage/storagefileiov9jstest/src/main/js/test/List.test.js +++ b/storage/storagefileiov9jstest/src/main/js/test/List.test.js @@ -13,12 +13,18 @@ * limitations under the License. */ +import fileIOFileLock from './members/fileLock.test.js' +import fileIOListfile from './members/listFile.test.js' +import fileIOMoveFile from './members/moveFile.test.js' import fileIOOpen from './members/open.test.js' import fileIORead from './members/read.test.js' import fileIOStat from './members/stat.test.js' import fileIOTruncate from './members/truncate.test.js' import fileIOWrite from './members/write.test.js' export default function testsuite() { + fileIOFileLock() + fileIOListfile() + fileIOMoveFile() fileIOOpen() fileIORead() fileIOStat() diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/fileLock.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/fileLock.test.js new file mode 100644 index 0000000000000000000000000000000000000000..d4126247fe49c63698fbed72e63d95931c22ff0a --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/fileLock.test.js @@ -0,0 +1,645 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +import { + fileIO, FILE_CONTENT, prepareFile, nextFileName, isIntNum, describe, it, expect, +} from '../Common'; + +export default function fileIOFileLock() { + describe('fileIO_fs_file_lock', function () { + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0000 + * @tc.name fileIO_test_filelock_promise_000 + * @tc.desc Test lock() interfaces. argument is : default. + * Open the file, file lock() and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_filelock_promise_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0100 + * @tc.name fileIO_test_filelock_promise_001 + * @tc.desc Test lock() interfaces. argument is : true. + * Open the file, file lock(true) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(true); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0200 + * @tc.name fileIO_test_filelock_promise_002 + * @tc.desc Test lock() interfaces. argument is : false. + * Open the file, file lock(false) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(false); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0300 + * @tc.name fileIO_test_filelock_promise_003 + * @tc.desc Test tryLock() interfaces. argument is : default. + * Open the file, file lock() ,tryLock() and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0400 + * @tc.name fileIO_test_filelock_promise_004 + * @tc.desc Test tryLock() interfaces. argument is : true. + * Open the file, file lock() ,tryLock(true) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_004', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(true); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_004 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0500 + * @tc.name fileIO_test_filelock_promise_005 + * @tc.desc Test tryLock() interfaces. argument is : false. + * Open the file, file lock() ,tryLock(false) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_005', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(false); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_005 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0600 + * @tc.name fileIO_test_filelock_promise_006 + * @tc.desc Open the file, no file lock(), tryLock(), unlock(), + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_006', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + file.tryLock(); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_006 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_0700 + * @tc.name fileIO_test_filelock_promise_007 + * @tc.desc Open the file, no file lock(), no tryLock(), unlock(), + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_007', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_007'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_filelock_promise_007 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0000 + * @tc.name fileIO_test_filelock_promise_abnormal_000 + * @tc.desc Test lock() interfaces. argument is : null. + * Open the file, file lock(null). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(null); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_000 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0100 + * @tc.name fileIO_test_filelock_promise_abnormal_001 + * @tc.desc Test lock() interfaces. argument is : true, true. + * Open the file, file lock(true, true). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(true, true); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_001 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0200 + * @tc.name fileIO_test_filelock_promise_abnormal_002 + * @tc.desc Test lock() interfaces. argument is : -1. + * Open the file, file lock(-1). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(-1); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0300 + * @tc.name fileIO_test_filelock_promise_abnormal_003 + * @tc.desc Test tryLock() interfaces. argument is : null. + * Open the file, file lock(), tryLock(null). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(null); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0400 + * @tc.name fileIO_test_filelock_promise_abnormal_004 + * @tc.desc Test tryLock() interfaces. argument is : true, true. + * Open the file, file lock(), tryLock(true, true). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_004', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(true, true); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0500 + * @tc.name fileIO_test_filelock_promise_abnormal_005 + * @tc.desc Test tryLock() interfaces. argument is : -1. + * Open the file, file lock(), tryLock(-1). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_005', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(-1); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_PROMISE_ABNORMAL_0600 + * @tc.name fileIO_test_filelock_promise_abnormal_006 + * @tc.desc Test unlock() interfaces. argument is : true. + * Open the file, file lock(), tryLock(), unlock(true), + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_promise_abnormal_006', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_promise_abnormal_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + await file.lock(); + file.tryLock(); + file.unlock(true); + expect(false).assertTrue(); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_promise_abnormal_006 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_0000 + * @tc.name fileIO_test_filelock_callback_000 + * @tc.desc Test lock() interfaces. argument is : default. + * Open the file, file lock() and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + file.lock((err) => { + if (err) { + console.log('fileIO_test_filelock_callback_000 err ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_filelock_callback_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_0100 + * @tc.name fileIO_test_filelock_callback_001 + * @tc.desc Test lock() interfaces. argument is : true. + * Open the file, file lock(true) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + file.lock(true, (err) => { + if (err) { + console.log('fileIO_test_filelock_callback_001 err ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_filelock_callback_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_0200 + * @tc.name fileIO_test_filelock_callback_002 + * @tc.desc Test lock() interfaces. argument is : false. + * Open the file, file lock(false) and unlock() + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + file.lock(false, (err) => { + if (err) { + console.log('fileIO_test_filelock_callback_002 err ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + file.unlock(); + expect(true).assertTrue(); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_filelock_callback_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_ABNORMAL_0000 + * @tc.name fileIO_test_filelock_callback_abnormal_000 + * @tc.desc Test lock() interfaces. argument is : null. + * Open the file, file lock(null). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_abnormal_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_abnormal_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + file.lock(null, (err) => { + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_callback_abnormal_000 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_ABNORMAL_0100 + * @tc.name fileIO_test_filelock_callback_abnormal_001 + * @tc.desc Test lock() interfaces. argument is : true, true. + * Open the file, file lock(true, true). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_abnormal_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_abnormal_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + file.lock(true, true, (err) => { + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_callback_abnormal_001 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number FILE_TEST_FILELOCK_CALLBACK_ABNORMAL_0200 + * @tc.name fileIO_test_filelock_callback_abnormal_002 + * @tc.desc Test lock() interfaces. argument is : -1. + * Open the file, file lock(-1). + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_filelock_callback_abnormal_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_filelock_callback_abnormal_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + + try { + expect(isIntNum(file.fd)).assertTrue(); + file.lock(-1, (err) => { + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + console.log('fileIO_test_filelock_callback_abnormal_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); +}); +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/listFile.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/listFile.test.js new file mode 100644 index 0000000000000000000000000000000000000000..a7093d793eec3a9d602a0be251768777137b6bdc --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/listFile.test.js @@ -0,0 +1,1269 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +import { + fileIO, FILE_CONTENT, prepareFile, nextFileName, describe, it, expect, randomString +} from '../Common'; + +export default function fileIOListfile() { + describe('fileIO_fs_listfile', function () { + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0000 + * @tc.name fileIO_test_listfile_sync_000 + * @tc.desc Test listFileSync()interfaces. + * Only path, do not filter file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_listfile_sync_000', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_000'); + let fpath1 = dpath + '/listfile_sync_000.txt'; + let fpath2 = dpath + '/listfile_sync_000.doc'; + let fpath3 = dpath + '/listfile_sync_000.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let dirents = fileIO.listFileSync(dpath); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_000 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0100 + * @tc.name fileIO_test_listfile_sync_001 + * @tc.desc Test listFileSync() interfaces. + * Do not have prop filter.Filter files by quantity only. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_001', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_001'); + let fpath1 = dpath + '/listfile_sync_001.txt'; + let fpath2 = dpath + '/listfile_sync_001.doc'; + let fpath3 = dpath + '/listfile_sync_001.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let dirents = fileIO.listFileSync(dpath, { + listNum: 2, + recursion: false + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_001 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0200 + * @tc.name fileIO_test_listfile_sync_002 + * @tc.desc Test listFileSync() interfaces. + * Do not have prop listNum. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_002', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_002'); + let fpath1 = dpath + '/listfile_sync_002.txt'; + let fpath2 = dpath + '/listfile_sync_002.doc'; + let fpath3 = dpath + '/listfile_sync_002.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + recursion: false, + filter:{ + suffix: [".txt", ".doc", ".png"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_002 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0300 + * @tc.name fileIO_test_listfile_sync_003 + * @tc.desc Test listFileSync() interfaces. + * Do not have prop suffix. Not filter files by suffix. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_003', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_003'); + let fpath1 = dpath + '/listfile_sync_003.txt'; + let fpath2 = dpath + '/listfile_sync_003.doc'; + let fpath3 = dpath + '/listfile_sync_003.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + listNum: 4, + recursion: false, + filter:{ + displayName: ["*listfile*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_003 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0400 + * @tc.name fileIO_test_listfile_sync_004 + * @tc.desc Test listFileSync() interfaces. + * Scanfile.Retain six files in the three-level directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_004', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_004'); + let fpath1 = dpath + '/listfile_sync_004.txt'; + let fpath2 = dpath + '/listfile_sync_004.doc'; + let fpath3 = dpath + '/listfile_sync_004.png'; + let ddpath = dpath + '/kids'; + let ffpath1 = ddpath + '/firstc.txt'; + let ffpath2 = ddpath + '/firsta.txt'; + let ffpath3 = ddpath + '/aabbcc.hap'; + let dddpath = ddpath + '/' + randomString(5); + let fffpath1 = dddpath + '/makef.doc'; + let fffpath2 = dddpath + '/akasd.txt'; + let fffpath3 = dddpath + '/tdd.log'; + let fffpath4 = dddpath + '/tdd.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + fileIO.mkdirSync(dddpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + listNum: 6, + recursion: true, + filter:{ + suffix: [".txt", ".doc"], + displayName: ["*listfile*", "*first*", "*akasd*", "*tdd*", "*makef*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 6).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_004 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0500 + * @tc.name fileIO_test_listfile_sync_005 + * @tc.desc Test listFileSync() interfaces. + * Filter out files whose suffix is not '. txt' and does not meet other conditions.Two files are retained. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_005', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_005') ; + let fpath1 = dpath + '/listfile_sync_005.txt'; + let fpath2 = dpath + '/listfile_sync_005.doc'; + let fpath3 = dpath + '/listfile_sync_005.png'; + let fpath4 = dpath + '/timer.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*time*"], + fileSizeOver: 4, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_005 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0600 + * @tc.name fileIO_test_listfile_sync_006 + * @tc.desc Test listFileSync() interfaces. + * Invalid file suffix name format. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_006', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_006'); + let fpath1 = dpath + '/listfile_sync_006.txt'; + let fpath2 = dpath + '/listfile_sync_006.doc'; + let fpath3 = dpath + '/listfile_sync_006.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFileSync(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: ["%.txt%"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time -3 + } + }); + expect(false).assertTrue(); + } catch (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_listfile_sync_006 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0700 + * @tc.name fileIO_test_listfile_sync_007 + * @tc.desc Test listFileSync() interfaces. + * Filter out all files because the size of all files less than 12. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_007', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_007'); + let fpath1 = dpath + '/listfile_sync_007.txt'; + let fpath2 = dpath + '/listfile_sync_007.doc'; + let fpath3 = dpath + '/listfile_sync_007.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*"], + fileSizeOver: 12, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 0).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_007 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0800 + * @tc.name fileIO_test_listfile_sync_008 + * @tc.desc Test listFileSync() interfaces. + * listNum is 0.Traverse all files. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_008', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_008'); + let fpath1 = dpath + '/listfile_sync_008.txt'; + let fpath2 = dpath + '/listfile_sync_008.doc'; + let fpath3 = dpath + '/listfile_sync_008.png'; + let fpath4 = dpath + '/trespass.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = fileIO.listFileSync(dpath, { + listNum: 0, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*trespass*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (err) { + console.log('fileIO_test_listfile_sync_008 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_0900 + * @tc.name fileIO_test_listfile_sync_009 + * @tc.desc Test listFileSync() interfaces. + * Missing all arguments. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_009', 0, async function () { + + try { + fileIO.listFileSync(); + expect(false).assertTrue(); + } catch (err) { + console.log('fileIO_test_listfile_sync_009 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_SYNC_1000 + * @tc.name fileIO_test_listfile_sync_010 + * @tc.desc Test listFileSync() interfaces. + * Invalid option args. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_sync_010', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_sync_010'); + let fpath1 = dpath + '/listfile_sync_009.txt'; + let fpath2 = dpath + '/listfile_sync_009.doc'; + let fpath3 = dpath + '/listfile_sync_009.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + fileIO.listFileSync(dpath, 4); + expect(false).assertTrue(); + } catch (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_listfile_sync_010 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + } + }); + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0000 + * @tc.name fileIO_test_listfile_async_000 + * @tc.desc Test listFile() interface. Callback. + * Only path, do not filter file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_listfile_async_000', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_000') ; + let fpath1 = dpath + '/listfile_async_000.txt'; + let fpath2 = dpath + '/listfile_async_000.doc'; + let fpath3 = dpath + '/listfile_async_000.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + fileIO.listFile(dpath, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_000 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_000 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0100 + * @tc.name fileIO_test_listfile_async_001 + * @tc.desc Test listFile() interface. Promise. + * Only path, do not filter file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_001', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_001'); + let fpath1 = dpath + '/listfile_async_001.txt'; + let fpath2 = dpath + '/listfile_async_001_1.doc'; + let fpath3 = dpath + '/listfile_async_001.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let dirents = await fileIO.listFile(dpath); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_001 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0200 + * @tc.name fileIO_test_listfile_async_002 + * @tc.desc Test listFile() interfaces. Callback. + * Do not have prop filter.Filter files by quantity only. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_002', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_002'); + let fpath1 = dpath + '/listfile_async_002.txt'; + let fpath2 = dpath + '/listfile_async_002.doc'; + let fpath3 = dpath + '/listfile_async_002.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + fileIO.listFile(dpath, { + listNum: 2, + recursion: false + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_002 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_002 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0300 + * @tc.name fileIO_test_listfile_async_003 + * @tc.desc Test listFile() interfaces. Promise. + * Do not have prop filter.Filter files by quantity only. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_003', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_003'); + let fpath1 = dpath + '/listfile_async_003.txt'; + let fpath2 = dpath + '/listfile_async_003.doc'; + let fpath3 = dpath + '/listfile_async_003.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let dirents = await fileIO.listFile(dpath, { + listNum: 2, + recursion: false + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_003 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0400 + * @tc.name fileIO_test_listfile_async_004 + * @tc.desc Test listFile() interface. Callback. + * Do not have prop listNum. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_004', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_004'); + let fpath1 = dpath + '/listfile_async_004.txt'; + let fpath2 = dpath + '/listfile_async_004.doc'; + let fpath3 = dpath + '/listfile_async_004.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + recursion: false, + filter:{ + suffix: [".txt", ".doc", ".png"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_004 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_004 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0500 + * @tc.name fileIO_test_listfile_async_005 + * @tc.desc Test listFile() interface. Promise. + * Do not have prop listNum. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_005', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_005'); + let fpath1 = dpath + '/listfile_async_005.txt'; + let fpath2 = dpath + '/listfile_async_005.doc'; + let fpath3 = dpath + '/listfile_async_005.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + recursion: false, + filter:{ + suffix: [".txt", ".doc", ".png"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_005 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0600 + * @tc.name fileIO_test_listfile_async_006 + * @tc.desc Test listFile() interface. Callback. + * Do not have prop suffix. Not filter files by suffix. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_006', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_006'); + let fpath1 = dpath + '/listfile_async_006.txt'; + let fpath2 = dpath + '/listfile_async_006.doc'; + let fpath3 = dpath + '/listfile_async_006.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 4, + recursion: false, + filter:{ + displayName: ["*listfile*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_006 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_006 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0700 + * @tc.name fileIO_test_listfile_async_007 + * @tc.desc Test listFile() interface. Promise. + * Do not have prop suffix. Not filter files by suffix. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_007', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_007'); + let fpath1 = dpath + '/listfile_async_007.txt'; + let fpath2 = dpath + '/listfile_async_007.doc'; + let fpath3 = dpath + '/listfile_async_007.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + listNum: 4, + recursion: false, + filter:{ + displayName: ["*listfile*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 3).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_007 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0800 + * @tc.name fileIO_test_listfile_async_008 + * @tc.desc Test listFile() interfaces. Callback. + * Scanfile.Retain six files in the three-level directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_008', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_008'); + let fpath1 = dpath + '/listfile_async_008.txt'; + let fpath2 = dpath + '/listfile_async_008.doc'; + let fpath3 = dpath + '/listfile_async_008.png'; + let ddpath = dpath + '/kids'; + let ffapth1 = ddpath + '/firstc.txt'; + let ffapth2 = ddpath + '/firsta.txt'; + let ffapth3 = ddpath + '/aabbcc.hap'; + let dddpath = ddpath + '/' + randomString(5); + let fffpath1 = dddpath + '/makef.doc'; + let fffpath2 = dddpath + '/akasd.txt'; + let fffpath3 = dddpath + '/tdd.log'; + let fffpath4 = dddpath + '/tdd.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + fileIO.mkdirSync(dddpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 6, + recursion: true, + filter:{ + suffix: [".txt", ".doc"], + displayName: ["*listfile*", "*first*", "*akasd*", "*tdd*", "*makef*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_008 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 6).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_008 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_0900 + * @tc.name fileIO_test_listfile_async_009 + * @tc.desc Test listFile() interfaces. Promise. + * Scanfile.Retain six files in the three-level directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_009', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_009'); + let fpath1 = dpath + '/listfile_async_009.txt'; + let fpath2 = dpath + '/listfile_async_009.doc'; + let fpath3 = dpath + '/listfile_async_009.png'; + let ddpath = dpath + '/kids'; + let ffapth1 = ddpath + '/firstc.txt'; + let ffapth2 = ddpath + '/firsta.txt'; + let ffapth3 = ddpath + '/aabbcc.hap'; + let dddpath = ddpath + '/' + randomString(5); + let fffpath1 = dddpath + '/makef.doc'; + let fffpath2 = dddpath + '/akasd.txt'; + let fffpath3 = dddpath + '/tdd.log'; + let fffpath4 = dddpath + '/tdd.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + fileIO.mkdirSync(dddpath); + expect(prepareFile(fpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + listNum: 6, + recursion: true, + filter:{ + suffix: [".txt", ".doc"], + displayName: ["*listfile*", "*first*", "*akasd*", "*tdd*", "*makef*"], + fileSizeOver: 0, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 6).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_009 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1000 + * @tc.name fileIO_test_listfile_async_010 + * @tc.desc Test listFile() interfaces. Callback. + * Filter out files whose suffix is not '. txt' and does not meet other conditions.Two files are retained. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_010', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_010'); + let fapth1 = dpath + '/listfile_async_010.txt'; + let fapth2 = dpath + '/listfile_async_010.doc'; + let fapth3 = dpath + '/listfile_async_010.png'; + let fapth4 = dpath + '/timer.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*time*"], + fileSizeOver: 4, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_010 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_010 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1100 + * @tc.name fileIO_test_listfile_async_011 + * @tc.desc Test listFile() interfaces. Promise. + * Filter out files whose suffix is not '. txt' and does not meet other conditions.Two files are retained. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_011', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_011'); + let fapth1 = dpath + '/listfile_async_011.txt'; + let fapth2 = dpath + '/listfile_async_011.doc'; + let fapth3 = dpath + '/listfile_async_011.png'; + let fapth4 = dpath + '/timer.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*time*"], + fileSizeOver: 4, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_011 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1200 + * @tc.name fileIO_test_listfile_async_012 + * @tc.desc Test listFile() interfaces. Callback. + * Invalid file suffix name format. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_012', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_012'); + let fapth1 = dpath + '/listfile_async_012.txt'; + let fapth2 = dpath + '/listfile_async_012.doc'; + let fapth3 = dpath + '/listfile_async_012.png'; + let fapth4 = dpath + '/timer.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: ["%.txt%"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time -3 + } + }, (err) => { + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_listfile_async_012 has failed for ' + e.message + ', code:' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1300 + * @tc.name fileIO_test_listfile_async_013 + * @tc.desc Test listFile() interfaces. Promise. + * Invalid file suffix name format. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_013', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_013'); + let fapth1 = dpath + '/listfile_async_013.txt'; + let fapth2 = dpath + '/listfile_async_013.doc'; + let fapth3 = dpath + '/listfile_async_013.png'; + let fapth4 = dpath + '/timer.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + await fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: ["%.txt%"], + displayName: ["*listfile*"], + fileSizeOver: 0, + lastModifiedAfter: time -3 + } + }); + expect(false).assertTrue(); + } catch (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_listfile_async_013 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1400 + * @tc.name fileIO_test_listfile_async_014 + * @tc.desc Test listFile() interfaces. Callback. + * Filter out all files because the size of all files less than 12. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_014', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_014'); + let fapth1 = dpath + '/listfile_async_014.txt'; + let fapth2 = dpath + '/listfile_async_014.doc'; + let fapth3 = dpath + '/listfile_async_014.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*"], + fileSizeOver: 12, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_014 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 0).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (e) { + console.log('fileIO_test_listfile_async_014 has failed for ' + e.message + ', code:' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1500 + * @tc.name fileIO_test_listfile_async_015 + * @tc.desc Test listFile() interfaces. Promise. + * Filter out all files because the size of all files less than 12. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_015', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_015'); + let fapth1 = dpath + '/listfile_async_015.txt'; + let fapth2 = dpath + '/listfile_async_015.doc'; + let fapth3 = dpath + '/listfile_async_015.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + listNum: 3, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*"], + fileSizeOver: 12, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 0).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_015 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1600 + * @tc.name fileIO_test_listfile_async_016 + * @tc.desc Test listFile() interfaces. Callback. + * listNum is 0.Traverse all files. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_016', 0, async function () { + let dpath = await nextFileName('fileIO_test_listfile_async_016'); + let fapth1 = dpath + '/listfile_async_016.txt'; + let fapth2 = dpath + '/listfile_async_016.doc'; + let fapth3 = dpath + '/listfile_async_016.png'; + let fapth4 = dpath + '/trespass.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + fileIO.listFile(dpath, { + listNum: 0, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*trespass*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }, (err, dirents) => { + if (err) { + console.log('fileIO_test_listfile_async_016 err package ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + }); + } catch (err) { + console.log('fileIO_test_listfile_async_016 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1700 + * @tc.name fileIO_test_listfile_async_017 + * @tc.desc Test listFile() interfaces. Promise. + * listNum is 0.Traverse all files. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_017', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_017'); + let fapth1 = dpath + '/listfile_async_017.txt'; + let fapth2 = dpath + '/listfile_async_017.doc'; + let fapth3 = dpath + '/listfile_async_017.png'; + let fapth4 = dpath + '/trespass.txt'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth4, FILE_CONTENT)).assertTrue(); + + try { + let time = new Date().getTime() / 1000; + let dirents = await fileIO.listFile(dpath, { + listNum: 0, + recursion: false, + filter:{ + suffix: [".txt"], + displayName: ["*listfile*", "*trespass*"], + fileSizeOver: 5, + lastModifiedAfter: time - 3 + } + }); + expect(dirents.length == 2).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (err) { + console.log('fileIO_test_listfile_async_017 has failed for ' + err.message + ', code:' + err.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1800 + * @tc.name fileIO_test_listfile_async_018 + * @tc.desc Test listFile() interfaces. Promise. + * Missing all arguments. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_018', 0, async function (done) { + + try { + await fileIO.listFile(); + expect(false).assertTrue(); + } catch (err) { + console.log('fileIO_test_listfile_async_018 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_LISTFILE_ASYNC_1900 + * @tc.name fileIO_test_listfile_async_019 + * @tc.desc Test listFile() interfaces. Promise. + * Invalid option args. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_listfile_async_019', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_listfile_async_019'); + let fapth1 = dpath + '/listfile_async_019.txt'; + let fapth2 = dpath + '/listfile_async_019.doc'; + let fapth3 = dpath + '/listfile_async_019.png'; + fileIO.mkdirSync(dpath); + expect(prepareFile(fapth1, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth2, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fapth3, FILE_CONTENT)).assertTrue(); + + try { + await fileIO.listFile(dpath, 4); + expect(false).assertTrue(); + } catch (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_listfile_async_019 has failed for ' + err.message + ', code:' + err.code); + expect(err.code == 13900020 && err.message == 'Invalid argument').assertTrue(); + done(); + } + }); +}); +} diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/moveFile.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/moveFile.test.js new file mode 100644 index 0000000000000000000000000000000000000000..c0f3af2725f5ef565bbd03ccb3731fdfa95a0620 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/moveFile.test.js @@ -0,0 +1,536 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed 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. + */ + +import { + fileIO, FILE_CONTENT, prepareFile, nextFileName, describe, it, expect, +} from '../Common'; + +export default function fileIOMoveFile() { + describe('fileIO_fs_moveFile', function () { + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0000 + * @tc.name fileIO_test_moveFile_sync_000 + * @tc.desc Test moveFileSync() interface. + * Move a single file, shall work properly in normal case. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_moveFile_sync_000', 0, async function () { + let dpath = await nextFileName('fileIO_test_moveFile_sync_000'); + let fpath = dpath + '/file_000.txt'; + let ddpath = dpath + '/dir_000'; + let ffpath = ddpath + '/file_000.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.moveFileSync(fpath, ffpath); + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (e) { + console.log('fileIO_test_moveFile_sync_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0100 + * @tc.name fileIO_test_moveFile_sync_001 + * @tc.desc Test moveFileSync() interface. + * A file with the same name already exists under the destination path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_sync_001', 0, async function () { + let dpath = await nextFileName('fileIO_test_moveFile_sync_001'); + let fpath = dpath + '/file_001.txt'; + let ddpath = dpath + '/dir_001'; + let ffpath = ddpath + '/file_001.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.copyFileSync(fpath, ffpath); + fileIO.moveFileSync(fpath, ffpath, 1); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_sync_001 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900015 && e.message == 'File exists').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0200 + * @tc.name fileIO_test_moveFile_sync_002 + * @tc.desc Test moveFileSync() interface. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_sync_002', 0, async function () { + let dpath = await nextFileName('fileIO_test_moveFile_sync_002'); + let fpath = dpath + '/file_002.txt'; + let ddpath = dpath + '/dir_002'; + let ffpath = ddpath + '/file_002.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + + try { + fileIO.moveFileSync(fpath, ffpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_sync_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0400 + * @tc.name fileIO_test_moveFile_sync_004 + * @tc.desc Test moveFileSync() interface. + * The path point to a directory, not a file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_sync_004', 0, async function () { + let dpath = await nextFileName('fileIO_test_moveFile_sync_004'); + let ddpath = dpath + '/dir_003'; + fileIO.mkdirSync(dpath); + + try { + fileIO.moveFileSync(dpath, ddpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_sync_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0500 + * @tc.name fileIO_test_moveFile_sync_005 + * @tc.desc Test moveFileSync() interface. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_sync_005', 0, async function () { + let fpath = await nextFileName('fileIO_test_moveFile_sync_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.moveFileSync(fpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_moveFile_sync_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_SYNC_0600 + * @tc.name fileIO_test_moveFile_sync_006 + * @tc.desc Test moveFileSync() interface. + * Entry parameters increase forced movement when mode = 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_sync_006', 0, async function () { + let dpath = await nextFileName('fileIO_test_moveFile_sync_006'); + let fpath = dpath + '/file_004.txt'; + let ddpath = dpath + '/dir_004'; + let ffpath = ddpath + '/file_004.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, '')).assertTrue(); + + try { + fileIO.moveFileSync(fpath, ffpath, 0); + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (e) { + console.log('fileIO_test_moveFile_sync_006 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0000 + * @tc.name fileIO_test_moveFile_async_000 + * @tc.desc Test moveFile() interface. Promise. + * Move a single file, shall work properly in normal case. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_moveFile_async_000', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_000'); + let fpath = dpath + '/file_005.txt'; + let ddpath = dpath + '/dir_005'; + let ffpath = ddpath + '/file_005.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + await fileIO.moveFile(fpath, ffpath); + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (e) { + console.log('fileIO_test_moveFile_async_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + +/** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0100 + * @tc.name fileIO_test_moveFile_async_001 + * @tc.desc Test moveFile() interface. Callback. + * Move a single file, shall work properly in normal case. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_001', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_001'); + let fpath = dpath + '/file_006.txt'; + let ddpath = dpath + '/dir_006'; + let ffpath = ddpath + '/file_006.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.moveFile(fpath, ffpath, (err) => { + if (err) { + console.log('fileIO_test_moveFile_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_moveFile_async_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0200 + * @tc.name fileIO_test_moveFile_async_002 + * @tc.desc Test moveFile() interface. Promise. + * A file with the same name already exists under the destination path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_002', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_002'); + let fpath = dpath + '/file_007.txt'; + let ddpath = dpath + '/dir_007'; + let ffpath = ddpath + '/file_007.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.copyFileSync(fpath, ffpath); + await fileIO.moveFile(fpath, ffpath, 1); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900015 && e.message == 'File exists').assertTrue(); + done(); + } + }); + +/** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0300 + * @tc.name fileIO_test_moveFile_async_003 + * @tc.desc Test moveFile() interface. Callback. + * A file with the same name already exists under the destination path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_003', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_003'); + let fpath = dpath + '/file_008.txt'; + let ddpath = dpath + '/dir_008'; + let ffpath = ddpath + '/file_008.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.copyFileSync(fpath, ffpath); + fileIO.moveFile(fpath, ffpath, 1, (err) => { + if (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_003 error: {message: ' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900015 && err.message == 'File exists').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_moveFile_async_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0400 + * @tc.name fileIO_test_moveFile_async_004 + * @tc.desc Test moveFile() interface. Promise. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_004', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_004'); + let fpath = dpath + '/file_009.txt'; + let ddpath = dpath + '/dir_009'; + let ffpath = ddpath + '/file_009.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + + try { + await fileIO.moveFile(fpath, ffpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0500 + * @tc.name fileIO_test_moveFile_async_005 + * @tc.desc Test moveFile() interface. Callback. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_005', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_005'); + let fpath = dpath + '/file_010.txt'; + let ddpath = dpath + '/dir_010'; + let ffpath = ddpath + '/file_010.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + + try { + fileIO.moveFile(fpath, ffpath, (err) => { + if (err) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_005 error: {message: ' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900002 && err.message == 'No such file or directory').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_moveFile_async_005 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0600 + * @tc.name fileIO_test_moveFile_async_006 + * @tc.desc Test moveFile() interface. Promise. + * The path point to a directory, not a file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_006', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_006'); + let ddpath = dpath + '/dir_011'; + fileIO.mkdirSync(dpath); + + try { + await fileIO.moveFile(dpath, ddpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_006 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0700 + * @tc.name fileIO_test_moveFile_async_007 + * @tc.desc Test moveFile() interface. Callback. + * The path point to a directory, not a file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_007', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_007'); + let ddpath = dpath + '/dir_012'; + fileIO.mkdirSync(dpath); + + try { + fileIO.moveFile(dpath, ddpath, (err) => { + if (err) { + console.log('fileIO_test_moveFile_async_007 error package: ' + JSON.stringify(err)); + } + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_moveFile_async_007 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0800 + * @tc.name fileIO_test_moveFile_async_008 + * @tc.desc Test moveFile() interface. Promise. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_008', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_moveFile_async_008'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + await fileIO.moveFile(fpath); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_moveFile_async_008 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_0900 + * @tc.name fileIO_test_moveFile_async_009 + * @tc.desc Test moveFile() interface. Promise. + * Entry parameters increase forced movement when mode = 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_009', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_009'); + let fpath = dpath + '/file_013.txt'; + let ddpath = dpath + '/dir_013'; + let ffpath = ddpath + '/file_013.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, '')).assertTrue(); + + try { + await fileIO.moveFile(fpath, ffpath, 0); + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (e) { + console.log('fileIO_test_moveFile_async_009 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MOVEFILE_ASYNC_1000 + * @tc.name fileIO_test_moveFile_async_010 + * @tc.desc Test moveFile() interface. Callback. + * Entry parameters increase forced movement when mode = 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_moveFile_async_010', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_moveFile_async_010'); + let fpath = dpath + '/file_014.txt'; + let ddpath = dpath + '/dir_014'; + let ffpath = ddpath + '/file_014.txt'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, '')).assertTrue(); + + try { + fileIO.moveFile(fpath, ffpath, 0, (err) => { + if (err) { + console.log('fileIO_test_moveFile_async_010 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + let stat = fileIO.statSync(ffpath); + expect(stat.size == FILE_CONTENT.length).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_moveFile_async_010 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + }); +} \ No newline at end of file