Skip to content

Commit 4414a15

Browse files
peffgitster
authored andcommitted
t/lib-git-daemon: add network-protocol helpers
All of our git-protocol tests rely on invoking the client and having it make a request of a server. That gives a nice real-world test of how the two behave together, but it doesn't leave any room for testing how a server might react to _other_ clients. Let's add a few test helper functions which can be used to manually conduct a git-protocol conversation with a remote git-daemon: 1. To connect to a remote git-daemon, we need something like "netcat". But not everybody will have netcat. And even if they do, the behavior with respect to half-duplex shutdowns is not portable (openbsd netcat has "-N", with others you must rely on "-q 1", which is racy). Here we provide a "fake_nc" that is capable of doing a client-side netcat, with sane half-duplex semantics. It relies on perl's IO::Socket::INET. That's been in the base distribution since 5.6.0, so it's probably available everywhere. But just to be on the safe side, we'll add a prereq. 2. To help tests speak and read pktline, this patch adds packetize() and depacketize() functions. I've put fake_nc() into lib-git-daemon.sh, since that's really the only server where we'd need to use a network socket. Whereas the pktline helpers may be of more general use, so I've added them to test-lib-functions.sh. Programs like upload-pack speak pktline, but can talk directly over stdio without a network socket. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 550fbca commit 4414a15

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

t/lib-git-daemon.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ LIB_GIT_DAEMON_PORT=${LIB_GIT_DAEMON_PORT-${this_test#t}}
3232

3333
GIT_DAEMON_PID=
3434
GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
35-
GIT_DAEMON_URL=git://127.0.0.1:$LIB_GIT_DAEMON_PORT
35+
GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
36+
GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
3637

3738
start_git_daemon() {
3839
if test -n "$GIT_DAEMON_PID"
@@ -98,3 +99,25 @@ stop_git_daemon() {
9899
GIT_DAEMON_PID=
99100
rm -f git_daemon_output
100101
}
102+
103+
# A stripped-down version of a netcat client, that connects to a "host:port"
104+
# given in $1, sends its stdin followed by EOF, then dumps the response (until
105+
# EOF) to stdout.
106+
fake_nc() {
107+
if ! test_declared_prereq FAKENC
108+
then
109+
echo >&4 "fake_nc: need to declare FAKENC prerequisite"
110+
return 127
111+
fi
112+
perl -Mstrict -MIO::Socket::INET -e '
113+
my $s = IO::Socket::INET->new(shift)
114+
or die "unable to open socket: $!";
115+
print $s <STDIN>;
116+
$s->shutdown(1);
117+
print <$s>;
118+
' "$@"
119+
}
120+
121+
test_lazy_prereq FAKENC '
122+
perl -MIO::Socket::INET -e "exit 0"
123+
'

t/test-lib-functions.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,3 +1020,37 @@ nongit () {
10201020
"$@"
10211021
)
10221022
}
1023+
1024+
# convert stdin to pktline representation; note that empty input becomes an
1025+
# empty packet, not a flush packet (for that you can just print 0000 yourself).
1026+
packetize() {
1027+
cat >packetize.tmp &&
1028+
len=$(wc -c <packetize.tmp) &&
1029+
printf '%04x%s' "$(($len + 4))" &&
1030+
cat packetize.tmp &&
1031+
rm -f packetize.tmp
1032+
}
1033+
1034+
# Parse the input as a series of pktlines, writing the result to stdout.
1035+
# Sideband markers are removed automatically, and the output is routed to
1036+
# stderr if appropriate.
1037+
#
1038+
# NUL bytes are converted to "\\0" for ease of parsing with text tools.
1039+
depacketize () {
1040+
perl -e '
1041+
while (read(STDIN, $len, 4) == 4) {
1042+
if ($len eq "0000") {
1043+
print "FLUSH\n";
1044+
} else {
1045+
read(STDIN, $buf, hex($len) - 4);
1046+
$buf =~ s/\0/\\0/g;
1047+
if ($buf =~ s/^[\x2\x3]//) {
1048+
print STDERR $buf;
1049+
} else {
1050+
$buf =~ s/^\x1//;
1051+
print $buf;
1052+
}
1053+
}
1054+
}
1055+
'
1056+
}

0 commit comments

Comments
 (0)