-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
1032 lines (855 loc) · 33 KB
/
lib.rs
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![recursion_limit = "256"]
use anyhow::anyhow;
use anyhow::Result;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::broadcast;
use tokio::sync::broadcast::Receiver;
use veilid_core::{
RouteId, UpdateCallback, VeilidAPI, VeilidConfigInner, VeilidUpdate, VALID_CRYPTO_KINDS,
};
pub mod iroh;
pub mod tunnels;
#[cfg(test)]
mod tests {
use crate::iroh::VeilidIrohBlobs;
use crate::tunnels::OnNewRouteCallback;
use crate::tunnels::OnNewTunnelCallback;
use crate::tunnels::OnRouteDisconnectedCallback;
use anyhow::anyhow;
use anyhow::Result;
use bytes::Bytes;
use core::str;
use futures_lite::StreamExt;
use std::path::Path;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use tokio::time::{sleep, Duration};
use veilid_core::VeilidUpdate;
#[tokio::test]
async fn test_tunnel() {
//unsafe { backtrace_on_stack_overflow::enable() }
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
/* four threads
veilid cb -> updates channel
tunnel1, wait for tunnel to come in and send an OK to output channel, spawn thread for listening to updates
tunnel2, open tunnel, wait for write, send OK to output channel, spawn thread for waiting for updates
test, listen for two Results and unwrap them
*/
let (send_update, read_update) = broadcast::channel::<VeilidUpdate>(256);
let (send_result, mut read_result) = mpsc::channel::<Result<()>>(2);
let send_result1 = send_result.clone();
let send_result2 = send_result.clone();
let read_update1 = read_update;
let read_update2 = send_update.subscribe();
let (veilid, mut rx) =
crate::init_veilid(Some("tunnels_test_1".to_string()), &base_dir.join("peer1"))
.await
.expect("Unable to init veilid and store");
let sender_handle = tokio::spawn(async move {
while let Ok(update) = rx.recv().await {
//println!("Received update: {:#?}", update);
if let Err(err) = send_update.send(update) {
eprintln!("Unable to process veilid update: {:?}", err);
}
}
});
println!("Cloning veilid API for tunnels");
let v1 = veilid.clone();
let v2 = veilid.clone();
println!("Initializing route1");
let router1 = v1.routing_context().unwrap();
let (route_id1, route_id1_blob) = crate::make_route(&v1).await.unwrap();
println!("Initializing route2");
let router2 = v2.routing_context().unwrap();
let (route_id2, route_id2_blob) = crate::make_route(&v2).await.unwrap();
println!("Routes ready");
let on_new_tunnel1: OnNewTunnelCallback = Arc::new(move |tunnel| {
println!("New tunnel {:?}", tunnel);
let send_result1 = send_result1.clone();
tokio::spawn(async move {
send_result1.send(Ok(())).await.unwrap();
let (sender, mut reader) = tunnel;
let result = reader.recv().await;
if result.is_none() {
send_result1
.send(Err(anyhow!("Unable to read first message")))
.await
.unwrap();
return;
}
let raw = result.unwrap();
let message = str::from_utf8(raw.as_slice()).unwrap();
if message.eq("Hello World!") {
send_result1.send(Ok(())).await.unwrap();
} else {
send_result1
.send(Err(anyhow!("Got invalid message from tunnel {0}", message)))
.await
.unwrap();
}
let result = sender.send("Goodbye World!".as_bytes().to_vec()).await;
send_result1
.send(if result.is_ok() {
Ok(())
} else {
Err(anyhow!(
"Unable to send response tunnel {:?}",
result.unwrap_err()
))
})
.await
.unwrap();
});
});
let tunnels1 = crate::tunnels::TunnelManager::new(
v1,
router1,
route_id1,
route_id1_blob.clone(),
Some(on_new_tunnel1),
None,
None,
);
let tunnels2 = crate::tunnels::TunnelManager::new(
v2,
router2,
route_id2,
route_id2_blob,
None,
None,
None,
);
println!("Spawn tunnel1");
let tunnel1_handle = tokio::spawn(async move {
println!("Listening in tunnel1");
tunnels1.listen(read_update1).await.unwrap();
});
println!("Spawn tunnel2");
let tunnel2_handle = tokio::spawn(async move {
let listening = tunnels2.clone();
tokio::spawn(async move {
println!("Listening in tunnel2");
listening.listen(read_update2).await.unwrap();
});
sleep(Duration::from_secs(10)).await;
let result = tunnels2.open(route_id1_blob).await;
if result.is_err() {
send_result2.send(Err(result.unwrap_err())).await.unwrap();
return;
}
println!("Tunnel opened");
send_result2.send(Ok(())).await.unwrap();
let (sender, mut reader) = result.unwrap();
// STart reading so the chanel isn't marked as closed
let read_result = reader.recv();
let result = sender.send("Hello World!".as_bytes().to_vec()).await;
send_result2
.send(if result.is_ok() {
Ok(())
} else {
Err(anyhow!(
"Unable to send down tunnel {:?}",
result.unwrap_err()
))
})
.await
.unwrap();
let result = read_result.await;
if result.is_none() {
send_result2
.send(Err(anyhow!("Unable to read first message")))
.await
.unwrap();
return;
}
let raw = result.unwrap();
let message = str::from_utf8(raw.as_slice()).unwrap();
if message.eq("Goodbye World!") {
send_result2.send(Ok(())).await.unwrap();
} else {
send_result2
.send(Err(anyhow!("Got invalid message from tunnel {0}", message)))
.await
.unwrap();
}
});
let expected = 6;
let mut count = 0;
while count < expected {
println!("wait result {0}", count + 1);
count += 1;
read_result.recv().await.unwrap().unwrap();
}
println!("cleanup");
sender_handle.abort();
tunnel1_handle.abort();
tunnel2_handle.abort();
veilid.shutdown().await;
}
#[tokio::test]
async fn test_tunnel_route_reset() {
//unsafe { backtrace_on_stack_overflow::enable() }
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let (send_update, read_update) = broadcast::channel::<VeilidUpdate>(256);
let (send_result, mut read_result) = mpsc::channel::<Result<()>>(2);
let send_result1 = send_result.clone();
let send_result2 = send_result.clone();
let (veilid, mut rx) = crate::init_veilid(Some("tunnels_test".to_string()), &base_dir)
.await
.expect("Unable to init veilid and store");
let sender_handle = tokio::spawn(async move {
while let Ok(update) = rx.recv().await {
//println!("Received update: {:#?}", update);
if let Err(err) = send_update.send(update) {
eprintln!("Unable to process veilid update: {:?}", err);
}
}
});
let on_new_route: OnNewRouteCallback = Arc::new(move |_, _| {
let send_result = send_result1.clone();
tokio::spawn(async move {
send_result.send(Ok(())).await.unwrap();
});
});
let on_disconnected: OnRouteDisconnectedCallback = Arc::new(move || {
let send_result = send_result2.clone();
tokio::spawn(async move {
send_result.send(Ok(())).await.unwrap();
});
});
let router = veilid.routing_context().unwrap();
let (route_id, route_id_blob) = crate::make_route(&veilid).await.unwrap();
let tunnels = crate::tunnels::TunnelManager::new(
veilid.clone(),
router,
route_id,
route_id_blob,
None,
Some(on_disconnected),
Some(on_new_route),
);
tokio::spawn(async move {
tunnels.listen(read_update).await.unwrap();
});
veilid.release_private_route(route_id).unwrap();
let expected = 2;
let mut count = 0;
while count < expected {
println!("wait result {0}", count + 1);
count += 1;
read_result.recv().await.unwrap().unwrap();
}
println!("cleanup");
sender_handle.abort();
veilid.shutdown().await;
}
#[tokio::test]
async fn test_blobs() {
//unsafe { backtrace_on_stack_overflow::enable() }
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
let hash = blobs
.upload_from_path(std::fs::canonicalize(Path::new("./README.md")).unwrap())
.await
.unwrap();
println!("Hash of README: {0}", hash);
let receiver = blobs.read_file(hash).await.unwrap();
let mut stream = tokio_stream::wrappers::ReceiverStream::new(receiver);
let data = stream.next().await;
println!("{:?}", data);
let has = blobs.has_hash(&hash).await;
println!("Blobs has hash: {}", has);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_blob_replication() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let (veilid, mut rx) = crate::init_veilid(None, &base_dir).await.unwrap();
let (send_update, read_update) = broadcast::channel::<VeilidUpdate>(256);
let read_update1 = read_update;
let read_update2 = send_update.subscribe();
let sender_handle = tokio::spawn(async move {
while let Result::Ok(update) = rx.recv().await {
if let VeilidUpdate::RouteChange(change) = update {
println!("Route change {:?}", change);
continue;
}
//println!("Received update: {:#?}", update);
if let Err(err) = send_update.send(update) {
eprintln!("Unable to process veilid update: {:?}", err);
}
}
});
let v1 = veilid.clone();
let v2 = veilid.clone();
let mut store1_dir = base_dir.clone();
store1_dir.push("peer1");
let store1 = iroh_blobs::store::fs::Store::load(store1_dir)
.await
.unwrap();
let mut store2_dir = base_dir.clone();
store2_dir.push("peer2");
let store2 = iroh_blobs::store::fs::Store::load(store2_dir)
.await
.unwrap();
let router1 = v1.routing_context().unwrap();
let (route_id1, route_id1_blob) = crate::make_route(&v1).await.unwrap();
println!("Initializing route2");
let router2 = v2.routing_context().unwrap();
let (route_id2, route_id2_blob) = crate::make_route(&v2).await.unwrap();
let blobs1 = VeilidIrohBlobs::new(
v1,
router1,
route_id1_blob,
route_id1,
read_update1,
store1,
None,
None,
);
let blobs2 = VeilidIrohBlobs::new(
v2,
router2,
route_id2_blob,
route_id2,
read_update2,
store2,
None,
None,
);
let hash = blobs1
.upload_from_path(std::fs::canonicalize(Path::new("./README.md")).unwrap())
.await
.unwrap();
blobs2
.download_file_from(blobs1.route_id_blob().await, &hash)
.await
.unwrap();
let has = blobs2.has_hash(&hash).await;
assert!(has, "Blobs has hash after download");
sender_handle.abort();
}
#[tokio::test]
async fn test_create_collection() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
// Log: Initializing blobs instance
println!("Initializing blobs instance from directory: {:?}", base_dir);
// Initialize the blobs instance
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Log: Creating collection
println!("Creating collection...");
// Call create_collection method
let collection_name = "my_test_collection".to_string();
let collection_hash = blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Ensure the collection hash is not empty
assert!(
!collection_hash.as_bytes().is_empty(),
"Collection hash should not be empty"
);
println!("The collection was created with hash: {}", collection_hash);
// Verify that the collection exists in the store
let has_collection = blobs.has_hash(&collection_hash).await;
assert!(
has_collection,
"Blobs should have the collection hash after creation"
);
// Clean up by shutting down blobs instance
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_collection_operations() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
// Initialize the blobs instance
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Log: Creating collection
println!("Creating collection...");
// Test create_collection
let collection_name = "my_test_collection".to_string();
let collection_hash = blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
assert!(
!collection_hash.as_bytes().is_empty(),
"Collection hash should not be empty"
);
println!("Created collection with hash: {}", collection_hash);
// Test set_file
let file_path = "test_file.txt".to_string();
// Create a temporary file to upload
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
// Ensure the path is absolute
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
// Add debug statements
println!("File hash: {}", file_hash);
let has_file = blobs.has_hash(&file_hash).await;
println!("Has file: {}", has_file);
assert!(has_file, "Store should have the file hash after upload");
let updated_collection_hash = blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
assert!(
!updated_collection_hash.as_bytes().is_empty(),
"Updated collection hash should not be empty"
);
// Test get_file
let retrieved_file_hash = blobs
.get_file(&collection_name.clone(), &file_path.clone())
.await
.unwrap();
assert_eq!(
file_hash, retrieved_file_hash,
"The file hash should match the uploaded file hash"
);
// Test list_files
let file_list = blobs.list_files(&collection_name.clone()).await.unwrap();
assert_eq!(
file_list.len(),
1,
"There should be one file in the collection"
);
assert_eq!(
file_list[0], file_path,
"The file path should match the uploaded file path"
);
// Test delete_file
let new_collection_hash = blobs
.delete_file(&collection_name.clone(), &file_path.clone())
.await
.unwrap();
assert!(
!new_collection_hash.as_bytes().is_empty(),
"New collection hash after deletion should not be empty"
);
let file_list_after_deletion = blobs.list_files(&collection_name.clone()).await.unwrap();
assert!(
file_list_after_deletion.is_empty(),
"There should be no files in the collection after deletion"
);
// Test collection_hash
let retrieved_collection_hash = blobs.collection_hash(&collection_name).await.unwrap();
assert_eq!(
retrieved_collection_hash, new_collection_hash,
"The retrieved collection hash should match the updated collection hash after deletion"
);
// Test upload_to (now using `set_file` with `upload_from_path`)
let new_file_path = "uploaded_file.txt".to_string();
let temp_new_file_path = base_dir.join(&new_file_path);
std::fs::write(&temp_new_file_path, "test file content for upload_to").unwrap();
let absolute_new_file_path = std::fs::canonicalize(&temp_new_file_path).unwrap();
// Upload the new file and add it to the collection
let new_file_hash = blobs
.upload_from_path(absolute_new_file_path)
.await
.unwrap();
let new_file_collection_hash = blobs
.set_file(
&collection_name.clone(),
&new_file_path.clone(),
&new_file_hash,
)
.await
.unwrap();
assert!(
!new_file_collection_hash.as_bytes().is_empty(),
"New collection hash after uploading a file should not be empty"
);
// Clean up by shutting down blobs instance
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_set_file() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
let collection_hash = blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
assert!(
!collection_hash.as_bytes().is_empty(),
"Collection hash should not be empty"
);
println!("Created collection with hash: {}", collection_hash);
// Attempt to retrieve the tag
println!(
"Attempting to retrieve tag for collection: {}",
collection_name
);
match blobs.get_tag(&collection_name).await {
Ok(tag_hash) => {
println!("Successfully retrieved tag: {:?}", tag_hash);
}
Err(e) => {
println!("Error retrieving tag: {:?}", e);
}
}
// Test set_file
let file_path = "test_file.txt".to_string();
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
println!("File hash: {}", file_hash);
let has_file = blobs.has_hash(&file_hash).await;
println!("Has file: {}", has_file);
assert!(has_file, "Store should have the file hash after upload");
let updated_collection_hash = blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
assert!(
!updated_collection_hash.as_bytes().is_empty(),
"Updated collection hash should not be empty"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_get_file() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Test set_file
let file_path = "test_file.txt".to_string();
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
// Test get_file
let retrieved_file_hash = blobs
.get_file(&collection_name.clone(), &file_path.clone())
.await
.unwrap();
assert_eq!(
file_hash, retrieved_file_hash,
"The file hash should match the uploaded file hash"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_delete_file() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Test set_file
let file_path = "test_file.txt".to_string();
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
// Test delete_file
let new_collection_hash = blobs
.delete_file(&collection_name.clone(), &file_path.clone())
.await
.unwrap();
assert!(
!new_collection_hash.as_bytes().is_empty(),
"New collection hash after deletion should not be empty"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_list_files() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Test set_file
let file_path = "test_file.txt".to_string();
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
// Test list_files
let file_list = blobs.list_files(&collection_name.clone()).await.unwrap();
assert_eq!(
file_list.len(),
1,
"There should be one file in the collection"
);
assert_eq!(
file_list[0], file_path,
"The file path should match the uploaded file path"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_collection_hash() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
let initial_collection_hash = blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Test collection_hash
let retrieved_collection_hash = blobs.collection_hash(&collection_name).await.unwrap();
assert_eq!(
initial_collection_hash, retrieved_collection_hash,
"The retrieved collection hash should match the created collection hash"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_upload_to() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Test create_collection
let collection_name = "my_test_collection".to_string();
blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
// Create a temporary file to upload
let file_path = "uploaded_file.txt".to_string();
let temp_file_path = base_dir.join("uploaded_file.txt");
std::fs::write(&temp_file_path, "test file content for upload_to").unwrap();
let absolute_temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
// Create a file stream using mpsc
let (sender, receiver) = mpsc::channel(1);
let file_content = Bytes::from("this is a test file content");
// Spawn a task to send the file content via the sender
tokio::spawn(async move {
sender.send(Ok(file_content)).await.unwrap();
});
// Call upload_to with the file stream and path
let file_path = "test_file.txt".to_string();
let file_hash = blobs
.upload_to(&collection_name, &file_path, receiver)
.await
.unwrap();
// Add the uploaded file to the collection
let new_file_collection_hash = blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
assert!(
!new_file_collection_hash.as_bytes().is_empty(),
"New collection hash after uploading a file should not be empty"
);
blobs.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_missing_collection() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
// Attempt to retrieve a file from a non-existent collection
let result = blobs
.get_file(
&"non_existent_collection".to_string(),
&"some_file.txt".to_string(),
)
.await;
assert!(
result.is_err(),
"Retrieving file from non-existent collection should fail"
);
// Attempt to list files in a non-existent collection
let result = blobs
.list_files(&"non_existent_collection".to_string())
.await;
assert!(
result.is_err(),
"Listing files from non-existent collection should fail"
);
}
#[tokio::test]
async fn test_overwrite_file() {
let mut base_dir = PathBuf::new();
base_dir.push(".veilid");
let blobs = VeilidIrohBlobs::from_directory(&base_dir, None, None, None)
.await
.unwrap();
let collection_name = "my_test_collection".to_string();
blobs
.create_collection(&collection_name.clone())
.await
.unwrap();
let file_path = "test_file.txt".to_string();
let temp_file_path = base_dir.join("test_file.txt");
std::fs::write(&temp_file_path, "test file content").unwrap();
let temp_file_path = std::fs::canonicalize(temp_file_path).unwrap();
let file_hash = blobs
.upload_from_path(temp_file_path.clone())
.await
.unwrap();
blobs
.set_file(&collection_name.clone(), &file_path.clone(), &file_hash)
.await
.unwrap();
// Overwrite the file with new content
std::fs::write(&temp_file_path, "new test file content").unwrap();
let new_file_hash = blobs.upload_from_path(temp_file_path).await.unwrap();
let updated_collection_hash = blobs
.set_file(&collection_name.clone(), &file_path.clone(), &new_file_hash)
.await
.unwrap();
assert!(
!updated_collection_hash.as_bytes().is_empty(),
"Updated collection hash should not be empty"
);
// Ensure that the file hash was updated
let retrieved_file_hash = blobs
.get_file(&collection_name.clone(), &file_path.clone())
.await
.unwrap();
assert_eq!(
new_file_hash, retrieved_file_hash,
"The file hash should be updated after overwrite"
);
}
}
async fn init_veilid(
namespace: Option<String>,
base_dir: &PathBuf,
) -> Result<(VeilidAPI, Receiver<VeilidUpdate>)> {
let config_inner = config_for_dir(base_dir.to_path_buf(), namespace);
let (tx, mut rx) = broadcast::channel(32);
let update_callback: UpdateCallback = Arc::new(move |update| {
let tx = tx.clone();
tokio::spawn(async move {
if tx.send(update).is_err() {
// TODO:
println!("receiver dropped");
}
});
});
println!("Init veilid");
let veilid = veilid_core::api_startup_config(update_callback, config_inner).await?;
println!("Attach veilid");
veilid.attach().await?;
println!("Wait for veilid network");
while let Ok(update) = rx.recv().await {
if let VeilidUpdate::Attachment(attachment_state) = update {
if attachment_state.public_internet_ready && attachment_state.state.is_attached() {
println!("Public internet ready!");
break;
}
}
}
println!("Network ready");
Ok((veilid, rx))
}
async fn init_deps(
namespace: Option<String>,
base_dir: &PathBuf,
) -> Result<(
VeilidAPI,
Receiver<VeilidUpdate>,
iroh_blobs::store::fs::Store,
)> {
let store = iroh_blobs::store::fs::Store::load(base_dir.join("iroh")).await?;
let (veilid, rx) = init_veilid(namespace, base_dir).await?;
Ok((veilid, rx, store))
}
// TODO: Put these into a utils module or something
async fn make_route(veilid: &VeilidAPI) -> Result<(RouteId, Vec<u8>)> {
let mut retries = 3;
while retries != 0 {
retries -= 1;
let result = veilid
.new_custom_private_route(
&VALID_CRYPTO_KINDS,
veilid_core::Stability::LowLatency,
veilid_core::Sequencing::NoPreference,
)
.await;