/*
* 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 multiple appIDs
private static final String AUTH_RESPONSE_XML_5 =
""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
private static final String TOKEN = "kZYfCEpSsMr88KZVmab5UsZVzl+nWSsX";
@Test
public void parseAuthenticateResponse() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML);
assertThat(xmlDoc.get("TOKEN", "token", "ap2004").get()).isEqualTo(TOKEN);
// Note "&" in input XML are un-escaped to "&".
assertThat(xmlDoc.get("APPLICATION", "ServiceFlow_UserData", "ap2004").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.get("TOKEN", "token", "ap2004").get()).isEqualTo(TOKEN);
// Note the "&" in input XML is kept as is
assertThat(xmlDoc.get("APPLICATION", "ServiceFlow_UserData", "ap2004").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.get("APPLICATION", "ServiceFlow_UserData", "ap2004").get())
.isEqualTo("PostData=U6%2FbQ%2BEP&l=en_US");
}
@Test
public void parseAuthenticateResponse4() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_4);
assertThat(xmlDoc.get("APPLICATION", "ServiceFlow_URL", "ap2004").isPresent()).isFalse();
assertThat(
xmlDoc.get("APPLICATION", "ServiceFlow_UserData", "ap2004").isPresent()).isFalse();
}
@Test
public void parseAuthenticateResponse5() {
XmlDoc xmlDoc = new XmlDoc(AUTH_RESPONSE_XML_5);
assertThat(xmlDoc.get("APPLICATION", "EntitlementStatus", "ap2004").get()).isEqualTo("0");
assertThat(xmlDoc.get("APPLICATION", "EntitlementStatus", "ap2005").get()).isEqualTo("1");
}
}