1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.wss4j.common.attachment;
20
21 import org.apache.wss4j.common.util.CRLFOutputStream;
22 import org.junit.jupiter.api.Test;
23
24 import java.io.ByteArrayOutputStream;
25 import java.util.Random;
26
27 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28
29 public class CRLFOutputStreamTest {
30
31 @Test
32 public void testBytePerByte() throws Exception {
33 ByteArrayOutputStream baos = new ByteArrayOutputStream();
34 CRLFOutputStream crlfOutputStream = new CRLFOutputStream(baos);
35 crlfOutputStream.write('\n');
36 crlfOutputStream.write('\r');
37 crlfOutputStream.write('\r');
38 crlfOutputStream.write('\n');
39 crlfOutputStream.write('\n');
40 crlfOutputStream.write('\n');
41 crlfOutputStream.write('\r');
42 crlfOutputStream.write('\r');
43 crlfOutputStream.write('\r');
44 crlfOutputStream.write('a');
45 crlfOutputStream.write('\n');
46 crlfOutputStream.write('\r');
47 crlfOutputStream.write('\n');
48 crlfOutputStream.write('a');
49 crlfOutputStream.write('a');
50 crlfOutputStream.write('a');
51 crlfOutputStream.close();
52 assertArrayEquals("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\na\r\n\r\naaa".getBytes(), baos.toByteArray());
53 }
54
55 @Test
56 public void testBytes() throws Exception {
57 ByteArrayOutputStream baos = new ByteArrayOutputStream();
58 CRLFOutputStream crlfOutputStream = new CRLFOutputStream(baos);
59 crlfOutputStream.write("\n\r\r\n\n\n\r\r\ra\n\r\naaa".getBytes());
60 crlfOutputStream.close();
61 assertArrayEquals("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\na\r\n\r\naaa".getBytes(), baos.toByteArray());
62 }
63
64 @Test
65 public void testBytes1() throws Exception {
66 ByteArrayOutputStream baos = new ByteArrayOutputStream();
67 CRLFOutputStream crlfOutputStream = new CRLFOutputStream(baos);
68 crlfOutputStream.write("aaaaaaaaaa".getBytes());
69 crlfOutputStream.close();
70 assertArrayEquals("aaaaaaaaaa".getBytes(), baos.toByteArray());
71 }
72
73 @Test
74 public void testRandom() throws Exception {
75 byte[] pool = new byte[] {'\r', '\n', 'a'};
76 Random random = new Random();
77
78 ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 CRLFOutputStream crlfOutputStream = new CRLFOutputStream(baos);
80
81 ByteArrayOutputStream testString = new ByteArrayOutputStream();
82
83 for (int h = 0; h < 10000; h++) {
84 if (random.nextBoolean()) {
85 byte b = pool[random.nextInt(pool.length)];
86 testString.write(b);
87 crlfOutputStream.write(b);
88 } else {
89 int byteCount = random.nextInt(1000);
90 byte[] bytes = new byte[byteCount];
91 for (int i = 0; i < byteCount; i++) {
92 bytes[i] = pool[random.nextInt(pool.length)];
93 }
94 testString.write(bytes);
95 crlfOutputStream.write(bytes);
96 }
97 }
98
99 crlfOutputStream.close();
100 byte[] res = baos.toByteArray();
101 for (int i = 0; i < res.length; i++) {
102 byte re = res[i];
103 if (re == '\r') {
104 if (res[i + 1] != '\n') {
105 throw new Exception("Missing \\n in\n" + createEscapedString(res) + "\n input string: " + createEscapedString(testString.toByteArray()));
106 }
107 } else if (re == '\n' && res[i - 1] != '\r') {
108 throw new Exception("Missing \\r in\n" + createEscapedString(res) + "\n input string: " + createEscapedString(testString.toByteArray()));
109 }
110 }
111 }
112
113 private String createEscapedString(byte[] bytes) {
114 StringBuilder stringBuilder = new StringBuilder();
115 for (byte aByte : bytes) {
116 if (aByte == '\r') {
117 stringBuilder.append("\\r");
118 } else if (aByte == '\n') {
119 stringBuilder.append("\\n");
120 } else {
121 stringBuilder.append((char)aByte);
122 }
123 }
124 return stringBuilder.toString();
125 }
126 }