-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPluginTest.php
198 lines (151 loc) · 6.71 KB
/
PluginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
declare(strict_types=1);
namespace Extism\Tests;
use Extism\CurrentPlugin;
use Extism\HostFunction;
use Extism\PluginOptions;
use PHPUnit\Framework\TestCase;
use Extism\Plugin;
use Extism\Manifest;
use Extism\Manifest\PathWasmSource;
use Extism\ExtismValType;
final class PluginTest extends TestCase
{
public function testAlloc(): void
{
$plugin = self::loadPlugin("alloc.wasm", []);
$response = $plugin->call("run_test", "");
$this->assertEquals("", $response);
}
public function testFail(): void
{
$this->expectException(\Exception::class);
$plugin = self::loadPlugin("fail.wasm", []);
$plugin->call("run_test", "");
}
public function testExit(): void
{
$plugin = self::loadPlugin("exit.wasm", [], function ($manifest) {
$manifest->config->code = "2";
});
try {
$plugin->call("_start", "");
} catch (\Exception $e) {
$this->assertStringContainsString("2", $e->getMessage());
}
}
public function testTimeout(): void
{
$plugin = self::loadPlugin("sleep.wasm", [], function ($manifest) {
$manifest->timeout_ms = 50;
$manifest->config->duration = "3"; // sleep for 3 seconds
});
try {
$plugin->call("run_test", "");
} catch (\Exception $e) {
$this->assertStringContainsString("timeout", $e->getMessage());
}
}
public function testFileSystem(): void
{
$plugin = self::loadPlugin("fs.wasm", [], function ($manifest) {
$manifest->allowed_paths = ["tests/data" => "/mnt"];
});
$response = $plugin->call("_start", "");
$this->assertEquals("hello world!", $response);
}
public function testFunctionExists(): void
{
$plugin = self::loadPlugin("alloc.wasm", []);
$this->assertTrue($plugin->functionExists("run_test"));
$this->assertFalse($plugin->functionExists("i_dont_exist"));
}
public function testHostFunctions(): void
{
$kvstore = [];
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$kvstore) {
return $kvstore[$key] ?? "\0\0\0\0";
});
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (string $key, string $value) use (&$kvstore) {
$kvstore[$key] = $value;
});
$plugin = self::loadPlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":3,"vowels":"aeiouAEIOU"}', $response);
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}
public function testCallWithHostContext(): void
{
$multiUserKvStore = [[]];
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$multiUserKvStore) {
$ctx = $p->getCallHostContext(); // get a copy of the host context data
$userId = $ctx["userId"];
$kvStore = $multiUserKvStore[$userId] ?? [];
return $kvStore[$key] ?? "\0\0\0\0";
});
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (CurrentPlugin $p, string $key, string $value) use (&$multiUserKvStore) {
$ctx = $p->getCallHostContext(); // get a copy of the host context data
$userId = $ctx["userId"];
$kvStore = $multiUserKvStore[$userId] ?? [];
$kvStore[$key] = $value;
$multiUserKvStore[$userId] = $kvStore;
});
$plugin = self::loadPlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);
$ctx = ["userId" => 1];
$response = $plugin->callWithContext("count_vowels", "Hello World!", $ctx);
$this->assertEquals('{"count":3,"total":3,"vowels":"aeiouAEIOU"}', $response);
$response = $plugin->callWithContext("count_vowels", "Hello World!", $ctx);
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}
public function testHostFunctionManual(): void
{
$kvstore = [];
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, int $keyPtr) use (&$kvstore) {
$key = $p->read_block($keyPtr);
$value = $kvstore[$key] ?? "\0\0\0\0";
return $p->write_block($value);
});
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (CurrentPlugin $p, int $keyPtr, int $valuePtr) use (&$kvstore) {
$key = $p->read_block($keyPtr);
$value = $p->read_block($valuePtr);
$kvstore[$key] = $value;
});
$plugin = self::loadPlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":3,"vowels":"aeiouAEIOU"}', $response);
$response = $plugin->call("count_vowels", "Hello World!");
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}
public function testHostFunctionNamespace(): void
{
$this->expectExceptionMessage("Extism: unable to load plugin: Unable to compile Extism plugin: unknown import: `extism:host/user::kv_read` has not been defined");
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (string $key) {
//
});
$kvRead->set_namespace("custom");
$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (string $key, string $value) {
//
});
$kvWrite->set_namespace("custom");
$plugin = self::loadPlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);
}
public function testFuelLimit(): void
{
$plugin = self::loadPlugin("sleep.wasm", [], null, new PluginOptions(true, 10));
try {
$plugin->call("run_test", "");
} catch (\Exception $e) {
$this->assertStringContainsString("fuel", $e->getMessage());
}
}
public static function loadPlugin(string $name, array $functions, ?callable $config = null, $withWasi = true)
{
$path = __DIR__ . '/../wasm/' . $name;
$manifest = new Manifest(new PathWasmSource($path, 'main'));
if ($config !== null) {
$config($manifest);
}
return new Plugin($manifest, $withWasi, $functions);
}
}