1/* 2 * Copyright (C) 2022-2025 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 fileIO from '@ohos.file.fs'; 17import util from '@ohos.util'; 18import featureAbility from '@ohos.ability.featureAbility'; 19import buffer from '@ohos.buffer'; 20 21export const FILE_CONTENT = 'hello world'; 22 23import { 24 describe, it, expect 25} 26from '@ohos/hypium' 27 28export function prepareFile(fpath, content) { 29 try { 30 let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE) 31 fileIO.truncateSync(file.fd) 32 fileIO.writeSync(file.fd, content) 33 fileIO.fsyncSync(file.fd) 34 fileIO.closeSync(file) 35 return true 36 } 37 catch (e) { 38 console.log('Failed to prepareFile for ' + e) 39 return false 40 } 41} 42 43export function prepare200MFile(fpath) { 44 try { 45 let file = fileIO.openSync(fpath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE) 46 fileIO.truncateSync(file.fd) 47 let bf = new ArrayBuffer(1024 * 1024 * 5); 48 for (let i = 0; i < 4; i++) { 49 let offset = bf.byteLength * i; 50 let writeLen = fileIO.writeSync(file.fd, bf, { offset: offset, length: bf.byteLength, encoding: 'utf-8' }); 51 } 52 fileIO.fsyncSync(file.fd) 53 fileIO.closeSync(file) 54 return true 55 } 56 catch (e) { 57 console.log('Failed to prepare200MFile for ' + e) 58 return false 59 } 60} 61 62export async function nextFileName(testName) { 63 let context = featureAbility.getContext(); 64 let data = await context.getCacheDir(); 65 let BASE_PATH = data + '/'; 66 return BASE_PATH + testName + '_' + randomString(testName.length); 67} 68 69export async function fileName(testName) { 70 let context = featureAbility.getContext(); 71 let data = await context.getFilesDir(); 72 let BASE_PATH = data + '/'; 73 return BASE_PATH + testName + '_' + randomString(testName.length); 74} 75 76export function randomString(num) { 77 let len= num; 78 var $chars = 'aaaabbbbcccc'; 79 var maxPos = $chars.length; 80 var pwd = ''; 81 for (var i = 0; i < len; i++) { 82 pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); 83 } 84 return pwd; 85} 86 87export async function sleep(times) { 88 if (!times) { 89 times = 10; 90 } 91 await new Promise((res) => setTimeout(res, times)); 92} 93 94function isIntNum(val) { 95 return typeof val === 'number' && val % 1 === 0; 96} 97 98function isBigInt(val) { 99 return typeof val === 'bigint'; 100} 101 102function isString(str) { 103 return (typeof str == 'string') && str.constructor == String; 104} 105 106export { 107 fileIO, 108 isIntNum, 109 isBigInt, 110 isString, 111 describe, 112 it, 113 expect, 114 util, 115 buffer 116};