@@ -26,8 +26,11 @@ use rustup_dist::temp;
26
26
use rustup_dist:: manifestation:: { Manifestation , UpdateStatus , Changes } ;
27
27
use rustup_dist:: manifest:: { Manifest , Component } ;
28
28
use url:: Url ;
29
+ use std:: cell:: Cell ;
29
30
use std:: fs;
30
31
use std:: path:: Path ;
32
+ use std:: sync:: Arc ;
33
+
31
34
use tempdir:: TempDir ;
32
35
33
36
// Creates a mock dist server populated with some test data
@@ -921,3 +924,58 @@ fn unable_to_download_component() {
921
924
}
922
925
} ) ;
923
926
}
927
+
928
+ fn prevent_installation ( prefix : & InstallPrefix ) {
929
+ utils:: ensure_dir_exists ( "installation path" , & prefix. path ( ) . join ( "lib" ) , & |_|{ } ) . unwrap ( ) ;
930
+ let install_blocker = prefix. path ( ) . join ( "lib" ) . join ( "rustlib" ) ;
931
+ utils:: write_file ( "install-blocker" , & install_blocker, "fail-installation" ) . unwrap ( ) ;
932
+ }
933
+
934
+ fn allow_installation ( prefix : & InstallPrefix ) {
935
+ let install_blocker = prefix. path ( ) . join ( "lib" ) . join ( "rustlib" ) ;
936
+ utils:: remove_file ( "install-blocker" , & install_blocker) . unwrap ( ) ;
937
+ }
938
+
939
+ #[ test]
940
+ fn reuse_downloaded_file ( ) {
941
+ setup ( None , & |url, toolchain, prefix, download_cfg, temp_cfg| {
942
+
943
+ prevent_installation ( prefix) ;
944
+
945
+ update_from_dist ( url, toolchain, prefix, & [ ] , & [ ] , download_cfg, temp_cfg, & |_| { } ) . unwrap_err ( ) ;
946
+
947
+ allow_installation ( & prefix) ;
948
+
949
+ let reuse_notification_fired = Arc :: new ( Cell :: new ( false ) ) ;
950
+ update_from_dist ( url, toolchain, prefix, & [ ] , & [ ] , download_cfg, temp_cfg, & |n| {
951
+ if let Notification :: FileAlreadyDownloaded = n {
952
+ reuse_notification_fired. set ( true ) ;
953
+ }
954
+ } ) . unwrap ( ) ;
955
+
956
+ assert ! ( reuse_notification_fired. get( ) ) ;
957
+ } )
958
+ }
959
+
960
+ #[ test]
961
+ fn checks_files_hashes_before_reuse ( ) {
962
+ setup ( None , & |url, toolchain, prefix, download_cfg, temp_cfg| {
963
+
964
+ let path = url. to_file_path ( ) . unwrap ( ) ;
965
+ let target_hash = utils:: read_file ( "target hash" , & path. join ( "dist/2016-02-02/rustc-nightly-x86_64-apple-darwin.tar.gz.sha256" ) ) . unwrap ( ) [ .. 64 ] . to_owned ( ) ;
966
+ let prev_download = download_cfg. download_dir . join ( target_hash) ;
967
+ utils:: ensure_dir_exists ( "download dir" , & download_cfg. download_dir , & |_|{ } ) . unwrap ( ) ;
968
+ utils:: write_file ( "bad previous download" , & prev_download, "bad content" ) . unwrap ( ) ;
969
+ println ! ( "wrote previous download to {}" , prev_download. display( ) ) ;
970
+
971
+ let noticed_bad_checksum = Arc :: new ( Cell :: new ( false ) ) ;
972
+ update_from_dist ( url, toolchain, prefix, & [ ] , & [ ] , download_cfg, temp_cfg, & |n| {
973
+ println ! ( "{:?}" , n) ;
974
+ if let Notification :: CachedFileChecksumFailed = n {
975
+ noticed_bad_checksum. set ( true ) ;
976
+ }
977
+ } ) . unwrap ( ) ;
978
+
979
+ assert ! ( noticed_bad_checksum. get( ) ) ;
980
+ } )
981
+ }
0 commit comments