/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.imsserviceentitlement.utils;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class XmlDocTest {
// XML sample from vendor A
private static final String AUTH_RESPONSE_XML =
"\n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ "\n";
// XML sample from vendor B, note unescaped "&" in ServiceFlow_UserData
private static final String AUTH_RESPONSE_XML_2 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
// A XML sample with "&" - unlikely to happen in practice but good to test
private static final String AUTH_RESPONSE_XML_3 =
""
+ ""
+ ""
+ ""
+ ""
+ "";
// A XML sample with server URL and user data unset.
private static final String AUTH_RESPONSE_XML_4 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
// A XML sample with server URL unset and user data set.
private static final String AUTH_RESPONSE_XML_5 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
// A XML sample with multiple appIDs
private static final String AUTH_RESPONSE_XML_6 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
// A TS43 v8 XML sample with separate 5G config for home and roaming networks
private static final String AUTH_RESPONSE_XML_7 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "" // 5G
+ "" // Home network
+ "" // Enabled
+ ""
+ ""
+ ""
+ "" // 5G
+ "" // Roaming network
+ "" // Incompatible
+ ""
+ ""
+ ""
+ ""
+ "";
// A TS43 v8 XML sample with single 4G and 5G config for both home and roaming networks
private static final String AUTH_RESPONSE_XML_8 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "" // 4G
+ "" // Home&Roaming
+ "" // Enabled
+ ""
+ ""
+ "" // 5G
+ "" // Both Home and Roaming
+ "" // Enabled
+ ""
+ ""
+ ""
+ ""
+ "";
// A TS43 v8 XML sample with single 4G for home network only - unlikely to happen
private static final String AUTH_RESPONSE_XML_9 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "" // 4G
+ "" // Home
+ "" // Enabled
+ ""
+ ""
+ ""
+ "";
// A TS43 v8 XML sample with single 5G for home network only - unlikely to happen
private static final String AUTH_RESPONSE_XML_10 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "" // 5G
+ "" // Home
+ "" // Enabled
+ ""
+ ""
+ ""
+ "";
private static final String TOKEN = "kZYfCEpSsMr88KZVmab5UsZVzl+nWSsX";
@Test
public void parseAuthenticateResponse() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML);
assertThat(xmlDoc.getFromToken("token").get()).isEqualTo(TOKEN);
assertThat(xmlDoc.getFromVersion("version").get()).isEqualTo("1");
// Note "&" in input XML are un-escaped to "&".
assertThat(xmlDoc.getFromVowifi("ServiceFlow_UserData").get())
.isEqualTo("token=Y5vcmc%3D"
+ "&entitlementStatus=0"
+ "&protocol=TS43"
+ "&provStatus=2"
+ "&deviceId=358316079424742"
+ "&subscriberId=0311580718847611%40nai.epc.mnc130.mcc310.3gppnetwork.org"
+ "&ShowAddress=true");
}
@Test
public void parseAuthenticateResponse2() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_2);
assertThat(xmlDoc.getFromToken("token").get()).isEqualTo(TOKEN);
// Note the "&" in input XML is kept as is
assertThat(xmlDoc.getFromVowifi("ServiceFlow_UserData").get())
.isEqualTo("PostData=U6%2FbQ%2BEP&req_locale=en_US");
}
@Test
public void parseAuthenticateResponse3() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_3);
// Note the "&" in input XML is un-escaped to "&"
assertThat(xmlDoc.getFromVowifi("ServiceFlow_UserData").get())
.isEqualTo("PostData=U6%2FbQ%2BEP&l=en_US");
}
@Test
public void parseAuthenticateResponse4() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_4);
assertThat(xmlDoc.getFromVowifi("ServiceFlow_URL").isPresent()).isFalse();
assertThat(xmlDoc.getFromVowifi("ServiceFlow_UserData").isPresent()).isFalse();
}
@Test
public void parseAuthenticateResponse5() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_5);
assertThat(xmlDoc.getFromVowifi("ServiceFlow_URL").isPresent()).isFalse();
assertThat(xmlDoc.getFromVowifi("ServiceFlow_UserData").isPresent()).isTrue();
}
@Test
public void parseAuthenticateResponse6() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_6);
assertThat(xmlDoc.getFromVowifi("EntitlementStatus").get()).isEqualTo("0");
assertThat(xmlDoc.getFromSmsoverip("EntitlementStatus").get()).isEqualTo("1");
}
@Test
public void parseAuthenticateResponse7() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_7);
assertThat(xmlDoc.getFromVonrHome("EntitlementStatus").get()).isEqualTo("1");
assertThat(xmlDoc.getFromVonrHome("NetworkVoiceIRATCapablity").get())
.isEqualTo("EPS-Fallback");
assertThat(xmlDoc.getFromVonrRoaming("EntitlementStatus").get()).isEqualTo("2");
assertThat(xmlDoc.getFromVonrRoaming("NetworkVoiceIRATCapablity").isPresent()).isFalse();
}
@Test
public void parseAuthenticateResponse8() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_8);
assertThat(xmlDoc.getFromVolte("EntitlementStatus").get()).isEqualTo("1");
assertThat(xmlDoc.getFromVonrHome("EntitlementStatus").get()).isEqualTo("1");
assertThat(xmlDoc.getFromVonrHome("NetworkVoiceIRATCapablity").get())
.isEqualTo("EPS-Fallback");
assertThat(xmlDoc.getFromVonrRoaming("EntitlementStatus").get()).isEqualTo("1");
assertThat(xmlDoc.getFromVonrRoaming("NetworkVoiceIRATCapablity").get())
.isEqualTo("EPS-Fallback");
}
@Test
public void parseAuthenticateResponse9() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_9);
assertThat(xmlDoc.getFromVolte("EntitlementStatus").get()).isEqualTo("1");
}
@Test
public void parseAuthenticateResponse10() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_10);
assertThat(xmlDoc.getFromVonrHome("EntitlementStatus").get()).isEqualTo("1");
assertThat(xmlDoc.getFromVonrRoaming("EntitlementStatus").isPresent()).isFalse();
}
}