mik-sdk 0.1.2

Ergonomic macros for WASI HTTP handlers - ok!, error!, json!
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Cryptographically secure random value generation.
//!
//! This module provides convenient functions for generating random values
//! using the runtime's cryptographic random number generator.
//!
//! # Platform Support
//!
//! | Platform | Implementation | Notes |
//! |----------|----------------|-------|
//! | **WASM** | `wasi:random/random` | Production - uses runtime's secure RNG |
//! | **Native (tests)** | `getrandom` crate | Testing only - uses OS entropy |
//! | **Native (non-test)** | Compile stub | Panics at runtime with helpful message |
//!
//! # Why WASM-only?
//!
//! This SDK is designed for WASI HTTP handlers that run on wasmtime, Spin, or wasmCloud.
//! The random functions use `wasi:random/random` for cryptographic randomness in WASM.
//!
//! Native builds compile (so `cargo check` works) but panic at runtime with a clear
//! error message directing you to compile for `wasm32-wasip2`.
//!
//! # Security
//!
//! All functions in this module use cryptographically secure randomness
//! suitable for generating tokens, secrets, and UUIDs.
//!
//! # Examples
//!
//! ```ignore
//! use mik_sdk::random;
//!
//! // Generate random bytes
//! let secret = random::bytes(32);
//! assert_eq!(secret.len(), 32);
//!
//! // Generate a random u64
//! let id = random::u64();
//!
//! // Generate a UUID v4
//! let uuid = random::uuid();
//! assert_eq!(uuid.len(), 36); // "550e8400-e29b-41d4-a716-446655440000"
//!
//! // Generate random hex string
//! let token = random::hex(16);
//! assert_eq!(token.len(), 32); // 16 bytes = 32 hex characters
//! ```

// =============================================================================
// WASM IMPLEMENTATION (Production)
// Uses wasi:random/random for cryptographic randomness
// =============================================================================

/// Generate cryptographically secure random bytes.
///
/// Uses the runtime's secure random number generator:
/// - WASM: `wasi:random/random` directly
/// - Native (tests): OS entropy via getrandom crate
///
/// # Panics
///
/// Panics if the underlying RNG fails (extremely rare, indicates critical system issue).
/// On native non-test builds, always panics with a message to use WASM target.
///
/// # Examples
///
/// ```ignore
/// let key = mik_sdk::random::bytes(32);
/// assert_eq!(key.len(), 32);
/// ```
#[must_use]
#[cfg(target_arch = "wasm32")]
pub fn bytes(len: usize) -> Vec<u8> {
    crate::wasi_http::wasi::random::random::get_random_bytes(len as u64)
}

// =============================================================================
// NATIVE TEST IMPLEMENTATION
// Uses getrandom crate for OS entropy (dev-dependency)
// =============================================================================

/// Generate cryptographically secure random bytes (native test implementation).
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), test))]
pub fn bytes(len: usize) -> Vec<u8> {
    let mut buf = vec![0u8; len];
    getrandom::fill(&mut buf).expect("system RNG failure");
    buf
}

// =============================================================================
// NATIVE NON-TEST STUBS
// These compile but panic at runtime - allows cargo check/build to work
// =============================================================================

/// Stub for native non-test builds. Panics with helpful error message.
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
pub fn bytes(_len: usize) -> Vec<u8> {
    panic!(
        "\n\
        ╔══════════════════════════════════════════════════════════════════╗\n\
        ║  random::bytes() is only available in WASM builds                ║\n\
        ╠══════════════════════════════════════════════════════════════════╣\n\
        ║                                                                  ║\n\
        ║  This SDK is designed for WASI HTTP handlers.                    ║\n\
        ║  Random functions use wasi:random/random in WASM.                ║\n\
        ║                                                                  ║\n\
        ║  To build for WASM:                                              ║\n\
        ║    cargo component build --target wasm32-wasip2                  ║\n\
        ║                                                                  ║\n\
        ║  Or run tests (which use getrandom):                             ║\n\
        ║    cargo test                                                    ║\n\
        ║                                                                  ║\n\
        ╚══════════════════════════════════════════════════════════════════╝\n"
    )
}

