View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  
25  import org.junit.jupiter.api.Test;
26  
27  class DotTerminatedMessageReaderTest {
28  
29      private static final String CRLF = "\r\n";
30      private static final String DOT = ".";
31      private static final String EOM = CRLF + DOT + CRLF;
32      private DotTerminatedMessageReader reader;
33      private final StringBuilder str = new StringBuilder();
34      private final char[] buf = new char[64];
35  
36      @Test
37      void testDoubleCrBeforeDot() throws IOException {
38          final String test = "Hello World!\r" + EOM;
39          reader = new DotTerminatedMessageReader(new StringReader(test));
40  
41          int read = 0;
42          while ((read = reader.read(buf)) != -1) {
43              str.append(buf, 0, read);
44          }
45  
46          assertEquals("Hello World!\r" + CRLF, str.toString());
47      }
48  
49      @Test
50      void testEmbeddedDot1() throws IOException {
51          final String test = "Hello . World!" + EOM;
52          reader = new DotTerminatedMessageReader(new StringReader(test));
53  
54          int read = 0;
55          while ((read = reader.read(buf)) != -1) {
56              str.append(buf, 0, read);
57          }
58  
59          assertEquals("Hello . World!" + CRLF, str.toString());
60      }
61  
62      @Test
63      void testEmbeddedDot2() throws IOException {
64          final String test = "Hello .. World!" + EOM;
65          reader = new DotTerminatedMessageReader(new StringReader(test));
66  
67          int read = 0;
68          while ((read = reader.read(buf)) != -1) {
69              str.append(buf, 0, read);
70          }
71  
72          assertEquals("Hello .. World!" + CRLF, str.toString());
73      }
74  
75      @Test
76      void testEmbeddedDot3() throws IOException {
77          final String test = "Hello World." + CRLF + "more" + EOM;
78          reader = new DotTerminatedMessageReader(new StringReader(test));
79  
80          int read = 0;
81          while ((read = reader.read(buf)) != -1) {
82              str.append(buf, 0, read);
83          }
84  
85          assertEquals("Hello World." + CRLF + "more" + CRLF, str.toString());
86      }
87  
88      @Test
89      void testEmbeddedDot4() throws IOException {
90          final String test = "Hello World\r.\nmore" + EOM;
91          reader = new DotTerminatedMessageReader(new StringReader(test));
92  
93          int read = 0;
94          while ((read = reader.read(buf)) != -1) {
95              str.append(buf, 0, read);
96          }
97  
98          assertEquals("Hello World\r.\nmore" + CRLF, str.toString());
99      }
100 
101     @Test
102     void testEmbeddedNewlines() throws IOException {
103         final String test = "Hello" + CRLF + "World\nA\rB" + EOM;
104         reader = new DotTerminatedMessageReader(new StringReader(test));
105 
106         int read = 0;
107         while ((read = reader.read(buf)) != -1) {
108             str.append(buf, 0, read);
109         }
110 
111         assertEquals("Hello" + CRLF + "World\nA\rB" + CRLF, str.toString());
112     }
113 
114     @Test
115     void testLeadingDot() throws IOException {
116         final String test = "Hello World!" + CRLF + "..text" + EOM;
117         reader = new DotTerminatedMessageReader(new StringReader(test));
118 
119         int read = 0;
120         while ((read = reader.read(buf)) != -1) {
121             str.append(buf, 0, read);
122         }
123 
124         assertEquals("Hello World!" + CRLF + ".text" + CRLF, str.toString());
125     }
126 
127     @Test
128     void testReadLine1() throws Exception {
129         final String test = "Hello World" + CRLF + "more" + EOM;
130         reader = new DotTerminatedMessageReader(new StringReader(test));
131 
132         String line;
133         while ((line = reader.readLine()) != null) {
134             str.append(line);
135             str.append("#");
136         }
137 
138         assertEquals("Hello World#more#", str.toString());
139 
140     }
141 
142     @Test
143     void testReadLine2() throws Exception {
144         final String test = "Hello World\r.\nmore" + EOM;
145         reader = new DotTerminatedMessageReader(new StringReader(test));
146 
147         String line;
148         while ((line = reader.readLine()) != null) {
149             str.append(line);
150             str.append("#");
151         }
152 
153         assertEquals("Hello World\r.\nmore#", str.toString());
154 
155     }
156 
157     @Test
158     void testReadSimpleStringCrLfLineEnding() throws IOException {
159         final String test = "Hello World!" + EOM;
160         reader = new DotTerminatedMessageReader(new StringReader(test));
161 
162         int read = 0;
163         while ((read = reader.read(buf)) != -1) {
164             str.append(buf, 0, read);
165         }
166 
167         assertEquals("Hello World!" + CRLF, str.toString());
168     }
169 
170     @Test
171     void testReadSimpleStringLfLineEnding() throws IOException {
172         final String test = "Hello World!" + EOM;
173         reader = new DotTerminatedMessageReader(new StringReader(test));
174 
175         int read = 0;
176         while ((read = reader.read(buf)) != -1) {
177             str.append(buf, 0, read);
178         }
179 
180         assertEquals("Hello World!" + CRLF, str.toString());
181     }
182 
183     @Test
184     void testSingleDotWithTrailingText() throws IOException {
185         final String test = "Hello World!" + CRLF + ".text" + EOM;
186         reader = new DotTerminatedMessageReader(new StringReader(test));
187 
188         int read = 0;
189         while ((read = reader.read(buf)) != -1) {
190             str.append(buf, 0, read);
191         }
192 
193         assertEquals("Hello World!" + CRLF + ".text" + CRLF, str.toString());
194     }
195 
196 }