summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/enums/SecurityQueryIDTests.java
blob: 4f73f81392985ab06c2f5fdef345222597c61941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.smartdevicelink.test.protocol.enums;

import com.smartdevicelink.protocol.enums.SecurityQueryID;
import com.smartdevicelink.test.Validator;

import junit.framework.TestCase;

import java.util.Vector;

public class SecurityQueryIDTests extends TestCase {

    private Vector<SecurityQueryID> list = SecurityQueryID.getList();

    public void testValidEnums() {
        final byte[] SEND_HANDSHAKE_DATA_BYTES = {(byte) 0x00, (byte) 0x00, (byte) 0x01};
        final String SEND_HANDSHAKE_DATA_STRING = "SEND_HANDSHAKE_DATA";

        final byte[] SEND_INTERNAL_ERROR_BYTES = {(byte) 0x00, (byte) 0x00, (byte) 0x02};
        final String SEND_INTERNAL_ERROR_STRING = "SEND_INTERNAL_ERROR";

        final byte[] INVALID_QUERY_ID_BYTES = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
        final String INVALID_QUERY_ID_STRING = "INVALID_QUERY_ID";

        try {
            assertNotNull("QueryID list returned null", list);

            SecurityQueryID enumHandshakeData = (SecurityQueryID) SecurityQueryID.get(list, SEND_HANDSHAKE_DATA_BYTES);
            SecurityQueryID enumInternalError = (SecurityQueryID) SecurityQueryID.get(list, SEND_INTERNAL_ERROR_BYTES);
            SecurityQueryID enumInvalidSecurityQueryId = (SecurityQueryID) SecurityQueryID.get(list, INVALID_QUERY_ID_BYTES);

            assertNotNull("Send Handshake Data byte match returned null", enumHandshakeData);
            assertNotNull("Send Internal Error byte match returned null", enumInternalError);
            assertNotNull("Send Invalid QueryID byte match returned null", enumInvalidSecurityQueryId);

            enumHandshakeData = (SecurityQueryID) SecurityQueryID.get(list, SEND_HANDSHAKE_DATA_STRING);
            enumInternalError = (SecurityQueryID) SecurityQueryID.get(list, SEND_INTERNAL_ERROR_STRING);
            enumInvalidSecurityQueryId = (SecurityQueryID) SecurityQueryID.get(list, INVALID_QUERY_ID_STRING);

            assertNotNull("Send Handshake Data string match returned null", enumHandshakeData);
            assertNotNull("Send Internal Error string match returned null", enumInternalError);
            assertNotNull("Send Invalid QueryID string match returned null", enumInvalidSecurityQueryId);
        } catch(NullPointerException exception) {
            fail("Null enum list throws NullPointerException.");
        }
    }

    public void testInvalidEnum() {

        final byte[] INVALID_BYTE_ARRAY = {(byte) 0xAB, (byte) 0xAB, (byte) 0xAB};
        final String INVALID_STRING = "Invalid";

        try {
            SecurityQueryID enumInvalid = (SecurityQueryID) SecurityQueryID.get(list, INVALID_BYTE_ARRAY);
            assertNull("Invalid byte[] match didn't return null", enumInvalid);

            enumInvalid = (SecurityQueryID) SecurityQueryID.get(list, INVALID_STRING);
            assertNull("Invalid string match didn't return null", enumInvalid);
        } catch (IllegalArgumentException exception) {
            fail("Invalid enum throws IllegalArgumentException.");
        }
    }

    public void testNullEnum() {
        try {
            SecurityQueryID enumNull = (SecurityQueryID) SecurityQueryID.get(list, (String) null);
            assertNull("Null lookup returns a null string value", enumNull);

            enumNull = (SecurityQueryID) SecurityQueryID.get(list, (byte[]) null);
            assertNull("Null lookup returns a null byte[] value", enumNull);
        }catch (NullPointerException exception) {
            fail("Null string throws NullPointerException.");
        }
    }

    public void testListEnum() {
        Vector<SecurityQueryID> enumTestList = new Vector<>();
        enumTestList.add(SecurityQueryID.SEND_HANDSHAKE_DATA);
        enumTestList.add(SecurityQueryID.SEND_INTERNAL_ERROR);
        enumTestList.add(SecurityQueryID.INVALID_QUERY_ID);

        assertTrue("List does not match enum test list.",
                list.containsAll(enumTestList) &&
                enumTestList.containsAll(list));

        SecurityQueryID[] enumValueArray = SecurityQueryID.values();
        SecurityQueryID[] enumTestArray = {SecurityQueryID.SEND_HANDSHAKE_DATA, SecurityQueryID.SEND_INTERNAL_ERROR, SecurityQueryID.INVALID_QUERY_ID};
        assertTrue("Array does not match enum values array.",
                Validator.validateQueryIDArray(enumValueArray, enumTestArray));
    }

}