1 /*
2 * Copyright (C) 2016, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.transport;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertSame;
49
50 import java.io.ByteArrayInputStream;
51 import java.io.ByteArrayOutputStream;
52 import java.io.IOException;
53
54 import org.eclipse.jgit.lib.Constants;
55 import org.eclipse.jgit.lib.ObjectId;
56 import org.eclipse.jgit.lib.ObjectInserter;
57 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
58 import org.eclipse.jgit.util.NB;
59 import org.junit.Test;
60
61 public class RefAdvertiserTest {
62 @Test
63 public void advertiser() throws IOException {
64 ByteArrayOutputStream buf = new ByteArrayOutputStream();
65 PacketLineOut pckOut = new PacketLineOut(buf);
66 PacketLineOutRefAdvertiser adv = new PacketLineOutRefAdvertiser(pckOut);
67
68 // Advertisement of capability and id both happen in order of call,
69 // which may not match Git standards. Callers are responsible for
70 // making calls in the correct ordering. Here in this test we do them
71 // in a "wrong" order to assert the method just writes to the network.
72
73 adv.advertiseCapability("test-1");
74 adv.advertiseCapability("sideband");
75 adv.advertiseId(id(1), "refs/heads/master");
76 adv.advertiseId(id(4), "refs/" + padStart("F", 987));
77 adv.advertiseId(id(2), "refs/heads/next");
78 adv.advertiseId(id(3), "refs/Iñtërnâtiônàlizætiøn☃💩");
79 adv.end();
80 assertFalse(adv.isEmpty());
81
82 PacketLineIn pckIn = new PacketLineIn(
83 new ByteArrayInputStream(buf.toByteArray()));
84 String s = pckIn.readStringRaw();
85 assertEquals(id(1).name() + " refs/heads/master\0 test-1 sideband\n",
86 s);
87
88 s = pckIn.readStringRaw();
89 assertEquals(id(4).name() + " refs/" + padStart("F", 987) + '\n', s);
90
91 s = pckIn.readStringRaw();
92 assertEquals(id(2).name() + " refs/heads/next\n", s);
93
94 s = pckIn.readStringRaw();
95 assertEquals(id(3).name() + " refs/Iñtërnâtiônàlizætiøn☃💩\n", s);
96
97 s = pckIn.readStringRaw();
98 assertSame(PacketLineIn.END, s);
99 }
100
101 private static ObjectId id(int i) {
102 try (ObjectInserter.Formatter f = new ObjectInserter.Formatter()) {
103 byte[] tmp = new byte[4];
104 NB.encodeInt32(tmp, 0, i);
105 return f.idFor(Constants.OBJ_BLOB, tmp);
106 }
107 }
108
109 private static String padStart(String s, int len) {
110 StringBuilder b = new StringBuilder(len);
111 for (int i = s.length(); i < len; i++) {
112 b.append((char) ('a' + (i % 26)));
113 }
114 return b.append(s).toString();
115 }
116 }