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 LOG_ECMA(FATAL) << "this branch is unreachable"; 68 UNREACHABLE(); 69 } 70 void *formBuffer = vm->GetNativeAreaAllocator()->AllocateBuffer(length); 71 JSHandle<JSNativePointer> fromNativePointer = 72 factory->NewJSNativePointer(formBuffer, nullptr, reinterpret_cast<void *>(data)); 73 int32_t fromIndex = 0; 74 int32_t count = 5; 75 JSArrayBuffer::CopyDataBlockBytes(JSHandle<JSTaggedValue>::Cast(toNativePointer).GetTaggedValue(), 76 JSHandle<JSTaggedValue>::Cast(fromNativePointer).GetTaggedValue(), fromIndex, count); 77 auto toData = reinterpret_cast<uint8_t *>(toNativePointer->GetExternalPointer()); 78 auto fromData = reinterpret_cast<uint8_t *>(fromNativePointer->GetExternalPointer()); 79 for (uint32_t i = 0; i < length; i++) { 80 EXPECT_EQ(*(toData + i), *(fromData + i)); 81 } 82 vm->GetNativeAreaAllocator()->FreeBuffer(toBuffer); 83 vm->GetNativeAreaAllocator()->FreeBuffer(data); 84 vm->GetNativeAreaAllocator()->FreeBuffer(formBuffer); 85 } 86 87 /** 88 * @tc.name: Attach & Detach & IsDetach 89 * @tc.desc: Attach the specified array buffer to the specified data, then detach the data, and verify the detach 90 * result using the isdetach method. 91 * @tc.type: FUNC 92 * @tc.require: 93 */ HWTEST_F_L0(JsArrayBufferTest,Attach_Detach_IsDetach)94 HWTEST_F_L0(JsArrayBufferTest, Attach_Detach_IsDetach) 95 { 96 auto vm = thread->GetEcmaVM(); 97 auto factory = vm->GetFactory(); 98 99 size_t length = 5; 100 uint8_t value = 100; 101 void *buffer = vm->GetNativeAreaAllocator()->AllocateBuffer(100); 102 uint8_t *data = static_cast<uint8_t *>(vm->GetNativeAreaAllocator()->AllocateBuffer(length)); 103 if (memset_s(data, length, value, length) != EOK) { 104 LOG_ECMA(FATAL) << "this branch is unreachable"; 105 UNREACHABLE(); 106 } 107 JSHandle<JSNativePointer> nativePointer = 108 factory->NewJSNativePointer(buffer, nullptr, reinterpret_cast<void *>(data)); 109 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(5); 110 arrBuf->Attach(thread, length + 1, JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue()); 111 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 6U); 112 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(), 113 JSHandle<JSTaggedValue>::Cast(nativePointer).GetTaggedValue().GetRawData()); 114 115 arrBuf->Detach(thread); 116 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 0U); 117 EXPECT_EQ(arrBuf->GetArrayBufferData().GetRawData(), JSTaggedValue::Null().GetRawData()); 118 EXPECT_TRUE(arrBuf->IsDetach()); 119 vm->GetNativeAreaAllocator()->FreeBuffer(buffer); 120 vm->GetNativeAreaAllocator()->FreeBuffer(data); 121 } 122 } // namespace panda::test 123