witmproxy 0.0.1-alpha

A WASM-in-the-middle proxy
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
mod tests {
    use crate::{
        AppConfig, Db, Runtime,
        cli::{Commands, ResolvedCli, load_plugins_from_directory, plugin::PluginCommands},
        config::confique_app_config_layer::AppConfigLayer,
        plugins::{WitmPlugin, registry::PluginRegistry},
        test_utils::test_component_path,
        wasm::bindgen::Event,
    };
    use anyhow::Result;
    use cel_cxx::{Env, EnvBuilder};
    use confique::{Config, Layer};
    use std::path::{Path, PathBuf};
    use std::sync::Arc;
    use tempfile::tempdir;
    use tokio::sync::RwLock;

    /// Helper function to create a static CEL environment for tests
    fn create_static_cel_env() -> Result<&'static Env<'static>> {
        let env = Event::register(EnvBuilder::new())?.build()?;
        // Leak the env to get a static reference since it contains only static data
        // and we want it to live for the program duration
        Ok(Box::leak(Box::new(env)))
    }

    /// Test helper that creates a ResolvedCli with test configuration
    async fn create_test_cli(temp_path: &Path) -> ResolvedCli {
        create_test_cli_with_options(temp_path, None, false).await
    }

    /// Test helper that creates a ResolvedCli with test configuration and options
    async fn create_test_cli_with_options(
        temp_path: &Path,
        plugin_dir: Option<PathBuf>,
        auto: bool,
    ) -> ResolvedCli {
        let db_path = temp_path.join("test.db");
        let mut partial_config = AppConfigLayer::default_values();
        partial_config.db.db_path = Some(db_path);
        partial_config.db.db_password = Some("test_password".to_string());

        // Create a resolved config directly for testing
        let config = AppConfig::builder()
            .preloaded(partial_config)
            .load()
            .expect("Failed to load test config")
            .with_resolved_paths()
            .expect("Failed to resolve paths in test config");

        ResolvedCli {
            command: None,
            config,
            verbose: true,
            plugin_dir,
            auto,
        }
    }

    #[tokio::test]
    async fn test_witm_plugin_add_local_wasm() -> Result<()> {
        // Create a temporary directory for the test config
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;

        // Test path to the signed WASM component
        let wasm_path = test_component_path()?;

        // Test adding the plugin
        let command = Commands::Plugin {
            command: PluginCommands::Add {
                source: wasm_path.clone(),
            },
        };
        cli.handle_command(&command).await?;
        // Verify the plugin was actually added to the database
        let db_file_path = temp_path.join("test.db");
        let mut db = Db::from_path(db_file_path, "test_password").await.unwrap();

        // Create runtime to check plugins
        let runtime = Runtime::try_default().unwrap();
        let env = create_static_cel_env()?;
        let plugins = WitmPlugin::all(&mut db, &runtime.engine, env)
            .await
            .unwrap();
        assert!(
            !plugins.is_empty(),
            "No plugins found in database after adding"
        );

        // Check that at least one plugin was added
        let test_plugin = plugins
            .iter()
            .find(|p| p.name.contains("test") || p.namespace.contains("test"));
        assert!(test_plugin.is_some(), "Test plugin not found in database");

        if let Some(plugin) = test_plugin {
            assert!(
                !plugin.component_bytes.is_empty(),
                "Plugin component bytes should not be empty"
            );
            assert!(
                !plugin.publickey.is_empty(),
                "Plugin should have a public key"
            );
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_witm_plugin_add_nonexistent_file() {
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;
        let command = Commands::Plugin {
            command: PluginCommands::Add {
                source: "/nonexistent/file.wasm".to_string(),
            },
        };

        // Test with non-existent file
        let result = cli.handle_command(&command).await;

        assert!(result.is_err(), "Should fail for non-existent file");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("File does not exist")
        );
    }

    #[tokio::test]
    async fn test_witm_plugin_add_non_wasm_file() {
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();
        let dummy_file = temp_path.join("not_a_wasm.txt");

        // Create a dummy non-WASM file
        std::fs::write(&dummy_file, "This is not a WASM file").unwrap();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;

        // Test with non-WASM file
        let command = Commands::Plugin {
            command: PluginCommands::Add {
                source: dummy_file.to_str().unwrap().to_string(),
            },
        };
        let result = cli.handle_command(&command).await;

        assert!(result.is_err(), "Should fail for non-WASM file");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Only .wasm files are supported")
        );
    }

