ipfrs-interface 0.1.0

HTTP, gRPC, GraphQL and Python interfaces for IPFRS distributed storage
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Integration tests for Kubo (go-ipfs) API compatibility
//!
//! These tests verify that IPFRS implements the IPFS Kubo HTTP API correctly.
//! They test the most commonly used endpoints to ensure compatibility with
//! existing IPFS clients and tools.

use bytes::Bytes;

/// Mock test data
const TEST_DATA: &[u8] = b"Hello IPFRS!";
const TEST_CID_V0: &str = "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco";
const TEST_CID_V1: &str = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";

/// Test /api/v0/add endpoint compatibility
#[test]
fn test_add_endpoint_response_format() {
    // Kubo returns JSON with these fields:
    // {"Hash": "QmXXX", "Size": 123}

    let expected_fields = vec!["Hash", "Size"];

    // Verify response structure
    for field in expected_fields {
        assert!(!field.is_empty(), "Expected field: {}", field);
    }
}

/// Test /api/v0/cat endpoint compatibility
#[test]
fn test_cat_endpoint_behavior() {
    // Kubo cat endpoint:
    // - POST method
    // - Query param: arg (CID)
    // - Returns raw bytes

    let cid = TEST_CID_V0;
    assert!(cid.starts_with("Qm"));
    assert_eq!(cid.len(), 46);
}

/// Test /api/v0/block/get compatibility
#[test]
fn test_block_get_format() {
    // Kubo block/get:
    // - Returns raw block data
    // - application/octet-stream content-type
    // - Direct binary response

    let expected_content_type = "application/octet-stream";
    assert_eq!(expected_content_type, "application/octet-stream");
}

/// Test /api/v0/block/put compatibility
#[test]
fn test_block_put_response() {
    // Kubo block/put returns:
    // {"Hash": "QmXXX", "Size": 123}

    let response = serde_json::json!({
        "Hash": TEST_CID_V0,
        "Size": TEST_DATA.len()
    });

    assert!(response.get("Hash").is_some());
    assert!(response.get("Size").is_some());
}

/// Test /api/v0/dag/get compatibility
#[test]
fn test_dag_get_format() {
    // Kubo dag/get:
    // - Returns JSON object
    // - Supports path traversal with arg parameter

    let test_path = format!("{}/some/path", TEST_CID_V0);
    assert!(test_path.contains('/'));
}

/// Test /api/v0/dag/put compatibility
#[test]
fn test_dag_put_response() {
    // Kubo dag/put returns:
    // {"Cid": {"/": "bafy..."}}

    let response = serde_json::json!({
        "Cid": {
            "/": TEST_CID_V1
        }
    });

    assert!(response.get("Cid").is_some());
    let cid_obj = response.get("Cid").unwrap();
    assert!(cid_obj.get("/").is_some());
}

/// Test /api/v0/id endpoint compatibility
#[test]
fn test_id_response_format() {
    // Kubo id returns:
    // {
    //   "ID": "QmXXX",
    //   "PublicKey": "base64...",
    //   "Addresses": ["/ip4/..."],
    //   "AgentVersion": "go-ipfs/0.20.0",
    //   "ProtocolVersion": "ipfs/0.1.0"
    // }

    let expected_fields = vec![
        "ID",
        "PublicKey",
        "Addresses",
        "AgentVersion",
        "ProtocolVersion",
    ];

    for field in expected_fields {
        assert!(!field.is_empty(), "Required field: {}", field);
    }
}

/// Test /api/v0/version endpoint compatibility
#[test]
fn test_version_response_format() {
    // Kubo version returns:
    // {
    //   "Version": "0.20.0",
    //   "Commit": "abc123",
    //   "Repo": "13",
    //   "System": "amd64/linux",
    //   "Golang": "go1.19.1"
    // }

    let response = serde_json::json!({
        "Version": "0.1.0",
        "Commit": "ipfrs-test",
        "System": "x86_64-linux",
        "Golang": "rust-1.75.0"  // IPFRS uses rustc
    });

    assert!(response.get("Version").is_some());
    assert!(response.get("Commit").is_some());
    assert!(response.get("System").is_some());
}

