• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 
16 #include "ani_gtest.h"
17 
18 namespace ark::ets::ani::testing {
19 // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
Sum(ani_env * env,ani_int a,ani_int b)20 static ani_int Sum([[maybe_unused]] ani_env *env, ani_int a, ani_int b)
21 {
22     return a + b;
23 }
24 
25 class GetCreatedVMsTest : public ::testing::Test {
26 public:
BuildOptionString()27     std::string BuildOptionString()
28     {
29         const char *stdlib = std::getenv("ARK_ETS_STDLIB_PATH");
30         if (stdlib == nullptr) {
31             return "";
32         }
33         const char *abcPath = std::getenv("ANI_GTEST_ABC_PATH");
34         std::string bootFileString = "--ext:--boot-panda-files=" + std::string(stdlib);
35 
36         if (abcPath != nullptr) {
37             bootFileString += ":" + std::string(abcPath);
38         }
39         return bootFileString;
40     }
41 };
42 
TEST_F(GetCreatedVMsTest,getCreatedVMs)43 TEST_F(GetCreatedVMsTest, getCreatedVMs)
44 {
45     ani_vm *vm = nullptr;
46     std::string bootFileString = BuildOptionString();
47     ani_option bootFileOption = {bootFileString.data(), nullptr};
48     std::vector<ani_option> options;
49     options.push_back(bootFileOption);
50     ani_options optionsPtr = {options.size(), options.data()};
51     ASSERT_EQ(ANI_CreateVM(&optionsPtr, ANI_VERSION_1, &vm), ANI_OK);
52 
53     vm = nullptr;
54     ani_size size = 0;
55     ani_size bufferSize = 1;
56     ASSERT_EQ(ANI_GetCreatedVMs(nullptr, bufferSize, &size), ANI_INVALID_ARGS);
57     ASSERT_EQ(ANI_GetCreatedVMs(&vm, 0, &size), ANI_INVALID_ARGS);
58     ASSERT_EQ(ANI_GetCreatedVMs(&vm, bufferSize, nullptr), ANI_INVALID_ARGS);
59     ASSERT_EQ(ANI_GetCreatedVMs(&vm, bufferSize, &size), ANI_OK);
60     ASSERT_NE(vm, nullptr);
61     ASSERT_EQ(size, bufferSize);
62     ASSERT_EQ(vm->DestroyVM(), ANI_OK) << "Cannot destroy ANI VM";
63     ASSERT_EQ(ANI_GetCreatedVMs(&vm, bufferSize, &size), ANI_OK);
64     ASSERT_EQ(size, 0);
65 }
66 
TEST_F(GetCreatedVMsTest,combination)67 TEST_F(GetCreatedVMsTest, combination)
68 {
69     ani_vm *vm = nullptr;
70     std::string bootFileString = BuildOptionString();
71     ani_option bootFileOption = {bootFileString.data(), nullptr};
72     std::vector<ani_option> options;
73     options.push_back(bootFileOption);
74     ani_options optionsPtr = {options.size(), options.data()};
75     ASSERT_EQ(ANI_CreateVM(&optionsPtr, ANI_VERSION_1, &vm), ANI_OK);
76     ani_size size = 0;
77     ani_size bufferSize = 1;
78     ASSERT_EQ(ANI_GetCreatedVMs(&vm, bufferSize, &size), ANI_OK);
79     ani_env *env = nullptr;
80     ASSERT_EQ(vm->GetEnv(ANI_VERSION_1, &env), ANI_OK);
81     ASSERT_NE(env, nullptr);
82     ASSERT_EQ(env->GetVM(&vm), ANI_OK);
83     ani_namespace ns;
84     ASSERT_EQ(env->FindNamespace("Lani_get_created_virtual_machines_test/ops;", &ns), ANI_OK);
85     ASSERT_NE(ns, nullptr);
86     std::array functions = {
87         ani_native_function {"sum", "II:I", reinterpret_cast<void *>(Sum)},
88     };
89     ASSERT_EQ(env->Namespace_BindNativeFunctions(ns, functions.data(), functions.size()), ANI_OK);
90 
91     ani_function fn {};
92     ASSERT_EQ(env->Namespace_FindFunction(ns, "checkSum", ":Z", &fn), ANI_OK);
93     ASSERT_NE(fn, nullptr);
94     ani_boolean value = 0U;
95     ASSERT_EQ(env->Function_Call_Boolean(fn, &value), ANI_OK);
96     ASSERT_EQ(value, true);
97     ASSERT_EQ(vm->DestroyVM(), ANI_OK);
98     vm = nullptr;
99     ASSERT_EQ(ANI_GetCreatedVMs(&vm, bufferSize, &size), ANI_OK);
100     ASSERT_EQ(vm, nullptr);
101 }  // NOLINTEND(cppcoreguidelines-pro-type-vararg)
102 }  // namespace ark::ets::ani::testing
103