|
| 1 | +package com.smartdevicelink.test.protocol; |
| 2 | + |
| 3 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 4 | + |
| 5 | +import com.smartdevicelink.protocol.SecurityQueryPayload; |
| 6 | +import com.smartdevicelink.protocol.enums.SecurityQueryID; |
| 7 | +import com.smartdevicelink.protocol.enums.SecurityQueryType; |
| 8 | +import com.smartdevicelink.util.BitConverter; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotEquals; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertNull; |
| 17 | +import static org.junit.Assert.fail; |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4.class) |
| 20 | +public class SecurityQueryPayloadTests { |
| 21 | + |
| 22 | + public static SecurityQueryPayload createDummyBqh() { |
| 23 | + SecurityQueryPayload bqh = new SecurityQueryPayload(); |
| 24 | + bqh.setCorrelationID(123); |
| 25 | + bqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); |
| 26 | + bqh.setQueryType(SecurityQueryType.REQUEST); |
| 27 | + bqh.setBulkData(null); |
| 28 | + bqh.setJsonSize(0); |
| 29 | + return bqh; |
| 30 | + } |
| 31 | + |
| 32 | + public SecurityQueryPayload safeParse(byte[] array) { |
| 33 | + try { |
| 34 | + return SecurityQueryPayload.parseBinaryQueryHeader(array); |
| 35 | + } catch (Exception e) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testCorrectParsing() { |
| 42 | + byte[] array = new byte[12]; |
| 43 | + array[0] = 0; |
| 44 | + array[1] = 0; |
| 45 | + array[2] = 0; |
| 46 | + array[3] = 2; |
| 47 | + array[4] = 0; |
| 48 | + array[5] = 0; |
| 49 | + array[6] = 0; |
| 50 | + array[7] = 3; |
| 51 | + array[8] = 0; |
| 52 | + array[9] = 0; |
| 53 | + array[10] = 0; |
| 54 | + array[11] = 0; |
| 55 | + |
| 56 | + SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(array); |
| 57 | + assertEquals(parsedBqh.getQueryType(), SecurityQueryType.REQUEST); |
| 58 | + assertEquals(parsedBqh.getQueryID(), SecurityQueryID.SEND_INTERNAL_ERROR); |
| 59 | + assertEquals(parsedBqh.getCorrelationID(), 3); |
| 60 | + assertEquals(parsedBqh.getJsonSize(), 0); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testCorrectHeaderAssembly() { |
| 65 | + SecurityQueryPayload dummyBqh = new SecurityQueryPayload(); |
| 66 | + dummyBqh.setQueryType(SecurityQueryType.REQUEST); |
| 67 | + dummyBqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); |
| 68 | + dummyBqh.setCorrelationID(3); |
| 69 | + dummyBqh.setJsonSize(0); |
| 70 | + |
| 71 | + byte[] assembledHeader = dummyBqh.assembleHeaderBytes(); |
| 72 | + assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0])); |
| 73 | + byte[] queryIDFromHeader = new byte[3]; |
| 74 | + System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3); |
| 75 | + assertEquals(dummyBqh.getQueryID(), SecurityQueryID.valueOf(queryIDFromHeader)); |
| 76 | + assertEquals(dummyBqh.getCorrelationID(), BitConverter.intFromByteArray(assembledHeader, 4)); |
| 77 | + assertEquals(dummyBqh.getJsonSize(), BitConverter.intFromByteArray(assembledHeader, 8)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testAssemblyAndParse() { |
| 82 | + SecurityQueryPayload bqh = createDummyBqh(); |
| 83 | + |
| 84 | + byte[] bqhBytes = bqh.assembleHeaderBytes(); |
| 85 | + assertNotNull(bqhBytes); |
| 86 | + |
| 87 | + SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); |
| 88 | + assertNotNull(parsedBqh); |
| 89 | + |
| 90 | + assertEquals(bqh.getCorrelationID(), parsedBqh.getCorrelationID()); |
| 91 | + assertEquals(bqh.getQueryID(), parsedBqh.getQueryID()); |
| 92 | + assertEquals(bqh.getQueryType(), parsedBqh.getQueryType()); |
| 93 | + assertEquals(bqh.getBulkData(), parsedBqh.getBulkData()); |
| 94 | + assertEquals(bqh.getJsonData(), parsedBqh.getJsonData()); |
| 95 | + assertEquals(bqh.getJsonSize(), parsedBqh.getJsonSize()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testCorruptHeader() { |
| 100 | + SecurityQueryPayload bqh = createDummyBqh(); |
| 101 | + |
| 102 | + byte[] bqhBytes = bqh.assembleHeaderBytes(); |
| 103 | + |
| 104 | + assertNotNull(safeParse(bqhBytes)); |
| 105 | + |
| 106 | + int size = bqhBytes.length; |
| 107 | + for (int i = 0; i < size; i++) { |
| 108 | + bqhBytes[i] = (byte) 0x99; |
| 109 | + } |
| 110 | + |
| 111 | + assertNull(safeParse(bqhBytes)); |
| 112 | + SecurityQueryPayload head = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); |
| 113 | + assertNull(head); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testJsonSetException() { |
| 118 | + try { |
| 119 | + SecurityQueryPayload bqh = createDummyBqh(); |
| 120 | + bqh.setJsonData(null); |
| 121 | + fail("Setting JSON data to null should have thrown an exception"); |
| 122 | + } catch (Exception e) { |
| 123 | + //Pass |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments