@@ -3,25 +3,15 @@ use crate::registry::{self, alt_api_path, FeatureMap};
3
3
use flate2:: read:: GzDecoder ;
4
4
use std:: collections:: { HashMap , HashSet } ;
5
5
use std:: fs;
6
- use std:: fs:: File ;
7
- use std:: io:: { self , prelude:: * , SeekFrom } ;
6
+ use std:: io:: prelude:: * ;
8
7
use std:: path:: { Path , PathBuf } ;
9
8
use tar:: Archive ;
10
9
11
- fn read_le_u32 < R > ( mut reader : R ) -> io:: Result < u32 >
12
- where
13
- R : Read ,
14
- {
15
- let mut buf = [ 0 ; 4 ] ;
16
- reader. read_exact ( & mut buf) ?;
17
- Ok ( u32:: from_le_bytes ( buf) )
18
- }
19
-
20
10
/// Checks the result of a crate publish.
21
11
pub fn validate_upload ( expected_json : & str , expected_crate_name : & str , expected_files : & [ & str ] ) {
22
12
let new_path = registry:: api_path ( ) . join ( "api/v1/crates/new" ) ;
23
13
_validate_upload (
24
- & new_path,
14
+ & fs :: read ( & new_path) . unwrap ( ) ,
25
15
expected_json,
26
16
expected_crate_name,
27
17
expected_files,
@@ -38,7 +28,7 @@ pub fn validate_upload_with_contents(
38
28
) {
39
29
let new_path = registry:: api_path ( ) . join ( "api/v1/crates/new" ) ;
40
30
_validate_upload (
41
- & new_path,
31
+ & fs :: read ( & new_path) . unwrap ( ) ,
42
32
expected_json,
43
33
expected_crate_name,
44
34
expected_files,
@@ -54,7 +44,7 @@ pub fn validate_alt_upload(
54
44
) {
55
45
let new_path = alt_api_path ( ) . join ( "api/v1/crates/new" ) ;
56
46
_validate_upload (
57
- & new_path,
47
+ & fs :: read ( & new_path) . unwrap ( ) ,
58
48
expected_json,
59
49
expected_crate_name,
60
50
expected_files,
@@ -63,17 +53,16 @@ pub fn validate_alt_upload(
63
53
}
64
54
65
55
fn _validate_upload (
66
- new_path : & Path ,
56
+ content : & [ u8 ] ,
67
57
expected_json : & str ,
68
58
expected_crate_name : & str ,
69
59
expected_files : & [ & str ] ,
70
60
expected_contents : & [ ( & str , & str ) ] ,
71
61
) {
72
- let mut f = File :: open ( new_path) . unwrap ( ) ;
73
62
// 32-bit little-endian integer of length of JSON data.
74
- let json_sz = read_le_u32 ( & mut f ) . expect ( "read json length" ) ;
75
- let mut json_bytes = vec ! [ 0 ; json_sz as usize ] ;
76
- f . read_exact ( & mut json_bytes) . expect ( "read JSON data" ) ;
63
+ let ( len , remaining ) = content . split_at ( 4 ) ;
64
+ let json_len = u32 :: from_le_bytes ( len . try_into ( ) . unwrap ( ) ) ;
65
+ let ( json_bytes, remaining ) = remaining . split_at ( json_len as usize ) ;
77
66
let actual_json = serde_json:: from_slice ( & json_bytes) . expect ( "uploaded JSON should be valid" ) ;
78
67
let expected_json = serde_json:: from_str ( expected_json) . expect ( "expected JSON does not parse" ) ;
79
68
@@ -82,12 +71,12 @@ fn _validate_upload(
82
71
}
83
72
84
73
// 32-bit little-endian integer of length of crate file.
85
- let crate_sz = read_le_u32 ( & mut f) . expect ( "read crate length" ) ;
86
- let mut krate_bytes = vec ! [ 0 ; crate_sz as usize ] ;
87
- f. read_exact ( & mut krate_bytes) . expect ( "read crate data" ) ;
74
+ let ( len, remaining) = remaining. split_at ( 4 ) ;
75
+ let file_len = u32:: from_le_bytes ( len. try_into ( ) . unwrap ( ) ) ;
76
+ let ( krate_bytes, _remaining) = remaining. split_at ( file_len as usize ) ;
77
+
88
78
// Check at end.
89
- let current = f. seek ( SeekFrom :: Current ( 0 ) ) . unwrap ( ) ;
90
- assert_eq ! ( f. seek( SeekFrom :: End ( 0 ) ) . unwrap( ) , current) ;
79
+ assert_eq ! ( _remaining. len( ) , 0 ) ;
91
80
92
81
// Verify the tarball.
93
82
validate_crate_contents (
0 commit comments