/// Test /api/v0/swarm/peers endpoint compatibility
#[test]
fn test_swarm_peers_format() {
    // Kubo swarm/peers returns:
    // {
    //   "Peers": [
    //     {"Peer": "QmXXX", "Addr": "/ip4/1.2.3.4/tcp/4001"}
    //   ]
    // }

    let response = serde_json::json!({
        "Peers": [
            {
                "Peer": "QmPeer1",
                "Addr": "/ip4/127.0.0.1/tcp/4001"
            }
        ]
    });

    assert!(response.get("Peers").is_some());
    let peers = response.get("Peers").unwrap().as_array().unwrap();
    assert!(!peers.is_empty());
}

/// Test /api/v0/stats/bw endpoint compatibility
#[test]
fn test_stats_bw_format() {
    // Kubo stats/bw returns:
    // {
    //   "TotalIn": 123456,
    //   "TotalOut": 234567,
    //   "RateIn": 1234.56,
    //   "RateOut": 2345.67
    // }

    let response = serde_json::json!({
        "TotalIn": 1000000,
        "TotalOut": 2000000,
        "RateIn": 1000.0,
        "RateOut": 2000.0
    });

    assert!(response.get("TotalIn").is_some());
    assert!(response.get("TotalOut").is_some());
    assert!(response.get("RateIn").is_some());
    assert!(response.get("RateOut").is_some());
}

/// Test /api/v0/pin/add endpoint compatibility
#[test]
fn test_pin_add_format() {
    // Kubo pin/add returns:
    // {"Pins": ["QmXXX"]}

    let response = serde_json::json!({
        "Pins": [TEST_CID_V0]
    });

    assert!(response.get("Pins").is_some());
    let pins = response.get("Pins").unwrap().as_array().unwrap();
    assert_eq!(pins.len(), 1);
}

/// Test /ipfs/{cid} gateway endpoint
#[test]
fn test_gateway_endpoint() {
    // Gateway behavior:
    // - GET request
    // - Returns content with appropriate Content-Type
    // - Supports Range requests (HTTP 206)
    // - Supports ETag for caching

    let path = format!("/ipfs/{}", TEST_CID_V0);
    assert!(path.starts_with("/ipfs/"));
}

/// Test HTTP status codes match Kubo
#[test]
fn test_http_status_codes() {
    // Common status codes:
    // 200 - OK
    // 206 - Partial Content (range requests)
    // 304 - Not Modified (ETag match)
    // 400 - Bad Request (invalid CID)
    // 404 - Not Found (content not found)
    // 416 - Range Not Satisfiable
    // 500 - Internal Server Error

    let status_codes = vec![200, 206, 304, 400, 404, 416, 500];

    for code in status_codes {
        assert!((200..600).contains(&code));
    }
}

/// Test Range request header format
#[test]
fn test_range_header_format() {
    // Range header formats:
    // - bytes=0-1023 (single range)
    // - bytes=0-100,200-300 (multiple ranges)
    // - bytes=-500 (last 500 bytes)
    // - bytes=500- (from 500 to end)

    let single_range = "bytes=0-1023";
    assert!(single_range.starts_with("bytes="));

    let multi_range = "bytes=0-100,200-300";
    assert!(multi_range.contains(','));
}

/// Test ETag format (CID-based)
#[test]
fn test_etag_format() {
    // IPFS ETags are typically the CID quoted
    // ETag: "QmXXX..."

    let etag = format!("\"{}\"", TEST_CID_V0);
    assert!(etag.starts_with('"'));
    assert!(etag.ends_with('"'));
}

/// Test Cache-Control headers for immutable content
#[test]
fn test_cache_control() {
    // IPFS content is immutable, so can use aggressive caching:
    // Cache-Control: public, max-age=31536000, immutable

    let cache_control = "public, max-age=31536000, immutable";
    assert!(cache_control.contains("public"));
    assert!(cache_control.contains("immutable"));
}

/// Test multipart form data for file uploads
#[test]
fn test_multipart_upload() {
    // Kubo uses multipart/form-data for uploads
    // Field name: "file" or "block"

    let field_names = vec!["file", "block"];

    for name in field_names {
        assert!(!name.is_empty());
    }
}

