MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / TEST

Function TEST

test/plugins/wasm_bpf/simple_map_test.cpp:54–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52static const uint32_t RESULT_VALUE_KEY = 0x7890;
53
54TEST(WasmBpfTest, SimpleMapTest) {
55 using namespace std::string_view_literals;
56 // Test loading and attaching a BPF program and some map operations.
57 auto module = dynamic_cast<WasmEdge::Host::WasmBpfModule *>(createModule());
58 ASSERT_NE(module, nullptr);
59
60 // Create the calling frame with memory instance.
61 WasmEdge::Runtime::Instance::ModuleInstance moduleInst("");
62 // moduleInst.addHostFunc()
63 moduleInst.addHostMemory(
64 "memory", std::make_unique<WasmEdge::Runtime::Instance::MemoryInstance>(
65 WasmEdge::AST::MemoryType(1)));
66 auto *memoryInst = moduleInst.findMemoryExports("memory");
67 ASSERT_NE(memoryInst, nullptr);
68 auto &memoryInstRef = *memoryInst;
69 WasmEdge::Executor::Executor executor((WasmEdge::Configure()));
70 WasmEdge::Runtime::CallingFrame CallFrame(&executor, &moduleInst);
71
72 namespace fs = std::filesystem;
73 auto bpfObject = getAssertsPath() / "simple_map.bpf.o";
74
75 // Ensure the BPF object we need exists.
76 ASSERT_TRUE(fs::exists(bpfObject));
77
78 // Read the BPF object into Wasm memory.
79 std::ifstream bpfObjStream(bpfObject);
80 ASSERT_TRUE(bpfObjStream.is_open());
81 ASSERT_TRUE(bpfObjStream.good());
82 std::vector<char> bpfObjectBytes(
83 (std::istreambuf_iterator<char>(bpfObjStream)),
84 std::istreambuf_iterator<char>());
85 ASSERT_FALSE(bpfObjectBytes.empty());
86 // Offset used to place data in memory.
87 uint32_t nextOffset = 1;
88
89 // Put the BPF object in memory.
90 const uint32_t bpfObjectMemoryOffset = nextOffset;
91 fillMemContent(memoryInstRef, bpfObjectMemoryOffset, bpfObjectBytes);
92 nextOffset += static_cast<uint32_t>(bpfObjectBytes.size());
93
94 // Write the strings to memory.
95 std::array<const char *, 3> strings = {
96 "test_map", // Map name
97 "sched_wakeup", // Program names
98 "" // An empty string
99 };
100 std::array<uint32_t, 3> stringOffsets;
101
102 for (size_t i = 0; i < strings.size(); i++) {
103 std::string currString(strings[i]);
104 std::vector<char> bytes(currString.begin(), currString.end());
105 // Ensure that strings are zero-terminated
106 bytes.push_back('\0');
107 fillMemContent(memoryInstRef, nextOffset, bytes);
108 stringOffsets[i] = nextOffset;
109 nextOffset += static_cast<uint32_t>(bytes.size());
110 }
111

Callers

nothing calls this directly

Calls 15

MemoryTypeClass · 0.85
ConfigureClass · 0.85
addHostMemoryMethod · 0.80
findMemoryExportsMethod · 0.80
is_openMethod · 0.80
goodMethod · 0.80
findFuncExportsMethod · 0.80
isHostFunctionMethod · 0.80
seedMethod · 0.80
createModuleFunction · 0.70
getAssertsPathFunction · 0.70
fillMemContentFunction · 0.70

Tested by

no test coverage detected