brlapi 0.4.1

Safe Rust bindings for the BrlAPI library
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
// SPDX-License-Identifier: LGPL-2.1

//! Tests for BrlAPI suspend mode functionality
//!
//! These tests verify the suspend mode implementation while handling the fact
//! that most test environments don't have hardware that supports suspension.

#[cfg(feature = "dangerous-suspend-mode")]
mod suspend_tests {
    use brlapi::{BrlApiError, Connection, suspend::SuspendMode};

    /// Common test helper to try connecting
    fn try_connect() -> Result<Connection, BrlApiError> {
        Connection::open().or_else(|_| {
            // If connection fails, create a mock or skip test
            Err(BrlApiError::custom(
                "No BrlAPI daemon available for testing",
            ))
        })
    }

    #[test]
    fn test_driver_support_detection() {
        // Test drivers that don't support suspend
        assert!(!SuspendMode::is_driver_supported("nb"));
        assert!(!SuspendMode::is_driver_supported("NoBraille"));
        assert!(!SuspendMode::is_driver_supported("fake"));
        assert!(!SuspendMode::is_driver_supported("test"));
        assert!(!SuspendMode::is_driver_supported("dummy"));

        // Test drivers that should support suspend
        assert!(SuspendMode::is_driver_supported("baum"));
        assert!(SuspendMode::is_driver_supported("hims"));
        assert!(SuspendMode::is_driver_supported("alva"));
        assert!(SuspendMode::is_driver_supported("papenmeier"));
        assert!(SuspendMode::is_driver_supported("freedom"));
        assert!(SuspendMode::is_driver_supported("unknown_driver"));
    }

    #[test]
    fn test_input_validation() {
        // These tests check input validation without requiring BrlAPI daemon
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping suspend mode input validation tests - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        // Test empty driver name
        let result = SuspendMode::enter(&connection, "");
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(e.to_string().contains("empty"));
        }

