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/tests/test_helper.h"
19
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class JsArrayBufferTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
29
TearDownTestCase()30 static void TearDownTestCase()
31 {
32 GTEST_LOG_(INFO) << "TearDownCase";
33 }
34
SetUp()35 void SetUp() override
36 {
37 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38 }
39
TearDown()40 void TearDown() override
41 {
42 TestHelper::DestroyEcmaVMWithScope(instance, scope);
43 }
44
45 EcmaVM *instance {nullptr};
46 EcmaHandleScope *scope {nullptr};
47 JSThread *thread {nullptr};
48 };
49
50 /**
51 * @tc.name: CopyDataBlockBytes
52 * @tc.desc: Copies the specified array buffer data block to a memory space with the specified size.
53 * @tc.type: FUNC
54 * @tc.require:
55 */
HWTEST_F_L0(JsArrayBufferTest,CopyDataBlockBytes)56 HWTEST_F_L0(JsArrayBufferTest, CopyDataBlockBytes)
57 {
58 auto vm = thread->GetEcmaVM();
59 auto factory = vm->GetFactory();
60
61 uint8_t value = 100;
62 size_t length = 5;
63 void *toBuffer = vm->GetNativeAreaAllocator()->AllocateBuffer(length);
64 JSHandle<JSNativePointer> toNativePointer = factory->NewJSNativePointer(toBuffer, nullptr, nullptr);
65 uint8_t *data = static_cast<uint8_t *>(vm->GetNativeAreaAllocator()->AllocateBuffer(length));
66 if (memset_s(data, length, value, length) != EOK) {
67 UNREACHABLE();
68 }
69 void *formBuffer = vm->GetNativeAreaAllocator()->AllocateBuffer(length);
70 JSHandle<JSNativePointer> fromNativePointer =
71 factory->NewJSNativePointer(formBuffer, nullptr, reinterpret_cast<void *>(data));
72 int32_t fromIndex = 0;
73 int32_t count = 5;
74 JSArrayBuffer::CopyDataBlockBytes(JSHandle<JSTaggedValue>::Cast(toNativePointer).GetTaggedValue(),
75 JSHandle<JSTaggedValue>::Cast(fromNativePointer).GetTaggedValue(), fromIndex, count);
76 auto toData = reinterpret_cast<uint8_t *>(toNativePointer->GetExternalPointer());
77 auto fromData = reinterpret_cast<uint8_t *>(fromNativePointer->GetExternalPointer());
78 for (uint32_t i = 0; i < length; i++) {
79 EXPECT_EQ(*(toData + i), *(fromData + i));
80 }
81 vm->GetNativeAreaAllocator()->FreeBuffer(toBuffer);
82 vm->GetNativeAreaAllocator()->FreeBuffer(data);
83 vm->GetNativeAreaAllocator()->FreeBuffer(formBuffer);
84 }
85
86 /**
87 * @tc.name: Attach & Detach & IsDetach
88 * @tc.desc: Attach the specified array buffer to the specified data, then detach the data, and verify the detach
89 * result using the isdetach method.
90 * @tc.type: FUNC
91 * @tc.require:
92 */
HWTEST_F_L0(JsArrayBufferTest,Attach_Detach_IsDetach)93 HWTEST_F_L0(JsArrayBufferTest, Attach_Detach_IsDetach)
94 {
95 auto vm = thread->GetEcmaVM();
96 auto factory = vm->GetFactory();
97
98 size_t length = 5;
99 uint8_t value = 100;
100 void *buffer = vm->GetNativeAreaAllocator()->AllocateBuffer(100);
101 uint8_t *data = static_cast<uint8_t *>(vm->GetNativeAreaAllocator()->AllocateBuffer(length));
102 if (memset_s(data, length, value, length) != EOK) {
103 UNREACHABLE();
104 }
105 JSHandle<JSNativePointer> nativePointer =
106 factory->NewJSNativePointer(buffer, nullptr, reinterpret_cast<void *>(data));
107 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(5);
108 arrBuf->Attach(thread, length + 1, JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue());
109 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 6U);
110 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(),
111 JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue().GetRawData());
112
113 arrBuf->Detach(thread);
114 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 0U);
115 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(), JSTaggedValue::Null().GetRawData());
116 EXPECT_TRUE(arrBuf->IsDetach());
117 vm->GetNativeAreaAllocator()->FreeBuffer(buffer);
118 vm->GetNativeAreaAllocator()->FreeBuffer(data);
119 }
120 } // namespace panda::test
121