• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2010 The Android Open Source Project
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package com.android.exchange.utility;
17 
18 import com.android.email.Utility;
19 
20 import junit.framework.TestCase;
21 
22 /**
23  * Test for {@link SimpleIcsWriter}.
24  * You can run this entire test case with:
25  *   runtest -c com.android.exchange.utility.SimpleIcsWriterTests email
26  */
27 public class SimpleIcsWriterTests extends TestCase {
28     private static final String CRLF = "\r\n";
29     private static final String UTF8_1_BYTE = "a";
30     private static final String UTF8_2_BYTES = "\u00A2";
31     private static final String UTF8_3_BYTES = "\u20AC";
32     private static final String UTF8_4_BYTES = "\uD852\uDF62";
33 
34     /**
35      * Test for {@link SimpleIcsWriter#writeTag}.  It also covers {@link SimpleIcsWriter#getBytes()}
36      * and {@link SimpleIcsWriter#escapeTextValue}.
37      */
testWriteTag()38     public void testWriteTag() {
39         final SimpleIcsWriter ics = new SimpleIcsWriter();
40         ics.writeTag("TAG1", null);
41         ics.writeTag("TAG2", "");
42         ics.writeTag("TAG3", "xyz");
43         ics.writeTag("SUMMARY", "TEST-TEST,;\r\n\\TEST");
44         ics.writeTag("SUMMARY2", "TEST-TEST,;\r\n\\TEST");
45         final String actual = Utility.fromUtf8(ics.getBytes());
46 
47         assertEquals(
48                 "TAG3:xyz" + CRLF +
49                 "SUMMARY:TEST-TEST\\,\\;\\n\\\\TEST" + CRLF + // escaped
50                 "SUMMARY2:TEST-TEST,;\r\n\\TEST" + CRLF // not escaped
51                 , actual);
52     }
53 
54     /**
55      * Verify that: We're folding lines correctly, and we're not splitting up a UTF-8 character.
56      */
testWriteLine()57     public void testWriteLine() {
58         for (String last : new String[] {UTF8_1_BYTE, UTF8_2_BYTES, UTF8_3_BYTES, UTF8_4_BYTES}) {
59             for (int i = 70; i < 160; i++) {
60                 String input = stringOfLength(i) + last;
61                 checkWriteLine(input);
62             }
63         }
64     }
65 
66     /**
67      * @return a String of {@code length} bytes in UTF-8.
68      */
stringOfLength(int length)69     private static String stringOfLength(int length) {
70         StringBuilder sb = new StringBuilder();
71         for (int i = 0; i < length; i++) {
72             sb.append('0' +(i % 10));
73         }
74         return sb.toString();
75     }
76 
checkWriteLine(String input)77     private void checkWriteLine(String input) {
78         final SimpleIcsWriter ics = new SimpleIcsWriter();
79         ics.writeLine(input);
80         final byte[] bytes = ics.getBytes();
81 
82         // Verify that no lines are longer than 75 bytes.
83         int numBytes = 0;
84         for (byte b : bytes) {
85             if (b == '\r') {
86                 continue; // ignore
87             }
88             if (b == '\n') {
89                 assertTrue("input=" + input, numBytes <= 75);
90                 numBytes = 0;
91                 continue;
92             }
93             numBytes++;
94         }
95         assertTrue("input=" + input, numBytes <= 75);
96 
97         // If we're splitting up a UTF-8 character, fromUtf8() won't restore it correctly.
98         // If it becomes the same as input, we're doing the right thing.
99         final String actual = Utility.fromUtf8(bytes);
100         final String unfolded = actual.replace("\r\n\t", "");
101         assertEquals("input=" + input, input + "\r\n", unfolded);
102     }
103 
testQuoteParamValue()104     public void testQuoteParamValue() {
105         assertNull(SimpleIcsWriter.quoteParamValue(null));
106         assertEquals("\"\"", SimpleIcsWriter.quoteParamValue(""));
107         assertEquals("\"a\"", SimpleIcsWriter.quoteParamValue("a"));
108         assertEquals("\"''\"", SimpleIcsWriter.quoteParamValue("\"'"));
109         assertEquals("\"abc\"", SimpleIcsWriter.quoteParamValue("abc"));
110         assertEquals("\"a'b'c\"", SimpleIcsWriter.quoteParamValue("a\"b\"c"));
111     }
112 }
113