Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
max_pages_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/backend.hpp>
2
3#include "utils.hpp"
4#include <catch2/catch.hpp>
5
6using namespace sysio::vm;
7
8extern wasm_allocator wa;
9
10namespace {
11
12/*
13 * (module
14 * (memory 3 6)
15 * (func (export "f") (result i32)
16 * (i32.const 1)
17 * (memory.grow)
18 * )
19 * )
20 */
21std::vector<uint8_t> mem_wasm = {
22 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60,
23 0x00, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x05, 0x04, 0x01, 0x01, 0x03,
24 0x06, 0x07, 0x05, 0x01, 0x01, 0x66, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06,
25 0x00, 0x41, 0x01, 0x40, 0x00, 0x0b
26};
27
28struct empty_options {};
29struct static_options_2 {
30 static constexpr std::uint32_t max_pages = 2;
31};
32struct static_options_3 {
33 static constexpr std::uint32_t max_pages = 3;
34};
35struct dynamic_options {
36 std::uint32_t max_pages;
37};
38
39}
40
41BACKEND_TEST_CASE("Test max_pages default", "[max_pages_test]") {
43 backend_t backend(mem_wasm, &wa);
44}
45
46BACKEND_TEST_CASE("Test max_pages unlimited", "[max_pages_test]") {
48 backend_t backend(mem_wasm, &wa);
49}
50
51BACKEND_TEST_CASE("Test max_pages static fail", "[max_pages_test]") {
53 CHECK_THROWS_AS(backend_t(mem_wasm, &wa), wasm_parse_exception);
54}
55
56BACKEND_TEST_CASE("Test max_pages static pass", "[max_pages_test]") {
58 backend_t backend(mem_wasm, &wa);
59 CHECK(backend.call_with_return("env", "f")->to_i32() == -1);
60}
61
62BACKEND_TEST_CASE("Test max_pages dynamic fail", "[max_pages_test]") {
64 CHECK_THROWS_AS(backend_t(mem_wasm, nullptr, dynamic_options{2}), wasm_parse_exception);
65}
66
67BACKEND_TEST_CASE("Test max_pages dynamic pass", "[max_pages_test]") {
69 backend_t backend(mem_wasm, &wa, dynamic_options{3});
70 CHECK(backend.call_with_return("env", "f")->to_i32() == -1);
71 backend.initialize(nullptr, dynamic_options{4});
72 CHECK(backend.call_with_return("env", "f")->to_i32() == 3);
73 backend.initialize(nullptr, dynamic_options{3});
74 CHECK_THROWS_AS(backend.initialize(nullptr, dynamic_options{2}), std::exception);
75}
backend & initialize(host_t *host, const Options &new_options)
Definition backend.hpp:118
auto call_with_return(host_t &host, const std::string_view &mod, const std::string_view &func, Args... args)
Definition backend.hpp:178
#define CHECK(cond)
Definition util.h:80
sysio::vm::backend< rhf_t > backend_t
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
wasm_allocator wa
Definition main.cpp:10
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59