coapum 0.2.0

A modern, ergonomic CoAP (Constrained Application Protocol) library for Rust with support for DTLS, observers, and asynchronous handlers
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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Comprehensive server tests
//!
//! These tests focus on server functionality including connection management,
//! security features, path validation, and error handling.

use std::sync::Arc;
use std::time::{Duration, Instant};

use coapum::{
    config::Config,
    extract::{State, StatusCode},
    observer::memory::MemObserver,
    router::RouterBuilder,
};
use tower::Service;

// Simple test state
#[derive(Debug, Clone)]
struct TestServerState {
    counter: Arc<std::sync::Mutex<u32>>,
}

impl AsRef<TestServerState> for TestServerState {
    fn as_ref(&self) -> &TestServerState {
        self
    }
}

// Simple handler for testing
async fn test_handler(State(state): State<TestServerState>) -> StatusCode {
    let mut counter = state.counter.lock().unwrap();
    *counter += 1;
    StatusCode::Content
}

// Handler that always returns error for testing error paths
async fn error_handler() -> Result<StatusCode, StatusCode> {
    Err(StatusCode::InternalServerError)
}

mod connection_info_tests {
    use super::*;

    #[test]
    fn test_connection_info_creation() {
        let (_tx, _rx) = tokio::sync::mpsc::channel::<()>(1);

        // Test connection info structure
        // Note: ConnectionInfo is private, so we test the concepts it represents
        let established_at = Instant::now();
        let reconnect_count = 0u32;

        // Verify timing constraints exist (from constants in serve.rs)
        const MIN_RECONNECT_INTERVAL: Duration = Duration::from_secs(5);
        #[allow(dead_code)]
        const MAX_RECONNECT_ATTEMPTS: u32 = 10;

        assert!(MIN_RECONNECT_INTERVAL.as_secs() >= 5);

        // Test that timing calculations work
        let elapsed = established_at.elapsed();
        assert!(elapsed < Duration::from_secs(1)); // Should be very recent

        // Test reconnection logic
        assert!(reconnect_count < MAX_RECONNECT_ATTEMPTS);
    }

    #[test]
    fn test_security_constants() {
        // Test that security constants are reasonable
        const MIN_RECONNECT_INTERVAL: Duration = Duration::from_secs(5);
        const MAX_RECONNECT_ATTEMPTS: u32 = 10;
        const MAX_IDENTITY_LENGTH: usize = 256;

        #[allow(dead_code)]
        const fn validate_constants() {
            const _: () = assert!(MAX_RECONNECT_ATTEMPTS >= 10);
            const _: () = assert!(
                MAX_RECONNECT_ATTEMPTS >= 3,
                "Should allow some reconnections"
            );
            const _: () = assert!(
                MAX_IDENTITY_LENGTH >= 32,
                "Should allow reasonable identity lengths"
            );
            const _: () = assert!(
                MAX_IDENTITY_LENGTH <= 1024,
                "Should prevent excessive identity lengths"
            );
        }

        assert!(
            MIN_RECONNECT_INTERVAL.as_secs() >= 1,
            "Reconnect interval should prevent rapid abuse"
        );
    }
}

mod path_validation_tests {

    // Since validate_observer_path is private, we need to create a public wrapper for testing
    // or test the behavior through integration tests. Let's create utility functions that
    // replicate the validation logic for testing purposes.

    fn test_path_validation(path: &str) -> Result<String, String> {
        if path.is_empty() {
            return Err("Path is empty".to_string());
        }

        // Security: Reject paths containing dangerous patterns
        if path.contains("..") || path.contains("./") || path.contains("\\") {
            return Err("Path traversal attempt detected".to_string());
        }

        // Normalize and validate path components
        let components: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

        // Security: Limit path depth to prevent resource exhaustion
        const MAX_PATH_DEPTH: usize = 10;
        if components.len() > MAX_PATH_DEPTH {
            return Err("Path too deep (max 10 components)".to_string());
        }

        // Security: Validate each path component for safe characters only
        for component in &components {
            if !component
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
            {
                return Err("Path contains invalid characters".to_string());
            }
        }

        // Return normalized path
        if components.is_empty() {
            Ok("/".to_string())
        } else {
            Ok(format!("/{}", components.join("/")))
        }
    }

    #[test]
    fn test_path_validation_empty_path() {
        let result = test_path_validation("");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Path is empty"));
    }