    #[tokio::test]
    async fn test_witm_plugin_remove_by_name() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;

        // Test path to the signed WASM component
        let wasm_path = test_component_path()?;

        // Add the plugin first
        let add_command = Commands::Plugin {
            command: PluginCommands::Add {
                source: wasm_path.clone(),
            },
        };
        let result = cli.handle_command(&add_command).await;
        assert!(result.is_ok(), "Failed to add plugin: {:?}", result.err());

        // Verify plugin was added
        let db_file_path = temp_path.join("test.db");
        let mut db = Db::from_path(db_file_path, "test_password").await.unwrap();

        let runtime = Runtime::try_default().unwrap();
        let env = create_static_cel_env()?;
        let plugins_before = WitmPlugin::all(&mut db, &runtime.engine, env)
            .await
            .unwrap();
        assert!(!plugins_before.is_empty(), "No plugins found after adding");

        let test_plugin = &plugins_before[0];
        let plugin_name = &test_plugin.name;

        // Test removing the plugin by name
        let remove_command = Commands::Plugin {
            command: PluginCommands::Remove {
                plugin_name: plugin_name.clone(),
            },
        };
        let remove_result = cli.handle_command(&remove_command).await;
        assert!(
            remove_result.is_ok(),
            "Failed to remove plugin: {:?}",
            remove_result.err()
        );

