You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PacketLineOutTest.java 4.8KB

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