    #[test]
    fn test_path_validation_traversal_attempts() {
        let malicious_paths = vec![
            "../secrets",
            "data/../../../etc/passwd",
            "./config",
            "/data/../admin",
            "normal/../../root",
            "data\\windows\\system32",
        ];

        for malicious_path in malicious_paths {
            let result = test_path_validation(malicious_path);
            assert!(result.is_err(), "Should reject path: {}", malicious_path);
            assert!(result.unwrap_err().contains("Path traversal attempt"));
        }
    }

    #[test]
    fn test_path_validation_depth_limits() {
        // Create a path that exceeds the maximum depth (10 components)
        let components = ["component"; 11];
        let deep_path = format!("/{}", components.join("/"));

        let result = test_path_validation(&deep_path);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Path too deep"));
    }

    #[test]
    fn test_path_validation_invalid_characters() {
        let invalid_paths = vec![
            "/data/sensor@1",
            "/api/user#123",
            "/device/temp$",
            "/path with spaces",
            "/api/user;rm",
            "/data\x00null",
        ];

        for invalid_path in invalid_paths {
            let result = test_path_validation(invalid_path);
            assert!(result.is_err(), "Should reject path: {}", invalid_path);
            assert!(result.unwrap_err().contains("invalid characters"));
        }
    }

    #[test]
    fn test_path_validation_valid_paths() {
        let valid_paths = vec![
            "/api/sensors",
            "/device_123/temperature",
            "/data-source/readings",
            "/sensors/device_1/temp",
            "sensors/data",         // Without leading slash
            "/a/b/c/d/e/f/g/h/i/j", // Exactly at depth limit (10 components)
        ];

        for valid_path in valid_paths {
            let result = test_path_validation(valid_path);
            assert!(
                result.is_ok(),
                "Should accept valid path: {} - Error: {:?}",
                valid_path,
                result.err()
            );

            let normalized = result.unwrap();
            // Should always start with /
            assert!(
                normalized.starts_with('/'),
                "Normalized path should start with /"
            );
            // Should not have double slashes
            assert!(
                !normalized.contains("//"),
                "Should not contain double slashes"
            );
        }
    }

    #[test]
    fn test_path_normalization() {
        let test_cases = vec![
            ("sensors/temp", "/sensors/temp"),
            ("/sensors/temp", "/sensors/temp"),
            ("///sensors///temp///", "/sensors/temp"),
        ];

        for (input, expected) in test_cases {
            let result = test_path_validation(input);
            assert!(result.is_ok(), "Should normalize path: {}", input);
            assert_eq!(
                result.unwrap(),
                expected,
                "Incorrect normalization for: {}",
                input
            );
        }
    }
}

mod identity_sanitization_tests {

    // Test identity sanitization logic based on serve.rs implementation
    fn test_identity_sanitization(identity: &str) -> Result<String, String> {
        const MAX_IDENTITY_LENGTH: usize = 256;

        if identity.len() > MAX_IDENTITY_LENGTH {
            return Err(format!("Identity too long: {} bytes", identity.len()));
        }

        // Sanitize identity to prevent injection attacks
        let sanitized: String = identity
            .chars()
            .filter(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-' || *c == '.')
            .take(MAX_IDENTITY_LENGTH)
            .collect();

        if sanitized.is_empty() {
            return Err("Identity contains no valid characters".to_string());
        }

        Ok(sanitized)
    }

    #[test]
    fn test_valid_identities() {
        let valid_identities = vec![
            "client123",
            "device-sensor_1",
            "gateway.domain.com",
            "sensor_node-42",
            "a",           // Single character
            "A1_b2-c3.d4", // Mixed valid characters
        ];

        for identity in valid_identities {
            let result = test_identity_sanitization(identity);
            assert!(result.is_ok(), "Should accept valid identity: {}", identity);
            assert_eq!(
                result.unwrap(),
                identity,
                "Should not change valid identity"
            );
        }
    }

    #[test]
    fn test_identity_sanitization_filtering() {
        let test_cases = vec![
            ("client@domain", "clientdomain"),
            ("device#123", "device123"),
            ("sensor;DROP", "sensorDROP"),
            ("node\x00null", "nodenull"),
            ("test!@#$%^&*()+=", "test"),
            ("spaces in name", "spacesinname"),
        ];

        for (input, expected) in test_cases {
            let result = test_identity_sanitization(input);
            assert!(result.is_ok(), "Should sanitize identity: {}", input);
            assert_eq!(
                result.unwrap(),
                expected,
                "Incorrect sanitization for: {}",
                input
            );
        }
    }

    #[test]
    fn test_identity_length_limits() {
        let long_identity = "a".repeat(300); // Exceeds 256 character limit
        let result = test_identity_sanitization(&long_identity);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Identity too long"));
    }

    #[test]
    fn test_empty_after_sanitization() {
        let invalid_identities = vec![
            "!@#$%^&*()",          // All invalid characters
            "\x00\x01\x02",        // All non-printable
            "   ",                 // Only spaces (filtered out)
            "+=[]{}|\\:;\"'<>,?/", // All symbols
        ];

        for identity in invalid_identities {
            let result = test_identity_sanitization(identity);
            assert!(
                result.is_err(),
                "Should reject identity with no valid chars: {}",
                identity
            );
            assert!(result.unwrap_err().contains("no valid characters"));
        }
    }

    #[test]
    fn test_identity_length_truncation() {
        // Create identity that would be truncated at MAX_LENGTH
        let base = "a".repeat(200);
        let extra = "b".repeat(100); // Total would be 300
        let long_valid_identity = format!("{}{}", base, extra);

        let result = test_identity_sanitization(&long_valid_identity);
        assert!(result.is_err(), "Should reject overly long identity");
    }
}

mod config_integration_tests {
    use super::*;

