1/*
2 * Copyright (c) 2023 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 { beforeEach, describe, expect, it } from '@ohos/hypium'
17import Utils from './Utils'
18import sendfile from 'libsendfilendk.so'
19import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
20import fs from '@ohos.file.fs'
21
22export default function sendfileNdkTest() {
23  describe('MuslSendfileTest', () => {
24
25    beforeEach(async () => {
26      await Utils.sleep(50)
27    })
28
29    /**
30     * @tc.number     : SUB_THIRDPARTY_MUSL_SENDFILE_SENDFILE_0100
31     * @tc.name       : testMuslSendfileSendfile001
32     * @tc.desc       : test sendfile
33     * @tc.size       : MediumTest
34     * @tc.type       : Function
35     * @tc.level      : Level 2
36     */
37    it('testMuslSendfileSendfile001', 0, async (done: Function) => {
38      let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
39      let context = abilityDelegator.getAppContext();
40      let filesDir = context.filesDir;
41      let file1 = fs.openSync(filesDir + '/test1.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
42      let file2 = fs.openSync(filesDir + '/test2.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
43      let writeLen1 = fs.writeSync(file1.fd, "Try to write str.");
44      let writeLen2 = fs.writeSync(file2.fd, "Try to write str.");
45      console.info("The length of str is: " + writeLen1);
46      console.info("The length of str is: " + writeLen2);
47      let buf = new ArrayBuffer(1024);
48      let firstReadLen = fs.readSync(file1.fd, buf, { offset: 0 });
49      let secondReadLen = fs.readSync(file2.fd, buf, { offset: 0 });
50      fs.closeSync(file1);
51      fs.closeSync(file2);
52      let result:number = sendfile.sendfile(1);
53      expect(result).assertEqual(-1);
54      done()
55    });
56
57      /**
58       * @tc.number     : SUB_THIRDPARTY_MUSL_SENDFILE_SENDFILE_0200
59       * @tc.name       : testMuslSendfileSendfile002
60       * @tc.desc       : test sendfile
61       * @tc.size       : MediumTest
62       * @tc.type       : Function
63       * @tc.level      : Level 2
64       */
65      it('testMuslSendfileSendfile002', 0, async (done: Function) => {
66        let result:number = sendfile.sendfile(0);
67        expect(result).assertEqual(1);
68        done()
69      });
70
71    /**
72     * @tc.number     : SUB_THIRDPARTY_MUSL_SENDFILE_SENDFILE64_0100
73     * @tc.name       : testMuslSendfileSendfile64001
74     * @tc.desc       : test sendfile64
75     * @tc.size       : MediumTest
76     * @tc.type       : Function
77     * @tc.level      : Level 2
78     */
79    it('testMuslSendfileSendfile64001', 0, async (done: Function) => {
80      let result:number = sendfile.sendfile64(1);
81      expect(result).assertEqual(-1);
82      done()
83    });
84
85    /**
86     * @tc.number     : SUB_THIRDPARTY_MUSL_SENDFILE_SENDFILE64_0200
87     * @tc.name       : testMuslSendfileSendfile64002
88     * @tc.desc       : test sendfile64
89     * @tc.size       : MediumTest
90     * @tc.type       : Function
91     * @tc.level      : Level 2
92     */
93    it('testMuslSendfileSendfile64002', 0, async (done: Function) => {
94      let result:number = sendfile.sendfile64(0);
95      expect(result).assertEqual(1);
96      done()
97    });
98  })
99}