/// Generate a cryptographically secure random u64.
///
/// # Panics
///
/// Panics if the underlying RNG fails (extremely rare, indicates critical system issue).
/// On native non-test builds, always panics with a message to use WASM target.
///
/// # Examples
///
/// ```ignore
/// let id = mik_sdk::random::u64();
/// ```
#[must_use]
#[cfg(target_arch = "wasm32")]
pub fn u64() -> u64 {
    crate::wasi_http::wasi::random::random::get_random_u64()
}

/// Generate a cryptographically secure random u64 (native test implementation).
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), test))]
pub fn u64() -> u64 {
    let mut buf = [0u8; 8];
    getrandom::fill(&mut buf).expect("system RNG failure");
    u64::from_le_bytes(buf)
}

/// Stub for native non-test builds. Panics with helpful error message.
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
pub fn u64() -> u64 {
    panic!(
        "\n\
        ╔══════════════════════════════════════════════════════════════════╗\n\
        ║  random::u64() is only available in WASM builds                  ║\n\
        ╠══════════════════════════════════════════════════════════════════╣\n\
        ║                                                                  ║\n\
        ║  This SDK is designed for WASI HTTP handlers.                    ║\n\
        ║  Random functions use wasi:random/random in WASM.                ║\n\
        ║                                                                  ║\n\
        ║  To build for WASM:                                              ║\n\
        ║    cargo component build --target wasm32-wasip2                  ║\n\
        ║                                                                  ║\n\
        ╚══════════════════════════════════════════════════════════════════╝\n"
    )
}

/// Generate a UUID v4 string.
///
/// Returns a standard UUID v4 format: `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`
/// where `x` is any hexadecimal digit and `y` is one of `8`, `9`, `a`, or `b`.
///
/// # Panics
///
/// Panics if the underlying RNG fails (extremely rare, indicates critical system issue).
/// On native non-test builds, always panics with a message to use WASM target.
///
/// # Examples
///
/// ```ignore
/// let id = mik_sdk::random::uuid();
/// assert_eq!(id.len(), 36);
/// assert_eq!(id.chars().nth(14), Some('4')); // Version 4
/// ```
#[must_use]
#[cfg(any(target_arch = "wasm32", test))]
pub fn uuid() -> String {
    let mut buf = bytes(16);

    // Set version bits (4 bits at byte 6, high nibble = 0100 for v4)
    buf[6] = (buf[6] & 0x0F) | 0x40;

    // Set variant bits (2 bits at byte 8, high 2 bits = 10 for RFC 4122)
    buf[8] = (buf[8] & 0x3F) | 0x80;

    // Format as UUID string
    format!(
        "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
        buf[0],
        buf[1],
        buf[2],
        buf[3],
        buf[4],
        buf[5],
        buf[6],
        buf[7],
        buf[8],
        buf[9],
        buf[10],
        buf[11],
        buf[12],
        buf[13],
        buf[14],
        buf[15]
    )
}

/// Stub for native non-test builds. Panics with helpful error message.
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
pub fn uuid() -> String {
    panic!(
        "\n\
        ╔══════════════════════════════════════════════════════════════════╗\n\
        ║  random::uuid() is only available in WASM builds                 ║\n\
        ╠══════════════════════════════════════════════════════════════════╣\n\
        ║                                                                  ║\n\
        ║  This SDK is designed for WASI HTTP handlers.                    ║\n\
        ║  Random functions use wasi:random/random in WASM.                ║\n\
        ║                                                                  ║\n\
        ║  To build for WASM:                                              ║\n\
        ║    cargo component build --target wasm32-wasip2                  ║\n\
        ║                                                                  ║\n\
        ╚══════════════════════════════════════════════════════════════════╝\n"
    )
}

/// Generate a random hexadecimal string.
///
/// # Panics
///
/// Panics if the underlying RNG fails (extremely rare, indicates critical system issue).
/// On native non-test builds, always panics with a message to use WASM target.
///
/// # Examples
///
/// ```ignore
/// let token = mik_sdk::random::hex(16);
/// assert_eq!(token.len(), 32); // 16 bytes = 32 hex chars
/// ```
#[must_use]
#[cfg(any(target_arch = "wasm32", test))]
pub fn hex(byte_len: usize) -> String {
    use crate::constants::HEX_CHARS;
    let random_bytes = bytes(byte_len);
    // Pre-allocate exact capacity: 2 hex chars per byte
    let mut result = String::with_capacity(byte_len * 2);
    for b in random_bytes {
        result.push(HEX_CHARS[(b >> 4) as usize] as char);
        result.push(HEX_CHARS[(b & 0x0f) as usize] as char);
    }
    result
}