    #[test]
    fn test_config_defaults_for_server() {
        let config = Config::default();

        // Test that default configuration is sensible for server use
        assert!(config.timeout > 0, "Timeout should be positive");
        assert!(
            config.buffer_size() >= 1024,
            "Buffer should be reasonable size"
        );

        // Test that DTLS config exists
        // Note: DTLSConfig fields are mostly private, but we can verify it exists
        let _dtls_config = &config.dtls_cfg;
    }

    #[tokio::test]
    async fn test_router_with_server_state() {
        let state = TestServerState {
            counter: Arc::new(std::sync::Mutex::new(0)),
        };
        let observer = MemObserver::new();
        let mut router = RouterBuilder::new(state.clone(), observer)
            .get("/test", test_handler)
            .get("/error", error_handler)
            .build();

        // Test successful request
        let request = coapum::test_utils::create_test_request("/test");
        let response = router.call(request).await.unwrap();
        assert_eq!(*response.get_status(), coapum::ResponseType::Content);

        // Verify state was modified
        {
            let counter = state.counter.lock().unwrap();
            assert_eq!(*counter, 1);
        }

        // Test error request
        let request = coapum::test_utils::create_test_request("/error");
        let response = router.call(request).await.unwrap();
        assert_eq!(
            *response.get_status(),
            coapum::ResponseType::InternalServerError
        );
    }

    #[tokio::test]
    async fn test_router_with_invalid_paths() {
        let state = TestServerState {
            counter: Arc::new(std::sync::Mutex::new(0)),
        };
        let observer = MemObserver::new();
        let mut router = RouterBuilder::new(state, observer)
            .get("/valid/path", test_handler)
            .build();

        // Test that router handles non-existent paths gracefully
        let request = coapum::test_utils::create_test_request("/nonexistent");
        let response = router.call(request).await.unwrap();
        assert_ne!(*response.get_status(), coapum::ResponseType::Content);
    }
}

mod observer_integration_tests {
    use super::*;

    #[tokio::test]
    async fn test_observer_registration_with_server() {
        let state = TestServerState {
            counter: Arc::new(std::sync::Mutex::new(0)),
        };
        let observer = MemObserver::new();
        let mut router = RouterBuilder::new(state.clone(), observer)
            .observe("/sensors/temp", test_handler, test_handler)
            .build();

        // Create simple GET request (observe flag is handled internally)
        let request = coapum::test_utils::create_test_request("/sensors/temp");

        let response = router.call(request).await.unwrap();
        assert_eq!(*response.get_status(), coapum::ResponseType::Content);

        // Verify handler was called
        let counter = state.counter.lock().unwrap();
        assert_eq!(*counter, 1);
    }

    #[tokio::test]
    async fn test_observer_path_validation_integration() {
        let state = TestServerState {
            counter: Arc::new(std::sync::Mutex::new(0)),
        };
        let observer = MemObserver::new();

        // Test that observe routes work with valid paths
        let mut router = RouterBuilder::new(state, observer)
            .observe("/valid_path-123", test_handler, test_handler)
            .build();

        let request = coapum::test_utils::create_test_request("/valid_path-123");

        let response = router.call(request).await.unwrap();
        // Should succeed with valid path
        assert_eq!(*response.get_status(), coapum::ResponseType::Content);
    }
}