View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc. 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  
11  package org.eclipse.jgit.transport;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  
21  import org.eclipse.jgit.lib.Constants;
22  import org.eclipse.jgit.lib.ObjectId;
23  import org.eclipse.jgit.lib.ObjectInserter;
24  import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
25  import org.eclipse.jgit.util.NB;
26  import org.junit.Test;
27  
28  public class RefAdvertiserTest {
29  	@Test
30  	public void advertiser() throws IOException {
31  		ByteArrayOutputStream buf = new ByteArrayOutputStream();
32  		PacketLineOut pckOut = new PacketLineOut(buf);
33  		PacketLineOutRefAdvertiser adv = new PacketLineOutRefAdvertiser(pckOut);
34  
35  		// Advertisement of capability and id both happen in order of call,
36  		// which may not match Git standards. Callers are responsible for
37  		// making calls in the correct ordering. Here in this test we do them
38  		// in a "wrong" order to assert the method just writes to the network.
39  
40  		adv.advertiseCapability("test-1");
41  		adv.advertiseCapability("sideband");
42  		adv.advertiseId(id(1), "refs/heads/master");
43  		adv.advertiseId(id(4), "refs/" + padStart("F", 987));
44  		adv.advertiseId(id(2), "refs/heads/next");
45  		adv.advertiseId(id(3), "refs/Iñtërnâtiônàlizætiøn☃💩");
46  		adv.end();
47  		assertFalse(adv.isEmpty());
48  
49  		PacketLineIn pckIn = new PacketLineIn(
50  				new ByteArrayInputStream(buf.toByteArray()));
51  		String s = pckIn.readStringRaw();
52  		assertEquals(id(1).name() + " refs/heads/master\0 test-1 sideband\n",
53  				s);
54  
55  		s = pckIn.readStringRaw();
56  		assertEquals(id(4).name() + " refs/" + padStart("F", 987) + '\n', s);
57  
58  		s = pckIn.readStringRaw();
59  		assertEquals(id(2).name() + " refs/heads/next\n", s);
60  
61  		s = pckIn.readStringRaw();
62  		assertEquals(id(3).name() + " refs/Iñtërnâtiônàlizætiøn☃💩\n", s);
63  
64  		s = pckIn.readStringRaw();
65  		assertTrue(PacketLineIn.isEnd(s));
66  	}
67  
68  	private static ObjectId id(int i) {
69  		try (ObjectInserter.Formatter f = new ObjectInserter.Formatter()) {
70  			byte[] tmp = new byte[4];
71  			NB.encodeInt32(tmp, 0, i);
72  			return f.idFor(Constants.OBJ_BLOB, tmp);
73  		}
74  	}
75  
76  	private static String padStart(String s, int len) {
77  		StringBuilder b = new StringBuilder(len);
78  		for (int i = s.length(); i < len; i++) {
79  			b.append((char) ('a' + (i % 26)));
80  		}
81  		return b.append(s).toString();
82  	}
83  }