1/*
2 * Copyright (C) 2017 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 */
16package android.service.euicc;
17
18import static org.junit.Assert.assertArrayEquals;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotEquals;
22import static org.junit.Assert.assertTrue;
23
24import android.os.Parcel;
25import android.service.carrier.CarrierIdentifier;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28import android.telephony.UiccAccessRule;
29
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33import java.util.Arrays;
34
35@SmallTest
36@RunWith(AndroidJUnit4.class)
37public class EuiccProfileInfoTest {
38    @Test
39    public void testWriteToParcel() {
40        EuiccProfileInfo p =
41                new EuiccProfileInfo.Builder("21430000000000006587")
42                        .setNickname("profile nickname")
43                        .setServiceProviderName("service provider")
44                        .setProfileName("profile name")
45                        .setProfileClass(EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL)
46                        .setState(EuiccProfileInfo.PROFILE_STATE_ENABLED)
47                        .setCarrierIdentifier(
48                                new CarrierIdentifier(
49                                        new byte[] {0x23, 0x45, 0x67},
50                                        "123",
51                                        "45"))
52                        .setPolicyRules(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE)
53                        .setUiccAccessRule(
54                                Arrays.asList(new UiccAccessRule(new byte[] {}, "package", 12345L)))
55                        .build();
56
57        Parcel parcel = Parcel.obtain();
58        assertTrue(parcel != null);
59        p.writeToParcel(parcel, 0);
60
61        parcel.setDataPosition(0);
62        EuiccProfileInfo fromParcel = EuiccProfileInfo.CREATOR.createFromParcel(parcel);
63
64        assertEquals(p, fromParcel);
65    }
66
67    @Test
68    public void testWriteToParcelNullCarrierId() {
69        EuiccProfileInfo p =
70                new EuiccProfileInfo.Builder("21430000000000006587")
71                        .setNickname("profile nickname")
72                        .setServiceProviderName("service provider")
73                        .setProfileName("profile name")
74                        .setProfileClass(EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL)
75                        .setState(EuiccProfileInfo.PROFILE_STATE_ENABLED)
76                        .setPolicyRules(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE)
77                        .setUiccAccessRule(
78                                Arrays.asList(new UiccAccessRule(new byte[] {}, "package", 12345L))
79                        )
80                        .build();
81
82        Parcel parcel = Parcel.obtain();
83        assertTrue(parcel != null);
84        p.writeToParcel(parcel, 0);
85
86        parcel.setDataPosition(0);
87        EuiccProfileInfo fromParcel = EuiccProfileInfo.CREATOR.createFromParcel(parcel);
88
89        assertEquals(p, fromParcel);
90    }
91
92    @Test
93    public void testBuilderAndGetters() {
94        EuiccProfileInfo p =
95                new EuiccProfileInfo.Builder("21430000000000006587")
96                        .setNickname("profile nickname")
97                        .setProfileName("profile name")
98                        .setServiceProviderName("service provider")
99                        .setCarrierIdentifier(
100                                new CarrierIdentifier(
101                                        new byte[] {0x23, 0x45, 0x67},
102                                        "123",
103                                        "45"))
104                        .setState(EuiccProfileInfo.PROFILE_STATE_ENABLED)
105                        .setProfileClass(EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL)
106                        .setPolicyRules(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE)
107                        .setUiccAccessRule(Arrays.asList(new UiccAccessRule(new byte[0], null, 0)))
108                        .build();
109
110        assertEquals("21430000000000006587", p.getIccid());
111        assertEquals("profile nickname", p.getNickname());
112        assertEquals("profile name", p.getProfileName());
113        assertEquals("service provider", p.getServiceProviderName());
114        assertEquals("325", p.getCarrierIdentifier().getMcc());
115        assertEquals("764", p.getCarrierIdentifier().getMnc());
116        assertEquals("123", p.getCarrierIdentifier().getGid1());
117        assertEquals("45", p.getCarrierIdentifier().getGid2());
118        assertEquals(EuiccProfileInfo.PROFILE_STATE_ENABLED, p.getState());
119        assertEquals(EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL, p.getProfileClass());
120        assertEquals(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE, p.getPolicyRules());
121        assertTrue(p.hasPolicyRules());
122        assertTrue(p.hasPolicyRule(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE));
123        assertFalse(p.hasPolicyRule(EuiccProfileInfo.POLICY_RULE_DO_NOT_DISABLE));
124        assertArrayEquals(
125                new UiccAccessRule[] {new UiccAccessRule(new byte[0], null, 0)},
126                p.getUiccAccessRules().toArray());
127    }
128
129    @Test
130    public void testBuilder_BasedOnAnotherProfile() {
131        EuiccProfileInfo p =
132                new EuiccProfileInfo.Builder("21430000000000006587")
133                        .setNickname("profile nickname")
134                        .setProfileName("profile name")
135                        .setServiceProviderName("service provider")
136                        .setCarrierIdentifier(
137                                new CarrierIdentifier(
138                                        new byte[] {0x23, 0x45, 0x67},
139                                        "123",
140                                        "45"))
141                        .setState(EuiccProfileInfo.PROFILE_STATE_ENABLED)
142                        .setProfileClass(EuiccProfileInfo.PROFILE_CLASS_OPERATIONAL)
143                        .setPolicyRules(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE)
144                        .setUiccAccessRule(
145                                Arrays.asList(new UiccAccessRule(new byte[] {}, "package", 12345L)))
146                        .build();
147
148        EuiccProfileInfo copied = new EuiccProfileInfo.Builder(p).build();
149
150        assertEquals(p, copied);
151        assertEquals(p.hashCode(), copied.hashCode());
152    }
153
154    @Test
155    public void testEqualsHashCode() {
156        EuiccProfileInfo p =
157                new EuiccProfileInfo.Builder("21430000000000006587")
158                        .setNickname("profile nickname")
159                        .setProfileName("profile name")
160                        .setServiceProviderName("service provider")
161                        .setCarrierIdentifier(
162                                new CarrierIdentifier(
163                                        new byte[] {0x23, 0x45, 0x67},
164                                        "123",
165                                        "45"))
166                        .setState(EuiccProfileInfo.PROFILE_STATE_ENABLED)
167                        .setProfileClass(EuiccProfileInfo.PROFILE_STATE_ENABLED)
168                        .setPolicyRules(EuiccProfileInfo.POLICY_RULE_DO_NOT_DELETE)
169                        .setUiccAccessRule(Arrays.asList(new UiccAccessRule(new byte[0], null, 0)))
170                        .build();
171
172        assertTrue(p.equals(p));
173        assertFalse(p.equals(new Object()));
174
175        EuiccProfileInfo t = null;
176        assertFalse(p.equals(t));
177
178        t = new EuiccProfileInfo.Builder(p).setIccid("21").build();
179        assertFalse(p.equals(t));
180        assertNotEquals(p.hashCode(), t.hashCode());
181
182        t = new EuiccProfileInfo.Builder(p).setNickname(null).build();
183        assertFalse(p.equals(t));
184        assertNotEquals(p.hashCode(), t.hashCode());
185
186        t = new EuiccProfileInfo.Builder(p).setProfileName(null).build();
187        assertFalse(p.equals(t));
188        assertNotEquals(p.hashCode(), t.hashCode());
189
190        t = new EuiccProfileInfo.Builder(p).setServiceProviderName(null).build();
191        assertFalse(p.equals(t));
192        assertNotEquals(p.hashCode(), t.hashCode());
193
194        t = new EuiccProfileInfo.Builder(p).setCarrierIdentifier(null).build();
195        assertFalse(p.equals(t));
196        assertNotEquals(p.hashCode(), t.hashCode());
197
198        t = new EuiccProfileInfo.Builder(p)
199                .setState(EuiccProfileInfo.PROFILE_STATE_DISABLED).build();
200        assertFalse(p.equals(t));
201        assertNotEquals(p.hashCode(), t.hashCode());
202
203        t = new EuiccProfileInfo.Builder(p)
204                .setProfileClass(EuiccProfileInfo.PROFILE_CLASS_TESTING).build();
205        assertFalse(p.equals(t));
206        assertNotEquals(p.hashCode(), t.hashCode());
207
208        t = new EuiccProfileInfo.Builder(p).setPolicyRules(0).build();
209        assertFalse(p.equals(t));
210        assertNotEquals(p.hashCode(), t.hashCode());
211
212        t = new EuiccProfileInfo.Builder(p).setUiccAccessRule(null).build();
213        assertFalse(p.equals(t));
214        assertNotEquals(p.hashCode(), t.hashCode());
215    }
216
217    @Test(expected = IllegalStateException.class)
218    public void testBuilderBuild_IllegalIccid() {
219        new EuiccProfileInfo.Builder("abc").build();
220    }
221
222    @Test(expected = IllegalArgumentException.class)
223    public void testBuilderSetOperatorMccMnc_Illegal() {
224        new EuiccProfileInfo.Builder("21430000000000006587")
225                .setCarrierIdentifier(new CarrierIdentifier(new byte[] {1, 2, 3, 4}, null, null));
226    }
227
228    @Test
229    public void testCreatorNewArray() {
230        EuiccProfileInfo[] profiles = EuiccProfileInfo.CREATOR.newArray(123);
231        assertEquals(123, profiles.length);
232    }
233}
234