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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2009-2010, 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. package org.eclipse.jgit.transport;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.fail;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.io.OutputStream;
  17. import org.junit.Before;
  18. import org.junit.Test;
  19. // Note, test vectors created with:
  20. //
  21. // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
  22. public class PacketLineOutTest {
  23. private ByteArrayOutputStream rawOut;
  24. private PacketLineOut out;
  25. @Before
  26. public void setUp() throws Exception {
  27. rawOut = new ByteArrayOutputStream();
  28. out = new PacketLineOut(rawOut);
  29. }
  30. // writeString
  31. @Test
  32. public void testWriteString1() throws IOException {
  33. out.writeString("a");
  34. out.writeString("bc");
  35. assertBuffer("0005a0006bc");
  36. }
  37. @Test
  38. public void testWriteString2() throws IOException {
  39. out.writeString("a\n");
  40. out.writeString("bc\n");
  41. assertBuffer("0006a\n0007bc\n");
  42. }
  43. @Test
  44. public void testWriteString3() throws IOException {
  45. out.writeString("");
  46. assertBuffer("0004");
  47. }
  48. // end
  49. @Test
  50. public void testWriteEnd() throws IOException {
  51. final int[] flushCnt = new int[1];
  52. final OutputStream mockout = new OutputStream() {
  53. @Override
  54. public void write(int arg0) throws IOException {
  55. rawOut.write(arg0);
  56. }
  57. @Override
  58. public void flush() throws IOException {
  59. flushCnt[0]++;
  60. }
  61. };
  62. new PacketLineOut(mockout).end();
  63. assertBuffer("0000");
  64. assertEquals(1, flushCnt[0]);
  65. }
  66. @Test
  67. public void testWriteDelim() throws IOException {
  68. out.writeDelim();
  69. assertBuffer("0001");
  70. }
  71. // writePacket
  72. @Test
  73. public void testWritePacket1() throws IOException {
  74. out.writePacket(new byte[] { 'a' });
  75. assertBuffer("0005a");
  76. }
  77. @Test
  78. public void testWritePacket2() throws IOException {
  79. out.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
  80. assertBuffer("0008abcd");
  81. }
  82. @Test
  83. public void testWritePacket3() throws IOException {
  84. final int buflen = SideBandOutputStream.MAX_BUF - 5;
  85. final byte[] buf = new byte[buflen];
  86. for (int i = 0; i < buf.length; i++) {
  87. buf[i] = (byte) i;
  88. }
  89. out.writePacket(buf);
  90. out.flush();
  91. final byte[] act = rawOut.toByteArray();
  92. final String explen = Integer.toString(buf.length + 4, 16);
  93. assertEquals(4 + buf.length, act.length);
  94. assertEquals(new String(act, 0, 4, "UTF-8"), explen);
  95. for (int i = 0, j = 4; i < buf.length; i++, j++) {
  96. assertEquals(buf[i], act[j]);
  97. }
  98. }
  99. // flush
  100. @Test
  101. public void testFlush() throws IOException {
  102. final int[] flushCnt = new int[1];
  103. final OutputStream mockout = new OutputStream() {
  104. @Override
  105. public void write(int arg0) throws IOException {
  106. fail("should not write");
  107. }
  108. @Override
  109. public void flush() throws IOException {
  110. flushCnt[0]++;
  111. }
  112. };
  113. new PacketLineOut(mockout).flush();
  114. assertEquals(1, flushCnt[0]);
  115. }
  116. private void assertBuffer(String exp) {
  117. assertEquals(exp, new String(rawOut.toByteArray(),
  118. UTF_8));
  119. }
  120. }