use std::collections::HashMap;
use bytes::{BufMut, Bytes, BytesMut};
use tokio_stream::wrappers::ReceiverStream;
use super::{
core::{AuthenticationService, RepositoryAccess},
pack::PackGenerator,
types::{
COMMON_CAP_LIST, Capability, LF, NUL, PKT_LINE_END_MARKER, ProtocolError, ProtocolStream,
RECEIVE_CAP_LIST, RefCommand, RefTypeEnum, SP, ServiceType, SideBand, TransportProtocol,
UPLOAD_CAP_LIST,
},
utils::{add_pkt_line_string, build_smart_reply, read_pkt_line, read_until_white_space},
};
use crate::hash::{HashKind, ObjectHash};
pub struct SmartProtocol<R, A>
where
R: RepositoryAccess,
A: AuthenticationService,
{
pub transport_protocol: TransportProtocol,
pub capabilities: Vec<Capability>,
pub side_band: Option<SideBand>,
pub command_list: Vec<RefCommand>,
pub wire_hash_kind: HashKind,
pub local_hash_kind: HashKind,
pub zero_id: String,
repo_storage: R,
auth_service: A,
}
impl<R, A> SmartProtocol<R, A>
where
R: RepositoryAccess,
A: AuthenticationService,
{
pub fn set_wire_hash_kind(&mut self, kind: HashKind) {
self.wire_hash_kind = kind;
self.zero_id = ObjectHash::zero_str(kind);
}
pub fn new(transport_protocol: TransportProtocol, repo_storage: R, auth_service: A) -> Self {
let local_hash_kind = repo_storage.object_hash_kind();
Self {
transport_protocol,
capabilities: Vec::new(),
side_band: None,
command_list: Vec::new(),
repo_storage,
auth_service,
wire_hash_kind: local_hash_kind,
local_hash_kind,
zero_id: ObjectHash::zero_str(local_hash_kind),
}
}
pub fn ensure_hash_kind_consistency(&self) -> Result<(), ProtocolError> {
let repository = self.repo_storage.object_hash_kind();
if repository != self.local_hash_kind || self.wire_hash_kind != self.local_hash_kind {
return Err(ProtocolError::object_format_mismatch(
self.wire_hash_kind,
self.local_hash_kind,
repository,
));
}
let expected_zero = ObjectHash::zero_str(self.wire_hash_kind);
if self.zero_id != expected_zero {
return Err(ProtocolError::invalid_request(&format!(
"zero ID `{}` does not match object-format {} (expected {} hex zeros)",
self.zero_id.chars().take(80).collect::<String>(),
self.wire_hash_kind.as_str(),
self.wire_hash_kind.hex_len()
)));
}
Ok(())
}
fn parse_wire_id(&self, command: &str, id: &str) -> Result<String, ProtocolError> {
let hash = ObjectHash::from_hex_for_kind(self.wire_hash_kind, id)
.map_err(|e| ProtocolError::invalid_wire_id(command, id, &e))?;
if id.bytes().any(|b| b.is_ascii_uppercase()) {
return Err(ProtocolError::invalid_wire_id(
command,
id,
&format!(
"object IDs on the wire must be raw lowercase hex (expected {} lowercase hex chars for {}, got uppercase characters)",
self.wire_hash_kind.hex_len(),
self.wire_hash_kind.as_str()
),
));
}
Ok(hash.to_string())
}
pub async fn authenticate_http(
&self,
headers: &HashMap<String, String>,
) -> Result<(), ProtocolError> {
self.auth_service.authenticate_http(headers).await
}
pub async fn authenticate_ssh(
&self,
username: &str,
public_key: &[u8],
) -> Result<(), ProtocolError> {
self.auth_service
.authenticate_ssh(username, public_key)
.await
}
pub fn set_transport_protocol(&mut self, protocol: TransportProtocol) {
self.transport_protocol = protocol;
}
pub async fn git_info_refs(
&self,
service_type: ServiceType,
) -> Result<BytesMut, ProtocolError> {
self.ensure_hash_kind_consistency()?;
let refs = self
.repo_storage
.get_repository_refs()
.await
.map_err(|e| ProtocolError::repository_error(format!("Failed to get refs: {e}")))?;
self.ensure_hash_kind_consistency()?;
let hex_len = self.wire_hash_kind.hex_len();
for (name, h) in &refs {
if h.len() != hex_len {
return Err(ProtocolError::invalid_request(&format!(
"Hash length mismatch for ref {}: expected {}, got {}",
name,
hex_len,
h.len()
)));
}
self.parse_wire_id("info-refs", h).map_err(|e| {
ProtocolError::invalid_request(&format!("Invalid hash for ref {name}: {e}"))
})?;
} let head_hash = refs
.iter()
.find(|(name, _)| {
name == "HEAD" || name.ends_with("/main") || name.ends_with("/master")
})
.map(|(_, hash)| hash.clone())
.unwrap_or_else(|| self.zero_id.clone());
let git_refs: Vec<super::types::GitRef> = refs
.into_iter()
.map(|(name, hash)| super::types::GitRef { name, hash })
.collect();
let format_cap = format!(" object-format={}", self.wire_hash_kind.as_str());
let cap_list = match service_type {
ServiceType::UploadPack => format!("{UPLOAD_CAP_LIST}{COMMON_CAP_LIST}{format_cap}"),
ServiceType::ReceivePack => format!("{RECEIVE_CAP_LIST}{COMMON_CAP_LIST}{format_cap}"),
};
let name = if head_hash == self.zero_id {
"capabilities^{}"
} else {
"HEAD"
};
let pkt_line = format!("{head_hash}{SP}{name}{NUL}{cap_list}{LF}");
let mut ref_list = vec![pkt_line];
for git_ref in git_refs {
let pkt_line = format!("{}{}{}{}", git_ref.hash, SP, git_ref.name, LF);
ref_list.push(pkt_line);
}
let pkt_line_stream =
build_smart_reply(self.transport_protocol, &ref_list, service_type.to_string());
tracing::debug!("git_info_refs, return: --------> {:?}", pkt_line_stream);
Ok(pkt_line_stream)
}
pub async fn git_upload_pack(
&mut self,
upload_request: Bytes,
) -> Result<(ReceiverStream<Result<Vec<u8>, ProtocolError>>, BytesMut), ProtocolError> {
self.capabilities.clear();
self.ensure_hash_kind_consistency()?;
let mut upload_request = upload_request;
let mut want: Vec<String> = Vec::new();
let mut have: Vec<String> = Vec::new();
let mut last_common_commit = String::new();
let mut read_first_line = false;
loop {
let (bytes_take, pkt_line) = read_pkt_line(&mut upload_request);
if bytes_take == 0 {
break;
}
if pkt_line.is_empty() {
break;
}
let mut pkt_line = pkt_line;
let command = read_until_white_space(&mut pkt_line);
match command.as_str() {
"want" => {
let hash = read_until_white_space(&mut pkt_line);
if !read_first_line {
let cap_str = String::from_utf8_lossy(&pkt_line).to_string();
self.parse_capabilities(&cap_str)?;
read_first_line = true;
}
want.push(self.parse_wire_id("want", &hash)?);
}
"have" => {
let hash = read_until_white_space(&mut pkt_line);
have.push(self.parse_wire_id("have", &hash)?);
}
"done" => {
break;
}
_ => {
tracing::warn!("Unknown upload-pack command: {}", command);
}
}
}
let mut protocol_buf = BytesMut::new();
let pack_generator =
PackGenerator::new_with_hash_kind(&self.repo_storage, self.local_hash_kind);
if have.is_empty() {
add_pkt_line_string(&mut protocol_buf, String::from("NAK\n"));
self.ensure_hash_kind_consistency()?;
let pack_stream = pack_generator.generate_full_pack(want).await?;
self.ensure_hash_kind_consistency()?;
return Ok((pack_stream, protocol_buf));
}
for hash in &have {
let exists = self.repo_storage.commit_exists(hash).await.map_err(|e| {
ProtocolError::repository_error(format!("Failed to check commit existence: {e}"))
})?;
if exists {
add_pkt_line_string(&mut protocol_buf, format!("ACK {hash} common\n"));
if last_common_commit.is_empty() {
last_common_commit = hash.clone();
}
}
}
self.ensure_hash_kind_consistency()?;
if last_common_commit.is_empty() {
add_pkt_line_string(&mut protocol_buf, String::from("NAK\n"));
let pack_stream = pack_generator.generate_full_pack(want).await?;
self.ensure_hash_kind_consistency()?;
return Ok((pack_stream, protocol_buf));
}
add_pkt_line_string(
&mut protocol_buf,
format!("ACK {last_common_commit} ready\n"),
);
let pack_stream = pack_generator.generate_incremental_pack(want, have).await?;
self.ensure_hash_kind_consistency()?;
Ok((pack_stream, protocol_buf))
}
pub fn parse_receive_pack_commands(
&mut self,
mut protocol_bytes: Bytes,
) -> Result<(), ProtocolError> {
self.command_list.clear();
self.capabilities.clear();
self.ensure_hash_kind_consistency()?;
let mut first_line = true;
loop {
let (bytes_take, pkt_line) = read_pkt_line(&mut protocol_bytes);
if bytes_take == 0 {
break;
}
if pkt_line.is_empty() {
break;
}
if first_line {
if let Some(pos) = pkt_line.iter().position(|b| *b == b'\0') {
let caps = String::from_utf8_lossy(&pkt_line[(pos + 1)..]).to_string();
self.parse_capabilities(&caps)?;
}
first_line = false;
}
let ref_command = self.parse_ref_command(&mut pkt_line.clone())?;
self.command_list.push(ref_command);
}
Ok(())
}
pub async fn git_receive_pack_stream(
&mut self,
data_stream: ProtocolStream,
) -> Result<Bytes, ProtocolError> {
self.ensure_hash_kind_consistency()?;
let mut request_data = BytesMut::new();
let mut stream = data_stream;
while let Some(chunk_result) = futures::StreamExt::next(&mut stream).await {
let chunk = chunk_result
.map_err(|e| ProtocolError::invalid_request(&format!("Stream error: {e}")))?;
request_data.extend_from_slice(&chunk);
}
let mut protocol_bytes = request_data.freeze();
self.command_list.clear();
self.capabilities.clear();
self.ensure_hash_kind_consistency()?;
let mut first_line = true;
let mut saw_flush = false;
loop {
let (bytes_take, pkt_line) = read_pkt_line(&mut protocol_bytes);
if bytes_take == 0 {
if protocol_bytes.is_empty() {
break;
}
return Err(ProtocolError::invalid_request(
"Invalid pkt-line in receive-pack request",
));
}
if pkt_line.is_empty() {
saw_flush = true;
break;
}
if first_line {
if let Some(pos) = pkt_line.iter().position(|b| *b == b'\0') {
let caps = String::from_utf8_lossy(&pkt_line[(pos + 1)..]).to_string();
self.parse_capabilities(&caps)?;
}
first_line = false;
}
let ref_command = self.parse_ref_command(&mut pkt_line.clone())?;
self.command_list.push(ref_command);
}
if !saw_flush {
return Err(ProtocolError::invalid_request(
"Missing flush before pack data",
));
}
let pack_data = if protocol_bytes.is_empty() {
None
} else {
Some(protocol_bytes)
};
if let Some(pack_data) = pack_data {
let pack_generator =
PackGenerator::new_with_hash_kind(&self.repo_storage, self.local_hash_kind);
let (commits, trees, blobs) = pack_generator.unpack_stream(pack_data).await?;
self.ensure_hash_kind_consistency()?;
self.repo_storage
.handle_pack_objects(commits, trees, blobs)
.await
.map_err(|e| {
ProtocolError::repository_error(format!("Failed to store pack objects: {e}"))
})?;
}
let mut report_status = BytesMut::new();
add_pkt_line_string(&mut report_status, "unpack ok\n".to_owned());
let mut default_exist = self.repo_storage.has_default_branch().await.map_err(|e| {
ProtocolError::repository_error(format!("Failed to check default branch: {e}"))
})?;
let mut commands = std::mem::take(&mut self.command_list);
let mut update_result = Ok(());
for command in &mut commands {
if let Err(e) = self.ensure_hash_kind_consistency() {
update_result = Err(e);
break;
}
if command.ref_type == RefTypeEnum::Tag {
let old_hash = if command.old_hash == self.zero_id {
None
} else {
Some(command.old_hash.as_str())
};
if let Err(e) = self
.repo_storage
.update_reference(&command.ref_name, old_hash, &command.new_hash)
.await
{
command.failed(e.to_string());
}
} else {
if !default_exist {
command.default_branch = true;
default_exist = true;
}
let old_hash = if command.old_hash == self.zero_id {
None
} else {
Some(command.old_hash.as_str())
};
if let Err(e) = self
.repo_storage
.update_reference(&command.ref_name, old_hash, &command.new_hash)
.await
{
command.failed(e.to_string());
}
}
add_pkt_line_string(&mut report_status, command.get_status());
}
self.command_list = commands;
update_result?;
self.ensure_hash_kind_consistency()?;
self.repo_storage.post_receive_hook().await.map_err(|e| {
ProtocolError::repository_error(format!("Post-receive hook failed: {e}"))
})?;
self.ensure_hash_kind_consistency()?;
report_status.put(&PKT_LINE_END_MARKER[..]);
Ok(report_status.freeze())
}
pub fn build_side_band_format(&self, from_bytes: BytesMut, length: usize) -> BytesMut {
let mut to_bytes = BytesMut::new();
if self.capabilities.contains(&Capability::SideBand)
|| self.capabilities.contains(&Capability::SideBand64k)
{
let length = length + 5;
to_bytes.put(Bytes::from(format!("{length:04x}")));
to_bytes.put_u8(SideBand::PackfileData.value());
to_bytes.put(from_bytes);
} else {
to_bytes.put(from_bytes);
}
to_bytes
}
pub fn parse_capabilities(&mut self, cap_str: &str) -> Result<(), ProtocolError> {
self.ensure_hash_kind_consistency()?;
for cap in cap_str.split_whitespace() {
if let Some(fmt) = cap.strip_prefix("object-format=") {
let kind = match fmt.parse::<HashKind>() {
Ok(kind) if kind.as_str() == fmt => kind,
_ => return Err(ProtocolError::unknown_object_format(fmt)),
};
let repository = self.repo_storage.object_hash_kind();
if kind != self.local_hash_kind || kind != repository {
return Err(ProtocolError::object_format_mismatch(
kind,
self.local_hash_kind,
repository,
));
}
self.set_wire_hash_kind(kind);
self.capabilities
.push(Capability::ObjectFormat(kind.as_str().to_string()));
continue;
}
if let Ok(capability) = cap.parse::<Capability>() {
self.capabilities.push(capability);
}
}
Ok(())
}
pub fn parse_ref_command(&self, pkt_line: &mut Bytes) -> Result<RefCommand, ProtocolError> {
let old_id = read_until_white_space(pkt_line);
let new_id = read_until_white_space(pkt_line);
let ref_name = read_until_white_space(pkt_line);
let _capabilities = String::from_utf8_lossy(&pkt_line[..]).to_string();
let old_id = self.parse_wire_id("receive-pack old-id", &old_id)?;
let new_id = self.parse_wire_id("receive-pack new-id", &new_id)?;
if ref_name.is_empty() {
return Err(ProtocolError::invalid_request(
"receive-pack command is missing the ref name",
));
}
Ok(RefCommand::new(old_id, new_id, ref_name))
}
}
#[cfg(test)]
mod tests {
use std::sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
};
use async_trait::async_trait;
use bytes::BytesMut;
use futures;
use tokio::sync::mpsc;
use super::*;
use crate::protocol::utils; use crate::{
hash::{HashKind, ObjectHash, set_hash_kind_for_test},
internal::{
metadata::{EntryMeta, MetaAttached},
object::{
blob::Blob,
commit::Commit,
signature::{Signature, SignatureType},
tree::{Tree, TreeItem, TreeItemMode},
},
pack::{encode::PackEncoder, entry::Entry},
},
};
type UpdateRecord = (String, Option<String>, String);
type UpdateList = Vec<UpdateRecord>;
type SharedUpdates = Arc<Mutex<UpdateList>>;
#[derive(Clone)]
struct TestRepoAccess {
kind: HashKind,
updates: SharedUpdates,
stored_count: Arc<Mutex<usize>>,
default_branch_exists: Arc<Mutex<bool>>,
post_called: Arc<AtomicBool>,
}
impl TestRepoAccess {
fn new() -> Self {
Self::with_kind(HashKind::Sha1)
}
fn with_kind(kind: HashKind) -> Self {
Self {
kind,
updates: Arc::new(Mutex::new(vec![])),
stored_count: Arc::new(Mutex::new(0)),
default_branch_exists: Arc::new(Mutex::new(false)),
post_called: Arc::new(AtomicBool::new(false)),
}
}
fn recorded_updates(&self) -> UpdateList {
self.updates.lock().unwrap().clone()
}
fn updates_len(&self) -> usize {
self.updates.lock().unwrap().len()
}
fn post_hook_called(&self) -> bool {
self.post_called.load(Ordering::SeqCst)
}
}
#[async_trait]
impl RepositoryAccess for TestRepoAccess {
fn object_hash_kind(&self) -> HashKind {
self.kind
}
async fn get_repository_refs(&self) -> Result<Vec<(String, String)>, ProtocolError> {
Ok(vec![
("HEAD".to_string(), ObjectHash::zero_str(self.kind)),
(
"refs/heads/main".to_string(),
"1".repeat(self.kind.hex_len()),
),
])
}
async fn has_object(&self, _object_hash: &str) -> Result<bool, ProtocolError> {
Ok(true)
}
async fn get_object(&self, _object_hash: &str) -> Result<Vec<u8>, ProtocolError> {
Ok(vec![])
}
async fn store_pack_data(&self, _pack_data: &[u8]) -> Result<(), ProtocolError> {
*self.stored_count.lock().unwrap() += 1;
Ok(())
}
async fn update_reference(
&self,
ref_name: &str,
old_hash: Option<&str>,
new_hash: &str,
) -> Result<(), ProtocolError> {
self.updates.lock().unwrap().push((
ref_name.to_string(),
old_hash.map(|s| s.to_string()),
new_hash.to_string(),
));
Ok(())
}
async fn get_objects_for_pack(
&self,
_wants: &[String],
_haves: &[String],
) -> Result<Vec<String>, ProtocolError> {
Ok(vec![])
}
async fn has_default_branch(&self) -> Result<bool, ProtocolError> {
let mut exists = self.default_branch_exists.lock().unwrap();
let current = *exists;
*exists = true; Ok(current)
}
async fn post_receive_hook(&self) -> Result<(), ProtocolError> {
self.post_called.store(true, Ordering::SeqCst);
Ok(())
}
}
struct TestAuth;
#[async_trait]
impl AuthenticationService for TestAuth {
async fn authenticate_http(
&self,
_headers: &std::collections::HashMap<String, String>,
) -> Result<(), ProtocolError> {
Ok(())
}
async fn authenticate_ssh(
&self,
_username: &str,
_public_key: &[u8],
) -> Result<(), ProtocolError> {
Ok(())
}
}
#[tokio::test]
async fn test_receive_pack_stream_status_report() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let blob1 = Blob::from_content("hello");
let blob2 = Blob::from_content("world");
let item1 = TreeItem::new(TreeItemMode::Blob, blob1.id, "hello.txt".to_string());
let item2 = TreeItem::new(TreeItemMode::Blob, blob2.id, "world.txt".to_string());
let tree = Tree::from_tree_items(vec![item1, item2]).unwrap();
let author = Signature::new(
SignatureType::Author,
"tester".to_string(),
"tester@example.com".to_string(),
);
let committer = Signature::new(
SignatureType::Committer,
"tester".to_string(),
"tester@example.com".to_string(),
);
let commit = Commit::new(author, committer, tree.id, vec![], "init commit");
let (pack_tx, mut pack_rx) = mpsc::channel(1024);
let (entry_tx, entry_rx) = mpsc::channel(1024);
let mut encoder = PackEncoder::new(4, 10, pack_tx);
tokio::spawn(async move {
if let Err(e) = encoder.encode(entry_rx).await {
panic!("Failed to encode pack: {}", e);
}
});
let commit_clone = commit.clone();
let tree_clone = tree.clone();
let blob1_clone = blob1.clone();
let blob2_clone = blob2.clone();
tokio::spawn(async move {
let _ = entry_tx
.send(MetaAttached {
inner: Entry::from(commit_clone),
meta: EntryMeta::new(),
})
.await;
let _ = entry_tx
.send(MetaAttached {
inner: Entry::from(tree_clone),
meta: EntryMeta::new(),
})
.await;
let _ = entry_tx
.send(MetaAttached {
inner: Entry::from(blob1_clone),
meta: EntryMeta::new(),
})
.await;
let _ = entry_tx
.send(MetaAttached {
inner: Entry::from(blob2_clone),
meta: EntryMeta::new(),
})
.await;
});
let mut pack_bytes: Vec<u8> = Vec::new();
while let Some(chunk) = pack_rx.recv().await {
pack_bytes.extend_from_slice(&chunk);
}
let repo_access = TestRepoAccess::new();
let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access.clone(), auth);
smart.set_wire_hash_kind(HashKind::Sha1);
let mut request = BytesMut::new();
add_pkt_line_string(
&mut request,
format!(
"{} {} refs/heads/main\0report-status\n",
smart.zero_id, commit.id
),
);
request.put(&PKT_LINE_END_MARKER[..]);
request.extend_from_slice(&pack_bytes);
let request_stream = Box::pin(futures::stream::once(async { Ok(request.freeze()) }));
let result_bytes = smart
.git_receive_pack_stream(request_stream)
.await
.expect("receive-pack should succeed");
let mut out = result_bytes.clone();
let (_c1, l1) = utils::read_pkt_line(&mut out);
assert_eq!(String::from_utf8(l1.to_vec()).unwrap(), "unpack ok\n");
let (_c2, l2) = utils::read_pkt_line(&mut out);
assert_eq!(
String::from_utf8(l2.to_vec()).unwrap(),
"ok refs/heads/main"
);
let (c3, l3) = utils::read_pkt_line(&mut out);
assert_eq!(c3, 4);
assert!(l3.is_empty());
assert_eq!(repo_access.updates_len(), 1);
assert!(repo_access.post_hook_called());
}
#[tokio::test]
async fn info_refs_rejects_sha256_with_sha1_refs() {
let _guard = set_hash_kind_for_test(HashKind::Sha1); let repo_access = TestRepoAccess::new(); let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access, auth);
smart.set_wire_hash_kind(HashKind::Sha256); let res = smart.git_info_refs(ServiceType::UploadPack).await;
assert!(res.is_err(), "expected failure when hash lengths mismatch");
smart.set_wire_hash_kind(HashKind::Sha1);
let res = smart.git_info_refs(ServiceType::UploadPack).await;
assert!(
res.is_ok(),
"expected SHA1 refs to be accepted when wire is SHA1"
);
}
fn blake3_repo_and_protocol() -> (TestRepoAccess, SmartProtocol<TestRepoAccess, TestAuth>) {
let repo = TestRepoAccess::with_kind(HashKind::Blake3);
let smart = SmartProtocol::new(TransportProtocol::Http, repo.clone(), TestAuth);
(repo, smart)
}
#[tokio::test]
async fn blake3_capability() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let (_repo, mut smart) = blake3_repo_and_protocol();
assert_eq!(smart.wire_hash_kind, HashKind::Blake3);
assert_eq!(smart.local_hash_kind, HashKind::Blake3);
assert_eq!(smart.zero_id, "0".repeat(64));
let resp = smart.git_info_refs(ServiceType::UploadPack).await.unwrap();
let resp_str = String::from_utf8(resp.to_vec()).unwrap();
assert!(resp_str.contains(" object-format=blake3"), "{resp_str}");
assert!(
resp_str.contains(&format!("{} refs/heads/main", "1".repeat(64))),
"{resp_str}"
);
assert!(!resp_str.contains("object-format=sha"), "{resp_str}");
smart
.parse_capabilities("report-status object-format=blake3 side-band-64k")
.unwrap();
assert_eq!(smart.wire_hash_kind, HashKind::Blake3);
assert_eq!(smart.zero_id.len(), HashKind::Blake3.hex_len());
assert!(smart.capabilities.contains(&Capability::SideBand64k));
assert!(smart.capabilities.contains(&Capability::ReportStatus));
assert!(
smart
.capabilities
.contains(&Capability::ObjectFormat("blake3".to_string()))
);
for bad in [
"A".repeat(64),
"zz".repeat(32),
format!("blake3:{}", "1".repeat(57)),
] {
let repo = BadRefRepo {
kind: HashKind::Blake3,
id: bad.clone(),
};
let smart = SmartProtocol::new(TransportProtocol::Http, repo, TestAuth);
let err = smart
.git_info_refs(ServiceType::UploadPack)
.await
.unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidRequest(_)),
"{bad}: {err:?}"
);
assert!(
err.to_string().contains("Invalid hash for ref"),
"{bad}: {err}"
);
}
for kind in [HashKind::Sha1, HashKind::Sha256] {
let repo = TestRepoAccess::with_kind(kind);
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo, TestAuth);
let resp = smart.git_info_refs(ServiceType::ReceivePack).await.unwrap();
let resp_str = String::from_utf8(resp.to_vec()).unwrap();
assert!(
resp_str.contains(&format!(" object-format={}", kind.as_str())),
"{resp_str}"
);
smart
.parse_capabilities(&format!("object-format={} side-band", kind.as_str()))
.unwrap();
assert_eq!(smart.wire_hash_kind, kind);
assert_eq!(smart.zero_id, ObjectHash::zero_str(kind));
}
}
#[tokio::test]
async fn blake3_zero_id() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let (repo, mut smart) = blake3_repo_and_protocol();
let zero = smart.zero_id.clone();
let new_id = "a".repeat(64);
let mut pkt = BytesMut::new();
add_pkt_line_string(
&mut pkt,
format!("{zero} {new_id} refs/heads/main\0report-status object-format=blake3\n"),
);
add_pkt_line_string(&mut pkt, format!("{new_id} {zero} refs/tags/v1.0\n"));
pkt.put(&PKT_LINE_END_MARKER[..]);
smart
.parse_receive_pack_commands(pkt.clone().freeze())
.unwrap();
assert_eq!(smart.command_list.len(), 2);
assert_eq!(smart.command_list[0].old_hash, zero);
assert_eq!(smart.command_list[0].new_hash, new_id);
assert_eq!(smart.command_list[1].ref_name, "refs/tags/v1.0");
let request_stream = Box::pin(futures::stream::once(async { Ok(pkt.freeze()) }));
let report = smart.git_receive_pack_stream(request_stream).await.unwrap();
assert!(report.starts_with(b"000eunpack ok\n"), "{report:?}");
assert_eq!(
repo.recorded_updates(),
vec![
("refs/heads/main".to_string(), None, new_id.clone()),
(
"refs/tags/v1.0".to_string(),
Some(new_id.clone()),
zero.clone()
),
]
);
let sha1_zero = ObjectHash::zero_str(HashKind::Sha1);
let tagged = format!("blake3:{new_id}");
let upper = new_id.to_uppercase();
for (old, new, needle) in [
(sha1_zero.as_str(), new_id.as_str(), "old-id"),
(zero.as_str(), tagged.as_str(), "new-id"),
(zero.as_str(), upper.as_str(), "lowercase"),
(zero.as_str(), &new_id[..63], "expected 64"),
] {
let mut pkt = BytesMut::new();
add_pkt_line_string(&mut pkt, format!("{old} {new} refs/heads/main\n"));
pkt.put(&PKT_LINE_END_MARKER[..]);
let err = smart.parse_receive_pack_commands(pkt.freeze()).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidRequest(_)), "{err:?}");
assert!(err.to_string().contains(needle), "{err}");
assert!(smart.command_list.is_empty());
}
}
#[tokio::test]
async fn object_format_mismatch() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let mut smart =
SmartProtocol::new(TransportProtocol::Http, TestRepoAccess::new(), TestAuth);
for (caps, needle) in [
(
"object-format=SHA256 side-band-64k",
"unknown object-format",
),
("object-format=md5", "unknown object-format"),
(
"object-format=sha256",
"mismatch: wire=sha256 local=sha1 repository=sha1",
),
(
"object-format=blake3",
"mismatch: wire=blake3 local=sha1 repository=sha1",
),
] {
let err = smart.parse_capabilities(caps).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidRequest(_)), "{err:?}");
assert!(err.to_string().contains(needle), "{err}");
assert_eq!(smart.wire_hash_kind, HashKind::Sha1);
assert_eq!(smart.zero_id, ObjectHash::zero_str(HashKind::Sha1));
}
smart.parse_capabilities("object-format=sha1").unwrap();
assert_eq!(smart.wire_hash_kind, HashKind::Sha1);
let (_repo, mut blake3) = blake3_repo_and_protocol();
let err = blake3
.parse_capabilities("object-format=sha256")
.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha256 local=blake3 repository=blake3"),
"{err}"
);
smart.set_wire_hash_kind(HashKind::Sha256);
let err = smart
.git_info_refs(ServiceType::UploadPack)
.await
.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha256 local=sha1 repository=sha1"),
"{err}"
);
let sha1_id = "a".repeat(40);
let mut req = BytesMut::new();
add_pkt_line_string(&mut req, format!("want {sha1_id} object-format=sha256\n"));
req.put(&PKT_LINE_END_MARKER[..]);
let err = smart.git_upload_pack(req.freeze()).await.unwrap_err();
assert!(err.to_string().contains("wire=sha256 local=sha1"), "{err}");
assert_eq!(smart.wire_hash_kind, HashKind::Sha256);
smart.set_wire_hash_kind(HashKind::Sha1);
smart.set_wire_hash_kind(HashKind::Sha256);
let err = smart.parse_capabilities("object-format=sha1").unwrap_err();
assert!(
err.to_string()
.contains("wire=sha256 local=sha1 repository=sha1"),
"{err}"
);
assert_eq!(smart.wire_hash_kind, HashKind::Sha256);
smart.set_wire_hash_kind(HashKind::Sha1);
{
let repo = DriftingRepo::new();
let mut dynamic = SmartProtocol::new(TransportProtocol::Http, repo.clone(), TestAuth);
repo.set_kind(HashKind::Sha256);
let err = dynamic
.parse_capabilities("object-format=sha1")
.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
}
let mut req = BytesMut::new();
add_pkt_line_string(&mut req, format!("want {}\n", sha1_id.to_uppercase()));
req.put(&PKT_LINE_END_MARKER[..]);
let err = smart.git_upload_pack(req.freeze()).await.unwrap_err();
assert!(
err.to_string()
.contains("expected 40 lowercase hex chars for sha1"),
"{err}"
);
for (line, needle) in [
(format!("want {} side-band-64k\n", "b".repeat(64)), "`want`"),
(format!("want sha1:{sha1_id}\n"), "`want`"),
(format!("want {sha1_id}\n"), "`have`"),
] {
let mut req = BytesMut::new();
add_pkt_line_string(&mut req, line);
if needle == "`have`" {
add_pkt_line_string(&mut req, format!("have {}\n", sha1_id.to_uppercase()));
}
req.put(&PKT_LINE_END_MARKER[..]);
let err = smart.git_upload_pack(req.freeze()).await.unwrap_err();
assert!(matches!(err, ProtocolError::InvalidRequest(_)), "{err:?}");
assert!(err.to_string().contains(needle), "{err}");
}
{
let mut tampered =
SmartProtocol::new(TransportProtocol::Http, TestRepoAccess::new(), TestAuth);
tampered.zero_id = "0".repeat(64);
let err = tampered
.git_info_refs(ServiceType::UploadPack)
.await
.unwrap_err();
assert!(err.to_string().contains("zero ID"), "{err}");
assert!(err.to_string().contains("expected 40 hex zeros"), "{err}");
}
{
let flipping = SmartProtocol::new(
TransportProtocol::Http,
DriftingRepo::flipping_refs(),
TestAuth,
);
let err = flipping
.git_info_refs(ServiceType::UploadPack)
.await
.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
let mut flipping = SmartProtocol::new(
TransportProtocol::Http,
DriftingRepo::flipping_has_object(),
TestAuth,
);
let mut req = BytesMut::new();
add_pkt_line_string(&mut req, format!("want {}\n", "a".repeat(40)));
add_pkt_line_string(&mut req, format!("have {}\n", "b".repeat(40)));
add_pkt_line_string(&mut req, "done\n".to_string());
let err = flipping.git_upload_pack(req.freeze()).await.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
}
{
let repo = DriftingRepo::new();
let mut drifting = SmartProtocol::new(TransportProtocol::Http, repo.clone(), TestAuth);
let zero = ObjectHash::zero_str(HashKind::Sha1);
let mut pkt = BytesMut::new();
add_pkt_line_string(
&mut pkt,
format!("{zero} {} refs/heads/main\n", "a".repeat(40)),
);
pkt.put(&PKT_LINE_END_MARKER[..]);
let bytes = pkt.freeze();
let handle = repo.clone();
let stream = Box::pin(futures::stream::once(async move {
handle.set_kind(HashKind::Sha256);
Ok(bytes)
}));
let err = drifting.git_receive_pack_stream(stream).await.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
assert!(drifting.command_list.is_empty());
}
{
let repo = DriftingRepo::flipping_update_reference();
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo.clone(), TestAuth);
let zero = ObjectHash::zero_str(HashKind::Sha1);
let mut pkt = BytesMut::new();
add_pkt_line_string(
&mut pkt,
format!("{zero} {} refs/heads/a\n", "a".repeat(40)),
);
add_pkt_line_string(
&mut pkt,
format!("{zero} {} refs/heads/b\n", "b".repeat(40)),
);
pkt.put(&PKT_LINE_END_MARKER[..]);
let stream = Box::pin(futures::stream::once(async { Ok(pkt.freeze()) }));
let err = smart.git_receive_pack_stream(stream).await.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
assert_eq!(repo.inner.updates_len(), 1);
assert_eq!(smart.command_list.len(), 2);
}
let repo = DriftingRepo::new();
let dynamic = SmartProtocol::new(TransportProtocol::Http, repo.clone(), TestAuth);
assert_eq!(dynamic.local_hash_kind, HashKind::Sha1);
repo.set_kind(HashKind::Sha256);
let err = dynamic
.git_info_refs(ServiceType::UploadPack)
.await
.unwrap_err();
assert!(
err.to_string()
.contains("wire=sha1 local=sha1 repository=sha256"),
"{err}"
);
repo.set_kind(HashKind::Sha1);
assert!(dynamic.git_info_refs(ServiceType::UploadPack).await.is_ok());
}
#[derive(Clone)]
struct ObjectRepo {
kind: HashKind,
head: String,
objects: Arc<std::collections::HashMap<String, Vec<u8>>>,
}
#[async_trait]
impl RepositoryAccess for ObjectRepo {
fn object_hash_kind(&self) -> HashKind {
self.kind
}
async fn get_repository_refs(&self) -> Result<Vec<(String, String)>, ProtocolError> {
Ok(vec![
("HEAD".to_string(), self.head.clone()),
("refs/heads/main".to_string(), self.head.clone()),
])
}
async fn has_object(&self, object_hash: &str) -> Result<bool, ProtocolError> {
Ok(self.objects.contains_key(object_hash))
}
async fn get_object(&self, object_hash: &str) -> Result<Vec<u8>, ProtocolError> {
self.objects
.get(object_hash)
.cloned()
.ok_or_else(|| ProtocolError::ObjectNotFound(object_hash.to_string()))
}
async fn store_pack_data(&self, _pack_data: &[u8]) -> Result<(), ProtocolError> {
Ok(())
}
async fn update_reference(
&self,
_ref_name: &str,
_old_hash: Option<&str>,
_new_hash: &str,
) -> Result<(), ProtocolError> {
Ok(())
}
async fn get_objects_for_pack(
&self,
_wants: &[String],
_haves: &[String],
) -> Result<Vec<String>, ProtocolError> {
Ok(vec![])
}
async fn has_default_branch(&self) -> Result<bool, ProtocolError> {
Ok(true)
}
async fn post_receive_hook(&self) -> Result<(), ProtocolError> {
Ok(())
}
}
#[tokio::test]
async fn wire_ids_round_trip_for_every_kind() {
use crate::internal::object::ObjectTrait;
let _guard = set_hash_kind_for_test(HashKind::Sha1);
for kind in [HashKind::Sha1, HashKind::Sha256, HashKind::Blake3] {
let blob = Blob::from_content_with_kind(kind, "wire").unwrap();
let tree = Tree::from_tree_items_with_kind(
kind,
vec![TreeItem::new(
TreeItemMode::Blob,
blob.id,
"wire.txt".to_string(),
)],
)
.unwrap();
let commit = Commit::from_tree_id_with_kind(kind, tree.id, vec![], "wire ids").unwrap();
let mut objects = std::collections::HashMap::new();
objects.insert(blob.id.to_string(), blob.to_data().unwrap());
objects.insert(tree.id.to_string(), tree.to_data().unwrap());
objects.insert(commit.id.to_string(), commit.to_data().unwrap());
let repo = ObjectRepo {
kind,
head: commit.id.to_string(),
objects: Arc::new(objects),
};
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo, TestAuth);
let resp = smart.git_info_refs(ServiceType::UploadPack).await.unwrap();
let text = String::from_utf8(resp.to_vec()).unwrap();
assert!(
text.contains(&format!("{} HEAD", commit.id))
&& text.contains(&format!(" object-format={}", kind.as_str())),
"{kind:?}: {text}"
);
let mut req = BytesMut::new();
add_pkt_line_string(
&mut req,
format!(
"want {} side-band-64k object-format={}\n",
commit.id,
kind.as_str()
),
);
add_pkt_line_string(&mut req, "done\n".to_string());
let (stream, buf) = smart.git_upload_pack(req.freeze()).await.unwrap();
assert!(String::from_utf8_lossy(&buf).contains("NAK"), "{kind:?}");
let mut stream = stream;
let mut pack_bytes = Vec::new();
while let Some(chunk) = futures::StreamExt::next(&mut stream).await {
pack_bytes.extend(chunk.unwrap());
}
let tmp = tempfile::tempdir().unwrap();
let mut pack = crate::internal::pack::Pack::new_with_hash_kind(
kind,
Some(1),
None,
Some(tmp.path().to_path_buf()),
true,
);
let seen = Arc::new(Mutex::new(Vec::new()));
let sink = seen.clone();
pack.decode(
&mut std::io::Cursor::new(&pack_bytes),
move |entry| sink.lock().unwrap().push(entry.inner.hash),
None::<fn(ObjectHash)>,
)
.unwrap();
let seen: Vec<ObjectHash> = seen.lock().unwrap().clone();
assert_eq!(seen.len(), 3, "{kind:?}");
assert!(seen.contains(&commit.id) && seen.iter().all(|h| h.kind() == kind));
let mut req = BytesMut::new();
add_pkt_line_string(&mut req, format!("want {}\n", commit.id));
add_pkt_line_string(&mut req, format!("have {}\n", commit.id));
add_pkt_line_string(&mut req, "done\n".to_string());
let (stream, buf) = smart.git_upload_pack(req.freeze()).await.unwrap();
assert!(
String::from_utf8_lossy(&buf).contains(&format!("ACK {}", commit.id)),
"{kind:?}"
);
let mut stream = stream;
let mut empty_pack = Vec::new();
while let Some(chunk) = futures::StreamExt::next(&mut stream).await {
empty_pack.extend(chunk.unwrap());
}
assert_eq!(empty_pack.len(), 12 + kind.size(), "{kind:?}");
assert_eq!(&empty_pack[..4], b"PACK");
assert_eq!(&empty_pack[8..12], &0u32.to_be_bytes());
assert_eq!(
ObjectHash::from_bytes_for_kind(kind, &empty_pack[12..]).unwrap(),
ObjectHash::new_for_kind(kind, &empty_pack[..12])
);
let zero = ObjectHash::zero_str(kind);
let mut pkt = BytesMut::new();
add_pkt_line_string(
&mut pkt,
format!(
"{zero} {} refs/heads/feature\0report-status object-format={}\n",
commit.id,
kind.as_str()
),
);
pkt.put(&PKT_LINE_END_MARKER[..]);
smart.parse_receive_pack_commands(pkt.freeze()).unwrap();
assert_eq!(smart.command_list.len(), 1);
assert_eq!(smart.command_list[0].old_hash, zero);
assert_eq!(smart.command_list[0].new_hash, commit.id.to_string());
assert!(smart.capabilities.contains(&Capability::ReportStatus));
}
}
#[derive(Clone)]
struct BadRefRepo {
kind: HashKind,
id: String,
}
#[async_trait]
impl RepositoryAccess for BadRefRepo {
fn object_hash_kind(&self) -> HashKind {
self.kind
}
async fn get_repository_refs(&self) -> Result<Vec<(String, String)>, ProtocolError> {
Ok(vec![("refs/heads/main".to_string(), self.id.clone())])
}
async fn has_object(&self, _object_hash: &str) -> Result<bool, ProtocolError> {
Ok(false)
}
async fn get_object(&self, object_hash: &str) -> Result<Vec<u8>, ProtocolError> {
Err(ProtocolError::ObjectNotFound(object_hash.to_string()))
}
async fn store_pack_data(&self, _pack_data: &[u8]) -> Result<(), ProtocolError> {
Ok(())
}
async fn update_reference(
&self,
_ref_name: &str,
_old_hash: Option<&str>,
_new_hash: &str,
) -> Result<(), ProtocolError> {
Ok(())
}
async fn get_objects_for_pack(
&self,
_wants: &[String],
_haves: &[String],
) -> Result<Vec<String>, ProtocolError> {
Ok(vec![])
}
async fn has_default_branch(&self) -> Result<bool, ProtocolError> {
Ok(true)
}
async fn post_receive_hook(&self) -> Result<(), ProtocolError> {
Ok(())
}
}
#[derive(Clone)]
struct DriftingRepo {
inner: TestRepoAccess,
kind: Arc<Mutex<HashKind>>,
flip_in_refs: bool,
flip_in_has_object: bool,
flip_in_update_reference: bool,
}
impl DriftingRepo {
fn new() -> Self {
Self {
inner: TestRepoAccess::new(),
kind: Arc::new(Mutex::new(HashKind::Sha1)),
flip_in_refs: false,
flip_in_has_object: false,
flip_in_update_reference: false,
}
}
fn flipping_update_reference() -> Self {
Self {
flip_in_update_reference: true,
..Self::new()
}
}
fn flipping_refs() -> Self {
Self {
flip_in_refs: true,
..Self::new()
}
}
fn flipping_has_object() -> Self {
Self {
flip_in_has_object: true,
..Self::new()
}
}
fn set_kind(&self, kind: HashKind) {
*self.kind.lock().unwrap() = kind;
}
}
#[async_trait]
impl RepositoryAccess for DriftingRepo {
fn object_hash_kind(&self) -> HashKind {
*self.kind.lock().unwrap()
}
async fn get_repository_refs(&self) -> Result<Vec<(String, String)>, ProtocolError> {
if self.flip_in_refs {
self.set_kind(HashKind::Sha256);
}
self.inner.get_repository_refs().await
}
async fn has_object(&self, object_hash: &str) -> Result<bool, ProtocolError> {
if self.flip_in_has_object {
self.set_kind(HashKind::Sha256);
}
self.inner.has_object(object_hash).await
}
async fn get_object(&self, object_hash: &str) -> Result<Vec<u8>, ProtocolError> {
self.inner.get_object(object_hash).await
}
async fn store_pack_data(&self, pack_data: &[u8]) -> Result<(), ProtocolError> {
self.inner.store_pack_data(pack_data).await
}
async fn update_reference(
&self,
ref_name: &str,
old_hash: Option<&str>,
new_hash: &str,
) -> Result<(), ProtocolError> {
let result = self
.inner
.update_reference(ref_name, old_hash, new_hash)
.await;
if self.flip_in_update_reference {
self.set_kind(HashKind::Sha256);
}
result
}
async fn get_objects_for_pack(
&self,
wants: &[String],
haves: &[String],
) -> Result<Vec<String>, ProtocolError> {
self.inner.get_objects_for_pack(wants, haves).await
}
async fn has_default_branch(&self) -> Result<bool, ProtocolError> {
self.inner.has_default_branch().await
}
async fn post_receive_hook(&self) -> Result<(), ProtocolError> {
self.inner.post_receive_hook().await
}
}
#[tokio::test]
async fn parse_capabilities_updates_hash_and_caps() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let repo_access = TestRepoAccess::with_kind(HashKind::Sha256);
let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access, auth);
smart
.parse_capabilities("object-format=sha256 side-band-64k multi_ack")
.unwrap();
assert_eq!(smart.wire_hash_kind, HashKind::Sha256);
assert_eq!(smart.zero_id.len(), HashKind::Sha256.hex_len());
assert!(
smart.capabilities.contains(&Capability::SideBand64k),
"side-band-64k should be recorded"
);
}
#[tokio::test]
async fn info_refs_accepts_sha256_refs_and_emits_capability() {
#[derive(Clone)]
struct Sha256Repo;
#[async_trait]
impl RepositoryAccess for Sha256Repo {
fn object_hash_kind(&self) -> HashKind {
HashKind::Sha256
}
async fn get_repository_refs(&self) -> Result<Vec<(String, String)>, ProtocolError> {
Ok(vec![
(
"HEAD".to_string(),
"0000000000000000000000000000000000000000000000000000000000000000"
.to_string(),
),
(
"refs/heads/main".to_string(),
"1111111111111111111111111111111111111111111111111111111111111111"
.to_string(),
),
])
}
async fn has_object(&self, _object_hash: &str) -> Result<bool, ProtocolError> {
Ok(true)
}
async fn get_object(&self, _object_hash: &str) -> Result<Vec<u8>, ProtocolError> {
Ok(vec![])
}
async fn store_pack_data(&self, _pack_data: &[u8]) -> Result<(), ProtocolError> {
Ok(())
}
async fn update_reference(
&self,
_ref_name: &str,
_old_hash: Option<&str>,
_new_hash: &str,
) -> Result<(), ProtocolError> {
Ok(())
}
async fn get_objects_for_pack(
&self,
_wants: &[String],
_haves: &[String],
) -> Result<Vec<String>, ProtocolError> {
Ok(vec![])
}
async fn has_default_branch(&self) -> Result<bool, ProtocolError> {
Ok(false)
}
async fn post_receive_hook(&self) -> Result<(), ProtocolError> {
Ok(())
}
}
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let repo_access = Sha256Repo;
let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access, auth);
smart.set_wire_hash_kind(HashKind::Sha256);
let resp = smart
.git_info_refs(ServiceType::UploadPack)
.await
.expect("sha256 refs should be accepted");
let resp_str = String::from_utf8(resp.to_vec()).expect("pkt-line should be valid UTF-8");
assert!(
resp_str.contains("object-format=sha256"),
"capability line should advertise sha256"
);
}
#[tokio::test]
async fn parse_receive_pack_commands_decodes_commands() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let repo_access = TestRepoAccess::new();
let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access, auth);
let zero = ObjectHash::zero_str(HashKind::Sha1);
let mut pkt = BytesMut::new();
add_pkt_line_string(&mut pkt, format!("{zero} {zero} refs/heads/main\n"));
add_pkt_line_string(&mut pkt, format!("{zero} {zero} refs/tags/v1.0\n"));
pkt.put(&PKT_LINE_END_MARKER[..]);
smart.parse_receive_pack_commands(pkt.freeze()).unwrap();
assert_eq!(smart.command_list.len(), 2);
assert_eq!(smart.command_list[0].ref_name, "refs/heads/main");
assert_eq!(smart.command_list[1].ref_name, "refs/tags/v1.0");
}
#[tokio::test]
async fn receive_pack_missing_flush_errors() {
let _guard = set_hash_kind_for_test(HashKind::Sha1);
let repo_access = TestRepoAccess::new();
let auth = TestAuth;
let mut smart = SmartProtocol::new(TransportProtocol::Http, repo_access, auth);
let zero = ObjectHash::zero_str(HashKind::Sha1);
let mut pkt = BytesMut::new();
add_pkt_line_string(&mut pkt, format!("{zero} {zero} refs/heads/main\n"));
let request_stream = Box::pin(futures::stream::once(async { Ok(pkt.freeze()) }));
let err = smart
.git_receive_pack_stream(request_stream)
.await
.unwrap_err();
assert!(matches!(err, ProtocolError::InvalidRequest(_)));
}
}