1 /*
2  * Copyright (C) 2022 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.telephony.qns;
18 
19 import static org.mockito.Mockito.spy;
20 
21 import android.content.Context;
22 import android.telephony.Rlog;
23 
24 import androidx.test.core.app.ApplicationProvider;
25 
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 
37 import java.io.InputStream;
38 import java.io.StringWriter;
39 
40 import javax.xml.parsers.DocumentBuilder;
41 import javax.xml.parsers.DocumentBuilderFactory;
42 import javax.xml.transform.OutputKeys;
43 import javax.xml.transform.Transformer;
44 import javax.xml.transform.TransformerException;
45 import javax.xml.transform.TransformerFactory;
46 import javax.xml.transform.dom.DOMSource;
47 import javax.xml.transform.stream.StreamResult;
48 import javax.xml.xpath.XPath;
49 import javax.xml.xpath.XPathConstants;
50 import javax.xml.xpath.XPathExpressionException;
51 import javax.xml.xpath.XPathFactory;
52 
53 @RunWith(JUnit4.class)
54 public class ConfigXmlTest {
55 
56     @Mock private Context mContext;
57 
58     @Before
setup()59     public void setup() {
60         MockitoAnnotations.initMocks(this);
61         mContext = spy(ApplicationProvider.getApplicationContext());
62     }
63 
slog(String log)64     protected static void slog(String log) {
65         Rlog.d(ConfigXmlTest.class.getSimpleName(), log);
66     }
67 
68     @Test
testNumValueInConfigArray()69     public void testNumValueInConfigArray() {
70         try {
71             String[] configName = mContext.getAssets().list("");
72             if (configName == null) {
73                 return;
74             }
75 
76             for (String fileName : configName) {
77                 if (fileName.startsWith("carrier_config_carrierid_")) {
78                     InputStream input = mContext.getAssets().open(fileName);
79                     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
80                     builderFactory.setNamespaceAware(true);
81                     DocumentBuilder builder = builderFactory.newDocumentBuilder();
82                     Document doc = builder.parse(input);
83 
84                     String queryIntArray = "//int-array[@num!=count(./item)]";
85                     String queryStringArray = "//string-array[@num!=count(./item)]";
86                     if (!verifyWithXmlQuery(doc, queryIntArray)
87                             || !verifyWithXmlQuery(doc, queryStringArray)) {
88                         Assert.fail("ConfigXmlTest has failed at " + fileName);
89                     }
90                 }
91             }
92         } catch (Exception e) {
93             Assert.fail("ConfigXmlTest has an exception : " + e);
94         }
95     }
96 
verifyWithXmlQuery(Document doc, String queryIntArray)97     private boolean verifyWithXmlQuery(Document doc, String queryIntArray)
98             throws XPathExpressionException, TransformerException {
99         XPath xpath = XPathFactory.newInstance().newXPath();
100         NodeList nodeList = (NodeList) xpath.evaluate(queryIntArray, doc, XPathConstants.NODESET);
101         if (nodeList == null || nodeList.getLength() <= 0) {
102             return true;
103         }
104         for (int i = 0; i < nodeList.getLength(); i++) {
105             Node node = nodeList.item(i);
106             StringWriter buf = new StringWriter();
107             Transformer xform = TransformerFactory.newInstance().newTransformer();
108             xform.setOutputProperty(OutputKeys.INDENT, "yes");
109             xform.transform(new DOMSource(node), new StreamResult(buf));
110             slog("wrong num value : " + buf);
111         }
112         return false;
113     }
114 }
115