|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use gix_hash::ObjectId; |
| 4 | +use gix_odb::store::init::Options as OdbOptions; |
| 5 | +use gix_packetline::blocking_io::encode::{data_to_write, delim_to_write, flush_to_write}; |
| 6 | +use gix_ref::file::Store; |
| 7 | +use gix_ref::store::init::Options as RefOptions; |
| 8 | +use gix_serve::refs::collect_refs; |
| 9 | +use gix_serve::serve::serve_upload_pack; |
| 10 | +use gix_transport::server::blocking_io::connection::Connection; |
| 11 | +use gix_transport::{Protocol, Service}; |
| 12 | + |
| 13 | +fn ref_store(script: &str) -> Store { |
| 14 | + let path = gix_testtools::scripted_fixture_read_only_standalone(script).expect("fixture"); |
| 15 | + Store::at( |
| 16 | + path.join(".git"), |
| 17 | + RefOptions { |
| 18 | + write_reflog: gix_ref::store::WriteReflog::Disable, |
| 19 | + object_hash: gix_hash::Kind::Sha1, |
| 20 | + precompose_unicode: false, |
| 21 | + prohibit_windows_device_names: false, |
| 22 | + }, |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +fn odb(script: &str) -> gix_odb::HandleArc { |
| 27 | + let path = gix_testtools::scripted_fixture_read_only_standalone(script).expect("fixture"); |
| 28 | + let store = |
| 29 | + gix_odb::Store::at_opts(path.join(".git/objects"), &mut None.into_iter(), OdbOptions::default()).expect("odb"); |
| 30 | + let mut cache = Arc::new(store).to_cache_arc(); |
| 31 | + cache.prevent_pack_unload(); |
| 32 | + cache |
| 33 | +} |
| 34 | + |
| 35 | +fn build_v1_want_done(oid: &ObjectId) -> Vec<u8> { |
| 36 | + let mut buf = Vec::new(); |
| 37 | + data_to_write(format!("want {}\n", oid.to_hex()).as_bytes(), &mut buf).unwrap(); |
| 38 | + flush_to_write(&mut buf).unwrap(); |
| 39 | + data_to_write(b"done\n", &mut buf).unwrap(); |
| 40 | + flush_to_write(&mut buf).unwrap(); |
| 41 | + buf |
| 42 | +} |
| 43 | + |
| 44 | +fn build_v2_fetch_input(wants: &[ObjectId]) -> Vec<u8> { |
| 45 | + let mut buf = Vec::new(); |
| 46 | + data_to_write(b"command=fetch\n", &mut buf).unwrap(); |
| 47 | + delim_to_write(&mut buf).unwrap(); |
| 48 | + for oid in wants { |
| 49 | + data_to_write(format!("want {}\n", oid.to_hex()).as_bytes(), &mut buf).unwrap(); |
| 50 | + } |
| 51 | + data_to_write(b"done\n", &mut buf).unwrap(); |
| 52 | + flush_to_write(&mut buf).unwrap(); |
| 53 | + buf |
| 54 | +} |
| 55 | + |
| 56 | +fn assert_has_valid_pack(output: &[u8]) { |
| 57 | + let pack_pos = output.windows(4).position(|w| w == b"PACK"); |
| 58 | + assert!(pack_pos.is_some(), "output should contain pack data"); |
| 59 | + |
| 60 | + let pack_start = pack_pos.unwrap(); |
| 61 | + assert_eq!( |
| 62 | + &output[pack_start + 4..pack_start + 8], |
| 63 | + &[0, 0, 0, 2], |
| 64 | + "pack format version 2" |
| 65 | + ); |
| 66 | + |
| 67 | + let num_entries = u32::from_be_bytes(output[pack_start + 8..pack_start + 12].try_into().unwrap()); |
| 68 | + assert!(num_entries > 0, "pack should contain objects"); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn v1_fresh_clone() { |
| 73 | + let store = ref_store("make_repo_simple.sh"); |
| 74 | + let db = odb("make_repo_simple.sh"); |
| 75 | + let refs = collect_refs(&store).unwrap(); |
| 76 | + let main_oid = refs.iter().find(|r| r.name == "refs/heads/main").unwrap().object_id; |
| 77 | + |
| 78 | + let input = build_v1_want_done(&main_oid); |
| 79 | + let mut output = Vec::new(); |
| 80 | + let mut conn = Connection::new( |
| 81 | + &input[..], |
| 82 | + &mut output, |
| 83 | + Service::UploadPack, |
| 84 | + "/repo.git", |
| 85 | + Protocol::V1, |
| 86 | + false, |
| 87 | + ); |
| 88 | + |
| 89 | + serve_upload_pack(&store, db, &mut conn, Protocol::V1).unwrap(); |
| 90 | + |
| 91 | + assert!(!output.is_empty()); |
| 92 | + let nak_pos = output.windows(3).position(|w| w == b"NAK"); |
| 93 | + let pack_pos = output.windows(4).position(|w| w == b"PACK").unwrap(); |
| 94 | + assert!(nak_pos.is_some()); |
| 95 | + assert!(nak_pos.unwrap() < pack_pos); |
| 96 | + assert_has_valid_pack(&output); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn v1_empty_wants() { |
| 101 | + let store = ref_store("make_repo_simple.sh"); |
| 102 | + let db = odb("make_repo_simple.sh"); |
| 103 | + |
| 104 | + let mut input = Vec::new(); |
| 105 | + flush_to_write(&mut input).unwrap(); |
| 106 | + let mut output = Vec::new(); |
| 107 | + let mut conn = Connection::new( |
| 108 | + &input[..], |
| 109 | + &mut output, |
| 110 | + Service::UploadPack, |
| 111 | + "/repo.git", |
| 112 | + Protocol::V1, |
| 113 | + false, |
| 114 | + ); |
| 115 | + |
| 116 | + serve_upload_pack(&store, db, &mut conn, Protocol::V1).unwrap(); |
| 117 | + |
| 118 | + assert!(!output.is_empty()); |
| 119 | + assert!(output.windows(4).position(|w| w == b"PACK").is_none()); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn v2_fresh_clone() { |
| 124 | + let store = ref_store("make_repo_simple.sh"); |
| 125 | + let db = odb("make_repo_simple.sh"); |
| 126 | + let refs = collect_refs(&store).unwrap(); |
| 127 | + let main_oid = refs.iter().find(|r| r.name == "refs/heads/main").unwrap().object_id; |
| 128 | + |
| 129 | + let input = build_v2_fetch_input(&[main_oid]); |
| 130 | + let mut output = Vec::new(); |
| 131 | + let mut conn = Connection::new( |
| 132 | + &input[..], |
| 133 | + &mut output, |
| 134 | + Service::UploadPack, |
| 135 | + "/repo.git", |
| 136 | + Protocol::V2, |
| 137 | + false, |
| 138 | + ); |
| 139 | + |
| 140 | + serve_upload_pack(&store, db, &mut conn, Protocol::V2).unwrap(); |
| 141 | + |
| 142 | + assert!(!output.is_empty()); |
| 143 | + assert_has_valid_pack(&output); |
| 144 | +} |
0 commit comments