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 
17 package com.android.server.wifi.hotspot2.anqp;
18 
19 import android.net.wifi.EAPConstants;
20 
21 import com.android.server.wifi.hotspot2.anqp.eap.AuthParam;
22 import com.android.server.wifi.hotspot2.anqp.eap.CredentialType;
23 import com.android.server.wifi.hotspot2.anqp.eap.EAPMethod;
24 
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 
36 /**
37  * Utility class containing test data for NAI Realm Data.
38  */
39 public class NAIRealmDataTestUtil {
40     /**
41      * Raw bytes for EAP Method.
42      */
43     private static final byte[] TEST_EAP_METHOD_BYTES =
44             new byte[] {0x05 /* length */, 0x0D /* EAP_TLS */, 0x01 /* Auth Param Count */,
45                         0x05 /* CredentialType */, 0x01, 0x02 /* USIM */};
46 
47     /**
48      * NAI Realm strings.
49      */
50     private static final String[] TEST_REALMS = new String[] {"test1", "test2"};
51 
52     /**
53      * Setup expected EAPMethod list.
54      */
55     private static final Map<Integer, Set<AuthParam>> TEST_EAP_METHOD_AUTH_PARAMS =
56             new HashMap<>();
57     private static final Set<AuthParam> TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS = new HashSet<>();
58     private static final List<EAPMethod> TEST_EAP_METHOD_LIST = new ArrayList<>();
59     static {
TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_USIM))60         TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS.add(new CredentialType(
61                 AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_USIM));
TEST_EAP_METHOD_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS)62         TEST_EAP_METHOD_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE,
63                 TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS);
64 
TEST_EAP_METHOD_LIST.add(new EAPMethod(EAPConstants.EAP_TLS, TEST_EAP_METHOD_AUTH_PARAMS))65         TEST_EAP_METHOD_LIST.add(new EAPMethod(EAPConstants.EAP_TLS, TEST_EAP_METHOD_AUTH_PARAMS));
66     }
67 
68     /**
69      * Setup expected NAIRealmData.
70      */
71     public static final NAIRealmData TEST_REALM_DATA =
72             new NAIRealmData(Arrays.asList(TEST_REALMS), TEST_EAP_METHOD_LIST);
73 
74     public static byte[] TEST_REAML_WITH_UTF8_DATA_BYTES = formatNAIRealmData(true);
75     public static byte[] TEST_REAML_WITH_NON_UTF8_DATA_BYTES = formatNAIRealmData(false);
76 
77     /**
78      * Helper function for returning raw bytes of NAI Realm Data (including the length field) for
79      * testing.
80      *
81      * @param utfEncoding Flag indicating the UTF encoding of the realm string
82      * @return byte[]
83      */
formatNAIRealmData(boolean utfEncoding)84     private static byte[] formatNAIRealmData(boolean utfEncoding) {
85         try {
86             byte[] realmData = getNAIRealmData(utfEncoding);
87             ByteArrayOutputStream stream = new ByteArrayOutputStream();
88             // Realm Data length in Little-Endian.
89             stream.write((byte) realmData.length);
90             stream.write((byte) realmData.length >> 8);
91             stream.write(realmData);
92             return stream.toByteArray();
93         } catch (IOException e) {
94             return null;
95         }
96     }
97 
98     /**
99      * Helper function for returning raw bytes of NAI Realm Data payload.
100      *
101      * @param utfEncoding Flag indicating the encoding of NAI Realm string
102      * @return byte[]
103      * @throws IOException
104      */
getNAIRealmData(boolean utfEncoding)105     private static byte[] getNAIRealmData(boolean utfEncoding) throws IOException {
106         String realmsStr = String.join(NAIRealmData.NAI_REALM_STRING_SEPARATOR, TEST_REALMS);
107         byte[] realmStrData = realmsStr.getBytes(
108                 utfEncoding ? StandardCharsets.UTF_8 : StandardCharsets.US_ASCII);
109 
110         ByteArrayOutputStream stream = new ByteArrayOutputStream();
111         // NAI Realm Encoding byte.
112         stream.write((byte) (utfEncoding ? NAIRealmData.NAI_ENCODING_UTF8_MASK : 0));
113         stream.write((byte) realmStrData.length);
114         stream.write(realmStrData);
115         stream.write((byte) 1);    // EAP Method count
116         stream.write(TEST_EAP_METHOD_BYTES);
117         return stream.toByteArray();
118     }
119 }
120