avmnif-rs 0.4.1

Safe NIF toolkit for AtomVM written in Rust
Documentation
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
//! Resource management testing suite
//! 
//! This module provides comprehensive tests for the resource management system
//! without requiring a running AtomVM instance. All mocks are in testing/mocks.rs.

use crate::resource::*;
use crate::term::NifError;
use crate::testing::mocks::MockResourceManager;

// Unit tests
#[cfg(test)]
mod tests {
    use super::*;

    // Test data structures
    #[repr(C)]
    #[derive(Debug, PartialEq)]
    struct TestResource {
        id: u32,
        name: [u8; 16],
        active: bool,
    }

    impl Default for TestResource {
        fn default() -> Self {
            Self {
                id: 42,
                name: *b"test_resource\0\0\0",
                active: true,
            }
        }
    }

    #[test]
    fn test_mock_resource_manager_creation() {
        let manager = MockResourceManager::new();
        assert_eq!(manager.get_resource_count(), 0);
        assert_eq!(manager.get_resource_type_count(), 0);
        assert_eq!(manager.get_monitor_count(), 0);
    }

    #[test]
    fn test_resource_type_initialization() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Test successful initialization
        let result = manager.init_resource_type(
            env,
            "test_resource",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        );
        
        assert!(result.is_ok());
        assert!(manager.verify_init_called("test_resource"));
        assert_eq!(manager.get_resource_type_count(), 1);
    }

    #[test]
    fn test_invalid_resource_name() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Test empty name
        let result = manager.init_resource_type(
            env,
            "",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        );
        
        assert_eq!(result.unwrap_err(), ResourceError::InvalidName);
        
        // Test too long name
        let long_name = "a".repeat(256);
        let result = manager.init_resource_type(
            env,
            &long_name,
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        );
        
        assert_eq!(result.unwrap_err(), ResourceError::InvalidName);
    }

    #[test]
    fn test_resource_allocation() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // First create a resource type
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        // Test successful allocation
        let result = manager.alloc_resource(resource_type, 1024);
        assert!(result.is_ok());
        assert_eq!(manager.get_resource_count(), 1);
        
        // Test null resource type
        let result = manager.alloc_resource(core::ptr::null_mut(), 1024);
        assert_eq!(result.unwrap_err(), ResourceError::BadResourceType);
        
        // Test zero size
        let result = manager.alloc_resource(resource_type, 0);
        assert_eq!(result.unwrap_err(), ResourceError::BadArg);
    }

    #[test]
    fn test_resource_reference_counting() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Create resource type and allocate resource
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        let resource_ptr = manager.alloc_resource(resource_type, 1024).unwrap();
        
        // Initial ref count should be 1
        assert_eq!(manager.get_resource_ref_count(resource_ptr), Some(1));
        
        // Test keep resource
        let result = manager.keep_resource(resource_ptr);
        assert!(result.is_ok());
        assert_eq!(manager.get_resource_ref_count(resource_ptr), Some(2));
        
        // Test release resource
        let result = manager.release_resource(resource_ptr);
        assert!(result.is_ok());
        assert_eq!(manager.get_resource_ref_count(resource_ptr), Some(1));
        
        // Release again to trigger destructor
        let result = manager.release_resource(resource_ptr);
        assert!(result.is_ok());
        assert_eq!(manager.get_resource_count(), 0); // Resource should be destroyed
        
        // Test null pointer
        let result = manager.keep_resource(core::ptr::null_mut());
        assert_eq!(result.unwrap_err(), ResourceError::BadArg);
    }

    #[test]
    fn test_make_and_get_resource() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Create resource type and allocate resource
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        let resource_ptr = manager.alloc_resource(resource_type, 1024).unwrap();
        
        // Test make resource
        let term = manager.make_resource(env, resource_ptr).unwrap();
        assert!(term != 0);
        
        // Test get resource - using term % 1000 to map back to resource
        let retrieved_ptr = manager.get_resource(env, term, resource_type).unwrap();
        assert_eq!(retrieved_ptr, resource_ptr);
        
        // Test with wrong type
        let other_type = manager.init_resource_type(
            env,
            "other_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        let result = manager.get_resource(env, term, other_type);
        assert_eq!(result.unwrap_err(), ResourceError::ResourceNotFound);
    }

    #[test]
    fn test_process_monitoring() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Create resource
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        let resource_ptr = manager.alloc_resource(resource_type, 1024).unwrap();
        let pid = 12345i32;
        let mut monitor = ErlNifMonitor {
            resource_type: core::ptr::null_mut(),
            ref_ticks: 0,
        };
        
        // Test monitor process
        let result = manager.monitor_process(env, resource_ptr, &pid, &mut monitor);
        assert!(result.is_ok());
        assert_eq!(manager.get_monitor_count(), 1);
        
        // Test demonitor process
        let result = manager.demonitor_process(env, resource_ptr, &monitor);
        assert!(result.is_ok());
        assert_eq!(manager.get_monitor_count(), 0);
    }

    #[test]
    fn test_select_operations() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Create resource
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        let resource_ptr = manager.alloc_resource(resource_type, 1024).unwrap();
        let pid = 12345i32;
        let reference = 0x98765432u64;
        
        // Test select
        let result = manager.select(
            env,
            5, // file descriptor
            ErlNifSelectFlags::ERL_NIF_SELECT_READ,
            resource_ptr,
            &pid,
            reference,
        );
        assert!(result.is_ok());
        
        // Verify the call was tracked
        let state = manager.get_state();
        assert_eq!(state.select_calls.len(), 1);
        assert_eq!(state.select_calls[0].0, 5); // event
        assert_eq!(state.select_calls[0].1, ErlNifSelectFlags::ERL_NIF_SELECT_READ); // mode
    }

    #[test]
    fn test_error_injection() {
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        // Test init failure
        manager.set_fail_init(true);
        let result = manager.init_resource_type(
            env,
            "test_resource",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        );
        assert_eq!(result.unwrap_err(), ResourceError::InitializationFailed);
        
        // Reset and test alloc failure
        manager.set_fail_init(false);
        let resource_type = manager.init_resource_type(
            env,
            "test_resource",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        manager.set_fail_alloc(true);
        let result = manager.alloc_resource(resource_type, 1024);
        assert_eq!(result.unwrap_err(), ResourceError::OutOfMemory);
    }

    #[test]
    fn test_resource_limits() {
        let mut manager = MockResourceManager::new().with_max_resources(1);
        let env = core::ptr::null_mut();
        
        let resource_type = manager.init_resource_type(
            env,
            "test_type",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        // First allocation should succeed
        let result = manager.alloc_resource(resource_type, 1024);
        assert!(result.is_ok());
        
        // Second allocation should fail due to limit
        let result = manager.alloc_resource(resource_type, 1024);
        assert_eq!(result.unwrap_err(), ResourceError::OutOfMemory);
    }

    #[test]
    fn test_error_conversion() {
        // Test ResourceError to NifError conversion
        assert_eq!(NifError::from(ResourceError::OutOfMemory), NifError::OutOfMemory);
        assert_eq!(NifError::from(ResourceError::BadArg), NifError::BadArg);
        assert_eq!(NifError::from(ResourceError::InvalidName), NifError::BadArg);
    }

    #[test]
    fn test_global_manager_integration() {
        // Test that we can initialize and use the global manager
        let manager = MockResourceManager::new();
        init_resource_manager(manager);
        
        // Test that we can get the manager
        let _global_manager = get_resource_manager();
        
        // Note: We cannot test the convenience functions keep_resource() and release_resource()
        // in tests because they would try to link to FFI functions that don't exist in test builds.
        // The convenience functions are only meant for production use when the global manager
        // is initialized with AtomVMResourceManager.
    }

    #[test]
    fn test_helper_functions() {
        // Test resource type init helpers
        let init = resource_type_init();
        assert_eq!(init.members, 0);
        assert!(init.dtor.is_none());
        assert!(init.stop.is_none());
        assert!(init.down.is_none());
        
        // Test with destructor
        unsafe extern "C" fn test_dtor(_env: *mut ErlNifEnv, _obj: *mut core::ffi::c_void) {}
        let init_with_dtor = resource_type_init_with_dtor(test_dtor);
        assert_eq!(init_with_dtor.members, 1);
        assert!(init_with_dtor.dtor.is_some());
        
        // Test full init
        let init_full = resource_type_init_full(Some(test_dtor), None, None);
        assert_eq!(init_full.members, 1);
        assert!(init_full.dtor.is_some());
        assert!(init_full.stop.is_none());
        assert!(init_full.down.is_none());
    }

    #[test]
    fn test_resource_type_flags() {
        // Test that our enums have correct values
        assert_eq!(ErlNifResourceFlags::ERL_NIF_RT_CREATE as i32, 1);
        
        assert_eq!(ErlNifSelectFlags::ERL_NIF_SELECT_READ as i32, 1);
        assert_eq!(ErlNifSelectFlags::ERL_NIF_SELECT_WRITE as i32, 2);
        assert_eq!(ErlNifSelectFlags::ERL_NIF_SELECT_STOP as i32, 4);
        
        // Test that flags implement required traits
        let flag1 = ErlNifResourceFlags::ERL_NIF_RT_CREATE;
        let flag2 = ErlNifResourceFlags::ERL_NIF_RT_CREATE;
        assert_eq!(flag1, flag2); // PartialEq
        
        let _flag3 = flag1; // Copy
        assert_eq!(flag1, flag2); // Original still usable after copy
    }

    #[test] 
    fn test_concurrent_access_simulation() {
        // Since we're in no_std, we can't actually test concurrency,
        // but we can simulate concurrent-like access patterns
        let mut manager = MockResourceManager::new();
        let env = core::ptr::null_mut();
        
        let resource_type = manager.init_resource_type(
            env,
            "concurrent_test",
            &resource_type_init(),
            ErlNifResourceFlags::ERL_NIF_RT_CREATE,
        ).unwrap();
        
        // Simulate multiple "threads" allocating resources
        let mut resources = alloc::vec::Vec::new();
        for i in 0..10 {
            let resource = manager.alloc_resource(resource_type, 100 + i).unwrap();
            resources.push(resource);
        }
        
        assert_eq!(manager.get_resource_count(), 10);
        
        // Simulate concurrent reference counting
        for resource in &resources {
            assert!(manager.keep_resource(*resource).is_ok());
        }
        
        // All resources should have ref count 2 now
        for resource in &resources {
            assert_eq!(manager.get_resource_ref_count(*resource), Some(2));
        }
        
        // Release all references
        for resource in &resources {
            assert!(manager.release_resource(*resource).is_ok());
            assert!(manager.release_resource(*resource).is_ok());
        }
        
        // All resources should be destroyed
        assert_eq!(manager.get_resource_count(), 0);
    }
}