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 org.junit.Assert.assertEquals;
47  import static org.junit.Assert.fail;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.IOException;
51  import java.io.OutputStream;
52  
53  import org.eclipse.jgit.lib.Constants;
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 	// writePacket
117 
118 	@Test
119 	public void testWritePacket1() throws IOException {
120 		out.writePacket(new byte[] { 'a' });
121 		assertBuffer("0005a");
122 	}
123 
124 	@Test
125 	public void testWritePacket2() throws IOException {
126 		out.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
127 		assertBuffer("0008abcd");
128 	}
129 
130 	@Test
131 	public void testWritePacket3() throws IOException {
132 		final int buflen = SideBandOutputStream.MAX_BUF - 5;
133 		final byte[] buf = new byte[buflen];
134 		for (int i = 0; i < buf.length; i++) {
135 			buf[i] = (byte) i;
136 		}
137 		out.writePacket(buf);
138 		out.flush();
139 
140 		final byte[] act = rawOut.toByteArray();
141 		final String explen = Integer.toString(buf.length + 4, 16);
142 		assertEquals(4 + buf.length, act.length);
143 		assertEquals(new String(act, 0, 4, "UTF-8"), explen);
144 		for (int i = 0, j = 4; i < buf.length; i++, j++) {
145 			assertEquals(buf[i], act[j]);
146 		}
147 	}
148 
149 	// flush
150 
151 	@Test
152 	public void testFlush() throws IOException {
153 		final int[] flushCnt = new int[1];
154 		final OutputStream mockout = new OutputStream() {
155 			@Override
156 			public void write(int arg0) throws IOException {
157 				fail("should not write");
158 			}
159 
160 			@Override
161 			public void flush() throws IOException {
162 				flushCnt[0]++;
163 			}
164 		};
165 
166 		new PacketLineOut(mockout).flush();
167 		assertEquals(1, flushCnt[0]);
168 	}
169 
170 	private void assertBuffer(final String exp) throws IOException {
171 		assertEquals(exp, new String(rawOut.toByteArray(),
172 				Constants.CHARACTER_ENCODING));
173 	}
174 }