1 /*
2  * Copyright (C) 2019 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;
18 
19 import static org.junit.Assert.*;
20 import static org.mockito.Mockito.*;
21 
22 import android.util.Xml;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.internal.util.FastXmlSerializer;
27 import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlSerializer;
33 
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.nio.charset.StandardCharsets;
37 import java.util.HashMap;
38 import java.util.Map;
39 
40 /**
41  * Unit tests for {@link com.android.server.wifi.RandomizedMacStoreData}.
42  */
43 @SmallTest
44 public class RandomizedMacStoreDataTest extends WifiBaseTest {
45     private static final String TEST_MAC_ADDRESS_1 = "da:a1:19:0:0:0";
46     private static final String TEST_MAC_ADDRESS_2 = "ff:ff:ff:0:0:0";
47     private static final String TEST_CONFIG_KEY_1 = "TP-LINK_B6C1_5GWPA_PSK";
48     private static final String TEST_CONFIG_KEY_2 = "GoogleGuest-LegacyNONE";
49     private RandomizedMacStoreData mRandomizedMacStoreData;
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         mRandomizedMacStoreData = new RandomizedMacStoreData();
54     }
55 
56     /**
57      * Helper function for serializing data to a XML block.
58      *
59      * @return byte[] of the XML data
60      * @throws Exception
61      */
serializeData()62     private byte[] serializeData() throws Exception {
63         final XmlSerializer out = new FastXmlSerializer();
64         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
65         out.setOutput(outputStream, StandardCharsets.UTF_8.name());
66         mRandomizedMacStoreData.serializeData(out, mock(WifiConfigStoreEncryptionUtil.class));
67         out.flush();
68         return outputStream.toByteArray();
69     }
70 
71     /**
72      * Helper function for parsing data from a XML block.
73      *
74      * @param data XML data to parse from
75      * @return Map from configKey to MAC address
76      * @throws Exception
77      */
deserializeData(byte[] data)78     private Map<String, String> deserializeData(byte[] data) throws Exception {
79         final XmlPullParser in = Xml.newPullParser();
80         final ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
81         in.setInput(inputStream, StandardCharsets.UTF_8.name());
82         mRandomizedMacStoreData.deserializeData(in, in.getDepth(),
83                 WifiConfigStore.ENCRYPT_CREDENTIALS_CONFIG_STORE_DATA_VERSION,
84                 mock(WifiConfigStoreEncryptionUtil.class));
85         return mRandomizedMacStoreData.getMacMapping();
86     }
87 
88     /**
89      * Verify that serializing empty Map causes no crash and no data should be serialized.
90      *
91      * @throws Exception
92      */
93     @Test
serializeEmptyMap()94     public void serializeEmptyMap() throws Exception {
95         assertEquals(0, serializeData().length);
96     }
97 
98     /**
99      * Verify that parsing an empty data doesn't cause any crash and no configuration should
100      * be deserialized.
101      *
102      * @throws Exception
103      */
104     @Test
deserializeEmptyData()105     public void deserializeEmptyData() throws Exception {
106         assertTrue(deserializeData(new byte[0]).isEmpty());
107     }
108 
109     /**
110      * Verify that RandomizedMacStoreData is written to
111      * {@link WifiConfigStore#STORE_FILE_SHARED_GENERAL}.
112      *
113      * @throws Exception
114      */
115     @Test
getSharedStoreFileId()116     public void getSharedStoreFileId() throws Exception {
117         assertEquals(WifiConfigStore.STORE_FILE_SHARED_GENERAL,
118                 mRandomizedMacStoreData.getStoreFileId());
119     }
120 
121     /**
122      * Verify that MAC address mapping data is serialized and deserialized correctly.
123      * @throws Exception
124      */
125     @Test
testSerializeDeserialize()126     public void testSerializeDeserialize() throws Exception {
127         Map<String, String> macMap = new HashMap<>();
128         macMap.put(TEST_CONFIG_KEY_1, TEST_MAC_ADDRESS_1);
129         macMap.put(TEST_CONFIG_KEY_2, TEST_MAC_ADDRESS_2);
130         mRandomizedMacStoreData.setMacMapping(macMap);
131         byte[] data = serializeData();
132         Map<String, String> deserializedMap = deserializeData(data);
133         assertEquals(macMap, deserializedMap);
134     }
135 }
136