Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | package com.android.tradefed.util; |
| 17 | |
| 18 | import com.android.tradefed.result.ByteArrayInputStreamSource; |
| 19 | import com.android.tradefed.result.InputStreamSource; |
| 20 | |
| 21 | import junit.framework.TestCase; |
| 22 | |
gopinath | a1ce4f1 | 2017-09-20 13:17:56 -0700 | [diff] [blame] | 23 | import java.io.BufferedReader; |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 24 | import java.io.ByteArrayInputStream; |
Guang Zhu | a2b015d | 2015-08-09 16:24:49 -0700 | [diff] [blame] | 25 | import java.io.ByteArrayOutputStream; |
xingdai | e89846a | 2015-04-15 12:10:02 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 27 | import java.io.InputStream; |
Guang Zhu | a2b015d | 2015-08-09 16:24:49 -0700 | [diff] [blame] | 28 | import java.io.OutputStreamWriter; |
| 29 | import java.io.Writer; |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 30 | |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 31 | /** Unit tests for the {@link com.android.tradefed.util.StreamUtil} utility class */ |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 32 | public class StreamUtilTest extends TestCase { |
| 33 | |
| 34 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 35 | * Verify that {@link com.android.tradefed.util.StreamUtil#getByteArrayListFromSource} works as |
| 36 | * expected. |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 37 | */ |
| 38 | public void testGetByteArrayListFromSource() throws Exception { |
| 39 | final String contents = "this is a string"; |
| 40 | final byte[] contentBytes = contents.getBytes(); |
jdesprez | cf701b2 | 2017-08-31 16:23:10 -0700 | [diff] [blame] | 41 | try (final InputStreamSource source = new ByteArrayInputStreamSource(contentBytes)) { |
| 42 | final InputStream stream = source.createInputStream(); |
| 43 | final ByteArrayList output = StreamUtil.getByteArrayListFromStream(stream); |
| 44 | final byte[] outputBytes = output.getContents(); |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 45 | |
jdesprez | cf701b2 | 2017-08-31 16:23:10 -0700 | [diff] [blame] | 46 | assertEquals(contentBytes.length, outputBytes.length); |
| 47 | for (int i = 0; i < contentBytes.length; ++i) { |
| 48 | assertEquals(contentBytes[i], outputBytes[i]); |
| 49 | } |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 54 | * Verify that {@link com.android.tradefed.util.StreamUtil#getByteArrayListFromStream} works as |
| 55 | * expected. |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 56 | */ |
| 57 | public void testGetByteArrayListFromStream() throws Exception { |
| 58 | final String contents = "this is a string"; |
| 59 | final byte[] contentBytes = contents.getBytes(); |
| 60 | final ByteArrayList output = StreamUtil.getByteArrayListFromStream( |
| 61 | new ByteArrayInputStream(contentBytes)); |
| 62 | final byte[] outputBytes = output.getContents(); |
| 63 | |
| 64 | assertEquals(contentBytes.length, outputBytes.length); |
| 65 | for (int i = 0; i < contentBytes.length; ++i) { |
| 66 | assertEquals(contentBytes[i], outputBytes[i]); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 71 | * Verify that {@link com.android.tradefed.util.StreamUtil#getStringFromSource} works as |
| 72 | * expected. |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 73 | */ |
| 74 | public void testGetStringFromSource() throws Exception { |
| 75 | final String contents = "this is a string"; |
jdesprez | cf701b2 | 2017-08-31 16:23:10 -0700 | [diff] [blame] | 76 | try (InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes())) { |
| 77 | final InputStream stream = source.createInputStream(); |
| 78 | final String output = StreamUtil.getStringFromStream(stream); |
| 79 | assertEquals(contents, output); |
| 80 | } |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 84 | * Verify that {@link com.android.tradefed.util.StreamUtil#getBufferedReaderFromStreamSrc} works |
| 85 | * as expected. |
gopinath | a1ce4f1 | 2017-09-20 13:17:56 -0700 | [diff] [blame] | 86 | */ |
| 87 | public void testGetBufferedReaderFromInputStream() throws Exception { |
| 88 | final String contents = "this is a string"; |
gopinath | f1ecfe6 | 2017-09-26 17:08:22 -0700 | [diff] [blame] | 89 | BufferedReader output = null; |
gopinath | a1ce4f1 | 2017-09-20 13:17:56 -0700 | [diff] [blame] | 90 | try (InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes())) { |
gopinath | f1ecfe6 | 2017-09-26 17:08:22 -0700 | [diff] [blame] | 91 | output = StreamUtil.getBufferedReaderFromStreamSrc(source); |
gopinath | a1ce4f1 | 2017-09-20 13:17:56 -0700 | [diff] [blame] | 92 | assertEquals(contents, output.readLine()); |
gopinath | f1ecfe6 | 2017-09-26 17:08:22 -0700 | [diff] [blame] | 93 | } finally { |
| 94 | if (null != output) { |
| 95 | output.close(); |
| 96 | } |
gopinath | a1ce4f1 | 2017-09-20 13:17:56 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 101 | * Verify that {@link com.android.tradefed.util.StreamUtil#countLinesFromSource} works as |
| 102 | * expected. |
Guang Zhu | 7ad6119 | 2016-12-15 17:37:04 -0800 | [diff] [blame] | 103 | */ |
| 104 | public void testCountLinesFromSource() throws Exception { |
| 105 | final String contents = "foo\nbar\n\foo\n"; |
| 106 | final InputStreamSource source = new ByteArrayInputStreamSource(contents.getBytes()); |
| 107 | assertEquals(3, StreamUtil.countLinesFromSource(source)); |
| 108 | } |
| 109 | |
| 110 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 111 | * Verify that {@link com.android.tradefed.util.StreamUtil#getStringFromStream} works as |
| 112 | * expected. |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 113 | */ |
| 114 | public void testGetStringFromStream() throws Exception { |
| 115 | final String contents = "this is a string"; |
| 116 | final String output = StreamUtil.getStringFromStream( |
| 117 | new ByteArrayInputStream(contents.getBytes())); |
| 118 | assertEquals(contents, output); |
| 119 | } |
xingdai | e89846a | 2015-04-15 12:10:02 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 122 | * Verify that {@link com.android.tradefed.util.StreamUtil#calculateMd5(InputStream)} works as |
| 123 | * expected. |
| 124 | * |
xingdai | e89846a | 2015-04-15 12:10:02 -0700 | [diff] [blame] | 125 | * @throws IOException |
| 126 | */ |
| 127 | public void testCalculateMd5() throws IOException { |
| 128 | final String source = "testtesttesttesttest"; |
| 129 | final String md5 = "f317f682fafe0309c6a423af0b4efa59"; |
| 130 | ByteArrayInputStream inputSource = new ByteArrayInputStream(source.getBytes()); |
| 131 | String actualMd5 = StreamUtil.calculateMd5(inputSource); |
| 132 | assertEquals(md5, actualMd5); |
| 133 | } |
Omari Stephens | 0857d93 | 2012-05-24 19:02:44 -0700 | [diff] [blame] | 134 | |
Xing Dai | 842a176 | 2019-01-02 12:13:10 -0800 | [diff] [blame^] | 135 | /** |
| 136 | * Verify that {@link com.android.tradefed.util.StreamUtil#calculateBase64Md5(InputStream)} |
| 137 | * works as expected. |
| 138 | * |
| 139 | * @throws IOException |
| 140 | */ |
| 141 | public void testCalculateBase64Md5() throws IOException { |
| 142 | final String source = "testtesttesttesttest"; |
| 143 | final String base64Md5 = "8xf2gvr+AwnGpCOvC076WQ=="; |
| 144 | ByteArrayInputStream inputSource = new ByteArrayInputStream(source.getBytes()); |
| 145 | String actualBase64Md5 = StreamUtil.calculateBase64Md5(inputSource); |
| 146 | assertEquals(base64Md5, actualBase64Md5); |
| 147 | } |
| 148 | |
Guang Zhu | a2b015d | 2015-08-09 16:24:49 -0700 | [diff] [blame] | 149 | public void testCopyStreams() throws Exception { |
| 150 | String text = getLargeText(); |
| 151 | ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes()); |
| 152 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 153 | StreamUtil.copyStreams(bais, baos); |
| 154 | bais.close(); |
| 155 | baos.close(); |
| 156 | assertEquals(text, baos.toString()); |
| 157 | } |
| 158 | |
| 159 | public void testCopyStreamToWriter() throws Exception { |
| 160 | String text = getLargeText(); |
| 161 | ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes()); |
| 162 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 163 | Writer writer = new OutputStreamWriter(baos); |
| 164 | StreamUtil.copyStreamToWriter(bais, writer); |
| 165 | bais.close(); |
| 166 | writer.close(); |
| 167 | baos.close(); |
| 168 | assertEquals(text, baos.toString()); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns a large chunk of text that's at least 16K in size |
Guang Zhu | a2b015d | 2015-08-09 16:24:49 -0700 | [diff] [blame] | 173 | */ |
| 174 | private String getLargeText() { |
| 175 | String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " |
| 176 | + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " |
| 177 | + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut " |
| 178 | + "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " |
| 179 | + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 180 | + "occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit " |
| 181 | + "anim id est laborum."; // 446 bytes |
| 182 | StringBuilder sb = new StringBuilder(); |
| 183 | for (int i = 0; i < 40; i ++) { |
| 184 | sb.append(text); |
| 185 | } |
| 186 | return sb.toString(); |
| 187 | } |
| 188 | } |