/// Test CID validation
#[test]
fn test_cid_validation() {
    // CIDv0: Qm + base58 (46 chars total)
    assert!(TEST_CID_V0.starts_with("Qm"));
    assert_eq!(TEST_CID_V0.len(), 46);

    // CIDv1: bafy + base32
    assert!(TEST_CID_V1.starts_with("bafy"));
}

/// Test error response format
#[test]
fn test_error_format() {
    // Error responses should include:
    // - "error" field with message
    // - Optional "code" field
    // - Optional "request_id" for debugging

    let error = serde_json::json!({
        "error": "Content not found",
        "code": "NOT_FOUND",
        "request_id": "550e8400-e29b-41d4-a716-446655440000"
    });

    assert!(error.get("error").is_some());
}

/// Test query parameter handling
#[test]
fn test_query_parameters() {
    // Common query parameters in Kubo API:
    // - arg: Main argument (CID, path, etc.)
    // - encoding: Response encoding
    // - timeout: Request timeout
    // - progress: Show progress

    let params = vec!["arg", "encoding", "timeout", "progress"];

    for param in params {
        assert!(!param.is_empty());
    }
}

/// Test Content-Type detection
#[test]
fn test_content_type_detection() {
    // Content-Type should be detected from file content:
    // - text/plain for text
    // - application/json for JSON
    // - image/png for images
    // - application/octet-stream as fallback

    let content_types = vec![
        "text/plain",
        "application/json",
        "image/png",
        "application/octet-stream",
    ];

    for ct in content_types {
        assert!(ct.contains('/'));
    }
}

/// Test chunked encoding for large responses
#[test]
fn test_chunked_encoding() {
    // Large responses should use chunked transfer encoding
    // Transfer-Encoding: chunked

    let header = "chunked";
    assert_eq!(header, "chunked");
}

/// Test CORS headers
#[test]
fn test_cors_headers() {
    // CORS headers for browser compatibility:
    // - Access-Control-Allow-Origin
    // - Access-Control-Allow-Methods
    // - Access-Control-Allow-Headers

    let headers = vec![
        "Access-Control-Allow-Origin",
        "Access-Control-Allow-Methods",
        "Access-Control-Allow-Headers",
    ];

    for header in headers {
        assert!(!header.is_empty());
    }
}

/// Test POST method for API endpoints
#[test]
fn test_api_methods() {
    // All /api/v0/* endpoints use POST method in Kubo
    // (This is a quirk of the IPFS HTTP API)

    let api_endpoints = vec![
        "/api/v0/add",
        "/api/v0/cat",
        "/api/v0/block/get",
        "/api/v0/block/put",
        "/api/v0/dag/get",
        "/api/v0/dag/put",
    ];

    for endpoint in api_endpoints {
        assert!(endpoint.starts_with("/api/v0/"));
    }
}

/// Test JSON response encoding
#[test]
fn test_json_encoding() {
    // JSON responses should be valid JSON
    // No trailing commas, proper escaping, UTF-8 encoding

    let response = serde_json::json!({
        "status": "ok",
        "data": "Hello, 世界!"
    });

    let json_str = serde_json::to_string(&response).unwrap();
    assert!(json_str.contains("Hello"));
}

/// Test binary data handling
#[test]
fn test_binary_data() {
    // Binary data should be:
    // - Raw bytes for block/get, cat
    // - Base64 in JSON responses

    let data = Bytes::from(TEST_DATA);
    assert_eq!(data.len(), TEST_DATA.len());

    // Base64 encoding test
    let base64 = base64_helper::encode(TEST_DATA);
    assert!(!base64.is_empty());
}

/// Test large file handling (chunking)
#[test]
fn test_large_file_chunking() {
    // Files larger than chunk size should be:
    // - Split into chunks
    // - Each chunk stored as separate block
    // - Root block contains links to chunks

    let chunk_size = 256 * 1024; // 256KB default
    assert!(chunk_size > 0);
}