/// Stub for native non-test builds. Panics with helpful error message.
#[must_use]
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
pub fn hex(_byte_len: usize) -> String {
    panic!(
        "\n\
        ╔══════════════════════════════════════════════════════════════════╗\n\
        ║  random::hex() is only available in WASM builds                  ║\n\
        ╠══════════════════════════════════════════════════════════════════╣\n\
        ║                                                                  ║\n\
        ║  This SDK is designed for WASI HTTP handlers.                    ║\n\
        ║  Random functions use wasi:random/random in WASM.                ║\n\
        ║                                                                  ║\n\
        ║  To build for WASM:                                              ║\n\
        ║    cargo component build --target wasm32-wasip2                  ║\n\
        ║                                                                  ║\n\
        ╚══════════════════════════════════════════════════════════════════╝\n"
    )
}

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

    #[test]
    fn test_bytes_length() {
        assert_eq!(bytes(16).len(), 16);
        assert_eq!(bytes(32).len(), 32);
        assert_eq!(bytes(0).len(), 0);
    }

    #[test]
    fn test_bytes_randomness() {
        // Multiple calls should produce different results
        let b1 = bytes(32);
        let b2 = bytes(32);
        assert_ne!(b1, b2, "Two random byte arrays should differ");
    }

    #[test]
    fn test_u64_returns_value() {
        let v = u64();
        // Just verify it returns something (randomness means any value is valid)
        // We can't test for a specific value, but we can test the function runs
        let _ = v;
    }

    #[test]
    fn test_u64_randomness() {
        // Multiple calls should produce different results
        let v1 = u64();
        let v2 = u64();
        // Note: theoretically could be same, but astronomically unlikely
        assert_ne!(v1, v2, "Two random u64s should differ");
    }

    #[test]
    fn test_uuid_format() {
        let id = uuid();
        assert_eq!(id.len(), 36);

        // Check hyphens
        assert_eq!(id.chars().nth(8), Some('-'));
        assert_eq!(id.chars().nth(13), Some('-'));
        assert_eq!(id.chars().nth(18), Some('-'));
        assert_eq!(id.chars().nth(23), Some('-'));

        // Check version is 4
        assert_eq!(id.chars().nth(14), Some('4'));

        // Check variant (8, 9, a, or b)
        let variant = id
            .chars()
            .nth(19)
            .expect("UUID must have character at position 19");
        assert!(matches!(variant, '8' | '9' | 'a' | 'b'));
    }

    #[test]
    fn test_uuid_version_bits() {
        // Generate many UUIDs and verify version bits are always correct
        for _ in 0..100 {
            let id = uuid();
            // Version should always be 4
            assert_eq!(id.chars().nth(14), Some('4'));
        }
    }

    #[test]
    fn test_uuid_variant_bits() {
        // Generate many UUIDs and verify variant bits are always correct
        for _ in 0..100 {
            let id = uuid();
            let variant = id.chars().nth(19).unwrap();
            assert!(
                matches!(variant, '8' | '9' | 'a' | 'b'),
                "Variant char '{variant}' not in valid set [8,9,a,b]"
            );
        }
    }

    #[test]
    fn test_uuid_version_byte_exact() {
        // Verify byte 6 has correct version bits (0x40 OR'd, 0x0F AND'd)
        // The 7th and 8th hex chars represent byte 6
        for _ in 0..100 {
            let id = uuid();
            // Extract the version byte (positions 14-15 in the string, after removing hyphens)
            // Format: xxxxxxxx-xxxx-Vxxx-yxxx-xxxxxxxxxxxx
            // Position 14 is 'V' (version nibble), should always be '4'
            let version_char = id.chars().nth(14).unwrap();
            assert_eq!(
                version_char, '4',
                "Version nibble must be 4, got {version_char}"
            );

            // The high nibble of byte 6 must be exactly 0x4 (binary 0100)
            // If mutation changed | to ^, with random input 0x4X, XOR would give wrong results
            let byte6_high = u8::from_str_radix(&id[14..15], 16).unwrap();
            assert_eq!(byte6_high, 4, "Version high nibble must be 0x4");
        }
    }

    #[test]
    fn test_uuid_variant_byte_exact() {
        // Verify byte 8 has correct variant bits (0x80 OR'd, 0x3F AND'd)
        // The variant byte is at position 19 in the UUID string
        // Format: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx
        // Y must be 8, 9, a, or b (binary 10xx)
        for _ in 0..100 {
            let id = uuid();
            let variant_char = id.chars().nth(19).unwrap();
            let variant_nibble = u8::from_str_radix(&variant_char.to_string(), 16).unwrap();

            // The high 2 bits must be 10 (binary), meaning value is 8-11 (0x8-0xB)
            assert!(
                (8..=11).contains(&variant_nibble),
                "Variant nibble must be 8-11 (0x8-0xB), got {variant_nibble}"
            );

            // If | was mutated to ^, the high bit pattern would be wrong
            // With 0x80 OR, we force bit 7 to 1. With XOR on random, it could be 0.
            // With 0x3F AND first, we clear bits 6-7, then OR sets bit 7.
            // The result must have bit 7 set (value >= 8) and bit 6 clear (value <= 11)
            assert!((variant_nibble & 0x8) != 0, "Variant bit 7 must be set");
            assert!((variant_nibble & 0x4) == 0, "Variant bit 6 must be clear");
        }
    }

    #[test]
    fn test_uuid_uniqueness() {
        let id1 = uuid();
        let id2 = uuid();
        assert_ne!(id1, id2, "Two UUIDs should be unique");
    }

    #[test]
    fn test_uuid_all_lowercase() {
        let id = uuid();
        let hex_chars: String = id.chars().filter(|c| *c != '-').collect();
        assert!(
            hex_chars
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()),
            "UUID should be all lowercase hex"
        );
    }

    #[test]
    fn test_uuid_raw_bytes_version() {
        // Parse UUID string back to bytes and verify exact bit patterns
        // This catches mutations to AND/OR operations that string tests might miss
        for _ in 0..100 {
            let id = uuid();
            // Parse UUID string back to bytes
            let hex_str: String = id.chars().filter(|c| *c != '-').collect();
            let bytes: Vec<u8> = (0..16)
                .map(|i| u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16).unwrap())
                .collect();

            // Verify version byte (byte 6): high nibble must be exactly 0x40
            // The operation is: (buf[6] & 0x0F) | 0x40
            // - AND 0x0F clears high nibble
            // - OR 0x40 sets exactly bit 6 (value 0x40)
            // Result: high nibble MUST be 0x4, low nibble is random
            assert_eq!(
                bytes[6] & 0xF0,
                0x40,
                "Version byte high nibble must be 0x40, got {:#04x}",
                bytes[6]
            );
        }
    }

    #[test]
    fn test_uuid_raw_bytes_variant() {
        // Parse UUID string back to bytes and verify variant bit pattern
        for _ in 0..100 {
            let id = uuid();
            let hex_str: String = id.chars().filter(|c| *c != '-').collect();
            let bytes: Vec<u8> = (0..16)
                .map(|i| u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16).unwrap())
                .collect();

            // Verify variant byte (byte 8): high 2 bits must be 10
            // The operation is: (buf[8] & 0x3F) | 0x80
            // - AND 0x3F clears bits 6-7
            // - OR 0x80 sets bit 7
            // Result: bits 7-6 MUST be 10 (binary), meaning 0x80-0xBF range
            assert_eq!(
                bytes[8] & 0xC0,
                0x80,
                "Variant byte high 2 bits must be 10, got {:#04x}",
                bytes[8]
            );
        }
    }

    #[test]
    fn test_hex_length() {
        assert_eq!(hex(8).len(), 16);
        assert_eq!(hex(16).len(), 32);
    }

    #[test]
    fn test_hex_zero_length() {
        assert_eq!(hex(0).len(), 0);
        assert_eq!(hex(0), "");
    }

    #[test]
    fn test_hex_is_valid() {
        let h = hex(16);
        assert!(h.chars().all(|c| c.is_ascii_hexdigit()));
        assert!(
            h.chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
        );
    }

    #[test]
    fn test_hex_uniqueness() {
        let h1 = hex(16);
        let h2 = hex(16);
        assert_ne!(h1, h2, "Two hex strings should differ");
    }
}