        // Verify plugin was removed
        let plugins_after = WitmPlugin::all(&mut db, &runtime.engine, env)
            .await
            .unwrap();
        assert!(
            plugins_after.is_empty(),
            "Plugin was not removed from database"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_witm_plugin_remove_by_namespace_name() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;

        // Test path to the signed WASM component
        let wasm_path = test_component_path()?;

        // Add the plugin first
        let add_command = Commands::Plugin {
            command: PluginCommands::Add {
                source: wasm_path.clone(),
            },
        };
        let result = cli.handle_command(&add_command).await;
        assert!(result.is_ok(), "Failed to add plugin: {:?}", result.err());

        // Verify plugin was added and get its full ID
        let db_file_path = temp_path.join("test.db");
        let mut db = Db::from_path(db_file_path, "test_password").await.unwrap();

        let runtime = Runtime::try_default().unwrap();
        let env = create_static_cel_env()?;
        let plugins_before = WitmPlugin::all(&mut db, &runtime.engine, env)
            .await
            .unwrap();
        assert!(!plugins_before.is_empty(), "No plugins found after adding");

        let test_plugin = &plugins_before[0];
        let full_plugin_id = format!("{}/{}", test_plugin.namespace, test_plugin.name);

        // Test removing the plugin by namespace/name
        let remove_command = Commands::Plugin {
            command: PluginCommands::Remove {
                plugin_name: full_plugin_id.clone(),
            },
        };
        let remove_result = cli.handle_command(&remove_command).await;
        assert!(
            remove_result.is_ok(),
            "Failed to remove plugin: {:?}",
            remove_result.err()
        );

        // Verify plugin was removed
        let plugins_after = WitmPlugin::all(&mut db, &runtime.engine, env)
            .await
            .unwrap();
        assert!(
            plugins_after.is_empty(),
            "Plugin was not removed from database"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_witm_plugin_remove_nonexistent() {
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create resolved CLI instance with test configuration
        let cli = create_test_cli(temp_path).await;

        // Test removing a nonexistent plugin
        let remove_command = Commands::Plugin {
            command: PluginCommands::Remove {
                plugin_name: "nonexistent_plugin".to_string(),
            },
        };
        let remove_result = cli.handle_command(&remove_command).await;
        assert!(
            remove_result.is_ok(),
            "Should not fail when removing nonexistent plugin"
        );
    }

    #[tokio::test]
    async fn test_plugin_dir_loading() -> Result<()> {
        // Create temporary directories
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();
        let plugin_dir = temp_path.join("plugins");
        std::fs::create_dir_all(&plugin_dir)?;

        // Initialize database
        let db_path = temp_path.join("test.db");
        let db = Db::from_path(db_path, "test_password").await?;
        db.migrate().await?;

        // Create runtime and plugin registry
        let runtime = Runtime::try_default()?;
        let registry = PluginRegistry::new(db, runtime)?;
        let registry = Arc::new(RwLock::new(registry));

        // Initially, plugin directory is empty, so no plugins should be loaded
        load_plugins_from_directory(&plugin_dir, registry.clone()).await?;
        {
            let reg = registry.read().await;
            assert!(
                reg.plugins().is_empty(),
                "No plugins should be loaded from empty directory"
            );
        }

        // Copy test component to plugin directory
        let wasm_path = test_component_path()?;
        let dest_path = plugin_dir.join("test_plugin.wasm");
        std::fs::copy(&wasm_path, &dest_path)?;

        // Load plugins again - should find the plugin now
        load_plugins_from_directory(&plugin_dir, registry.clone()).await?;
        {
            let reg = registry.read().await;
            assert_eq!(
                reg.plugins().len(),
                1,
                "Expected exactly one plugin to be loaded from directory"
            );

            // Verify plugin was loaded correctly
            let plugin = reg.plugins().values().next().unwrap();
            assert!(
                !plugin.component_bytes.is_empty(),
                "Plugin component bytes should not be empty"
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_plugin_dir_invalid_wasm_skipped() -> Result<()> {
        // Create temporary directories
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();
        let plugin_dir = temp_path.join("plugins");
        std::fs::create_dir_all(&plugin_dir)?;

        // Initialize database
        let db_path = temp_path.join("test.db");
        let db = Db::from_path(db_path, "test_password").await?;
        db.migrate().await?;

        // Create runtime and plugin registry
        let runtime = Runtime::try_default()?;
        let registry = PluginRegistry::new(db, runtime)?;
        let registry = Arc::new(RwLock::new(registry));

        // Create an invalid wasm file
        let invalid_path = plugin_dir.join("invalid.wasm");
        std::fs::write(&invalid_path, b"not a valid wasm file")?;

        // Also copy a valid plugin
        let wasm_path = test_component_path()?;
        let valid_path = plugin_dir.join("valid_plugin.wasm");
        std::fs::copy(&wasm_path, &valid_path)?;

        // Load plugins - should load valid one and skip invalid
        let result = load_plugins_from_directory(&plugin_dir, registry.clone()).await;
        assert!(
            result.is_ok(),
            "Should not fail even with invalid wasm files"
        );

        {
            let reg = registry.read().await;
            assert_eq!(
                reg.plugins().len(),
                1,
                "Should load only the valid plugin, skipping invalid"
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_plugin_dir_non_wasm_files_ignored() -> Result<()> {
        // Create temporary directories
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();
        let plugin_dir = temp_path.join("plugins");
        std::fs::create_dir_all(&plugin_dir)?;

        // Initialize database
        let db_path = temp_path.join("test.db");
        let db = Db::from_path(db_path, "test_password").await?;
        db.migrate().await?;

        // Create runtime and plugin registry
        let runtime = Runtime::try_default()?;
        let registry = PluginRegistry::new(db, runtime)?;
        let registry = Arc::new(RwLock::new(registry));

        // Create non-wasm files that should be ignored
        std::fs::write(plugin_dir.join("readme.txt"), b"readme content")?;
        std::fs::write(plugin_dir.join("config.json"), b"{}")?;

        // Copy a valid plugin
        let wasm_path = test_component_path()?;
        let valid_path = plugin_dir.join("plugin.wasm");
        std::fs::copy(&wasm_path, &valid_path)?;

        // Load plugins - should only load .wasm files
        load_plugins_from_directory(&plugin_dir, registry.clone()).await?;

        {
            let reg = registry.read().await;
            assert_eq!(
                reg.plugins().len(),
                1,
                "Should only load .wasm files, ignoring other extensions"
            );
        }

        Ok(())
    }
}