diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/open.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/open.test.js index 9589c56b5aaad02a0a6352f43bd86f7f4bcf55d0..e6c3e59af33829eef568d84867c7870e218fe513 100644 --- a/storage/storagefileiov9jstest/src/main/js/test/members/open.test.js +++ b/storage/storagefileiov9jstest/src/main/js/test/members/open.test.js @@ -14,70 +14,72 @@ */ import { - fileio, fileIO, FILE_CONTENT, prepareFile, nextFileName, isIntNum, - describe, it, expect, - } from '../Common'; - - export default function fileIOOpen() { + fileio, fileIO, FILE_CONTENT, prepareFile, nextFileName, isIntNum, + describe, it, expect, +} from '../Common'; + +export default function fileIOOpen() { describe('fileIO_fs_open', function () { /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0000 * @tc.name fileIO_test_open_sync_000 * @tc.desc Test openSync() interfaces. mode=0o0. + * Open the file in read-only mode, verifying the file is readable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_sync_000', 0, async function () { + it('fileIO_test_open_sync_000', 0, async function () { let fpath = await nextFileName('fileIO_test_open_sync_000'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - try { - let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); - expect(isIntNum(file.fd)).assertTrue(); - let readlen = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(readlen == FILE_CONTENT.length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - } catch (e) { - console.log('fileIO_test_open_sync_000 has failed for ' + e.message + ', code: ' + e.code); - expect(false).assertTrue(); - } + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); + expect(isIntNum(file.fd)).assertTrue(); + let readlen = fileIO.readSync(file.fd, new ArrayBuffer(4096)); + expect(readlen == FILE_CONTENT.length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + } catch (e) { + console.log('fileIO_test_open_sync_000 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } }); /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0100 * @tc.name fileIO_test_open_sync_001 * @tc.desc Test openSync() interfaces. mode=0o1. + * Open the file in write-only mode, verifying the file is writable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_sync_001', 0, async function () { + it('fileIO_test_open_sync_001', 0, async function () { let fpath = await nextFileName('fileIO_test_open_sync_001'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - try { - let file = fileIO.openSync(fpath, fileIO.OpenMode.WRITE_ONLY); - expect(isIntNum(file.fd)).assertTrue(); - let length = 20; - let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); - expect(num == length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - } catch (e) { - console.log('fileIO_test_open_sync_001 has failed for ' + e.message + ', code: ' + e.code); - expect(false).assertTrue(); - } + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.WRITE_ONLY); + expect(isIntNum(file.fd)).assertTrue(); + let num = fileIO.writeSync(file.fd, FILE_CONTENT); + expect(num == FILE_CONTENT.length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + } catch (e) { + console.log('fileIO_test_open_sync_001 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } }); /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0200 * @tc.name fileIO_test_open_sync_002 * @tc.desc Test openSync() interfaces. mode=0o2. + * Open the file in read-write mode, verifying the file is readable and writable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -87,45 +89,46 @@ import { let fpath = await nextFileName('fileIO_test_open_sync_002'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - try { - let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); - expect(isIntNum(file.fd)).assertTrue(); - let readlen = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(readlen == FILE_CONTENT.length).assertTrue(); - let length = 20; - let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); - expect(num == length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - } catch (e) { - console.log('fileIO_test_open_sync_002 has failed for ' + e.message + ', code: ' + e.code); - expect(false).assertTrue(); - } + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + let readlen = fileIO.readSync(file.fd, new ArrayBuffer(4096)); + expect(readlen == FILE_CONTENT.length).assertTrue(); + let length = 20; + let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); + expect(num == length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + } catch (e) { + console.log('fileIO_test_open_sync_002 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } }); /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0300 * @tc.name fileIO_test_open_sync_003 - * @tc.desc Test openSync() interfaces. Missing Parameter. + * @tc.desc Test openSync() interfaces. Missing parameters. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ it('fileIO_test_open_sync_003', 0, function () { - try { - fileIO.openSync(); - expect(false).assertTrue(); - } catch (e) { - console.log('fileIO_test_open_sync_003 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue; - } + try { + fileIO.openSync(); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_open_sync_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + } }); /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0400 * @tc.name fileIO_test_open_sync_004 * @tc.desc Test openSync() interfaces. mode=0o102. + * If the path point to the file which does not exist, the file can be created. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -149,7 +152,7 @@ import { * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0500 * @tc.name fileIO_test_open_sync_005 * @tc.desc Test openSync() interfaces. mode=0o1002. - * If the file exists and the file is opened for write-only or read-write, trim its length to zero. + * If the file exists and the file is opened for read-write, trim its length to zero. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -206,6 +209,7 @@ import { * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0700 * @tc.name fileIO_test_open_sync_007 * @tc.desc Test openSync() interfaces. mode=0o4002. + * If the file is a special file, the opening and subsequent IOs perform non-blocking operations. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -230,6 +234,7 @@ import { * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0800 * @tc.name fileIO_test_open_sync_008 * @tc.desc Test openSync() interfaces. mode=0o200000. + * The path points to a directory. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -252,13 +257,14 @@ import { /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0900 * @tc.name fileIO_test_open_sync_009 - * @tc.desc Test openSync() interfaces. mode=0o200000. Not a directory. + * @tc.desc Test openSync() interfaces. mode=0o200000. + * The path does not a directory, throw error. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_sync_009', 0, async function () { + it('fileIO_test_open_sync_009', 0, async function () { let fpath = await nextFileName('fileIO_test_open_sync_009'); expect(prepareFile(fpath, '')).assertTrue(); @@ -266,9 +272,9 @@ import { fileIO.openSync(fpath, fileIO.OpenMode.DIR); expect(false).assertTrue(); } catch (e) { - console.log('fileIO_test_open_sync_009 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue; fileio.unlinkSync(fpath); + console.log('fileIO_test_open_sync_009 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900018 && e.message == 'Not a directory').assertTrue(); } }); @@ -276,6 +282,7 @@ import { * @tc.number SUB_DF_FILEIO_OPEN_SYNC_1000 * @tc.name fileIO_test_open_sync_010 * @tc.desc Test openSync() interfaces. mode=0o400002. + * The path does not point to a symbolic link. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -299,13 +306,14 @@ import { /** * @tc.number SUB_DF_FILEIO_OPEN_SYNC_1100 * @tc.name fileIO_test_open_sync_011 - * @tc.desc Test openSync() interfaces. mode=0o400002. Shouldn't be Symbolic link. + * @tc.desc Test openSync() interfaces. mode=0o400002. + * The path points to a symbolic link, throw error. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_sync_011', 0, async function () { + it('fileIO_test_open_sync_011', 0, async function () { let fpath = await nextFileName('fileIO_test_open_sync_011'); let ffpath = fpath + 'aaaa'; expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); @@ -315,10 +323,10 @@ import { fileIO.openSync(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); expect(false).assertTrue(); } catch (e) { - console.log('fileIO_test_open_sync_011 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900033 && e.message == 'Too many symbolic links encountered').assertTrue; fileio.unlinkSync(fpath); fileio.unlinkSync(ffpath); + console.log('fileIO_test_open_sync_011 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900033 && e.message == 'Too many symbolic links encountered').assertTrue(); } }); @@ -326,6 +334,7 @@ import { * @tc.number SUB_DF_FILEIO_OPEN_SYNC_1200 * @tc.name fileIO_test_open_sync_012 * @tc.desc Test openSync() interfaces. mode=0o4010002. + * Open the file in the way of synchronous IO. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -346,39 +355,62 @@ import { } }); + /** + * @tc.number SUB_DF_FILEIO_OPEN_SYNC_1300 + * @tc.name fileIO_test_open_sync_013 + * @tc.desc Test openSync() interfaces. Invalid path. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_sync_013', 0, async function (done) { + + try { + fileIO.openSync(-1, fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_open_sync_013 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + done(); + } + }); + /** * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0000 * @tc.name fileIO_test_open_async_000 - * @tc.desc Test open() interfaces. mode=0o0. + * @tc.desc Test open() interfaces. mode=0o0. Promise. + * Open the file in read-only mode, verifying the file is readable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_000', 0, async function (done) { + it('fileIO_test_open_async_000', 0, async function (done) { let fpath = await nextFileName('fileIO_test_open_async_000'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { - fileIO.open(fpath, fileIO.OpenMode.READ_ONLY, function (err, file) { - fileIO.read(file.fd, new ArrayBuffer(4096)) - .then(function (res) { - expect(res == FILE_CONTENT.length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - done(); - }); - }); + let file = await fileIO.open(fpath, fileIO.OpenMode.READ_ONLY); + expect(isIntNum(file.fd)).assertTrue(); + fileIO.read(file.fd, new ArrayBuffer(4096)) + .then((res) => { + expect(res == FILE_CONTENT.length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }); } catch (e) { console.log('fileIO_test_open_async_000 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); - /** + /** * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0100 * @tc.name fileIO_test_open_async_001 - * @tc.desc Test open() interfaces. mode=0o2. No such file. + * @tc.desc Test open() interfaces. mode=0o0. Callback. + * Open the file in read-only mode, verifying the file is readable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -386,69 +418,101 @@ import { */ it('fileIO_test_open_async_001', 0, async function (done) { let fpath = await nextFileName('fileIO_test_open_async_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { - await fileIO.open(fpath, fileIO.OpenMode.READ_WRITE); - expect(false).assertTrue(); + fileIO.open(fpath, fileIO.OpenMode.READ_ONLY, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileIO.read(file.fd, new ArrayBuffer(4096)) + .then((res) => { + expect(res == FILE_CONTENT.length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }); + }); } catch (e) { console.log('fileIO_test_open_async_001 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue; - done(); + expect(false).assertTrue(); } }); /** * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0200 * @tc.name fileIO_test_open_async_002 - * @tc.desc Test open() interfaces. Invalid fp. + * @tc.desc Test open() interfaces. mode=0o1. Promise. + * Open the file in write-only mode, verifying the file is writable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ it('fileIO_test_open_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let length = 20; + try { - await fileIO.open(-1, fileIO.OpenMode.READ_WRITE); - expect(false).assertTrue(); + let file = await fileIO.open(fpath, fileIO.OpenMode.WRITE_ONLY); + expect(isIntNum(file.fd)).assertTrue(); + fileIO.write(file.fd, new ArrayBuffer(length)) + .then((num) => { + expect(num == length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }); } catch (e) { console.log('fileIO_test_open_async_002 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); - done(); + expect(false).assertTrue(); } }); /** * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0300 * @tc.name fileIO_test_open_async_003 - * @tc.desc Test open() interfaces. mode=0o1. + * @tc.desc Test open() interfaces. mode=0o1. Callback. + * Open the file in write-only mode, verifying the file is writable. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_003', 0, async function (done) { + it('fileIO_test_open_async_003', 0, async function (done) { let fpath = await nextFileName('fileIO_test_open_async_003'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let length = 20; - try { - let file = await fileIO.open(fpath, fileIO.OpenMode.WRITE_ONLY); + try { + fileIO.open(fpath, fileIO.OpenMode.WRITE_ONLY, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_003 error package' + JSON.stringify(err)); + expect(false).assertTrue(); + } expect(isIntNum(file.fd)).assertTrue(); - let length = 20; - let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); - expect(num == length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); + fileIO.write(file.fd, new ArrayBuffer(length)) + .then((num) => { + expect(num == length).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + }); + }); done(); - } catch (e) { - console.log('fileIO_test_open_async_003 has failed for ' + e.message + ', code: ' + e.code); - expect(false).assertTrue(); - } + } catch (e) { + console.log('fileIO_test_open_async_003 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } }); /** * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0400 * @tc.name fileIO_test_open_async_004 - * @tc.desc Test open() interfaces. mode=0o2. + * @tc.desc Test open() interfaces. mode=0o2. Promise. + * The path point to nothing, no such file. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -456,29 +520,22 @@ import { */ it('fileIO_test_open_async_004', 0, async function (done) { let fpath = await nextFileName('fileIO_test_open_async_004'); - expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - try { - let file = await fileIO.open(fpath, fileIO.OpenMode.READ_WRITE); - expect(isIntNum(file.fd)).assertTrue(); - let readlen = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(readlen == FILE_CONTENT.length).assertTrue(); - let length = 20; - let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); - expect(num == length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - done(); - } catch (e) { - console.log('fileIO_test_open_async_004 has failed for ' + e.message + ', code: ' + e.code); - expect(false).assertTrue(); - } + try { + await fileIO.open(fpath, fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_open_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_OPEN_ASYNC_0500 * @tc.name fileIO_test_open_async_005 - * @tc.desc Test open() interfaces. mode=0o102. Promise. + * @tc.desc Test open() interfaces. mode=0o2. Callback. + * The path point to nothing, no such file. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -487,6 +544,105 @@ import { it('fileIO_test_open_async_005', 0, async function (done) { let fpath = await nextFileName('fileIO_test_open_async_005'); + try { + fileIO.open(fpath, fileIO.OpenMode.READ_WRITE, (err) => { + if(err) { + console.log('fileIO_test_open_async_005 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_open_async_005 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0600 + * @tc.name fileIO_test_open_async_006 + * @tc.desc Test open() interfaces. mode=0o2. Promise.then().catch() + * The path point to nothing, no such file. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_006', 0, async function () { + let fpath = await nextFileName('fileIO_test_open_async_006'); + + try { + fileIO.open(fpath, fileIO.OpenMode.READ_WRITE).then(() => { + expect(false).assertTrue(); + }).catch((err) => { + fileio.unlinkSync(fpath); + console.log('fileIO_test_open_async_006 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900002 && err.message == 'No such file or directory').assertTrue(); + }); + } catch (e) { + console.log('fileIO_test_open_async_006 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0700 + * @tc.name fileIO_test_open_async_007 + * @tc.desc Test open() interfaces. Promise. + * Invalid path. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_007', 0, async function (done) { + + try { + await fileIO.open(-1, fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_open_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_OPEN_ASYNC_0800 + * @tc.name fileIO_test_open_async_008 + * @tc.desc Test open() interfaces. Callback. + * Invalid path. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_008', 0, async function (done) { + + try { + fileIO.open(-1, fileIO.OpenMode.READ_WRITE, () => { + expect(false).assertTrue(); + }); + } catch (e) { + console.log('fileIO_test_open_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_OPEN_ASYNC_0900 + * @tc.name fileIO_test_open_async_009 + * @tc.desc Test open() interfaces. mode=0o102. Promise. + * If the path point to the file which does not exist, the file can be created. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_009', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_009'); + try { let file = await fileIO.open(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); @@ -494,107 +650,187 @@ import { fileio.unlinkSync(fpath); done(); } catch (e) { - console.log('fileIO_test_open_async_005 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_009 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0600 - * @tc.name fileIO_test_open_async_006 + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1000 + * @tc.name fileIO_test_open_async_010 * @tc.desc Test open() interfaces. mode=0o102. Callback. + * If the path point to the file which does not exist, the file can be created. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_006', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_006'); - expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + it('fileIO_test_open_async_010', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_010'); try { - fileIO.open(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, function (err, file) { + fileIO.open(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.WRITE_ONLY, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_010 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } expect(isIntNum(file.fd)).assertTrue(); fileio.closeSync(file.fd); fileio.unlinkSync(fpath); done(); }); } catch (e) { - console.log('fileIO_test_open_async_006 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_010 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0700 - * @tc.name fileIO_test_open_async_007 - * @tc.desc Test open() interfaces. mode=0o1002. - * If the file exists and the file is opened for write-only or read-write, trim its length to zero. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1100 + * @tc.name fileIO_test_open_async_011 + * @tc.desc Test open() interfaces. mode=0o1002. Promise. + * If the file exists and the file is opened for read-write, trim its length to zero. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_007', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_007'); + it('fileIO_test_open_async_011', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_011'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { let file = await fileIO.open(fpath, fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); - let number = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(number == 0).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); + fileIO.read(file.fd, new ArrayBuffer(4096)) + .then((number) => { + expect(number == 0).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + }); done(); } catch (e) { - console.log('fileIO_test_open_async_007 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_011 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0800 - * @tc.name fileIO_test_open_async_008 - * @tc.desc Test open() interfaces. mode=0o2002. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1200 + * @tc.name fileIO_test_open_async_012 + * @tc.desc Test open() interfaces. mode=0o1002. Callback. + * If the file exists and the file is opened for read-write, trim its length to zero. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_012', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_012'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_012 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileIO.read(file.fd, new ArrayBuffer(4096)) + .then((number) => { + expect(number == 0).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + }); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_012 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1300 + * @tc.name fileIO_test_open_async_013 + * @tc.desc Test open() interfaces. mode=0o2002. Promise. * Open as append, subsequent writes will append to the end of the file. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_008', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_008'); + it('fileIO_test_open_async_013', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_013'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let length = 100; try { let file = await fileIO.open(fpath, fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); - let length = 100; - let num = fileIO.writeSync(file.fd, new ArrayBuffer(length)); - expect(num == length).assertTrue(); - let number = fileIO.readSync(file.fd, new ArrayBuffer(4096), { offset: 0 }); - expect(number == length + FILE_CONTENT.length).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); + fileIO.writeSync(file.fd, new ArrayBuffer(length)); + fileIO.read(file.fd, new ArrayBuffer(4096), { offset: 0 }) + .then((number) => { + expect(number == length + FILE_CONTENT.length).assertTrue(); + fileIO.closeSync(file.fd); + fileio.unlinkSync(fpath); + }); done(); } catch (e) { - console.log('fileIO_test_open_async_008 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_013 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0900 - * @tc.name fileIO_test_open_async_009 - * @tc.desc Test open() interfaces. mode=0o4002. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1400 + * @tc.name fileIO_test_open_async_014 + * @tc.desc Test open() interfaces. mode=0o2002. Callback. + * Open as append, subsequent writes will append to the end of the file. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_009', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_009'); + it('fileIO_test_open_async_014', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_014'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let length = 100; + + try { + fileIO.open(fpath, fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_014 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileIO.writeSync(file.fd, new ArrayBuffer(length)); + fileIO.read(file.fd, new ArrayBuffer(4096), { offset: 0 }) + .then((number) => { + expect(number == length + FILE_CONTENT.length).assertTrue(); + fileIO.closeSync(file.fd); + fileio.unlinkSync(fpath); + }); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_014 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1500 + * @tc.name fileIO_test_open_async_015 + * @tc.desc Test open() interfaces. mode=0o4002. Promise. + * If the file is a special file, the opening and subsequent IOs perform non-blocking operations. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_015', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_015'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { @@ -604,22 +840,54 @@ import { fileio.unlinkSync(fpath); done(); } catch (e) { - console.log('fileIO_test_open_async_009 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_015 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1000 - * @tc.name fileIO_test_open_async_010 - * @tc.desc Test open() interfaces. mode=0o200000. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1600 + * @tc.name fileIO_test_open_async_016 + * @tc.desc Test open() interfaces. mode=0o4002. Callback. + * If the file is a special file, the opening and subsequent IOs perform non-blocking operations. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_010', 0, async function (done) { - let dpath = await nextFileName('fileIO_test_open_async_010'); + it('fileIO_test_open_async_016', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_016'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.NONBLOCK | fileIO.OpenMode.READ_WRITE, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_016 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_016 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1700 + * @tc.name fileIO_test_open_async_017 + * @tc.desc Test open() interfaces. mode=0o200000. Promise. + * The path points to a directory. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_017', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_open_async_017'); fileio.mkdirSync(dpath); try { @@ -628,46 +896,136 @@ import { fileio.rmdirSync(dpath); done(); } catch (e) { - console.log('fileIO_test_open_async_010 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_017 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1100 - * @tc.name fileIO_test_open_async_011 - * @tc.desc Test open() interfaces. mode=0o200000. Not a directory. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1800 + * @tc.name fileIO_test_open_async_018 + * @tc.desc Test open() interfaces. mode=0o200000. Callback. + * The path points to a directory. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_011', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_011'); + it('fileIO_test_open_async_018', 0, async function (done) { + let dpath = await nextFileName('fileIO_test_open_async_018'); + fileio.mkdirSync(dpath); + + try { + fileIO.open(dpath, fileIO.OpenMode.DIR, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_018 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileio.rmdirSync(dpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_018 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1900 + * @tc.name fileIO_test_open_async_019 + * @tc.desc Test open() interfaces. mode=0o200000. Promise. + * The path does not point to a directory, throw error. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_019', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_019'); expect(prepareFile(fpath, '')).assertTrue(); try { - await fileIO.open(fpath, fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE); + await fileIO.open(fpath, fileIO.OpenMode.DIR); expect(false).assertTrue(); } catch (e) { fileio.unlinkSync(fpath); - console.log('fileIO_test_open_async_011 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue; + console.log('fileIO_test_open_async_019 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900018 && e.message == 'Not a directory').assertTrue(); done(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1200 - * @tc.name fileIO_test_open_async_012 - * @tc.desc Test open() interfaces. mode=0o400002. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2000 + * @tc.name fileIO_test_open_async_020 + * @tc.desc Test open() interfaces. mode=0o200000. Callback. + * The path does not point to a directory, throw error. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_012', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_012'); + it('fileIO_test_open_async_020', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_020'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.DIR, (err) => { + if(err) { + fileio.unlinkSync(fpath); + console.log('fileIO_test_open_async_020 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900018 && err.message == 'Not a directory').assertTrue(); + done(); + } + }); + } catch (e) { + console.log('fileIO_test_open_async_020 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2100 + * @tc.name fileIO_test_open_async_021 + * @tc.desc Test open() interfaces. mode=0o200000. Promise.then().catch(). + * The path does not point to a directory, throw error. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_021', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_021'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.DIR).then(() => { + expect(false).assertTrue(); + }).catch((err) => { + fileio.unlinkSync(fpath); + console.log('fileIO_test_open_async_021 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900018 && err.message == 'Not a directory').assertTrue(); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_021 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2200 + * @tc.name fileIO_test_open_async_022 + * @tc.desc Test open() interfaces. mode=0o400002. Promise. + * The path does not point to a symbolic link. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_022', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_022'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { @@ -677,22 +1035,54 @@ import { fileio.unlinkSync(fpath); done(); } catch (e) { - console.log('fileIO_test_open_async_012 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_open_async_022 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1300 - * @tc.name fileIO_test_open_async_013 - * @tc.desc Test open() interfaces. mode=0o400002. Shouldn't be Symbolic link. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2300 + * @tc.name fileIO_test_open_async_023 + * @tc.desc Test open() interfaces. mode=0o400002. Callback. + * The path does not point to a symbolic link. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_013', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_013'); + it('fileIO_test_open_async_023', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_023'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_023 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } + expect(isIntNum(file.fd)).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_023 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2400 + * @tc.name fileIO_test_open_async_024 + * @tc.desc Test open() interfaces. mode=0o400002. Promise. + * The path point to a symbolic link, throw error. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_024', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_024'); let ffpath = fpath + 'aaaa'; expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); @@ -701,36 +1091,131 @@ import { await fileIO.open(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); expect(false).assertTrue(); } catch (e) { - console.log('fileIO_test_open_async_013 has failed for ' + e.message + ', code: ' + e.code); - expect(e.code == 13900033 && e.message == 'Too many symbolic links encountered').assertTrue; fileio.unlinkSync(fpath); fileio.unlinkSync(ffpath); + console.log('fileIO_test_open_async_024 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900033 && e.message == 'Too many symbolic links encountered').assertTrue(); done(); } }); /** - * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1400 - * @tc.name fileIO_test_open_async_014 - * @tc.desc Test open() interfaces. mode=0o4010002. + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2500 + * @tc.name fileIO_test_open_async_025 + * @tc.desc Test open() interfaces. mode=0o400002. Callback. + * The path point to a symbolic link, throw error. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ - it('fileIO_test_open_async_014', 0, async function (done) { - let fpath = await nextFileName('fileIO_test_open_async_014'); + it('fileIO_test_open_async_025', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_025'); + let ffpath = fpath + 'link'; expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); try { - fileIO.open(fpath, fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE, function(err, file) { + fileio.symlinkSync(fpath, ffpath); + fileIO.open(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE, (err) => { + if(err) { + console.log('fileIO_test_open_async_025 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900033 && err.message == 'Too many symbolic links encountered').assertTrue(); + } + fileio.unlinkSync(fpath); + fileio.unlinkSync(ffpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_025 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2600 + * @tc.name fileIO_test_open_async_026 + * @tc.desc Test open() interfaces. mode=0o400002. Promise.then().catch() + * The path point to a symbolic link, throw error. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_026', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_026'); + let ffpath = fpath + 'link'; + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileio.symlinkSync(fpath, ffpath); + fileIO.open(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE).then(() => { + expect(false).assertTrue(); + }).catch((err) => { + console.log('fileIO_test_open_async_026 error package: {' + err.message + ', code: ' + err.code + '}'); + expect(err.code == 13900033 && err.message == 'Too many symbolic links encountered').assertTrue(); + fileio.unlinkSync(fpath); + fileio.unlinkSync(ffpath); + done(); + }); + } catch (e) { + console.log('fileIO_test_open_async_026 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2700 + * @tc.name fileIO_test_open_async_027 + * @tc.desc Test open() interfaces. mode=0o4010002. Promise. + * Open the file in the way of synchronous IO. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_027', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_027'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = await fileIO.open(fpath, fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + } catch (e) { + console.log('fileIO_test_open_async_027 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_2800 + * @tc.name fileIO_test_open_async_028 + * @tc.desc Test open() interfaces. mode=0o4010002. Callback. + * Open the file in the way of synchronous IO. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_test_open_async_028', 0, async function (done) { + let fpath = await nextFileName('fileIO_test_open_async_028'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.open(fpath, fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE, (err, file) => { + if(err) { + console.log('fileIO_test_open_async_028 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } expect(isIntNum(file.fd)).assertTrue(); fileio.closeSync(file.fd); fileio.unlinkSync(fpath); done(); }); - } catch (err) { - console.log('fileIO_test_open_async_014 has failed for ' + err.message + ', code: ' + err.code); + } catch (e) { + console.log('fileIO_test_open_async_028 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/truncate.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/truncate.test.js index ad6002eca25323d1713fc309ec42602eb0d70596..39d2b2bc7c11054614ddd3cba8e9e0ff90309ac5 100644 --- a/storage/storagefileiov9jstest/src/main/js/test/members/truncate.test.js +++ b/storage/storagefileiov9jstest/src/main/js/test/members/truncate.test.js @@ -24,7 +24,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0000 * @tc.name fileIO_truncate_async_000 - * @tc.desc Test the truncate() interface that receives the fpath parameter. + * @tc.desc Test the truncate() interface. Promise. + * Truncate the file with path and truncateLen = 5. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -53,7 +54,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0100 * @tc.name fileIO_truncate_async_001 - * @tc.desc Test the truncate() interface that receives the fd parameter. + * @tc.desc Test the truncate() interface. Callback. + * Truncate the file with fd and truncateLen = 5. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -62,13 +64,16 @@ import { it('fileIO_truncate_async_001', 0, async function (done) { let fpath = await nextFileName('fileIO_truncate_async_001'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - let file; + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); let truncateLen = 5; try { - file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); - fileIO.truncate(file.fd, truncateLen, function (err) { + fileIO.truncate(file.fd, truncateLen, (err) => { + if(err) { + console.log('fileIO_truncate_async_001 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + } let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); expect(len == truncateLen).assertTrue(); fileio.closeSync(file.fd); @@ -84,7 +89,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0200 * @tc.name fileIO_truncate_async_002 - * @tc.desc Test the truncate() interface that receives the fpath parameter. + * @tc.desc Test the truncate() interface. Promise.then().catch + * Truncate the file with path and truncateLen = 2. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -96,23 +102,20 @@ import { let truncateLen = 2; try { - fileIO - .truncate(fpath, truncateLen) - .then(function (err) { - expect(err == null).assertTrue(); - let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); - expect(isIntNum(file.fd)).assertTrue(); - let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(len == truncateLen).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - done(); - }) - .catch(function (e) { - console.log('catch ' + e.message + ', code: ' + e.code); - }); + fileIO.truncate(fpath, truncateLen).then(() => { + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(file.fd)).assertTrue(); + let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); + expect(len == truncateLen).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }).catch((err) => { + console.log('fileIO_truncate_async_002 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + }); } catch(e) { - console.log('fileIO_truncate_async_002 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_truncate_async_002 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); @@ -120,7 +123,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0300 * @tc.name fileIO_truncate_async_003 - * @tc.desc Test the truncate() interface that receives the fd parameter. + * @tc.desc Test the truncate() interface. Promise.then().catch + * Truncate the file with fd and truncateLen = 2. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -129,26 +133,23 @@ import { it('fileIO_truncate_async_003', 0, async function (done) { let fpath = await nextFileName('fileIO_truncate_async_003'); expect(prepareFile(fpath, 'truncate')).assertTrue(); - let file; + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); let truncateLen = 2; try { - file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); - fileIO.truncate(file.fd, truncateLen) - .then(function (err) { - expect(err == null).assertTrue(); - let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(len == truncateLen).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - done(); - }) - .catch(function (e) { - console.log('catch ' + e.message + ', code: ' + e.code); - }) + fileIO.truncate(file.fd, truncateLen).then(() => { + let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); + expect(len == truncateLen).assertTrue(); + fileio.closeSync(file.fd); + fileio.unlinkSync(fpath); + done(); + }).catch((err) => { + console.log('fileIO_truncate_async_003 error package: ' + JSON.stringify(err)); + expect(false).assertTrue(); + }) } catch(e) { - console.log('fileIO_truncate_async_003 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_truncate_async_003 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); @@ -156,7 +157,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0400 * @tc.name fileIO_truncate_async_004 - * @tc.desc Test the truncate() interface that receives the fpath parameter. + * @tc.desc Test the truncate() interface. Callback. + * Truncate the file with path and truncateLen = 2. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -167,13 +169,39 @@ import { let truncateLen = 2; try { - fileIO.truncate(fpath, truncateLen, function (err) { - done(); + fileIO.truncate(fpath, truncateLen, (err) => { + if (err) { + console.log('fileIO_truncate_async_004 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_truncate_async_004 has failed for ' + e.message + ', code: ' + e.code); - expect(!!e).assertTrue(); - expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); + console.log('fileIO_truncate_async_004 has failed for ' + e.message + ', code: ' + e.code); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_DF_FILEIO_TRUNCATE_ASYNC_0500 + * @tc.name fileIO_truncate_async_005 + * @tc.desc Test the truncate() interface. Promise. + * Truncate the file with path and truncateLen = 2. + * @tc.size MEDIUM + * @tc.type Functoin + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_truncate_async_005', 0, async function (done) { + let fpath = await nextFileName('fileIO_truncate_async_005'); + let truncateLen = 2; + + try { + await fileIO.truncate(fpath, truncateLen); + expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_truncate_async_005 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); done(); } }); @@ -181,7 +209,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0000 * @tc.name fileIO_test_truncate_sync_000 - * @tc.desc Test the truncateSync() interface that receives the fpath parameter. + * @tc.desc Test the truncateSync() interface. + * Truncate the file with path. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -196,7 +225,7 @@ import { expect(fileIO.statSync(fpath).size == 0).assertTrue(); fileio.unlinkSync(fpath); } catch (e) { - console.log('fileIO_test_truncate_sync_000 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_truncate_sync_000 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); @@ -204,7 +233,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0100 * @tc.name fileIO_test_truncate_sync_001 - * @tc.desc Test the truncateSync() interface that receives the fd parameter. + * @tc.desc Test the truncateSync() interface. + * Truncate the file with fd and truncateLen = 5. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -213,11 +243,10 @@ import { it('fileIO_test_truncate_sync_001', 0, async function () { let fpath = await nextFileName('fileIO_test_truncate_sync_001'); expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); - let file; + let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); let truncateLen = 5; try { - file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); expect(isIntNum(file.fd)).assertTrue(); fileIO.truncateSync(file.fd, truncateLen); let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); @@ -225,7 +254,7 @@ import { fileio.closeSync(file.fd); fileio.unlinkSync(fpath); } catch (e) { - console.log('fileIO_test_truncate_sync_001 has failed for ' + e.message + ', code: ' + e.code); + console.log('fileIO_test_truncate_sync_001 has failed for ' + e.message + ', code: ' + e.code); expect(false).assertTrue(); } }); @@ -233,7 +262,8 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0200 * @tc.name fileIO_test_truncate_sync_002 - * @tc.desc Test the truncateSync() interface that receives the fpath parameter. + * @tc.desc Test the truncateSync() interface. + * The path point to nothing, no such file. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 @@ -243,10 +273,10 @@ import { let fpath = await nextFileName('fileIO_test_truncate_sync_002'); try { - let stat = fileIO.truncateSync(fpath); - console.info('fileIO_test_truncate_sync_002 = ' + stat); + fileIO.truncateSync(fpath); expect(false).assertTrue(); } catch (e) { + console.log('fileIO_test_truncate_sync_002 has failed for ' + e.message + ', code: ' + e.code); expect(e.code == 13900002 && e.message == 'No such file or directory').assertTrue(); } }); @@ -254,69 +284,42 @@ import { /** * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0300 * @tc.name fileIO_test_truncate_sync_003 - * @tc.desc Test the truncateSync() interface that receives the fd parameter. + * @tc.desc Test the truncateSync() interface. Invalid parameter. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ it('fileIO_test_truncate_sync_003', 0, async function () { - let fpath = await nextFileName('fileIO_test_truncate_sync_003'); - expect(prepareFile(fpath, 'truncate')).assertTrue(); - let file; let truncateLen = 2; + try { - file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); - expect(isIntNum(file.fd)).assertTrue(); - fileIO.truncateSync(file.fd, truncateLen); - let len = fileIO.readSync(file.fd, new ArrayBuffer(4096)); - expect(len == truncateLen).assertTrue(); - fileio.closeSync(file.fd); - fileio.unlinkSync(fpath); - } catch (e) { - console.log('fileIO_test_truncate_sync_003 has failed for ' + e.message + ', code: ' + e.code); + fileIO.truncateSync(-1, truncateLen); expect(false).assertTrue(); + } catch (e) { + console.log('fileIO_test_truncate_sync_003 has failed for ' + e.message + ', code: ' + e.code); + expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); } }); /** * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0400 * @tc.name fileIO_test_truncate_sync_004 - * @tc.desc Test truncateSync() interfaces. + * @tc.desc Test truncateSync() interfaces. Missing parameters. * @tc.size MEDIUM * @tc.type Functoin * @tc.level Level 0 * @tc.require */ it('fileIO_test_truncate_sync_004', 0, function () { + try { - let stat = fileIO.truncateSync(); - console.info('fileIO_test_truncate_sync_004 = ' + stat); + fileIO.truncateSync(); expect(false).assertTrue(); } catch (e) { + console.log('fileIO_test_truncate_sync_004 has failed for ' + e.message + ', code: ' + e.code); expect(e.code == 13900020 && e.message == 'Invalid argument').assertTrue(); } }); - - /** - * @tc.number SUB_DF_FILEIO_TRUNCATE_SYNC_0500 - * @tc.name fileIO_test_truncate_sync_005 - * @tc.desc Test truncateSync() interfaces. - * @tc.size MEDIUM - * @tc.type Functoin - * @tc.level Level 0 - * @tc.require - */ - it('fileIO_test_truncate_sync_005', 0, async function () { - let fpath = await nextFileName('fileIO_test_truncate_sync_005'); - let truncateLen = 2; - try { - fileIO.truncateSync(fpath, truncateLen); - } catch (e) { - console.log('fileIO_test_truncate_sync_005 has failed for ' + e.message + ', code: ' + e.code); - expect(!!e).assertTrue(); - } - }); -}); +}) } -