@ -24,6 +24,8 @@ import java.util.Collections;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.Objects ;
import java.util.function.Consumer ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCache ;
import org.eclipse.jgit.dircache.DirCacheBuilder ;
import org.eclipse.jgit.dircache.DirCacheBuilder ;
@ -424,22 +426,18 @@ public class UploadPackTest {
* Invokes UploadPack with protocol v2 and sends it the given lines ,
* Invokes UploadPack with protocol v2 and sends it the given lines ,
* and returns UploadPack ' s output stream .
* and returns UploadPack ' s output stream .
* /
* /
private ByteArrayInputStream uploadPackV2Setup ( RequestPolicy requestPolicy ,
private ByteArrayInputStream uploadPackV2Setup (
RefFilter refFilter , ProtocolV2Hook hook , String . . . inputLines )
Consumer < UploadPack > postConstructionSetup , String . . . inputLines )
throws Exception {
throws Exception {
ByteArrayInputStream send = linesAsInputStream ( inputLines ) ;
ByteArrayInputStream send = linesAsInputStream ( inputLines ) ;
server . getConfig ( ) . setString ( "protocol" , null , "version" , "2" ) ;
server . getConfig ( ) . setString ( "protocol" , null , "version" , "2" ) ;
UploadPack up = new UploadPack ( server ) ;
UploadPack up = new UploadPack ( server ) ;
if ( requestPolicy ! = null )
if ( postConstructionSetup ! = null ) {
up . setRequestPolicy ( requestPolicy ) ;
postConstructionSetup . accept ( up ) ;
if ( refFilter ! = null )
up . setRefFilter ( refFilter ) ;
up . setExtraParameters ( Sets . of ( "version=2" ) ) ;
if ( hook ! = null ) {
up . setProtocolV2Hook ( hook ) ;
}
}
up . setExtraParameters ( Sets . of ( "version=2" ) ) ;
ByteArrayOutputStream recv = new ByteArrayOutputStream ( ) ;
ByteArrayOutputStream recv = new ByteArrayOutputStream ( ) ;
up . upload ( send , recv , null ) ;
up . upload ( send , recv , null ) ;
@ -453,6 +451,7 @@ public class UploadPackTest {
try ( ByteArrayOutputStream send = new ByteArrayOutputStream ( ) ) {
try ( ByteArrayOutputStream send = new ByteArrayOutputStream ( ) ) {
PacketLineOut pckOut = new PacketLineOut ( send ) ;
PacketLineOut pckOut = new PacketLineOut ( send ) ;
for ( String line : inputLines ) {
for ( String line : inputLines ) {
Objects . requireNonNull ( line ) ;
if ( PacketLineIn . isEnd ( line ) ) {
if ( PacketLineIn . isEnd ( line ) ) {
pckOut . end ( ) ;
pckOut . end ( ) ;
} else if ( PacketLineIn . isDelimiter ( line ) ) {
} else if ( PacketLineIn . isDelimiter ( line ) ) {
@ -470,11 +469,12 @@ public class UploadPackTest {
* Returns UploadPack ' s output stream , not including the capability
* Returns UploadPack ' s output stream , not including the capability
* advertisement by the server .
* advertisement by the server .
* /
* /
private ByteArrayInputStream uploadPackV2 ( RequestPolicy requestPolicy ,
private ByteArrayInputStream uploadPackV2 (
RefFilter refFilter , ProtocolV2Hook hook , String . . . inputLines )
Consumer < UploadPack > postConstructionSetup ,
String . . . inputLines )
throws Exception {
throws Exception {
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream =
uploadPackV2Setup ( requestPolicy , refFilter , hook , inputLines ) ;
uploadPackV2Setup ( postConstructionSetup , inputLines ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
// drain capabilities
// drain capabilities
@ -485,7 +485,7 @@ public class UploadPackTest {
}
}
private ByteArrayInputStream uploadPackV2 ( String . . . inputLines ) throws Exception {
private ByteArrayInputStream uploadPackV2 ( String . . . inputLines ) throws Exception {
return uploadPackV2 ( null , null , null , inputLines ) ;
return uploadPackV2 ( null , inputLines ) ;
}
}
private static class TestV2Hook implements ProtocolV2Hook {
private static class TestV2Hook implements ProtocolV2Hook {
@ -514,8 +514,9 @@ public class UploadPackTest {
@Test
@Test
public void testV2Capabilities ( ) throws Exception {
public void testV2Capabilities ( ) throws Exception {
TestV2Hook hook = new TestV2Hook ( ) ;
TestV2Hook hook = new TestV2Hook ( ) ;
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream = uploadPackV2Setup (
uploadPackV2Setup ( null , null , hook , PacketLineIn . end ( ) ) ;
( UploadPack up ) - > { up . setProtocolV2Hook ( hook ) ; } ,
PacketLineIn . end ( ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
assertThat ( hook . capabilitiesRequest , notNullValue ( ) ) ;
assertThat ( hook . capabilitiesRequest , notNullValue ( ) ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
@ -536,7 +537,7 @@ public class UploadPackTest {
String fetchCapability ) throws Exception {
String fetchCapability ) throws Exception {
server . getConfig ( ) . setBoolean ( configSection , null , configName , true ) ;
server . getConfig ( ) . setBoolean ( configSection , null , configName , true ) ;
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream =
uploadPackV2Setup ( null , null , null , PacketLineIn . end ( ) ) ;
uploadPackV2Setup ( null , PacketLineIn . end ( ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
@ -558,7 +559,7 @@ public class UploadPackTest {
private void checkUnadvertisedIfUnallowed ( String fetchCapability ) throws Exception {
private void checkUnadvertisedIfUnallowed ( String fetchCapability ) throws Exception {
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream =
uploadPackV2Setup ( null , null , null , PacketLineIn . end ( ) ) ;
uploadPackV2Setup ( null , PacketLineIn . end ( ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
@ -605,7 +606,7 @@ public class UploadPackTest {
server . getConfig ( ) . setBoolean ( "uploadpack" , null , "allowrefinwant" , true ) ;
server . getConfig ( ) . setBoolean ( "uploadpack" , null , "allowrefinwant" , true ) ;
server . getConfig ( ) . setBoolean ( "uploadpack" , null , "advertiserefinwant" , false ) ;
server . getConfig ( ) . setBoolean ( "uploadpack" , null , "advertiserefinwant" , false ) ;
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream =
uploadPackV2Setup ( null , null , null , PacketLineIn . end ( ) ) ;
uploadPackV2Setup ( null , PacketLineIn . end ( ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
assertThat ( pckIn . readString ( ) , is ( "version 2" ) ) ;
@ -633,7 +634,8 @@ public class UploadPackTest {
remote . update ( "refs/tags/tag" , tag ) ;
remote . update ( "refs/tags/tag" , tag ) ;
TestV2Hook hook = new TestV2Hook ( ) ;
TestV2Hook hook = new TestV2Hook ( ) ;
ByteArrayInputStream recvStream = uploadPackV2 ( null , null , hook ,
ByteArrayInputStream recvStream = uploadPackV2 (
( UploadPack up ) - > { up . setProtocolV2Hook ( hook ) ; } ,
"command=ls-refs\n" , PacketLineIn . end ( ) ) ;
"command=ls-refs\n" , PacketLineIn . end ( ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
@ -768,7 +770,7 @@ public class UploadPackTest {
PacketLineIn . end ( ) } ;
PacketLineIn . end ( ) } ;
TestV2Hook testHook = new TestV2Hook ( ) ;
TestV2Hook testHook = new TestV2Hook ( ) ;
uploadPackV2Setup ( null , null , testHook , lines ) ;
uploadPackV2Setup ( ( UploadPack up ) - > { up . setProtocolV2Hook ( testHook ) ; } , lines ) ;
LsRefsV2Request req = testHook . lsRefsRequest ;
LsRefsV2Request req = testHook . lsRefsRequest ;
assertEquals ( 2 , req . getServerOptions ( ) . size ( ) ) ;
assertEquals ( 2 , req . getServerOptions ( ) . size ( ) ) ;
@ -804,14 +806,18 @@ public class UploadPackTest {
remote . update ( "branch1" , advertized ) ;
remote . update ( "branch1" , advertized ) ;
// This works
// This works
uploadPackV2 ( RequestPolicy . ADVERTISED , null , null , "command=fetch\n" ,
uploadPackV2 (
PacketLineIn . delimiter ( ) , "want " + advertized . name ( ) + "\n" ,
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . ADVERTISED ) ; } ,
PacketLineIn . end ( ) ) ;
"command=fetch\n" ,
PacketLineIn . delimiter ( ) ,
"want " + advertized . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ;
// This doesn't
// This doesn't
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException . class ,
UploadPackInternalServerErrorException . class ,
( ) - > uploadPackV2 ( RequestPolicy . ADVERTISED , null , null ,
( ) - > uploadPackV2 (
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . ADVERTISED ) ; } ,
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
"want " + unadvertized . name ( ) + "\n" ,
"want " + unadvertized . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ) ;
PacketLineIn . end ( ) ) ) ;
@ -828,14 +834,18 @@ public class UploadPackTest {
remote . update ( "branch1" , advertized ) ;
remote . update ( "branch1" , advertized ) ;
// This works
// This works
uploadPackV2 ( RequestPolicy . REACHABLE_COMMIT , null , null ,
uploadPackV2 (
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . REACHABLE_COMMIT ) ; } ,
"want " + reachable . name ( ) + "\n" , PacketLineIn . end ( ) ) ;
"command=fetch\n" ,
PacketLineIn . delimiter ( ) ,
"want " + reachable . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ;
// This doesn't
// This doesn't
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException . class ,
UploadPackInternalServerErrorException . class ,
( ) - > uploadPackV2 ( RequestPolicy . REACHABLE_COMMIT , null , null ,
( ) - > uploadPackV2 (
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . REACHABLE_COMMIT ) ; } ,
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
"want " + unreachable . name ( ) + "\n" ,
"want " + unreachable . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ) ;
PacketLineIn . end ( ) ) ) ;
@ -851,15 +861,25 @@ public class UploadPackTest {
remote . update ( "secret" , tip ) ;
remote . update ( "secret" , tip ) ;
// This works
// This works
uploadPackV2 ( RequestPolicy . TIP , new RejectAllRefFilter ( ) , null ,
uploadPackV2 (
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
( UploadPack up ) - > {
"want " + tip . name ( ) + "\n" , PacketLineIn . end ( ) ) ;
up . setRequestPolicy ( RequestPolicy . TIP ) ;
up . setRefFilter ( new RejectAllRefFilter ( ) ) ;
} ,
"command=fetch\n" ,
PacketLineIn . delimiter ( ) ,
"want " + tip . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ;
// This doesn't
// This doesn't
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException . class ,
UploadPackInternalServerErrorException . class ,
( ) - > uploadPackV2 ( RequestPolicy . TIP , new RejectAllRefFilter ( ) ,
( ) - > uploadPackV2 (
null , "command=fetch\n" , PacketLineIn . delimiter ( ) ,
( UploadPack up ) - > {
up . setRequestPolicy ( RequestPolicy . TIP ) ;
up . setRefFilter ( new RejectAllRefFilter ( ) ) ;
} ,
"command=fetch\n" , PacketLineIn . delimiter ( ) ,
"want " + parentOfTip . name ( ) + "\n" ,
"want " + parentOfTip . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ) ;
PacketLineIn . end ( ) ) ) ;
assertThat ( e . getCause ( ) . getMessage ( ) ,
assertThat ( e . getCause ( ) . getMessage ( ) ,
@ -875,16 +895,24 @@ public class UploadPackTest {
remote . update ( "secret" , tip ) ;
remote . update ( "secret" , tip ) ;
// This works
// This works
uploadPackV2 ( RequestPolicy . REACHABLE_COMMIT_TIP ,
uploadPackV2 (
new RejectAllRefFilter ( ) , null , "command=fetch\n" ,
( UploadPack up ) - > {
up . setRequestPolicy ( RequestPolicy . REACHABLE_COMMIT_TIP ) ;
up . setRefFilter ( new RejectAllRefFilter ( ) ) ;
} ,
"command=fetch\n" ,
PacketLineIn . delimiter ( ) , "want " + parentOfTip . name ( ) + "\n" ,
PacketLineIn . delimiter ( ) , "want " + parentOfTip . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ;
PacketLineIn . end ( ) ) ;
// This doesn't
// This doesn't
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException e = assertThrows (
UploadPackInternalServerErrorException . class ,
UploadPackInternalServerErrorException . class ,
( ) - > uploadPackV2 ( RequestPolicy . REACHABLE_COMMIT_TIP ,
( ) - > uploadPackV2 (
new RejectAllRefFilter ( ) , null , "command=fetch\n" ,
( UploadPack up ) - > {
up . setRequestPolicy ( RequestPolicy . REACHABLE_COMMIT_TIP ) ;
up . setRefFilter ( new RejectAllRefFilter ( ) ) ;
} ,
"command=fetch\n" ,
PacketLineIn . delimiter ( ) ,
PacketLineIn . delimiter ( ) ,
"want " + unreachable . name ( ) + "\n" ,
"want " + unreachable . name ( ) + "\n" ,
PacketLineIn . end ( ) ) ) ;
PacketLineIn . end ( ) ) ) ;
@ -898,9 +926,7 @@ public class UploadPackTest {
// Exercise to make sure that even unreachable commits can be fetched
// Exercise to make sure that even unreachable commits can be fetched
uploadPackV2 (
uploadPackV2 (
RequestPolicy . ANY ,
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . ANY ) ; } ,
null ,
null ,
"command=fetch\n" ,
"command=fetch\n" ,
PacketLineIn . delimiter ( ) ,
PacketLineIn . delimiter ( ) ,
"want " + unreachable . name ( ) + "\n" ,
"want " + unreachable . name ( ) + "\n" ,
@ -1500,7 +1526,7 @@ public class UploadPackTest {
PacketLineIn . end ( ) } ;
PacketLineIn . end ( ) } ;
TestV2Hook testHook = new TestV2Hook ( ) ;
TestV2Hook testHook = new TestV2Hook ( ) ;
uploadPackV2Setup ( null , null , testHook , lines ) ;
uploadPackV2Setup ( ( UploadPack up ) - > { up . setProtocolV2Hook ( testHook ) ; } , lines ) ;
FetchV2Request req = testHook . fetchRequest ;
FetchV2Request req = testHook . fetchRequest ;
assertNotNull ( req ) ;
assertNotNull ( req ) ;
@ -1584,8 +1610,9 @@ public class UploadPackTest {
input . add ( "done\n" ) ;
input . add ( "done\n" ) ;
input . add ( PacketLineIn . end ( ) ) ;
input . add ( PacketLineIn . end ( ) ) ;
ByteArrayInputStream recvStream =
ByteArrayInputStream recvStream =
uploadPackV2 ( RequestPolicy . ANY , /*refFilter=*/ null ,
uploadPackV2 (
/*hook=*/ null , input . toArray ( new String [ 0 ] ) ) ;
( UploadPack up ) - > { up . setRequestPolicy ( RequestPolicy . ANY ) ; } ,
input . toArray ( new String [ 0 ] ) ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
PacketLineIn pckIn = new PacketLineIn ( recvStream ) ;
assertThat ( pckIn . readString ( ) , is ( "packfile" ) ) ;
assertThat ( pckIn . readString ( ) , is ( "packfile" ) ) ;
parsePack ( recvStream ) ;
parsePack ( recvStream ) ;