diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/close.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/close.test.js new file mode 100644 index 0000000000000000000000000000000000000000..d02dba932f809d39f73b191aa98c7bb3ea72b356 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/close.test.js @@ -0,0 +1,398 @@ +/* + * 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, nextFileName, prepareFile, + describe, it, expect, +} from '../Common'; + +export default function fileIOClose() { +describe('fileIO_fs_close', function () { + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0000 + * @tc.name fileIO_test_close_sync_000 + * @tc.desc Test closeSync() interfaces. + * Open file and close file by fd. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_close_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_test_close_sync_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + } catch (e) { + console.log('fileIO_test_close_sync_000 has failed for ' + e.message + ', code: ' + e.code); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0100 + * @tc.name fileIO_test_close_sync_001 + * @tc.desc Test closeSync() interfaces. + * Open file and close file by file object. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_close_sync_001', 0, async function () { + let fpath = await nextFileName('fileIO_test_close_sync_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.closeSync(file); + fileIO.unlinkSync(fpath); + } catch (e) { + console.log('fileIO_test_close_sync_001 has failed for ' + e.message + ', code: ' + e.code); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0200 + * @tc.name fileIO_test_close_sync_002 + * @tc.desc Test closeSync() interfaces. + * Test fd has been closed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_sync_002', 0, async function () { + let fpath = await nextFileName('fileIO_test_close_sync_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.closeSync(file.fd); + fileIO.closeSync(file.fd); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_close_sync_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900008 && e.message == 'Bad file descriptor').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0300 + * @tc.name fileIO_test_close_sync_003 + * @tc.desc Test closeSync() interfaces. + * Test file has been closed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_sync_003', 0, async function () { + let fpath = await nextFileName('fileIO_test_close_sync_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.closeSync(file); + fileIO.closeSync(file); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_close_sync_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0400 + * @tc.name fileIO_test_close_sync_004 + * @tc.desc Test closeSync() interfaces. + * No parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_sync_004', 0, function () { + + try { + fileIO.closeSync(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_close_sync_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSESYNC_0500 + * @tc.name fileIO_test_close_sync_005 + * @tc.desc Test closeSync() interfaces. + * Illegal type of parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_sync_005', 0, function () { + + try { + fileIO.closeSync(-1); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_close_sync_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900008 && e.message == 'Bad file descriptor').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0000 + * @tc.name fileIO_test_close_async_000 + * @tc.desc Test close() interfaces. Callback. + * Open file and close file by fd. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.close(file.fd, (err) => { + if(err) { + console.log('fileIO_test_close_async_000 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + fileIO.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_close_async_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0100 + * @tc.name fileIO_test_close_async_001 + * @tc.desc Test close() interfaces. Callback. + * Open file and close file by file object. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + fileIO.close(file, (err) => { + if(err) { + console.log('fileIO_test_close_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + fileIO.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_close_async_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0200 + * @tc.name fileIO_test_close_async_002 + * @tc.desc Test close() interfaces. Promise. + * Open file and close file by fd. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + await fileIO.close(file.fd); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_close_async_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0300 + * @tc.name fileIO_test_close_async_003 + * @tc.desc Test close() interfaces. Promise. + * Open file and close file by file object. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + await fileIO.close(file); + fileIO.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_close_async_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0400 + * @tc.name fileIO_test_close_async_004 + * @tc.desc Test close() interfaces. Promise. + * Test file has been closed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_004', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + await fileIO.close(file.fd); + await fileIO.close(file.fd); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_close_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900008 && e.message == 'Bad file descriptor').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0500 + * @tc.name fileIO_test_close_async_005 + * @tc.desc Test close() interfaces. Promise. + * Test file has been closed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_005', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + await fileIO.close(file); + fileIO.close(file); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_close_async_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + fileIO.unlinkSync(fpath); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0600 + * @tc.name fileIO_test_close_async_006 + * @tc.desc Test close() interfaces. Promise. + * There are multiple parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_006', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_close_async_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + await fileIO.close(file.fd, 2); + expect(false).assertTrue(); + } catch (e) { + fileIO.unlinkSync(fpath); + console.log('fileIO_test_close_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_CLOSE_ASYNC_0700 + * @tc.name fileIO_test_close_async_007 + * @tc.desc Test close() interfaces. + * Illegal type of parameter. Promise. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_007', 0, async function (done) { + + try { + await fileIO.close(-1); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_close_async_007 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900008 && e.message == 'Bad file descriptor').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_CLOSE_ASYNC_0800 + * @tc.name fileIO_test_close_async_008 + * @tc.desc Test close() interfaces. Promise. + * No parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_close_async_008', 0, async function (done) { + + try { + await fileIO.close(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_close_async_008 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); +}); +} diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/mkdir.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/mkdir.test.js new file mode 100644 index 0000000000000000000000000000000000000000..eee69563fb6faf62312ef9a3fafba1f6082e214f --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/mkdir.test.js @@ -0,0 +1,235 @@ +/* + * 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, nextFileName, describe, it, expect, prepareFile, FILE_CONTENT, +} from '../Common'; + +export default function fileIOMkdir() { +describe('fileIO_fs_mkdir', function () { + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_SYNC_0000 + * @tc.name fileIO_test_mkdir_sync_000 + * @tc.desc Test mkdirSync() interfaces. + * Create a directory, verify normal function. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_mkdir_sync_000', 0, async function () { + let dpath = await nextFileName('fileIO_test_mkdir_sync_000') + 'd'; + + try { + fileIO.mkdirSync(dpath); + expect(fileIO.accessSync(dpath)).assertTrue(); + fileIO.rmdirSync(dpath); + } catch (e) { + console.log('fileIO_test_mkdir_sync_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_SYNC_0100 + * @tc.name fileIO_test_mkdir_sync_001 + * @tc.desc Test mkdirSync() interfaces. + * The directory has been existed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_sync_001', 0, async function () { + + try { + fileIO.mkdirSync('/'); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_mkdir_sync_001 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900015 && e.message == 'File exists').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_SYNC_0200 + * @tc.name fileIO_test_mkdir_sync_002 + * @tc.desc Test mkdirSync() interfaces. + * The path is empty. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_sync_002', 0, async function () { + + try { + fileIO.mkdirSync(''); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_mkdir_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_MKDIR_SYNC_0300 + * @tc.name fileIO_test_mkdir_sync_003 + * @tc.desc Test mkdirSync() interfaces. + * The path has pointed to a file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_sync_003', 0, async function () { + let fpath = await nextFileName('fileIO_test_open_sync_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.mkdirSync(fpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_mkdir_sync_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900015 && e.message == 'File exists').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0000 + * @tc.name fileIO_test_mkdir_async_000 + * @tc.desc Test mkdir() interfaces. Promise. + * Create a directory, verify normal function. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_async_000', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_mkdir_async_000') + 'd'; + + try { + await fileIO.mkdir(dpath); + expect(fileIO.accessSync(dpath)).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + } catch (e) { + console.log('fileIO_test_mkdir_async_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0100 + * @tc.name fileIO_test_mkdir_async_001 + * @tc.desc Test mkdir() interfaces. Callback. + * Create a directory, verify normal function. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_async_001', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_mkdir_async_001') + 'd'; + + try { + fileIO.mkdir(dpath, (err) => { + if(err) { + console.log('fileIO_test_mkdir_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(fileIO.accessSync(dpath)).assertTrue(); + fileIO.rmdirSync(dpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_mkdir_async_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0200 + * @tc.name fileIO_test_mkdir_async_002 + * @tc.desc Test mkdir() interfaces. Promise. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_async_002', 0, async function (done) { + + try { + await fileIO.mkdir(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_mkdir_async_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0300 + * @tc.name fileIO_test_mkdir_async_003 + * @tc.desc Test mkdir() interfaces. Callback. + * The directory has been existed. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_async_003', 0, async function (done) { + + try { + fileIO.mkdir('/', (err) => { + if(err) { + console.log('fileIO_test_mkdir_async_003 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900015 && err.message == 'File exists').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_mkdir_async_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0400 + * @tc.name fileIO_test_mkdir_async_004 + * @tc.desc Test mkdir() interfaces. Promise. + * The path has pointed to a file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_mkdir_async_004', 0, async function () { + let fpath = await nextFileName('fileIO_test_mkdir_async_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + await fileIO.mkdirSync(fpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_mkdir_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900015 && e.message == 'File exists').assertTrue(); + } + }); +}) +} diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/rmdir.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/rmdir.test.js new file mode 100644 index 0000000000000000000000000000000000000000..828f73baa03af69e2710051e4e8434ff75f15fad --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/rmdir.test.js @@ -0,0 +1,346 @@ +/* + * 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 fileIORmdir() { +describe('fileIO_fs_rmdir', function () { + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_SYNC_0000 + * @tc.name fileIO_test_rmdir_sync_000 + * @tc.desc Test rmdirSync() interface. + * Recursively delete all files and subfolders in a directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_rmdir_sync_000', 0, async function () { + let dpath = await nextFileName('fileIO_test_rmdir_sync_000') + 'd'; + let fpath = dpath + '/rmdir_sync_000'; + let ffpath = dpath + '/rmdir_sync_000_1'; + let ddpath = dpath + '/rmdir_sync_000_1d'; + let fffpath = ddpath + '/rmdir_sync_000'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(ddpath)).assertTrue(); + expect(fileIO.accessSync(fffpath)).assertTrue(); + fileIO.rmdirSync(dpath); + expect(!fileIO.accessSync(dpath)).assertTrue(); + expect(!fileIO.accessSync(ddpath)).assertTrue(); + expect(!fileIO.accessSync(fffpath)).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_sync_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } +}); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_SYNC_0100 + * @tc.name fileIO_test_rmdir_sync_001 + * @tc.desc Test rmdirSync() interface. + * Invalid path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_sync_001', 0, async function () { + let dpath = await nextFileName('fileIO_test_rmdir_sync_001') + 'd'; + + try { + fileIO.rmdirSync(dpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_sync_001 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_SYNC_0200 + * @tc.name fileIO_test_rmdir_sync_002 + * @tc.desc Test rmdirSync() interface. + * Missing parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_sync_002', 0, async function () { + + try { + fileIO.rmdirSync(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_sync_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_SYNC_0300 + * @tc.name fileIO_test_rmdir_sync_003 + * @tc.desc Test rmdirSync() interface. + * Invalid type of parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdirsync_003', 0, function () { + + try { + fileIO.rmdirSync(12); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdirsync_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_SYNC_0400 + * @tc.name fileIO_test_rmdir_sync_004 + * @tc.desc Test rmdirSync() interface. + * The path contains ../, normal call. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_sync_004', 0, async function () { + let dpath = await nextFileName('../cache/fileIO_test_rmdir_sync_003') + 'd'; + + try { + fileIO.mkdirSync(dpath); + expect(fileIO.accessSync(dpath)).assertTrue(); + fileIO.rmdirSync(dpath); + expect(!fileIO.accessSync(dpath)).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_sync_004 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0000 + * @tc.name fileIO_test_rmdir_async_000 + * @tc.desc Test rmdir() interface. Promise. + * Recursively delete all files and subfolders in a directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_000', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_rmdir_async_000') + 'd'; + let fpath = dpath + '/rmdir_async_000'; + let ffpath = dpath + '/rmdir_async_000_1'; + let ddpath = dpath + '/rmdir_async_000_1d'; + let fffpath = ddpath + '/rmdir_async_000_2'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(ddpath)).assertTrue(); + expect(fileIO.accessSync(fffpath)).assertTrue(); + await fileIO.rmdir(dpath); + expect(!fileIO.accessSync(dpath)).assertTrue(); + expect(!fileIO.accessSync(ddpath)).assertTrue(); + expect(!fileIO.accessSync(fffpath)).assertTrue(); + done(); + } catch (e) { + console.log('fileIO_test_rmdir_async_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0100 + * @tc.name fileIO_test_rmdir_async_001 + * @tc.desc Test rmdir() interface. Callback. + * Recursively delete all files and subfolders in a directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_001', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_rmdir_async_001') + 'd'; + let fpath = dpath + '/rmdir_async_001'; + let ffpath = dpath + '/rmdir_async_001_1'; + let ddpath = dpath + '/rmdir_async_001_1d'; + let fffpath = ddpath + '/rmdir_async_001_2'; + fileIO.mkdirSync(dpath); + fileIO.mkdirSync(ddpath); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(ffpath, FILE_CONTENT)).assertTrue(); + expect(prepareFile(fffpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(ddpath)).assertTrue(); + expect(fileIO.accessSync(fffpath)).assertTrue(); + fileIO.rmdir(dpath, (err) => { + if(err) { + console.log('fileIO_test_rmdir_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(!fileIO.accessSync(dpath)).assertTrue(); + expect(!fileIO.accessSync(ddpath)).assertTrue(); + expect(!fileIO.accessSync(fffpath)).assertTrue(); + done(); + }); + } catch (e) { + console.log('fileIO_test_rmdir_async_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0200 + * @tc.name fileIO_test_rmdir_async_002 + * @tc.desc Test rmdir() interface. Promise. + * Invalid path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_002', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_rmdir_async_002') + 'd'; + + try { + await fileIO.rmdir(dpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_async_002 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0300 + * @tc.name fileIO_test_rmdir_async_003 + * @tc.desc Test rmdir() interface. Callback. + * Invalid path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_003', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_rmdir_async_003') + 'd'; + + try { + fileIO.rmdir(dpath, (err) => { + if (err) { + console.log('fileIO_test_rmdir_async_003 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900002 && err.message == 'No such file or directory').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_rmdir_async_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0400 + * @tc.name fileIO_test_rmdir_async_004 + * @tc.desc Test rmdir() interface. Callback. + * Parameter mismatch. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_004', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_rmdir_async_004') + 'd'; + + try { + fileIO.mkdirSync(dpath); + expect(fileIO.accessSync(dpath)).assertTrue(); + fileIO.rmdir(dpath, '', () => { + expect(false).assertTrue(); + }); + } catch (e) { + fileIO.rmdirSync(dpath); + console.log('fileIO_test_rmdir_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_MKDIR_ASYNC_0500 + * @tc.name fileIO_test_rmdir_async_005 + * @tc.desc Test rmdir() interfaces. Promise. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_005', 0, async function (done) { + + try { + await fileIO.rmdir(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_async_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FileIO_RMDIR_ASYNC_0600 + * @tc.name fileIO_test_rmdir_async_006 + * @tc.desc Test rmdirSync() interface. Promise. + * The path contains ../, normal call. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_rmdir_async_006', 0, async function () { + let dpath = await nextFileName('../cache/fileIO_test_rmdir_async_006') + 'd'; + + try { + fileIO.mkdirSync(dpath); + expect(fileIO.accessSync(dpath)).assertTrue(); + await fileIO.rmdir(dpath); + expect(!fileIO.accessSync(dpath)).assertTrue(); + } catch (e) { + console.log('fileIO_test_rmdir_async_006 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); +}) +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/unlink.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/unlink.test.js new file mode 100644 index 0000000000000000000000000000000000000000..b0113c88f8792d6608ac131dc23007db39186117 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/unlink.test.js @@ -0,0 +1,217 @@ +/* + * 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 fileIOUnlink() { +describe('fileIO_fs_unlink', function () { + + /** + * @tc.number SUB_DF_FILEIO_UNLINK_SYNC_0000 + * @tc.name fileIO_test_unlink_sync_000 + * @tc.desc Test unlinkSync() interfaces. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_sync_000', 0, function () { + try { + fileIO.unlinkSync(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_unlink_sync_000 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_UNLINK_SYNC_0100 + * @tc.name fileIO_test_unlink_sync_001 + * @tc.desc Test unlinkSync() interfaces. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_sync_001', 0, async function () { + let fpath = await nextFileName('fileIOTest'); + + try { + fileIO.unlinkSync(fpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_unlink_sync_001 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_UNLINK_SYNC_0200 + * @tc.name fileIO_test_unlink_sync_002 + * @tc.desc Test unlinkSync() interfaces. + * Delete the file by path, verify the normal function. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_unlink_sync_002', 0, async function () { + let fpath = await nextFileName('fileIO_test_unlink_sync_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(fpath)).assertTrue(); + fileIO.unlinkSync(fpath); + expect(!fileIO.accessSync(fpath)).assertTrue(); + } catch (e) { + console.log('fileIO_test_unlink_sync_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FileIO_UNLINKASYNC_0000 + * @tc.name fileIO_test_unlink_async_000 + * @tc.desc Test unlinkAsync() interfaces. Promise. + * Delete the file by path, verify the normal function. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_async_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_unlink_async_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(fpath)).assertTrue(); + await fileIO.unlink(fpath); + expect(!fileIO.accessSync(fpath)).assertTrue(); + done(); + } catch (e) { + console.log('fileIO_test_unlink_async_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FileIO_UNLINKASYNC_0100 + * @tc.name fileIO_test_unlink_async_001 + * @tc.desc Test unlinkAsync() interfaces. Callback. + * Delete the file by path, verify the normal function. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_async_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_unlink_async_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + expect(fileIO.accessSync(fpath)).assertTrue(); + fileIO.unlink(fpath, (err) => { + if (err) { + console.log('fileIO_test_unlink_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(!fileIO.accessSync(fpath)).assertTrue(); + done(); + }); + } catch (e) { + console.log('fileIO_test_unlink_async_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FileIO_UNLINKASYNC_0200 + * @tc.name fileIO_test_unlink_async_002 + * @tc.desc Test unlink() interfaces. Callback. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIOTest'); + + try { + fileIO.unlink(fpath, (err) => { + if (err) { + console.log('fileIO_test_unlink_async_002 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900002 && err.message == 'No such file or directory').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_unlink_async_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FileIO_UNLINKASYNC_0300 + * @tc.name fileIO_test_unlink_async_003 + * @tc.desc Test unlink() interfaces. Promise. + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_async_003', 0, async function (done) { + let fpath = await nextFileName('fileIOTest'); + + try { + await fileIO.unlink(fpath); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_unlink_async_003 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_UNLINKASYNC_0400 + * @tc.name fileIO_test_unlink_async_004 + * @tc.desc Test unlink() interfaces. Promise. + * Missing parameter. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_test_unlink_async_004', 0, async function (done) { + + try { + await fileIO.unlink(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_unlink_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); +}); +}