View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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 java.nio.charset.StandardCharsets.UTF_8;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.fail;
49  
50  import java.io.ByteArrayOutputStream;
51  import java.io.IOException;
52  import java.io.OutputStream;
53  
54  import org.junit.Before;
55  import org.junit.Test;
56  
57  // Note, test vectors created with:
58  //
59  // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
60  
61  public class PacketLineOutTest {
62  	private ByteArrayOutputStream rawOut;
63  
64  	private PacketLineOut out;
65  
66  	@Before
67  	public void setUp() throws Exception {
68  		rawOut = new ByteArrayOutputStream();
69  		out = new PacketLineOut(rawOut);
70  	}
71  
72  	// writeString
73  
74  	@Test
75  	public void testWriteString1() throws IOException {
76  		out.writeString("a");
77  		out.writeString("bc");
78  		assertBuffer("0005a0006bc");
79  	}
80  
81  	@Test
82  	public void testWriteString2() throws IOException {
83  		out.writeString("a\n");
84  		out.writeString("bc\n");
85  		assertBuffer("0006a\n0007bc\n");
86  	}
87  
88  	@Test
89  	public void testWriteString3() throws IOException {
90  		out.writeString("");
91  		assertBuffer("0004");
92  	}
93  
94  	// end
95  
96  	@Test
97  	public void testWriteEnd() throws IOException {
98  		final int[] flushCnt = new int[1];
99  		final OutputStream mockout = new OutputStream() {
100 			@Override
101 			public void write(int arg0) throws IOException {
102 				rawOut.write(arg0);
103 			}
104 
105 			@Override
106 			public void flush() throws IOException {
107 				flushCnt[0]++;
108 			}
109 		};
110 
111 		new PacketLineOut(mockout).end();
112 		assertBuffer("0000");
113 		assertEquals(1, flushCnt[0]);
114 	}
115 
116 	@Test
117 	public void testWriteDelim() throws IOException {
118 		out.writeDelim();
119 		assertBuffer("0001");
120 	}
121 
122 	// writePacket
123 
124 	@Test
125 	public void testWritePacket1() throws IOException {
126 		out.writePacket(new byte[] { 'a' });
127 		assertBuffer("0005a");
128 	}
129 
130 	@Test
131 	public void testWritePacket2() throws IOException {
132 		out.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
133 		assertBuffer("0008abcd");
134 	}
135 
136 	@Test
137 	public void testWritePacket3() throws IOException {
138 		final int buflen = SideBandOutputStream.MAX_BUF - 5;
139 		final byte[] buf = new byte[buflen];
140 		for (int i = 0; i < buf.length; i++) {
141 			buf[i] = (byte) i;
142 		}
143 		out.writePacket(buf);
144 		out.flush();
145 
146 		final byte[] act = rawOut.toByteArray();
147 		final String explen = Integer.toString(buf.length + 4, 16);
148 		assertEquals(4 + buf.length, act.length);
149 		assertEquals(new String(act, 0, 4, "UTF-8"), explen);
150 		for (int i = 0, j = 4; i < buf.length; i++, j++) {
151 			assertEquals(buf[i], act[j]);
152 		}
153 	}
154 
155 	// flush
156 
157 	@Test
158 	public void testFlush() throws IOException {
159 		final int[] flushCnt = new int[1];
160 		final OutputStream mockout = new OutputStream() {
161 			@Override
162 			public void write(int arg0) throws IOException {
163 				fail("should not write");
164 			}
165 
166 			@Override
167 			public void flush() throws IOException {
168 				flushCnt[0]++;
169 			}
170 		};
171 
172 		new PacketLineOut(mockout).flush();
173 		assertEquals(1, flushCnt[0]);
174 	}
175 
176 	private void assertBuffer(String exp) {
177 		assertEquals(exp, new String(rawOut.toByteArray(),
178 				UTF_8));
179 	}
180 }