StringUtilTest.java revision e31290e1c2eebf70c5ab22091a9520c7a001161e
1/*
2 * Copyright (C) 2016 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.server.wifi.util;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import android.test.suitebuilder.annotation.SmallTest;
23
24import org.junit.Test;
25
26/**
27 * Unit tests for {@link com.android.server.wifi.util.StringUtil}.
28 */
29@SmallTest
30public class StringUtilTest {
31    static final byte ASCII_UNIT_SEPARATOR = 31;
32    static final byte ASCII_DEL = 127;
33
34    /** Verifies that isAsciiPrintable() does not crash when passed a null array. */
35    @Test
36    public void nullArrayDoesNotCauseCrash() {
37        assertTrue(StringUtil.isAsciiPrintable(null));
38    }
39
40    /** Verifies that isAsciiPrintable() considers an empty array to be printable. */
41    @Test
42    public void emptyArrayIsPrintable() {
43        assertTrue(StringUtil.isAsciiPrintable(new byte[]{}));
44    }
45
46    /** Verifies that isAsciiPrintable() considers a single printable byte to be printable. */
47    @Test
48    public void arrayWithSinglePrintableByteIsPrintable() {
49        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'a'}));
50    }
51
52    /**
53     * Verifies that isAsciiPrintable() considers an array of multiple printable bytes to be
54     * printable.
55     */
56    @Test
57    public void arrayWithMultiplePrintableBytesIsPrintable() {
58        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'a', 'b'}));
59    }
60
61    /**
62     * Verifies that isAsciiPrintable() considers bell, form feed, newline, horizontal tab,
63     * and vertical tab to be printable.
64     */
65    @Test
66    public void printableControlCharactersAreConsideredPrintable() {
67        assertTrue(StringUtil.isAsciiPrintable(new byte[]{0x07}));  // bell
68        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'\f'}));  // form feed
69        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'\n'}));
70        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'\t'}));
71        assertTrue(StringUtil.isAsciiPrintable(new byte[]{0x0b}));  // vertical tab
72    }
73
74    /** Verifies that isAsciiPrintable() considers a newline to be printable. */
75    @Test
76    public void arrayWithNewlineIsPrintable() {
77        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'\n'}));
78    }
79
80    /** Verifies that isAsciiPrintable() considers a space to be printable. */
81    @Test
82    public void arrayWithSpaceIsPrintable() {
83        assertTrue(StringUtil.isAsciiPrintable(new byte[]{' '}));
84    }
85
86    /** Verifies that isAsciiPrintable() considers a tilde to be printable. */
87    @Test
88    public void arrayWithTildeIsPrintable() {
89        assertTrue(StringUtil.isAsciiPrintable(new byte[]{'~'}));
90    }
91
92    /** Verifies that isAsciiPrintable() considers a null to be unprintable. */
93    @Test
94    public void arrayWithNullByteIsNotPrintable() {
95        assertFalse(StringUtil.isAsciiPrintable(new byte[]{0}));
96    }
97
98    /** Verifies that isAsciiPrintable() considers (space-1) to be unprintable. */
99    @Test
100    public void arrayWithUnitSeparatorIsNotPrintable() {
101        assertFalse(StringUtil.isAsciiPrintable(new byte[]{ASCII_UNIT_SEPARATOR}));
102    }
103
104    /** Verifies that isAsciiPrintable() considers (tilde+1) to be unprintable. */
105    @Test
106    public void arrayWithDelIsNotPrintable() {
107        assertFalse(StringUtil.isAsciiPrintable(new byte[]{ASCII_DEL}));
108    }
109
110    /**
111     * Verifies that isAsciiPrintable() considers negative bytes to be unprintable.
112     * (In unsigned representation, these are values greater than DEL.)
113     */
114    @Test
115    public void arrayWithNegativeByteIsNotPrintable() {
116        assertFalse(StringUtil.isAsciiPrintable(new byte[]{-128}));
117        assertFalse(StringUtil.isAsciiPrintable(new byte[]{-1}));
118    }
119}
120