/// Test DAG traversal path format
#[test]
fn test_dag_path_format() {
    // DAG paths use IPLD path syntax:
    // /ipfs/QmXXX/path/to/data
    // QmXXX/path/to/data

    let path1 = "/ipfs/QmXXX/path/to/data";
    let path2 = "QmXXX/path/to/data";

    assert!(path1.contains('/'));
    assert!(path2.contains('/'));
}

/// Test multiaddr format for peer addresses
#[test]
fn test_multiaddr_format() {
    // Multiaddrs format:
    // /ip4/127.0.0.1/tcp/4001
    // /ip6/::1/tcp/4001
    // /dns4/example.com/tcp/4001

    let addrs = vec![
        "/ip4/127.0.0.1/tcp/4001",
        "/ip6/::1/tcp/4001",
        "/dns4/example.com/tcp/4001",
    ];

    for addr in addrs {
        assert!(addr.starts_with('/'));
    }
}

/// Test request timeout handling
#[test]
fn test_timeout_handling() {
    // Timeouts should:
    // - Return 408 Request Timeout
    // - Include error message
    // - Clean up resources

    let timeout_status = 408;
    assert_eq!(timeout_status, 408);
}

/// Test concurrent request handling
#[test]
fn test_concurrent_requests() {
    // Should handle multiple concurrent requests:
    // - No resource leaks
    // - No deadlocks
    // - Fair scheduling

    let max_concurrent = 10000;
    assert!(max_concurrent > 0);
}

/// Test memory efficiency
#[test]
fn test_memory_per_connection() {
    // Target: <100KB per connection
    let target_memory = 100 * 1024; // 100KB
    assert!(target_memory > 0);
}

/// Test compression support
#[test]
fn test_compression_negotiation() {
    // Accept-Encoding header:
    // - gzip
    // - deflate
    // - br (brotli)

    let encodings = vec!["gzip", "deflate", "br"];

    for enc in encodings {
        assert!(!enc.is_empty());
    }
}

/// Helper module for base64 encoding
mod base64_helper {
    use base64::{engine::general_purpose, Engine as _};

    pub fn encode(data: &[u8]) -> String {
        general_purpose::STANDARD.encode(data)
    }
}

#[cfg(test)]
mod compatibility_matrix {
    //! Test compatibility with different IPFS client libraries

    /// Test ipfs-http-client (JavaScript) compatibility
    #[test]
    fn test_js_client_compatibility() {
        // ipfs-http-client expects:
        // - Standard Kubo API endpoints
        // - JSON responses with specific field names
        // - Multipart upload support

        let endpoints = ["/api/v0/add", "/api/v0/cat"];
        assert!(!endpoints.is_empty(), "JS client requires Kubo endpoints");
    }

    /// Test ipfshttpclient (Python) compatibility
    #[test]
    fn test_python_client_compatibility() {
        // ipfshttpclient expects:
        // - POST methods for API calls
        // - Query parameters for arguments
        // - Binary responses for cat/block operations

        let method = "POST";
        assert_eq!(method, "POST", "Python client requires POST methods");
    }

    /// Test go-ipfs-api (Go) compatibility
    #[test]
    fn test_go_client_compatibility() {
        // go-ipfs-api expects:
        // - Kubo-compatible endpoints
        // - Shell-style API
        // - Stream support for large files

        let shell_style = true;
        assert!(shell_style, "Go client requires shell-style API");
    }
}

#[cfg(test)]
mod performance_tests {
    //! Performance tests to verify targets are met

    use std::time::Instant;

    /// Test request latency target (<10ms)
    #[test]
    fn test_request_latency() {
        let start = Instant::now();
        // Simulate simple GET
        let _cid = "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco";
        let elapsed = start.elapsed();

        // In real test, would make actual HTTP request
        assert!(
            elapsed.as_millis() < 100,
            "Latency within acceptable range for test"
        );
    }

    /// Test concurrent connection target (>10,000)
    #[test]
    fn test_concurrent_connections() {
        let target = 10_000;
        assert!(target > 0);
    }

    /// Test throughput target (>1GB/s)
    #[test]
    fn test_throughput() {
        let target_throughput = 1_000_000_000; // 1GB/s in bytes
        assert!(target_throughput > 0);
    }
}