1/* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the 'License'); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an 'AS IS' BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { 17 fileio, FILE_CONTENT, prepareFile, nextFileName, isIntNum, 18 describe, it, expect, 19} from '../../Common'; 20 21export default function fileioOpen() { 22describe('fileio_open', function () { 23 24 /** 25 * @tc.number SUB_DF_FILEIO_OPEN_SYNC_0000 26 * @tc.name fileio_test_open_sync_000 27 * @tc.desc Test openSync() interfaces. 28 * @tc.size MEDIUM 29 * @tc.type Functoin 30 * @tc.level Level 0 31 * @tc.require 32 */ 33 it('fileio_test_open_sync_000', 0, function () { 34 try { 35 fileio.openSync('/', 0o102, 0o666); 36 expect(null).assertFail(); 37 } catch (e) { 38 console.log('fileio_test_open_sync_000 has failed for ' + e); 39 } 40 }); 41 42 /** 43 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0000 44 * @tc.name fileio_test_open_async_000 45 * @tc.desc Test openASync() interfaces. 46 * @tc.size MEDIUM 47 * @tc.type Functoin 48 * @tc.level Level 0 49 * @tc.require 50 */ 51 it('fileio_test_open_async_000', 0, async function (done) { 52 let fpath = await nextFileName('fileio_test_open_async_000'); 53 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 54 55 try { 56 await fileio.access(fpath).then(function (err) { 57 fileio.open(fpath, 0, 0o0400, function (err, fd) { 58 fileio.read(fd, new ArrayBuffer(4096)) 59 .then(function (res) { 60 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 61 fileio.closeSync(fd); 62 fileio.unlinkSync(fpath); 63 done(); 64 }); 65 }); 66 }); 67 } catch (e) { 68 expect(null).assertFail(); 69 } 70 }); 71 72 /** 73 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0100 74 * @tc.name fileio_test_open_async_001 75 * @tc.desc Test openASync() interfaces. 76 * @tc.size MEDIUM 77 * @tc.type Functoin 78 * @tc.level Level 0 79 * @tc.require 80 */ 81 it('fileio_test_open_async_001', 0, async function (done) { 82 let fpath = await nextFileName('fileio_test_open_async_001'); 83 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 84 85 try { 86 await fileio.open(fpath, 0, 0o0400, function (err, fd) { 87 fileio.read(fd, new ArrayBuffer(4096)) 88 .then(function (res) { 89 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 90 fileio.closeSync(fd); 91 fileio.unlinkSync(fpath); 92 done(); 93 }); 94 }); 95 } catch (e) { 96 expect(null).assertFail(); 97 } 98 }); 99 100 /** 101 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0200 102 * @tc.name fileio_test_open_async_002 103 * @tc.desc Test openASync() interfaces. 104 * @tc.size MEDIUM 105 * @tc.type Functoin 106 * @tc.level Level 0 107 * @tc.require 108 */ 109 it('fileio_test_open_async_002', 0, async function (done) { 110 let fpath = await nextFileName('fileio_test_open_async_002'); 111 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 112 113 try { 114 await fileio.open(fpath, 0, function (err, fd) { 115 fileio.read(fd, new ArrayBuffer(4096)) 116 .then(function (res) { 117 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 118 fileio.closeSync(fd); 119 fileio.unlinkSync(fpath); 120 done(); 121 }); 122 }); 123 } catch (e) { 124 expect(null).assertFail(); 125 } 126 }); 127 128 /** 129 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0300 130 * @tc.name fileio_test_open_async_003 131 * @tc.desc Test openASync() interfaces. 132 * @tc.size MEDIUM 133 * @tc.type Functoin 134 * @tc.level Level 0 135 * @tc.require 136 */ 137 it('fileio_test_open_async_003', 0, async function (done) { 138 let fpath = await nextFileName('fileio_test_open_async_003'); 139 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 140 141 try { 142 fileio.open(fpath, 0o2, function (err, fd) { 143 fileio.read(fd, new ArrayBuffer(4096)) 144 .then(function (res) { 145 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 146 fileio.closeSync(fd); 147 fileio.unlinkSync(fpath); 148 done(); 149 }); 150 }); 151 } catch (e) { 152 expect(null).assertFail(); 153 } 154 }); 155 156 /** 157 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0400 158 * @tc.name fileio_test_open_async_004 159 * @tc.desc Test openASync() interfaces. 160 * @tc.size MEDIUM 161 * @tc.type Functoin 162 * @tc.level Level 0 163 * @tc.require 164 */ 165 it('fileio_test_open_async_004', 0, async function (done) { 166 let fpath = await nextFileName('fileio_test_open_async_004'); 167 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 168 169 try { 170 await fileio.open(fpath) 171 .then(function (fd) { 172 fileio.read(fd, new ArrayBuffer(4096)) 173 .then(function (res) { 174 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 175 fileio.closeSync(fd); 176 fileio.unlinkSync(fpath); 177 }); 178 }) 179 .catch(function (err) { 180 expect(err == null).assertTrue(); 181 }) 182 done(); 183 } catch (e) { 184 expect(null).assertFail(); 185 } 186 }); 187 188 /** 189 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0500 190 * @tc.name fileio_test_open_async_005 191 * @tc.desc Test openASync() interfaces. 192 * @tc.size MEDIUM 193 * @tc.type Functoin 194 * @tc.level Level 0 195 * @tc.require 196 */ 197 it('fileio_test_open_async_005', 0, async function (done) { 198 let fpath = await nextFileName('fileio_test_open_async_005'); 199 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 200 try { 201 await fileio.open(fpath, 0o0) 202 .then(function (fd) { 203 fileio.read(fd, new ArrayBuffer(4096)) 204 .then(function (res) { 205 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 206 fileio.closeSync(fd); 207 fileio.unlinkSync(fpath); 208 }) 209 }) 210 .catch(function (err) { 211 expect(err == null).assertTrue(); 212 }) 213 done(); 214 } catch (e) { 215 expect(null).assertFail(); 216 } 217 }); 218 219 /** 220 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0600 221 * @tc.name fileio_test_open_async_006 222 * @tc.desc Test openASync() interfaces. 223 * @tc.size MEDIUM 224 * @tc.type Functoin 225 * @tc.level Level 0 226 * @tc.require 227 */ 228 it('fileio_test_open_async_006', 0, async function (done) { 229 let fpath = await nextFileName('fileio_test_open_async_006'); 230 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 231 try { 232 await fileio.open(fpath, 0o2, 0o0200) 233 .then(function (fd) { 234 fileio.read(fd, new ArrayBuffer(4096)) 235 .then(function (res) { 236 expect(res.bytesRead == FILE_CONTENT.length).assertTrue(); 237 fileio.closeSync(fd); 238 fileio.unlinkSync(fpath); 239 }); 240 }) 241 .catch(function (err) { 242 expect(err == null).assertTrue(); 243 }) 244 done(); 245 } catch (e) { 246 expect(null).assertFail(); 247 } 248 }); 249 250 /** 251 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0700 252 * @tc.name fileio_test_open_async_007 253 * @tc.desc Test openASync() interfaces. 254 * @tc.size MEDIUM 255 * @tc.type Functoin 256 * @tc.level Level 0 257 * @tc.require 258 */ 259 it('fileio_test_open_async_007', 0, async function (done) { 260 let fpath = await nextFileName('fileio_test_open_async_007'); 261 try { 262 fileio.open(fpath, 0o0101, 0o0222, function (err, fd) { 263 expect(fd !== null).assertTrue(); 264 fileio.writeSync(fd, FILE_CONTENT, { 265 encoding: 'utf-8', 266 offset: 1, 267 length: 1, 268 }); 269 expect(fileio.closeSync(fd) !== null).assertTrue(); 270 expect(fileio.unlinkSync(fpath) !== null).assertTrue(); 271 done(); 272 }); 273 } catch (e) { 274 expect(null).assertFail(); 275 } 276 }); 277 278 /** 279 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0800 280 * @tc.name fileio_test_open_async_008 281 * @tc.desc Test openASync() interfaces. 282 * @tc.size MEDIUM 283 * @tc.type Functoin 284 * @tc.level Level 0 285 * @tc.require 286 */ 287 it('fileio_test_open_async_008', 0, async function (done) { 288 let fpath = await nextFileName('fileio_test_open_async_008'); 289 try { 290 fileio.open(fpath, 0o100, 0o0444, function (err, fd) { 291 expect(fd !== null).assertTrue(); 292 expect(fileio.closeSync(fd) !== null).assertTrue(); 293 expect(fileio.unlinkSync(fpath) !== null).assertTrue(); 294 done(); 295 }); 296 } catch (e) { 297 expect(null).assertFail(); 298 } 299 }); 300 301 /** 302 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_0900 303 * @tc.name fileio_test_open_async_009 304 * @tc.desc Test openASync() interfaces. 305 * @tc.size MEDIUM 306 * @tc.type Functoin 307 * @tc.level Level 0 308 * @tc.require 309 */ 310 it('fileio_test_open_async_009', 0, async function (done) { 311 let fpath = await nextFileName('fileio_test_open_async_009'); 312 try { 313 fileio.open(fpath, 0o2101, 0o0222, function (err, fd) { 314 expect(fd !== null).assertTrue(); 315 let wri = fileio.writeSync(fd, 'hello1', { 316 encoding: 'utf-8', 317 offset: 1, 318 length: 1, 319 }); 320 expect(wri !== null).assertTrue(); 321 let writ = fileio.writeSync(fd, 'hello2', { 322 encoding: 'utf-8', 323 offset: 1, 324 length: 1, 325 }); 326 expect(writ !== null).assertTrue(); 327 expect(fileio.closeSync(fd) !== null).assertTrue(); 328 expect(fileio.unlinkSync(fpath) !== null).assertTrue(); 329 done(); 330 }); 331 } catch (e) { 332 expect(null).assertFail(); 333 } 334 }); 335 336 /** 337 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1000 338 * @tc.name fileio_test_open_async_010 339 * @tc.desc Test open() interfaces. 340 * Use default: flag = O_RDONLY, mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP. 341 * @tc.size MEDIUM 342 * @tc.type Functoin 343 * @tc.level Level 3 344 * @tc.require 345 */ 346 it('fileio_test_open_async_010', 3, async function (done) { 347 let fpath = await nextFileName('fileio_test_open_async_010'); 348 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 349 350 try { 351 fileio.open(fpath, undefined, undefined, (err, fd) => { 352 if (err) { 353 console.log('fileio_test_open_async_010 error: ' + e); 354 expect(false).assertTrue(); 355 } 356 expect(isIntNum(fd)).assertTrue(); 357 let readLen = fileio.readSync(fd, new ArrayBuffer(4096)); 358 expect(readLen == FILE_CONTENT.length).assertTrue(); 359 fileio.write(fd, FILE_CONTENT, (err, bytesWritten) => { 360 if (err) { 361 fileio.closeSync(fd); 362 fileio.unlinkSync(fpath); 363 console.log('fileIO_test_open_async_010 error package: ' + err); 364 expect(err.message == 'Bad file descriptor').assertTrue(); 365 done(); 366 } else { 367 expect(false).assertTrue(); 368 } 369 }); 370 }); 371 } catch (e) { 372 console.log('fileio_test_open_async_010 has failed for ' + e); 373 expect(false).assertTrue(); 374 } 375 }); 376 377 /** 378 * @tc.number SUB_DF_FILEIO_OPEN_ASYNC_1100 379 * @tc.name fileio_test_open_async_011 380 * @tc.desc Test open() interfaces. 381 * Use default: flag = O_RDONLY, mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP. 382 * @tc.size MEDIUM 383 * @tc.type Functoin 384 * @tc.level Level 3 385 * @tc.require 386 */ 387 it('fileio_test_open_async_011', 3, async function (done) { 388 let fpath = await nextFileName('fileio_test_open_async_011'); 389 expect(prepareFile(fpath, FILE_CONTENT)).assertTrue(); 390 let fd; 391 392 try { 393 fd = await fileio.open(fpath, undefined, undefined); 394 expect(isIntNum(fd)).assertTrue(); 395 let readLen = fileio.readSync(fd, new ArrayBuffer(4096)); 396 expect(readLen == FILE_CONTENT.length).assertTrue(); 397 fileio.writeSync(fd, FILE_CONTENT); 398 expect(false).assertTrue(); 399 } catch (e) { 400 fileio.closeSync(fd); 401 fileio.unlinkSync(fpath); 402 console.log('fileio_test_open_async_011 has failed for' + e); 403 expect(e.message == 'Bad file descriptor').assertTrue(); 404 done(); 405 } 406 }); 407}); 408} 409