1 /*
2 * Copyright (c) 2022 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
16 #include "ecmascript/ecma_vm.h"
17 #include "ecmascript/js_arraybuffer.h"
18 #include "ecmascript/object_factory-inl.h"
19 #include "ecmascript/tests/test_helper.h"
20
21 using namespace panda::ecmascript;
22
23 namespace panda::test {
24 class JsArrayBufferTest : public BaseTestWithScope<false> {
25 };
26
27 /**
28 * @tc.name: CopyDataBlockBytes
29 * @tc.desc: Copies the specified array buffer data block to a memory space with the specified size.
30 * @tc.type: FUNC
31 * @tc.require:
32 */
HWTEST_F_L0(JsArrayBufferTest,CopyDataBlockBytes)33 HWTEST_F_L0(JsArrayBufferTest, CopyDataBlockBytes)
34 {
35 auto vm = thread->GetEcmaVM();
36 auto factory = vm->GetFactory();
37
38 uint8_t value = 100;
39 size_t length = 5;
40 void *toBuffer = vm->GetNativeAreaAllocator()->AllocateBuffer(length);
41 JSHandle<JSNativePointer> toNativePointer = factory->NewJSNativePointer(toBuffer, nullptr, nullptr);
42 uint8_t *data = static_cast<uint8_t *>(vm->GetNativeAreaAllocator()->AllocateBuffer(length));
43 if (memset_s(data, length, value, length) != EOK) {
44 LOG_ECMA(FATAL) << "this branch is unreachable";
45 UNREACHABLE();
46 }
47 void *formBuffer = vm->GetNativeAreaAllocator()->AllocateBuffer(length);
48 JSHandle<JSNativePointer> fromNativePointer =
49 factory->NewJSNativePointer(formBuffer, nullptr, reinterpret_cast<void *>(data));
50 int32_t fromIndex = 0;
51 int32_t count = 5;
52 JSArrayBuffer::CopyDataBlockBytes(JSHandle<JSTaggedValue>::Cast(toNativePointer).GetTaggedValue(),
53 JSHandle<JSTaggedValue>::Cast(fromNativePointer).GetTaggedValue(), fromIndex, count);
54 auto toData = reinterpret_cast<uint8_t *>(toNativePointer->GetExternalPointer());
55 auto fromData = reinterpret_cast<uint8_t *>(fromNativePointer->GetExternalPointer());
56 for (uint32_t i = 0; i < length; i++) {
57 EXPECT_EQ(*(toData + i), *(fromData + i));
58 }
59 vm->GetNativeAreaAllocator()->FreeBuffer(toBuffer);
60 vm->GetNativeAreaAllocator()->FreeBuffer(data);
61 vm->GetNativeAreaAllocator()->FreeBuffer(formBuffer);
62 }
63
64 /**
65 * @tc.name: Attach & Detach & IsDetach
66 * @tc.desc: Attach the specified array buffer to the specified data, then detach the data, and verify the detach
67 * result using the isdetach method.
68 * @tc.type: FUNC
69 * @tc.require:
70 */
HWTEST_F_L0(JsArrayBufferTest,Attach_Detach_IsDetach)71 HWTEST_F_L0(JsArrayBufferTest, Attach_Detach_IsDetach)
72 {
73 auto vm = thread->GetEcmaVM();
74 auto factory = vm->GetFactory();
75
76 size_t length = 5;
77 uint8_t value = 100;
78 void *buffer = vm->GetNativeAreaAllocator()->AllocateBuffer(100);
79 uint8_t *data = static_cast<uint8_t *>(vm->GetNativeAreaAllocator()->AllocateBuffer(length));
80 if (memset_s(data, length, value, length) != EOK) {
81 LOG_ECMA(FATAL) << "this branch is unreachable";
82 UNREACHABLE();
83 }
84 JSHandle<JSNativePointer> nativePointer =
85 factory->NewJSNativePointer(buffer, nullptr, reinterpret_cast<void *>(data));
86 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(5);
87 arrBuf->Attach(thread, length + 1, JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue());
88 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 6U);
89 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(),
90 JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue().GetRawData());
91
92 arrBuf->Detach(thread);
93 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 0U);
94 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(), JSTaggedValue::Null().GetRawData());
95 EXPECT_TRUE(arrBuf->IsDetach());
96 vm->GetNativeAreaAllocator()->FreeBuffer(buffer);
97 vm->GetNativeAreaAllocator()->FreeBuffer(data);
98 }
99 } // namespace panda::test
100