1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2007 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight Remote Protocol Library.
10   * 
11   * Insight Remote Protocol Library is free software: you can redistribute it 
12   * and/or modify it under the terms of the GNU General Public License as 
13   * published by the Free Software Foundation, either version 3 of the License, 
14   * or (at your option) any later version.
15   * 
16   * Insight Remote Protocol Library is distributed in the hope that it will be 
17   * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight Remote Protocol Library.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.remoteprotocol.util;
25  
26  import java.io.UnsupportedEncodingException;
27  import java.nio.charset.CharacterCodingException;
28  import java.util.Arrays;
29  import java.util.Random;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * Tests for the {@link ByteUtils} class
35   * 
36   * @see ByteUtils
37   * 
38   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
39   * @version $Revision: $ $Date: $
40   */
41  public class ByteUtilsTest extends TestCase {
42  	
43  	/**
44  	 * The test values for shorts
45  	 */
46  	private byte[] inputShortBytes = null;
47  	
48  	/**
49  	 * Test values for shorts
50  	 */
51  	private short[] inputShort = null;
52  	
53  	/**
54  	 * Test values for integers
55  	 */
56  	private byte[] inputIntBytes = null;
57  	
58  	/**
59  	 * Input values for integers
60  	 */
61  	private int[] inputInt = null;
62  	
63  	/**
64  	 * Input values for strings
65  	 */
66  	private String[] inputStrings = null;
67  
68  	/* (non-Javadoc)
69  	 * @see junit.framework.TestCase#setUp()
70  	 */
71  	protected void setUp() throws Exception {
72  		// The input bytes are organized as
73  		// 	- -32,768
74  		//  - -1
75  		//  - 0
76  		//  - 10
77  		//  - 32,767
78  		//  - 1004
79  		inputShortBytes = new byte[]{-128, 0, 
80  				-1, -1, 
81  				Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), 
82  				Byte.parseByte("00000000", 2), Byte.parseByte("00001010", 2),
83  				Byte.parseByte("01111111", 2), (byte)Short.parseShort("11111111", 2),
84  				Byte.parseByte("00000011", 2), (byte)Short.parseShort("11101100", 2)};
85  		
86  		inputShort = new short[] {-32768, -1, 0 , 10, 32767, 1004};
87  		
88  		// The input bytes are organized as
89  		// 	- -2 147 483 648
90  		//  - -1
91  		//  - 0
92  		//  - 10
93  		//  - 2147483647
94  		//  - 1004
95  		inputIntBytes = new byte[]{-128, 0, 0, 0,
96  				-1, -1, -1, -1,
97  				Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), 
98  				Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2), Byte.parseByte("00001010", 2),
99  				Byte.parseByte("01111111", 2), (byte)Short.parseShort("11111111", 2), (byte)Short.parseShort("11111111", 2), (byte)Short.parseShort("11111111", 2),
100 				Byte.parseByte("00000000", 2), Byte.parseByte("00000000", 2),  Byte.parseByte("00000011", 2), (byte)Short.parseShort("11101100", 2)};
101 		
102 		inputInt = new int[] {-2147483648, -1, 0, 10, 2147483647, 1004};
103 		
104 		// Input Strings
105 		inputStrings = new String[] {
106 				"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
107 				"Donec non sem non velit hendrerit faucibus.",
108 				"Phasellus convallis laoreet ligula.",
109 				"Sed volutpat auctor turpis",
110 				"Phasellus pharetra varius diam.",
111 				"Fusce a erat vel lorem luctus rutrum.",
112 				"Proin vitae dolor vel enim mattis lobortis.",
113 				"Donec eu turpis at mi elementum accumsan."};
114 	}
115 
116 	/* (non-Javadoc)
117 	 * @see junit.framework.TestCase#tearDown()
118 	 */
119 	protected void tearDown() throws Exception {
120 		super.tearDown();
121 	}
122 
123 	/**
124 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#readShortFromByteArray(byte[], int)}.
125 	 */
126 	public void testReadShortFromByteArray() {
127 		
128 		int offSet = 0;
129 		for (int i = 0; i < inputShort.length; i++) {
130 			short testNum = inputShort[i];
131 			assertEquals(testNum, ByteUtils.readShortFromByteArray(inputShortBytes, offSet));
132 			offSet += 2;
133 		}
134 	}
135 
136 	/**
137 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#readIntFromByteArray(byte[], int)}.
138 	 */
139 	public void testReadIntFromByteArray() {
140 		
141 		int offset = 0;
142 		for (int i = 0; i < inputInt.length; i++) {
143 			int testNum = inputInt[i];
144 			assertEquals(testNum, ByteUtils.readIntFromByteArray(inputIntBytes, offset));
145 			offset += 4;
146 		}
147 	}
148 
149 	/**
150 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#getByteArrayForShort(short)}.
151 	 */
152 	public void testGetByteArrayForShort() {
153 		
154 		int offset = 0;
155 		for (int i = 0; i < inputShort.length; i++) {
156 			short testNum = inputShort[i];
157 			assertTrue(Arrays.equals(ByteUtils.getByteArrayForShort(testNum), subArray(inputShortBytes, offset, 2)));
158 			offset += 2;
159 		}
160 	}
161 
162 	/**
163 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#putBytesInArrayForShort(short, byte[], int)}.
164 	 */
165 	public void testPutBytesInArrayForShort() {
166 		int offset = 0;
167 		Random random = new Random();
168 		int bufferLength = 20;
169 		for (int i = 0; i < inputShort.length; i++) {
170 			short testNum = inputShort[i];
171 			// Get a position between 0 and 17 to fill in the buffer
172 			int pos = random.nextInt(bufferLength - 2);
173 			byte[] outputBuf = new byte[bufferLength];
174 			random.nextBytes(outputBuf);
175 			byte[] testBuf = new byte[bufferLength];
176 			System.arraycopy(outputBuf, 0, testBuf, 0, testBuf.length);
177 			System.arraycopy(inputShortBytes, offset, testBuf, pos, 2);
178 			
179 			ByteUtils.putBytesInArrayForShort(testNum, outputBuf, pos);
180 			assertTrue(Arrays.equals(outputBuf, testBuf));
181 			offset += 2;
182 		}
183 	}
184 
185 	/**
186 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#getByteArrayForInt(int)}.
187 	 */
188 	public void testGetByteArrayForInt() {
189 		
190 		int offset = 0;
191 		for (int i = 0; i < inputInt.length; i++) {
192 			int testNum = inputInt[i];
193 			assertTrue(Arrays.equals(ByteUtils.getByteArrayForInt(testNum), subArray(inputIntBytes, offset, 4)));
194 			offset += 4;
195 		}
196 	}
197 
198 	/**
199 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#putBytesInArrayForInt(int, byte[], int)}.
200 	 */
201 	public void testPutBytesInArrayForInt() {
202 		int offset = 0;
203 		Random random = new Random();
204 		int bufferLength = 20;
205 		for (int i = 0; i < inputInt.length; i++) {
206 			int testNum = inputInt[i];
207 			// Get a position between 0 and 15 to fill in the buffer
208 			int pos = random.nextInt(bufferLength - 4);
209 			byte[] outputBuf = new byte[bufferLength];
210 			random.nextBytes(outputBuf);
211 			byte[] testBuf = new byte[bufferLength];
212 			System.arraycopy(outputBuf, 0, testBuf, 0, testBuf.length);
213 			System.arraycopy(inputIntBytes, offset, testBuf, pos, 4);
214 			
215 			ByteUtils.putBytesInArrayForInt(testNum, outputBuf, pos);
216 			assertTrue(Arrays.equals(outputBuf, testBuf));
217 			offset += 4;
218 		}
219 	}
220 
221 	/**
222 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#getByteArrayForString(java.lang.String)}.
223 	 */
224 	public void testGetByteArrayForString() {
225 		for (int i = 0; i < inputStrings.length; i++) {
226 			String inputString = inputStrings[i];
227 			try {
228 				assertTrue(Arrays.equals(ByteUtils.getByteArrayForString(inputString), inputString.getBytes("UTF-8")));
229 			} catch (CharacterCodingException e) {
230 				fail("Exception encoding characters");
231 			} catch (UnsupportedEncodingException e) {
232 				fail("Platform does not support encoding, test will fail");
233 			}
234 		}
235 	}
236 
237 	/**
238 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#getByteArrayForCharacterArray(char[])}.
239 	 */
240 	public void testGetByteArrayForCharacterArray() {
241 		for (int i = 0; i < inputStrings.length; i++) {
242 			String inputString = inputStrings[i];
243 			try {
244 				assertTrue(Arrays.equals(ByteUtils.getByteArrayForCharacterArray(inputString.toCharArray()), inputString.getBytes("UTF-8")));
245 			} catch (CharacterCodingException e) {
246 				fail("Exception encoding characters");
247 			} catch (UnsupportedEncodingException e) {
248 				fail("Platform does not support encoding, test will fail");
249 			}
250 		}
251 	}
252 	
253 	/**
254 	 * Test method for {@link com.mindtree.techworks.insight.remoteprotocol.util.ByteUtils#readStringFromByteArray(byte[], int, int)}
255 	 */
256 	public void testReadStringFromByteArray () {
257 		for (int i = 0; i < inputStrings.length; i++) {
258 			String inputString = inputStrings[i];
259 			byte[] encodedString = null;
260 			try {
261 				encodedString = inputString.getBytes("UTF-8");
262 			} catch (UnsupportedEncodingException e) {
263 				fail("Platform does not support encoding, test will fail");
264 			}
265 			try {
266 				assertEquals(inputString, ByteUtils.readStringFromByteArray(encodedString, 0, encodedString.length));
267 			} catch (CharacterCodingException e) {
268 				fail("Exception encoding characters");
269 			}
270 		}
271 	}
272 
273 	/**
274 	 * Creates an array of bytes using the range provided
275 	 * @param inputArray The array to subset
276 	 * @param offset The start point of the range
277 	 * @param length The length of the range
278 	 * @return The subset of the array
279 	 */
280 	private byte[] subArray (byte[] inputArray, int offset, int length) {
281 		if (offset < 0) {
282 			throw new ArrayIndexOutOfBoundsException("Offset cannot be less than 0");
283 		}
284 		if (offset > (inputArray.length + 1) || (offset + length) > (inputArray.length)) {
285 			throw new ArrayIndexOutOfBoundsException("Range should be withing the input array");
286 		}
287 		
288 		byte[] returnArray = new byte[length];
289 		System.arraycopy(inputArray, offset, returnArray, 0, length);
290 		
291 		return returnArray;
292 	}
293 }