View Javadoc
1   /*
2    * Copyright (C) 2021, Saša Živkov <sasa.zivkov@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.transport;
11  
12  import static org.hamcrest.MatcherAssert.assertThat;
13  import static org.hamcrest.Matchers.is;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.ByteArrayOutputStream;
18  import java.io.IOException;
19  import java.util.Objects;
20  import java.util.function.Consumer;
21  
22  import org.eclipse.jgit.internal.storage.file.FileRepository;
23  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
24  import org.eclipse.jgit.junit.TestRepository;
25  import org.eclipse.jgit.lib.Sets;
26  import org.eclipse.jgit.revwalk.RevCommit;
27  import org.eclipse.jgit.revwalk.RevTag;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  // TODO: refactor UploadPackTest to run against both DfsRepository and FileRepository
32  public class UploadPackLsRefsFileRepositoryTest
33  		extends LocalDiskRepositoryTestCase {
34  
35  	private FileRepository server;
36  
37  	private TestRepository<FileRepository> remote;
38  
39  	@Before
40  	@Override
41  	public void setUp() throws Exception {
42  		super.setUp();
43  		server = createWorkRepository();
44  		remote = new TestRepository<>(server);
45  	}
46  
47  	@Test
48  	public void testV2LsRefsPeel() throws Exception {
49  		RevCommit tip = remote.commit().message("message").create();
50  		remote.update("master", tip);
51  		server.updateRef("HEAD").link("refs/heads/master");
52  		RevTag tag = remote.tag("tag", tip);
53  		remote.update("refs/tags/tag", tag);
54  
55  		ByteArrayInputStream recvStream = uploadPackV2("command=ls-refs\n",
56  				PacketLineIn.delimiter(), "peel", PacketLineIn.end());
57  		PacketLineIn pckIn = new PacketLineIn(recvStream);
58  
59  		assertThat(pckIn.readString(),
60  				is(tip.toObjectId().getName() + " HEAD"));
61  		assertThat(pckIn.readString(),
62  				is(tip.toObjectId().getName() + " refs/heads/master"));
63  		assertThat(pckIn.readString(), is(tag.toObjectId().getName()
64  				+ " refs/tags/tag peeled:" + tip.toObjectId().getName()));
65  		assertTrue(PacketLineIn.isEnd(pckIn.readString()));
66  	}
67  
68  	private ByteArrayInputStream uploadPackV2(String... inputLines)
69  			throws Exception {
70  		return uploadPackV2(null, inputLines);
71  	}
72  
73  	private ByteArrayInputStream uploadPackV2(
74  			Consumer<UploadPack> postConstructionSetup, String... inputLines)
75  			throws Exception {
76  		ByteArrayInputStream recvStream = uploadPackV2Setup(
77  				postConstructionSetup, inputLines);
78  		PacketLineIn pckIn = new PacketLineIn(recvStream);
79  
80  		// drain capabilities
81  		while (!PacketLineIn.isEnd(pckIn.readString())) {
82  			// do nothing
83  		}
84  		return recvStream;
85  	}
86  
87  	private ByteArrayInputStream uploadPackV2Setup(
88  			Consumer<UploadPack> postConstructionSetup, String... inputLines)
89  			throws Exception {
90  
91  		ByteArrayInputStream send = linesAsInputStream(inputLines);
92  
93  		server.getConfig().setString("protocol", null, "version", "2");
94  		UploadPack up = new UploadPack(server);
95  		if (postConstructionSetup != null) {
96  			postConstructionSetup.accept(up);
97  		}
98  		up.setExtraParameters(Sets.of("version=2"));
99  
100 		ByteArrayOutputStream recv = new ByteArrayOutputStream();
101 		up.upload(send, recv, null);
102 
103 		return new ByteArrayInputStream(recv.toByteArray());
104 	}
105 
106 	private static ByteArrayInputStream linesAsInputStream(String... inputLines)
107 			throws IOException {
108 		try (ByteArrayOutputStream send = new ByteArrayOutputStream()) {
109 			PacketLineOut pckOut = new PacketLineOut(send);
110 			for (String line : inputLines) {
111 				Objects.requireNonNull(line);
112 				if (PacketLineIn.isEnd(line)) {
113 					pckOut.end();
114 				} else if (PacketLineIn.isDelimiter(line)) {
115 					pckOut.writeDelim();
116 				} else {
117 					pckOut.writeString(line);
118 				}
119 			}
120 			return new ByteArrayInputStream(send.toByteArray());
121 		}
122 	}
123 }