diff --git a/storage/storagefileiov9jstest/src/main/js/test/Common.js b/storage/storagefileiov9jstest/src/main/js/test/Common.js index 3ba48a1cb1a68f3f8c2249d04ca5b9c371589086..3bf01e107cd039aebabf523dca180d2ae6d88df7 100644 --- a/storage/storagefileiov9jstest/src/main/js/test/Common.js +++ b/storage/storagefileiov9jstest/src/main/js/test/Common.js @@ -45,8 +45,8 @@ export function prepare200MFile(fpath) { fileIO.truncateSync(file.fd) let bf = new ArrayBuffer(1024 * 1024 * 20); for (let i = 0; i < 10; i++) { - let position = bf.byteLength * i; - let writeLen = fileIO.writeSync(file.fd, bf, { offset: 0, length: bf.byteLength, position: position, encoding: 'utf-8' }); + let offset = bf.byteLength * i; + let writeLen = fileIO.writeSync(file.fd, bf, { offset: offset, length: bf.byteLength, encoding: 'utf-8' }); } fileIO.fsyncSync(file.fd) fileIO.closeSync(file) diff --git a/storage/storagefileiov9jstest/src/main/js/test/List.test.js b/storage/storagefileiov9jstest/src/main/js/test/List.test.js index 905f173b971f973d2e248851adee2a4a19db4bbb..b8ba6d44d3cbfc63f387e04464f53cacb41b8668 100644 --- a/storage/storagefileiov9jstest/src/main/js/test/List.test.js +++ b/storage/storagefileiov9jstest/src/main/js/test/List.test.js @@ -17,6 +17,7 @@ import fileIOAccess from './members/access.test.js' import fileIOClose from './members/close.test.js' import fileIOCopyDir from './members/copyDir.test' import fileIOCopyfile from './members/copyFile.test.js' +import fileIOCreateRandomAccessFile from './members/createRandomAccessFile.test.js' import fileIOCreateStream from './members/createStream.test.js' import fileIOFdatasync from './members/fdatasync.test.js' import fileIOFdOpenStream from './members/fdopenStream.test.js' @@ -30,6 +31,10 @@ import fileIOMkdtemp from './members/mkdtemp.test.js' import fileIOMoveDir from './members/moveDir.test.js' import fileIOMoveFile from './members/moveFile.test.js' import fileIOOpen from './members/open.test.js' +import fileIORandomAccessFileClose from './class_randomAccessFile/close.test.js' +import fileIORandomAccessFileRead from './class_randomAccessFile/read.test.js' +import fileIORandomAccessFileSetFilePointer from './class_randomAccessFile/setFilePointer.test.js' +import fileIORandomAccessFileWrite from './class_randomAccessFile/write.test.js' import fileIORead from './members/read.test.js' import fileIOReadtext from './members/readtext.test.js' import fileIORename from './members/rename.test.js' @@ -49,6 +54,7 @@ export default function testsuite() { fileIOClose() fileIOCopyDir() fileIOCopyfile() + fileIOCreateRandomAccessFile() fileIOCreateStream() fileIOFdatasync() fileIOFdOpenStream() @@ -62,6 +68,10 @@ export default function testsuite() { fileIOMoveDir() fileIOMoveFile() fileIOOpen() + fileIORandomAccessFileClose() + fileIORandomAccessFileRead() + fileIORandomAccessFileSetFilePointer() + fileIORandomAccessFileWrite() fileIORead() fileIOReadtext() fileIORename() diff --git a/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/close.test.js b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/close.test.js new file mode 100644 index 0000000000000000000000000000000000000000..bf29bee9bd2ddc4129c390a04794aa8dbe24ac64 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/close.test.js @@ -0,0 +1,70 @@ +/* + * 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 +} from '../Common'; + +export default function fileIORandomAccessFileClose() { +describe('fileIO_randomAccessFile_close', function () { + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_CLOSE_SYNC_0000 + * @tc.name fileIO_randomaccessfile_close_sync_000 + * @tc.desc Test close() interface. Close the RandomAccessFile object. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_close_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_close_sync_000'); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_close_sync_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_CLOSE_SYNC_0100 + * @tc.name fileIO_randomaccessfile_close_sync_001 + * @tc.desc Test close() interface. Parameter mismatch. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_close_sync_001', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_close_sync_001'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.close(1); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_close_sync_001 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); +}) +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/read.test.js b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/read.test.js new file mode 100644 index 0000000000000000000000000000000000000000..ca6720b0f42d97f7265c674211607e3152b18576 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/read.test.js @@ -0,0 +1,852 @@ +/* + * 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 fileIORandomAccessFileRead() { +describe('fileIO_randomAccessFile_read', function () { + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0000 + * @tc.name fileIO_randomaccessfile_read_sync_000 + * @tc.desc Test readSync() interface. Test to read data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(length)); + expect(readLength == length).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0100 + * @tc.name fileIO_randomaccessfile_read_sync_001 + * @tc.desc Test readSync() interface. When the offset is 1. Test to read data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_001', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(length), { offset: 1 }); + expect(readLength == length - 1).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0300 + * @tc.name fileIO_randomaccessfile_read_sync_003 + * @tc.desc Test readSync() interface. When the offset is 1 and the length is 5. Test to read data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_003', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(length), { offset: 1, length: 5 }); + expect(readLength == 5).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0400 + * @tc.name fileIO_randomaccessfile_read_sync_004 + * @tc.desc Test readSync() interface. When offset equals buffer length. Test to read data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_004', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(length), { offset: length }); + expect(readLength == 0).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_004 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0600 + * @tc.name fileIO_randomaccessfile_read_sync_006 + * @tc.desc Test readSync() interface. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_006', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + + try { + randomaccessfile.readSync(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_006 has failed for ' + err); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0700 + * @tc.name fileIO_randomaccessfile_read_sync_007 + * @tc.desc Test readSync() interface. When offset+length>buffer.size, read file_content.length - offset + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_007', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_007'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + + try { + let readLength = randomaccessfile.readSync(new ArrayBuffer(4096), { offset: 1, length: 4096 }); + expect(readLength == FILE_CONTENT.length - 1).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + expect(true).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_007 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0800 + * @tc.name fileIO_randomaccessfile_read_sync_008 + * @tc.desc Test readSync() interface. When the offset is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_008', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_008'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + let readLength = randomaccessfile.readSync(new ArrayBuffer(length), { offset: length + 1 }); + expect(readLength == 0).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(true).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_008 has failed for ' + err); + expect(false).assertTrue(); + + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_0900 + * @tc.name fileIO_randomaccessfile_read_sync_009 + * @tc.desc Test readSync() interface. When the length is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_009', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_009'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + randomaccessfile.readSync(new ArrayBuffer(length), { length: length + 1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_009 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_1000 + * @tc.name fileio_randomaccessfile_read_sync_010 + * @tc.desc Test readSync() interface. The "length" of option must > 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileio_randomaccessfile_read_sync_010', 3, async function () { + let fpath = await nextFileName('fileio_randomaccessfile_read_sync_010'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile; + + try { + randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + randomaccessfile.readSync(new ArrayBuffer(16), { offset: 13, length: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileio_randomaccessfile_read_sync_010 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_1100 + * @tc.name fileIO_randomaccessfile_read_sync_011 + * @tc.desc Test readSync() interface. When there are no parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_011', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_011'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.readSync(); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_011 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_1200 + * @tc.name fileIO_randomaccessfile_read_sync_012 + * @tc.desc Test readSync() interface. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_012', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_012'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.readSync(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_012 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_1300 + * @tc.name fileIO_randomaccessfile_read_sync_013 + * @tc.desc Test readSync() interface. When the parameter type is wrong. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_013', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_013'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.readSync(''); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_013 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_SYNC_1400 + * @tc.name fileIO_randomaccessfile_read_sync_014 + * @tc.desc Test readSync() interface. When the offset is 1 and the length is 5. Test to read data synchronously and filePointer after reading. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_sync_014', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_read_sync_014'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + expect(randomaccessfile.filePointer == bytesWritten); + randomaccessfile.setFilePointer(0); + let initOffset = 1; + let readLength = randomaccessfile.readSync(new ArrayBuffer(length), { offset: initOffset, length: 5 }); + expect(readLength == 5).assertTrue(); + expect(randomaccessfile.filePointer == initOffset + readLength); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch (err) { + console.info('fileIO_randomaccessfile_read_sync_014 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0000 + * @tc.name fileIO_randomaccessfile_read_async_000 + * @tc.desc Test read() interface. return in callback mode. Test to read data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + randomaccessfile.read(new ArrayBuffer(length), function (err, readLength) { + expect(readLength == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0100 + * @tc.name fileIO_randomaccessfile_read_async_001 + * @tc.desc Test read() interface. return in promise mode. When the offset is 1. Test to read data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, async function (err, randomaccessfile) { + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = await randomaccessfile.read(new ArrayBuffer(length), { offset: 1 }); + expect(readLength == length - 1).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0200 + * @tc.name fileIO_randomaccessfile_read_async_002 + * @tc.desc Test read() interface. return in promise mode. When the offset is 1. Test to read data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_002'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = await randomaccessfile.read(new ArrayBuffer(length), { offset: 1 }); + expect(readLength == length - 1).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_002 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0300 + * @tc.name fileIO_randomaccessfile_read_async_003 + * @tc.desc Test read() interface. return in callback mode. When the offset is 1 and the length is 5. Test to read data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + randomaccessfile.read(new ArrayBuffer(length), { offset: 1, length: 5 }, function (err, readLength) { + expect(readLength == 5).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0400 + * @tc.name fileIO_randomaccessfile_read_async_004 + * @tc.desc Test read() interface. return in promise mode. When offset equals buffer length. Test to read data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_004', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = await randomaccessfile.read(new ArrayBuffer(length), { offset: length }); + expect(readLength == 0).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_004 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0600 + * @tc.name fileIO_randomaccessfile_read_async_006 + * @tc.desc Test read() interface. return in callback mode. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_005', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.read(new ArrayBuffer(4096), { offset: -1 }, function (err, readLength) { + expect(false).assertTrue(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_006 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0600 + * @tc.name fileIO_randomaccessfile_read_async_006 + * @tc.desc Test read() interface. return in promise mode. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_006', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_006'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + await randomaccessfile.read(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_006 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0700 + * @tc.name fileIO_randomaccessfile_read_async_007 + * @tc.desc Test read() interface. return in promise mode. When offset+length>buffer.size, read file_content.length - offset + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_007', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_007'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let readLength = await randomaccessfile.read(new ArrayBuffer(4096), { offset: 1, length: 4096 }); + expect(readLength== FILE_CONTENT.length - 1).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(true).assertTrue(); + done(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_007 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0800 + * @tc.name fileIO_randomaccessfile_read_async_008 + * @tc.desc Test read() interface. return in callback mode. When the offset is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_008', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_008'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_ONLY); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + + try { + let length = 4096; + randomaccessfile.read(new ArrayBuffer(length), { offset: length + 1 }, function (err, readLength) { + expect(readLength == 0).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_008 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_0900 + * @tc.name fileIO_randomaccessfile_read_async_009 + * @tc.desc Test read() interface. return in promise mode. When the length is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_009', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_009'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + await randomaccessfile.read(new ArrayBuffer(length), { length: length + 1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_009 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1000 + * @tc.name fileio_randomaccessfile_read_async_010 + * @tc.desc Test read() interface. return in promise mode. The "length" of option must > 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileio_randomaccessfile_read_async_010', 3, async function (done) { + let fpath = await nextFileName('fileio_randomaccessfile_read_async_010'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile; + + try { + randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + await randomaccessfile.read(new ArrayBuffer(16), { offset: 13, length: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileio_randomaccessfile_read_async_010 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1100 + * @tc.name fileIO_randomaccessfile_read_async_011 + * @tc.desc Test read() interface. return in promise mode. When there are no parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_011', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_011'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + try { + await randomaccessfile.read(); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_011 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1200 + * @tc.name fileIO_randomaccessfile_read_async_012 + * @tc.desc Test read() interface. return in promise mode. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_012', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_012'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + try { + await randomaccessfile.read(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_012 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1300 + * @tc.name fileIO_randomaccessfile_read_async_013 + * @tc.desc Test read() interface. return in promise mode. When the parameter type is wrong. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_013', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_013'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + try { + await randomaccessfile.read(''); + expect(false).assertTrue(); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_013 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done() + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1400 + * @tc.name fileIO_randomaccessfile_read_async_014 + * @tc.desc Test read() interface. return in callback mode. Test to read data asynchronously and checkout filePointer. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_014', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_014'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + expect(randomaccessfile.filePointer == bytesWritten).assertTrue(); + randomaccessfile.setFilePointer(0); + randomaccessfile.read(new ArrayBuffer(length), function (err, readLength) { + expect(readLength == length).assertTrue(); + expect(randomaccessfile.filePointer == readLength).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_014 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_READ_ASYNC_1500 + * @tc.name fileIO_randomaccessfile_read_async_015 + * @tc.desc Test read() interface. return in promise mode. When the offset is 1. Test to read data asynchronously and checkout filePointer. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_read_async_015', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_read_async_015'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, async function (err, randomaccessfile) { + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let initOffset = 1; + let readLength = await randomaccessfile.read(new ArrayBuffer(length), { offset: initOffset }); + expect(readLength == length - 1).assertTrue(); + expect(randomaccessfile.filePointer == readLength + initOffset).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch (err) { + console.info('fileIO_randomaccessfile_read_async_015 has failed for ' + err); + expect(false).assertTrue(); + } + }); +}) +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/setFilePointer.test.js b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/setFilePointer.test.js new file mode 100644 index 0000000000000000000000000000000000000000..ddc07d8e87b7a49aa2205a3f93472fdc4dc3b6de --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/setFilePointer.test.js @@ -0,0 +1,101 @@ +/* + * 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 fileIORandomAccessFileSetFilePointer() { +describe('fileIO_randomAccessFile_setFilePointer', function () { + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_SET_FILE_POINTER_SYNC_0000 + * @tc.name fileIO_randomaccessfile_set_file_pointer_sync_000 + * @tc.desc Test setFilePointer() interface. Set file offset pointer position and read will start from filePointer + offset + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_set_file_pointer_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_set_file_pointer_sync_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + expect(randomaccessfile.filePointer == 0).assertTrue(); + let initFilePointer = 5; + randomaccessfile.setFilePointer(initFilePointer); + expect(randomaccessfile.filePointer == initFilePointer).assertTrue(); + + let initOffset = 5; + let readLen = randomaccessfile.readSync(new ArrayBuffer(4096), { offset: initOffset }); + expect(readLen == FILE_CONTENT.length - initFilePointer - initOffset).assertTrue(); + + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_set_file_pointer_sync_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_SET_FILE_POINTER_SYNC_0100 + * @tc.name fileIO_randomaccessfile_set_file_pointer_sync_001 + * @tc.desc Test setFilePointer() interface. Invalid fpointer. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_set_file_pointer_sync_001', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_set_file_pointer_sync_001'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + try { + randomaccessfile.setFilePointer('5'); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_set_file_pointer_sync_001 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_SET_FILE_POINTER_SYNC_0200 + * @tc.name fileIO_randomaccessfile_set_file_pointer_sync_002 + * @tc.desc Test setFilePointer() interface. Missing Parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_set_file_pointer_sync_002', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_set_file_pointer_sync_002'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + try { + randomaccessfile.setFilePointer(); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_set_file_pointer_sync_002 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); +}) +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/write.test.js b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/write.test.js new file mode 100644 index 0000000000000000000000000000000000000000..7f6a63b08c253b246e94625db31c24d120098ed3 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/class_randomAccessFile/write.test.js @@ -0,0 +1,722 @@ +/* + * 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, prepareFile, FILE_CONTENT, nextFileName, + describe, it, expect +} from '../Common'; + +export default function fileIORandomAccessFileWrite() { +describe('fileIO_randomAccessFile_write', function () { + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0000 + * @tc.name fileIO_randomaccessfile_write_sync_000 + * @tc.desc Test writeSync() interface. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_000'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0100 + * @tc.name fileIO_randomaccessfile_write_sync_001 + * @tc.desc Test writeSync() interface. When the offset is 1. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_001', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length), { offset: 1 }); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0200 + * @tc.name fileIO_randomaccessfile_write_sync_002 + * @tc.desc Test writeSync() interface. When the offset is 1. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_002', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_002'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length), { offset: 1 }); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_002 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0300 + * @tc.name fileIO_randomaccessfile_write_sync_003 + * @tc.desc Test writeSync() interface. When the offset is 1 and length is 10. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_003', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_003'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length), { offset: 1, length: 10 }); + expect(bytesWritten == 10).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0400 + * @tc.name fileIO_randomaccessfile_write_sync_004 + * @tc.desc Test writeSync() interface. When the offset is 5. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_004', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let options = { + offset: 5 + } + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length), options); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(4096)); + expect(readLength == (length + options.offset)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_004 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0500 + * @tc.name fileIO_randomaccessfile_write_sync_005 + * @tc.desc Test writeSync() interface. When offset equals buffer length. Test write data synchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_005', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length), { offset: length }); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_005 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0600 + * @tc.name fileIO_randomaccessfile_write_sync_006 + * @tc.desc Test writeSync() interface. When offset+length>buffer.size. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_006', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_006'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.writeSync(new ArrayBuffer(4096), { offset: 5, length: 4095 }); + expect(true).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_006 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0700 + * @tc.name fileIO_randomaccessfile_write_sync_007 + * @tc.desc Test writeSync() interface. When the offset is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_007', 0, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_007'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + randomaccessfile.writeSync(new ArrayBuffer(length), { offset: length + 1 }); + expect(true).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_007 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0800 + * @tc.name fileIO_randomaccessfile_write_sync_008 + * @tc.desc Test writeSync() interface. When there are no parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_008', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_008'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.writeSync(); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_008 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_0900 + * @tc.name fileIO_randomaccessfile_write_sync_009 + * @tc.desc Test writeSync() interface. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_009', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_009'); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.WRITE_ONLY); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + + try { + randomaccessfile.writeSync(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_009 has failed for ' + err); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_1000 + * @tc.name fileio_randomaccessfile_write_sync_010 + * @tc.desc Test writeSync() interface. The "length" of option must > 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileio_randomaccessfile_write_sync_010', 3, async function () { + let fpath = await nextFileName('fileio_randomaccessfile_write_sync_010'); + let randomaccessfile; + + try { + randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 100; + randomaccessfile.writeSync(new ArrayBuffer(length), { offset: 1, length: -1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileio_randomaccessfile_write_sync_010 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_1100 + * @tc.name fileIO_randomaccessfile_write_sync_011 + * @tc.desc Test writeSync() interface. When the buffer parameter type is wrong. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_011', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_011'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + randomaccessfile.writeSync(10, { length: -1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_011 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_SYNC_1200 + * @tc.name fileIO_randomaccessfile_write_sync_012 + * @tc.desc Test writeSync() interface. When the length is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_sync_012', 3, async function () { + let fpath = await nextFileName('fileIO_randomaccessfile_write_sync_012'); + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + randomaccessfile.writeSync(new ArrayBuffer(length), { length: length + 1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_sync_012 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0000 + * @tc.name fileIO_randomaccessfile_write_async_000 + * @tc.desc Test write() interface. return in promise mode. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_000'); + + try { + fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, async function(err, randomaccessfile) { + let length = 4096; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0100 + * @tc.name fileIO_randomaccessfile_write_async_001 + * @tc.desc Test write() interface. return in callback mode. When the offset is 1. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_001'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.WRITE_ONLY); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + let length = 20; + randomaccessfile.write(new ArrayBuffer(length), { offset: 1 }, function(err, bytesWritten) { + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0200 + * @tc.name fileIO_randomaccessfile_write_async_002 + * @tc.desc Test write() interface. return in promise mode. When the offset is 1. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_002'); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length), { offset: 1 }); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_002 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0300 + * @tc.name fileIO_randomaccessfile_write_async_003 + * @tc.desc Test write() interface. return in promise mode. When the offset is 1 and length is 10. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_003'); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length), { offset: 1, length: 10 }); + expect(bytesWritten == 10).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0400 + * @tc.name fileIO_randomaccessfile_write_async_004 + * @tc.desc Test write() interface. return in promise mode. When the offset is 5. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_004', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_004'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 20; + let options = { + offset: 5 + } + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length), options); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = await randomaccessfile.read(new ArrayBuffer(4096)); + expect(readLength == (length + options.offset)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_004 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0500 + * @tc.name fileIO_randomaccessfile_write_async_005 + * @tc.desc Test write() interface. return in promise mode. When offset equals buffer length. Test write data asynchronously. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_005', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_005'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 4096; + let bytesWritten = await randomaccessfile.write(new ArrayBuffer(length), { offset: length }); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_005 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0600 + * @tc.name fileIO_randomaccessfile_write_async_006 + * @tc.desc Test write() interface. return in promise mode. When offset+length>buffer.size. Write correctly. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_006', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_006'); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + await randomaccessfile.write(new ArrayBuffer(length), { offset: 5, length: 4095 }); + expect(true).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_006 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == 'Invalide argument').assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0700 + * @tc.name fileIO_randomaccessfile_write_async_007 + * @tc.desc Test write() interface. return in callback mode. When the offset is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_007', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_007'); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + randomaccessfile.write(new ArrayBuffer(length), { offset: length + 1 }, function(err, bytesWritten) { + expect(bytesWritten == length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_007 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0800 + * @tc.name fileIO_randomaccessfile_write_async_008 + * @tc.desc Test write() interface. return in promise mode. When there are no parameters. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_008', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_008'); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + await randomaccessfile.write(); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_008 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_0900 + * @tc.name fileIO_randomaccessfile_write_async_009 + * @tc.desc Test write() interface. return in promise mode. When the offset is negative. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_009', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_009'); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.WRITE_ONLY); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + + try { + await randomaccessfile.write(new ArrayBuffer(4096), { offset: -1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_009 has failed for ' + err); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_1000 + * @tc.name fileio_randomaccessfile_write_async_010 + * @tc.desc Test write() interface. return in promise mode. The "length" of option must > 0. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileio_randomaccessfile_write_async_010', 3, async function (done) { + let fpath = await nextFileName('fileio_randomaccessfile_write_async_010'); + let randomaccessfile; + + try { + randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let length = 100; + await randomaccessfile.write(new ArrayBuffer(length), { offset: 1, length: -1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileio_randomaccessfile_write_async_010 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_1200 + * @tc.name fileIO_randomaccessfile_write_async_012 + * @tc.desc Test write() interface. return in promise mode. When the length is greater than the buffer length. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_012', 3, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_012'); + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let length = 4096; + await randomaccessfile.write(new ArrayBuffer(length), { length: length + 1 }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_012 has failed for ' + err); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_WRITE_ASYNC_1300 + * @tc.name fileIO_randomaccessfile_write_async_013 + * @tc.desc Test write() interface. return in callback mode. When the offset is 1. Test write data asynchronously and checkout filePointer. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_write_async_013', 0, async function (done) { + let fpath = await nextFileName('fileIO_randomaccessfile_write_async_013'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.WRITE_ONLY); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + let length = 20; + let initOffset = 1; + randomaccessfile.write(new ArrayBuffer(length), { offset: initOffset }, function(err, bytesWritten) { + expect(bytesWritten == length).assertTrue(); + console.info('fileIO_randomaccessfile_write_async_013 randomaccessfile.filePointer' + randomaccessfile.filePointer); + expect(randomaccessfile.filePointer == bytesWritten + initOffset).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_randomaccessfile_write_async_013 has failed for ' + err); + expect(false).assertTrue(); + } + }); +}) +} \ No newline at end of file diff --git a/storage/storagefileiov9jstest/src/main/js/test/members/createRandomAccessFile.test.js b/storage/storagefileiov9jstest/src/main/js/test/members/createRandomAccessFile.test.js new file mode 100644 index 0000000000000000000000000000000000000000..0023f042d8805cfe361b1a0e82348184f064eba0 --- /dev/null +++ b/storage/storagefileiov9jstest/src/main/js/test/members/createRandomAccessFile.test.js @@ -0,0 +1,937 @@ +/* + * 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, prepareFile, nextFileName, isIntNum, FILE_CONTENT, + describe, it, expect +} from '../Common'; + +export default function fileIOCreateRandomAccessFile() { +describe('fileIO_create_randomAccessFile', function () { + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0000 + * @tc.name fileIO_create_randomaccessfile_sync_000 + * @tc.desc Test createRandomAccessFileSync() interface. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_000', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_000'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0100 + * @tc.name fileIO_create_randomaccessfile_sync_001 + * @tc.desc Test createRandomAccessFileSync() interface. filePointer = 5. + * Create RandomAccessFile object to access file from filePointer location. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_001', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_001'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.setFilePointer(5); + expect(randomaccessfile.filePointer == 5).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0200 + * @tc.name fileIO_create_randomaccessfile_sync_002 + * @tc.desc Test createRandomAccessFileSync() interface. + * Create RandomAccessFile object based on file descriptor to access file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_002', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_002'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_002 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0300 + * @tc.name fileIO_create_randomaccessfile_sync_003 + * @tc.desc Test createRandomAccessFileSync() interface. filePointer = 1. + * Create RandomAccessFile object based on file descriptor to access file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_003', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_003'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.setFilePointer(1); + expect(randomaccessfile.filePointer == 1).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0400 + * @tc.name fileIO_create_randomaccessfile_sync_004 + * @tc.desc Test createRandomAccessFileSync() interface. No such file or directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_004', 3, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_004'); + + try { + fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_004 has failed for ' + err); + expect(err.code == 13900002 && err.message == "No such file or directory").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0500 + * @tc.name fileIO_create_randomaccessfile_sync_005 + * @tc.desc Test createRandomAccessFileSync() interface. Invalid fd. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_005', 3, async function () { + try { + fileIO.createRandomAccessFileSync(-1, 0); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_005 has failed for ' + err); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0600 + * @tc.name fileIO_create_randomaccessfile_sync_006 + * @tc.desc Test createRandomAccessFileSync() interface. Invalid fp. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_006', 3, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_006'); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(file); + randomaccessfile.setFilePointer("1"); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_006 has failed for ' + err); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0700 + * @tc.name fileIO_create_randomaccessfile_sync_007 + * @tc.desc Test createRandomAccessFileSync() interface. Missing Parameter. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_007', 3, async function () { + + try { + fileIO.createRandomAccessFileSync(); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_007 has failed for ' + err); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0800 + * @tc.name fileIO_create_randomaccessfile_sync_008 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_008', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_008'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_008 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0900 + * @tc.name fileIO_create_randomaccessfile_sync_009 + * @tc.desc Test createRandomAccessFileSync() interface. flags=-1. Invalid Mode. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_009', 3, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_009'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + fileIO.createRandomAccessFileSync(fpath, -1); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_009 has failed for ' + err); + fileIO.unlinkSync(fpath); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1000 + * @tc.name fileIO_create_randomaccessfile_sync_010 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE. + * If the file exists and the file is opened for write-only or read-write, trim its length to zero. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_010', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_010'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE); + let number = randomaccessfile.readSync(new ArrayBuffer(4096)); + expect(number == 0).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_010 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1100 + * @tc.name fileIO_create_randomaccessfile_sync_011 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE. + * Open as append, subsequent writes will append to the end of the file. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_011', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_011'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE); + let length = 100; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let readLength = randomaccessfile.readSync(new ArrayBuffer(4096), { offset: 0 }); + expect(readLength == length + FILE_CONTENT.length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_011 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1200 + * @tc.name fileIO_create_randomaccessfile_sync_012 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE. Not a directory. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_012', 3, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_012'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_012 has failed for ' + err); + expect(err.code == 13900018 && err.message == "Not a directory").assertTrue(); + fileIO.unlinkSync(fpath); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1300 + * @tc.name fileIO_create_randomaccessfile_sync_013 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_013', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_013'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_013 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1400 + * @tc.name fileIO_create_randomaccessfile_sync_014 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_014', 0, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_014'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = fileIO.createRandomAccessFileSync(fpath, fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_014 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1500 + * @tc.name fileIO_create_randomaccessfile_sync_015 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE. Invalid filepath. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_015', 3, async function () { + let dpath = await nextFileName('fileIO_create_randomaccessfile_sync_015') + 'd'; + fileIO.mkdirSync(dpath); + + try { + fileIO.createRandomAccessFileSync(dpath, fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_015 has failed for ' + err); + expect(err.code == 13900019 && err.message == "Is a directory").assertTrue(); + fileIO.rmdirSync(dpath); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_1600 + * @tc.name fileIO_create_randomaccessfile_sync_016 + * @tc.desc Test createRandomAccessFileSync() interface. flags=fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE. Symbolic link loop. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_sync_016', 3, async function () { + let fpath = await nextFileName('fileIO_create_randomaccessfile_sync_016'); + let ffpath = fpath + 'aaaa'; + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.symlinkSync(fpath, ffpath); + fileIO.createRandomAccessFileSync(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_sync_016 has failed for ' + err); + expect(err.code == 13900033 && (err.message == 'Symbolic link loop' || + err.message == 'Too many symbolic links encountered')).assertTrue(); + fileIO.unlinkSync(fpath); + fileIO.unlinkSync(ffpath); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0000 + * @tc.name fileIO_create_randomaccessfile_async_000 + * @tc.desc Test createRandomAccessFile() interface. return in promise mode. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_000', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_000'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0100 + * @tc.name fileIO_create_randomaccessfile_async_001 + * @tc.desc Test createRandomAccessFile() interface. filePointer = 10. return in callback mode. + * Create RandomAccessFile object to access file based on file path. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_001', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_001'); + + try { + fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, function(err, randomaccessfile) { + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.setFilePointer(10); + expect(randomaccessfile.filePointer == 10).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_001 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0200 + * @tc.name fileIO_create_randomaccessfile_async_002 + * @tc.desc Test createRandomAccessFile() interface. + * Create RandomAccessFile object based on file descriptor to access file. return in callback mode. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_002', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_002'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + fileIO.createRandomAccessFile(file, function(err, randomaccessfile) { + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_002 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0300 + * @tc.name fileIO_create_randomaccessfile_async_003 + * @tc.desc Test createRandomAccessFile() interface. filePointer = 1. + * Create RandomAccessFile object based on file descriptor to access file. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_003', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_003'); + + try { + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile = await fileIO.createRandomAccessFile(file); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.setFilePointer(1); + expect(randomaccessfile.filePointer == 1).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_003 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0400 + * @tc.name fileIO_create_randomaccessfile_async_004 + * @tc.desc Test createRandomAccessFile() interface. No such file or directory. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_004', 3, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_004'); + + try { + await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_004 has failed for ' + err); + expect(err.code == 13900002 && err.message == "No such file or directory").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0500 + * @tc.name fileIO_create_randomaccessfile_async_005 + * @tc.desc Test createRandomAccessFile() interface. Invalid fd. return in callback mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_005', 3, async function (done) { + try { + fileIO.createRandomAccessFile(-1, 0, function(err) { + }); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_005 has failed for ' + err); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0600 + * @tc.name fileIO_create_randomaccessfile_async_006 + * @tc.desc Test createRandomAccessFile() interface. Invalid fp. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_006', 3, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_006'); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + let randomaccessfile; + try { + randomaccessfile = await fileIO.createRandomAccessFile(file); + randomaccessfile.setFilePointer('1'); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_006 has failed for ' + err); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0700 + * @tc.name fileIO_create_randomaccessfile_async_007 + * @tc.desc Test createRandomAccessFile() interface. Missing Parameter. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_007', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_007'); + let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(file); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.closeSync(file.fd); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_007 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_0800 + * @tc.name fileIO_create_randomaccessfile_async_008 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + * Create RandomAccessFile object to access file based on file path. return in callback mode. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_008', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_008'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE, function(err, randomaccessfile) { + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + }); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_008 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_SYNC_0900 + * @tc.name fileIO_create_randomaccessfile_sync_009 + * @tc.desc Test createRandomAccessFile() interface. flags=-1. Invalid Mode. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_009', 3, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_009'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + await fileIO.createRandomAccessFile(fpath, -1); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_009 has failed for ' + err); + expect(err.code == 13900020 && err.message == "Invalid argument").assertTrue(); + fileIO.unlinkSync(fpath); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1000 + * @tc.name fileIO_create_randomaccessfile_async_010 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE. + * If the file exists and the file is opened for write-only or read-write, trim its length to zero. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_010', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_010'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.TRUNC | fileIO.OpenMode.READ_WRITE); + let number = randomaccessfile.readSync(new ArrayBuffer(4096)); + expect(number == 0).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_010 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1100 + * @tc.name fileIO_create_randomaccessfile_async_011 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE. + * Open as append, subsequent writes will append to the end of the file. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_011', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_011'); + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.APPEND | fileIO.OpenMode.READ_WRITE); + let length = 100; + let bytesWritten = randomaccessfile.writeSync(new ArrayBuffer(length)); + expect(bytesWritten == length).assertTrue(); + randomaccessfile.setFilePointer(0); + let number = randomaccessfile.readSync(new ArrayBuffer(4096), { offset: 0 }); + expect(number == length + FILE_CONTENT.length).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_011 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1200 + * @tc.name fileIO_create_randomaccessfile_async_012 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE. Not a directory. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_012', 3, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_012'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_012 has failed for ' + err); + expect(err.code == 13900018 && err.message == "Not a directory").assertTrue(); + fileIO.unlinkSync(fpath); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1300 + * @tc.name fileIO_create_randomaccessfile_async_013 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE. + * Create RandomAccessFile object to access file based on file path. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_013', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_013'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_013 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1400 + * @tc.name fileIO_create_randomaccessfile_async_014 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.SYNC. + * Create RandomAccessFile object to access file based on file path. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_014', 0, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_014'); + expect(prepareFile(fpath, '')).assertTrue(); + + try { + let randomaccessfile = await fileIO.createRandomAccessFile(fpath, fileIO.OpenMode.SYNC | fileIO.OpenMode.READ_WRITE); + expect(isIntNum(randomaccessfile.fd)).assertTrue(); + randomaccessfile.close(); + fileIO.unlinkSync(fpath); + done(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_014 has failed for ' + err); + expect(false).assertTrue(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1500 + * @tc.name fileIO_create_randomaccessfile_async_015 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE. Invalid filepath. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_015', 3, async function (done) { + let dpath = await nextFileName('fileIO_create_randomaccessfile_async_015') + 'd'; + fileIO.mkdirSync(dpath); + + try { + await fileIO.createRandomAccessFile(dpath, fileIO.OpenMode.DIR | fileIO.OpenMode.READ_WRITE); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_015 has failed for ' + err); + fileIO.rmdirSync(dpath); + expect(err.code == 13900019 && err.message == "Is a directory").assertTrue(); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_CREATE_RANDOMACCESSFILE_ASYNC_1600 + * @tc.name fileIO_create_randomaccessfile_async_016 + * @tc.desc Test createRandomAccessFile() interface. flags=fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE. Symbolic link loop. return in promise mode + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 3 + * @tc.require + */ + it('fileIO_create_randomaccessfile_async_016', 3, async function (done) { + let fpath = await nextFileName('fileIO_create_randomaccessfile_async_016'); + let ffpath = fpath + 'aaaa'; + expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); + + try { + fileIO.symlinkSync(fpath, ffpath); + await fileIO.createRandomAccessFile(ffpath, fileIO.OpenMode.NOFOLLOW | fileIO.OpenMode.READ_WRITE); + expect(false).assertTrue(); + } catch(err) { + console.info('fileIO_create_randomaccessfile_async_016 has failed for ' + err); + expect(err.code == 13900033 && (err.message == 'Symbolic link loop' || + err.message == 'Too many symbolic links encountered')).assertTrue(); + fileIO.unlinkSync(fpath); + fileIO.unlinkSync(ffpath); + done(); + } + }); + + /** + * @tc.number SUB_STORAGE_FILEIO_RANDOMACCESSFILE_MULTITHREADED_REPLICATION_0000 + * @tc.name fileIO_randomaccessfile_multithreaded_replication_000 + * @tc.desc Test createRandomAccessFileSync() interface. Test multi-threaded replication. + * @tc.size MEDIUM + * @tc.type Function + * @tc.level Level 0 + * @tc.require + */ + it('fileIO_randomaccessfile_multithreaded_replication_000', 0, async function (done) { + let srcpath = await nextFileName('fileIO_randomaccessfile_multithreaded_replication_000'); + let dstpath = await nextFileName('fileIO_randomaccessfile_multithreaded_replication_000_1'); + let length = 4096; + let buffer = new ArrayBuffer(length); + expect(prepareFile(srcpath, buffer)).assertTrue(); + + try { + let fileSize = fileIO.statSync(srcpath).size; + // init randomaccessfiles + let threadNums = 4; + let srcfiles = new Array(); + let dstfiles = new Array(); + for (let i = 0; i < threadNums; i++) { + srcfiles[i] = fileIO.createRandomAccessFileSync(srcpath, fileIO.OpenMode.READ_WRITE); + dstfiles[i] = fileIO.createRandomAccessFileSync(dstpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE); + srcfiles[i].setFilePointer(fileSize / threadNums * i); + dstfiles[i].setFilePointer(fileSize / threadNums * i); + } + // copy in every thread i from multi-thread + let bufs = new Array(threadNums); + let len = length / threadNums; + for(let i = 0; i < threadNums; i++) { + bufs[i] = new ArrayBuffer(len); + srcfiles[i].read(bufs[i]).then(async function(readLength) { + let writeLen = await dstfiles[i].write(bufs[i]); + expect(writeLen == len).assertTrue(); + dstfiles[i].close(); + srcfiles[i].close(); + if (i == threadNums - 1) { + let size = fileIO.statSync(dstpath).size; + expect(size == fileSize).assertTrue(); + fileIO.unlinkSync(srcpath); + fileIO.unlinkSync(dstpath); + done(); + } + }); + } + } catch (err) { + console.info('fileIO_randomaccessfile_multithreaded_replication_000 has failed for ' + err); + expect(false).assertTrue(); + } + }); +}) +}