@@ -7,18 +7,6 @@ use crate::{
77 repo:: RepoManifest ,
88} ;
99
10- /// Reads a manifest without verifying. This is best for AFTER it has been downloaded.
11- ///
12- /// # Errors
13- ///
14- /// - Filesystem errors (Permissions or doesn't exist)
15- pub fn read_manifest_unsigned ( repo_path : & Path ) -> Result < RepoManifest > {
16- let manifest_serialized = fs:: read_to_string ( repo_path. join ( "manifest.yml" ) ) ?;
17- let manifest = serde_yaml:: from_str ( & manifest_serialized) ?;
18-
19- Ok ( manifest)
20- }
21-
2210/// Reads a manifest and verifys it from the EXISTING key. This is best for GENERAL reading.
2311///
2412/// # Warning
@@ -42,6 +30,14 @@ pub fn read_manifest(repo_path: &Path) -> Result<RepoManifest> {
4230 Ok ( manifest)
4331}
4432
33+ fn read_manifest_unsigned ( repo_path : & Path ) -> Result < RepoManifest > {
34+ let manifest_serialized = fs:: read_to_string ( repo_path. join ( "manifest.yml" ) ) ?;
35+
36+ let manifest: RepoManifest = serde_yaml:: from_str ( & manifest_serialized) ?;
37+
38+ Ok ( manifest)
39+ }
40+
4541/// Reads a manifest and verifys it. This is best for WHEN it has been downloaded.
4642///
4743/// # Errors
@@ -62,8 +58,7 @@ pub fn read_manifest_signed(repo_path: &Path, public_key_serialized: &str) -> Re
6258 Ok ( manifest)
6359}
6460
65- /// Replaces the existing manifest with another one
66- /// Verifies that it is correct
61+ /// Replaces the existing manifest with another one, and verifies that it is correct
6762///
6863/// # Errors
6964///
@@ -98,11 +93,79 @@ pub fn update_manifest(
9893 Ok ( ( ) )
9994}
10095
101- fn atomic_replace ( repo_path : & Path , filename : & str , contents : & [ u8 ] ) -> Result < ( ) > {
102- let new_path = & repo_path . join ( filename. to_owned ( ) + ".new" ) ;
96+ fn atomic_replace ( base_path : & Path , filename : & str , contents : & [ u8 ] ) -> Result < ( ) > {
97+ let new_path = & base_path . join ( filename. to_owned ( ) + ".new" ) ;
10398
10499 fs:: write ( new_path, contents) ?;
105- fs:: rename ( new_path, repo_path . join ( filename) ) ?;
100+ fs:: rename ( new_path, base_path . join ( filename) ) ?;
106101
107102 Ok ( ( ) )
108103}
104+
105+ #[ cfg( test) ]
106+ mod tests {
107+ use temp_dir:: TempDir ;
108+
109+ use crate :: { crypto:: signing:: sign, repo:: create} ;
110+
111+ use super :: * ;
112+
113+ #[ test]
114+ fn test_atomic_replace_basic ( ) -> Result < ( ) > {
115+ let temp_dir = TempDir :: new ( ) ?;
116+ fs:: write ( temp_dir. path ( ) . join ( "file" ) , "previous_contents" ) ?;
117+ atomic_replace ( temp_dir. path ( ) , "file" , b"new_contents" ) ?;
118+
119+ assert_eq ! (
120+ fs:: read_to_string( temp_dir. path( ) . join( "file" ) ) ?,
121+ "new_contents"
122+ ) ;
123+ assert ! ( !temp_dir. path( ) . join( "file.new" ) . exists( ) ) ;
124+
125+ Ok ( ( ) )
126+ }
127+
128+ #[ test]
129+ fn test_update_manifest_valid_and_invalid ( ) -> Result < ( ) > {
130+ let repo = TempDir :: new ( ) ?;
131+ let repo_path = repo. path ( ) ;
132+ create ( repo_path) ?;
133+
134+ let old_manifest = read_manifest ( repo_path) ?;
135+
136+ // Build a new manifest with small change
137+ let mut new_manifest = old_manifest;
138+ new_manifest. metadata . title = Some ( "NewName" . into ( ) ) ;
139+
140+ let serialized = serde_yaml:: to_string ( & new_manifest) ?;
141+
142+ // Sign it with the right key
143+ let signature = sign ( repo_path, & serialized) ?;
144+
145+ // Update should succeed
146+ update_manifest ( repo_path, & serialized, & signature. to_bytes ( ) ) ?;
147+
148+ let updated = read_manifest ( repo_path) ?;
149+ assert_eq ! ( updated. metadata. title, Some ( "NewName" . into( ) ) ) ;
150+
151+ // Now try with invalid signature
152+ let bad_signature = b"garbage_signature" ;
153+ assert ! ( update_manifest( repo_path, & serialized, bad_signature) . is_err( ) ) ;
154+
155+ Ok ( ( ) )
156+ }
157+
158+ #[ test]
159+ fn test_read_signed_manifest ( ) -> Result < ( ) > {
160+ let repo = TempDir :: new ( ) ?;
161+ let repo_path = repo. path ( ) ;
162+ create ( repo_path) ?;
163+
164+ let manifest = read_manifest ( repo_path) ?;
165+ let manifest_signed = read_manifest_signed ( repo_path, & manifest. public_key ) ?;
166+
167+ assert_eq ! ( manifest. edition, manifest_signed. edition) ;
168+
169+ Ok ( ( ) )
170+ }
171+ }
0 commit comments