1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin.utils;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import java.io.EOFException;
23import java.io.IOException;
24import java.io.LineNumberReader;
25import java.io.StringReader;
26
27@SmallTest
28public class Base64ReaderTests extends AndroidTestCase {
29    private static final String EMPTY_STRING = "";
30    private static final String INCOMPLETE_CHAR1 = "Q";
31    // Encode 'A'.
32    private static final String INCOMPLETE_CHAR2 = "QQ";
33    // Encode 'A', 'B'
34    private static final String INCOMPLETE_CHAR3 = "QUI";
35    // Encode 'A', 'B', 'C'
36    private static final String COMPLETE_CHAR4 = "QUJD";
37    private static final String ALL_BYTE_PATTERN =
38            "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj\n"
39            + "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH\n"
40            + "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr\n"
41            + "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
42            + "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz\n"
43            + "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX\n"
44            + "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7\n"
45            + "/P3+/w==";
46
47    public void test0CharInt8() {
48        final Base64Reader reader = new Base64Reader(
49                new LineNumberReader(new StringReader(EMPTY_STRING)));
50        try {
51            reader.readUint8();
52            fail("0 char");
53        } catch (final EOFException e) {
54            assertEquals("0 char", 0, reader.getByteCount());
55        } catch (final IOException e) {
56            fail("IOException: " + e);
57        }
58    }
59
60    public void test1CharInt8() {
61        final Base64Reader reader = new Base64Reader(
62                new LineNumberReader(new StringReader(INCOMPLETE_CHAR1)));
63        try {
64            reader.readUint8();
65            fail("1 char");
66        } catch (final EOFException e) {
67            assertEquals("1 char", 0, reader.getByteCount());
68        } catch (final IOException e) {
69            fail("IOException: " + e);
70        }
71    }
72
73    public void test2CharsInt8() {
74        final Base64Reader reader = new Base64Reader(
75                new LineNumberReader(new StringReader(INCOMPLETE_CHAR2)));
76        try {
77            final int v1 = reader.readUint8();
78            assertEquals("2 chars pos 0", 'A', v1);
79            reader.readUint8();
80            fail("2 chars");
81        } catch (final EOFException e) {
82            assertEquals("2 chars", 1, reader.getByteCount());
83        } catch (final IOException e) {
84            fail("IOException: " + e);
85        }
86    }
87
88    public void test3CharsInt8() {
89        final Base64Reader reader = new Base64Reader(
90                new LineNumberReader(new StringReader(INCOMPLETE_CHAR3)));
91        try {
92            final int v1 = reader.readUint8();
93            assertEquals("3 chars pos 0", 'A', v1);
94            final int v2 = reader.readUint8();
95            assertEquals("3 chars pos 1", 'B', v2);
96            reader.readUint8();
97            fail("3 chars");
98        } catch (final EOFException e) {
99            assertEquals("3 chars", 2, reader.getByteCount());
100        } catch (final IOException e) {
101            fail("IOException: " + e);
102        }
103    }
104
105    public void test4CharsInt8() {
106        final Base64Reader reader = new Base64Reader(
107                new LineNumberReader(new StringReader(COMPLETE_CHAR4)));
108        try {
109            final int v1 = reader.readUint8();
110            assertEquals("4 chars pos 0", 'A', v1);
111            final int v2 = reader.readUint8();
112            assertEquals("4 chars pos 1", 'B', v2);
113            final int v3 = reader.readUint8();
114            assertEquals("4 chars pos 2", 'C', v3);
115            reader.readUint8();
116            fail("4 chars");
117        } catch (final EOFException e) {
118            assertEquals("4 chars", 3, reader.getByteCount());
119        } catch (final IOException e) {
120            fail("IOException: " + e);
121        }
122    }
123
124    public void testAllBytePatternInt8() {
125        final Base64Reader reader = new Base64Reader(
126                new LineNumberReader(new StringReader(ALL_BYTE_PATTERN)));
127        try {
128            for (int i = 0; i <= 0xff; i++) {
129                final int v = reader.readUint8();
130                assertEquals("value: all byte pattern: pos " + i, i, v);
131                assertEquals("count: all byte pattern: pos " + i, i + 1, reader.getByteCount());
132            }
133        } catch (final EOFException e) {
134            assertEquals("all byte pattern", 256, reader.getByteCount());
135        } catch (final IOException e) {
136            fail("IOException: " + e);
137        }
138    }
139
140    public void test0CharInt16() {
141        final Base64Reader reader = new Base64Reader(
142                new LineNumberReader(new StringReader(EMPTY_STRING)));
143        try {
144            reader.readInt16();
145            fail("0 char");
146        } catch (final EOFException e) {
147            assertEquals("0 char", 0, reader.getByteCount());
148        } catch (final IOException e) {
149            fail("IOException: " + e);
150        }
151    }
152
153    public void test1CharInt16() {
154        final Base64Reader reader = new Base64Reader(
155                new LineNumberReader(new StringReader(INCOMPLETE_CHAR1)));
156        try {
157            reader.readInt16();
158            fail("1 char");
159        } catch (final EOFException e) {
160            assertEquals("1 char", 0, reader.getByteCount());
161        } catch (final IOException e) {
162            fail("IOException: " + e);
163        }
164    }
165
166    public void test2CharsInt16() {
167        final Base64Reader reader = new Base64Reader(
168                new LineNumberReader(new StringReader(INCOMPLETE_CHAR2)));
169        try {
170            reader.readInt16();
171            fail("2 chars");
172        } catch (final EOFException e) {
173            assertEquals("2 chars", 1, reader.getByteCount());
174        } catch (final IOException e) {
175            fail("IOException: " + e);
176        }
177    }
178
179    public void test3CharsInt16() {
180        final Base64Reader reader = new Base64Reader(
181                new LineNumberReader(new StringReader(INCOMPLETE_CHAR3)));
182        try {
183            final short v1 = reader.readInt16();
184            assertEquals("3 chars pos 0", 'A' << 8 | 'B', v1);
185            reader.readInt16();
186            fail("3 chars");
187        } catch (final EOFException e) {
188            assertEquals("3 chars", 2, reader.getByteCount());
189        } catch (final IOException e) {
190            fail("IOException: " + e);
191        }
192    }
193
194    public void test4CharsInt16() {
195        final Base64Reader reader = new Base64Reader(
196                new LineNumberReader(new StringReader(COMPLETE_CHAR4)));
197        try {
198            final short v1 = reader.readInt16();
199            assertEquals("4 chars pos 0", 'A' << 8 | 'B', v1);
200            reader.readInt16();
201            fail("4 chars");
202        } catch (final EOFException e) {
203            assertEquals("4 chars", 3, reader.getByteCount());
204        } catch (final IOException e) {
205            fail("IOException: " + e);
206        }
207    }
208
209    public void testAllBytePatternInt16() {
210        final Base64Reader reader = new Base64Reader(
211                new LineNumberReader(new StringReader(ALL_BYTE_PATTERN)));
212        try {
213            for (int i = 0; i <= 0xff; i += 2) {
214                final short v = reader.readInt16();
215                final short expected = (short)(i << 8 | (i + 1));
216                assertEquals("value: all byte pattern: pos " + i, expected, v);
217                assertEquals("count: all byte pattern: pos " + i, i + 2, reader.getByteCount());
218            }
219        } catch (final EOFException e) {
220            assertEquals("all byte pattern", 256, reader.getByteCount());
221        } catch (final IOException e) {
222            fail("IOException: " + e);
223        }
224    }
225}
226