1 /*
2 * Copyright (C) 2018 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 #include <cstdio> // fclose
18
19 #include "TestHelpers.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "idmap2/Xml.h"
23 #include "idmap2/ZipFile.h"
24
25 using ::testing::IsNull;
26 using ::testing::NotNull;
27
28 namespace android::idmap2 {
29
TEST(XmlTests,Create)30 TEST(XmlTests, Create) {
31 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
32 ASSERT_THAT(zip, NotNull());
33
34 auto data = zip->Uncompress("AndroidManifest.xml");
35 ASSERT_THAT(data, NotNull());
36
37 auto xml = Xml::Create(data->buf, data->size);
38 ASSERT_THAT(xml, NotNull());
39
40 fclose(stderr); // silence expected warnings from libandroidfw
41 const char* not_xml = "foo";
42 auto fail = Xml::Create(reinterpret_cast<const uint8_t*>(not_xml), strlen(not_xml));
43 ASSERT_THAT(fail, IsNull());
44 }
45
TEST(XmlTests,FindTag)46 TEST(XmlTests, FindTag) {
47 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
48 ASSERT_THAT(zip, NotNull());
49
50 auto data = zip->Uncompress("res/xml/test.xml");
51 ASSERT_THAT(data, NotNull());
52
53 auto xml = Xml::Create(data->buf, data->size);
54 ASSERT_THAT(xml, NotNull());
55
56 auto attrs = xml->FindTag("c");
57 ASSERT_THAT(attrs, NotNull());
58 ASSERT_EQ(attrs->size(), 4U);
59 ASSERT_EQ(attrs->at("type_string"), "fortytwo");
60 ASSERT_EQ(std::stoi(attrs->at("type_int_dec")), 42);
61 ASSERT_EQ(std::stoi(attrs->at("type_int_hex")), 42);
62 ASSERT_NE(std::stoul(attrs->at("type_int_boolean")), 0U);
63
64 auto fail = xml->FindTag("does-not-exist");
65 ASSERT_THAT(fail, IsNull());
66 }
67
68 } // namespace android::idmap2
69