        // Test driver name with null bytes
        let result = SuspendMode::enter(&connection, "driver\0name");
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(e.to_string().contains("null bytes"));
        }
    }

    #[test]
    fn test_suspend_mode_with_current_driver() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping suspend mode current driver test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        // Get current driver
        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        println!(
            "Testing suspend mode with current driver: {}",
            current_driver
        );

        // Test suspend mode entry
        match SuspendMode::enter(&connection, &current_driver) {
            Ok(suspend_mode) => {
                println!(
                    "SUCCESS: Entered suspend mode with driver '{}'",
                    current_driver
                );

                // Test suspend mode state
                assert!(suspend_mode.is_suspended());
                assert_eq!(suspend_mode.driver_name(), current_driver);

                // Driver should be automatically resumed when suspend_mode is dropped
                println!("Suspend mode will be automatically resumed via Drop trait");
            }
            Err(e) => {
                println!(
                    "EXPECTED: Suspend mode failed with driver '{}': {}",
                    current_driver, e
                );
                println!("This is normal for most drivers/configurations");

                // For NoBraille driver, this should definitely fail
                if current_driver == "nb" || current_driver == "NoBraille" {
                    assert!(
                        e.to_string().contains("not currently active")
                            || e.to_string().contains("suspend")
                            || e.to_string().contains("not supported")
                    );
                }
            }
        }
    }

    #[test]
    fn test_suspend_mode_with_wrong_driver() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping suspend mode wrong driver test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        // Get current driver
        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        // Try to suspend a different driver than what's currently active
        let wrong_driver = if current_driver == "baum" {
            "hims"
        } else {
            "baum"
        };

        println!(
            "Testing suspend mode with wrong driver: {} (current: {})",
            wrong_driver, current_driver
        );

        let result = SuspendMode::enter(&connection, &wrong_driver);
        assert!(result.is_err());

        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(error_msg.contains("not currently active") || error_msg.contains("not found"));
            println!("EXPECTED: Got appropriate error for wrong driver: {}", e);
        }
    }

    #[test]
    fn test_manual_resume() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping manual resume test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        // Get current driver
        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        // Try manual resume without suspension first
        match SuspendMode::enter(&connection, &current_driver) {
            Ok(mut suspend_mode) => {
                println!("Testing manual resume with driver '{}'", current_driver);

                assert!(suspend_mode.is_suspended());

                // Test manual resume
                match suspend_mode.resume() {
                    Ok(()) => {
                        println!("SUCCESS: Manual resume succeeded");
                        assert!(!suspend_mode.is_suspended());

                        // Test double resume (should fail)
                        let double_resume = suspend_mode.resume();
                        assert!(double_resume.is_err());
                        if let Err(e) = double_resume {
                            assert!(e.to_string().contains("not currently suspended"));
                        }
                    }
                    Err(e) => {
                        println!("Manual resume failed: {}", e);
                        // This might be expected depending on driver support
                    }
                }
            }
            Err(e) => {
                println!("Cannot enter suspend mode for manual resume test: {}", e);
                println!("This is expected for most test configurations");
            }
        }
    }

    #[test]
    fn test_suspend_mode_state_tracking() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping state tracking test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        match SuspendMode::enter(&connection, &current_driver) {
            Ok(suspend_mode) => {
                println!("Testing state tracking with driver '{}'", current_driver);

                // Test initial state
                assert!(suspend_mode.is_suspended());
                assert_eq!(suspend_mode.driver_name(), current_driver);

                // State should remain consistent
                assert!(suspend_mode.is_suspended());
                assert_eq!(suspend_mode.driver_name(), current_driver);

                println!("State tracking test passed");
            }
            Err(e) => {
                println!("Cannot enter suspend mode for state tracking test: {}", e);
                println!("This is expected for most test configurations");
            }
        }
    }

    #[test]
    fn test_suspend_mode_drop_cleanup() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping drop cleanup test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        match SuspendMode::enter(&connection, &current_driver) {
            Ok(suspend_mode) => {
                println!(
                    "Testing automatic cleanup via Drop trait with driver '{}'",
                    current_driver
                );

                assert!(suspend_mode.is_suspended());

                // When suspend_mode goes out of scope here, Drop should automatically resume
                drop(suspend_mode);

                println!("Drop cleanup completed - driver should be resumed");

                // We can't directly verify the driver was resumed without more complex
                // state tracking, but the Drop implementation should handle it
            }
            Err(e) => {
                println!("Cannot enter suspend mode for drop cleanup test: {}", e);
                println!("This is expected for most test configurations");
            }
        }
    }

    #[test]
    fn test_error_conditions() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping error conditions test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        // Test various error conditions
        let error_cases = [
            ("", "empty driver name"),
            ("nonexistent_driver_123", "nonexistent driver"),
            ("driver\0with\0nulls", "null bytes in name"),
        ];

        for (driver_name, description) in error_cases.iter() {
            println!("Testing error condition: {}", description);

            let result = SuspendMode::enter(&connection, driver_name);
            assert!(result.is_err(), "Expected error for {}", description);

            if let Err(e) = result {
                println!("Got expected error for {}: {}", description, e);
            }
        }
    }

    #[test]
    fn test_concurrent_suspend_prevention() {
        let connection = try_connect();

        // Skip if no daemon available
        if connection.is_err() {
            println!("Skipping concurrent suspend test - no BrlAPI daemon");
            return;
        }

        let connection = connection.unwrap();

        let current_driver = match connection.display_driver() {
            Ok(driver) => driver,
            Err(e) => {
                println!("Cannot get current driver: {} - skipping test", e);
                return;
            }
        };

        match SuspendMode::enter(&connection, &current_driver) {
            Ok(_suspend_mode1) => {
                println!("First suspend mode entered successfully");

                // Try to enter suspend mode again with same driver - should fail
                let result2 = SuspendMode::enter(&connection, &current_driver);
                match result2 {
                    Ok(_) => {
                        println!(
                            "WARNING: Second suspend mode succeeded - this may not be expected"
                        );
                    }
                    Err(e) => {
                        println!("EXPECTED: Second suspend mode failed: {}", e);
                        // This is the expected behavior - only one suspend mode at a time
                    }
                }

                // _suspend_mode1 will be automatically cleaned up
            }
            Err(e) => {
                println!("Cannot enter suspend mode for concurrent test: {}", e);
                println!("This is expected for most test configurations");
            }
        }
    }
}

// Tests that run even without the feature flag
#[test]
fn test_feature_flag_requirement() {
    // This test verifies that suspend mode is properly feature-gated

    #[cfg(feature = "dangerous-suspend-mode")]
    {
        // Feature is enabled - suspend module should be available
        let _ = brlapi::suspend::SuspendMode::is_driver_supported("test");
        println!("Suspend mode feature is enabled");
    }

    #[cfg(not(feature = "dangerous-suspend-mode"))]
    {
        // Feature is disabled - suspend module should not be available
        // This test just verifies the feature flag works correctly
        println!("Suspend mode feature is disabled (as expected without feature flag)");
    }
}