blob: df63211a92097433c3a8e7c254c88b6b09e5566b [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070019#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include "gmock/gmock.h"
21#include "gtest/gtest.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070022#include "idmap2/Xml.h"
23#include "idmap2/ZipFile.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024
25using ::testing::IsNull;
26using ::testing::NotNull;
27
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010028namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020029
30TEST(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
46TEST(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());
Mårten Kongstadb8779022018-11-29 09:53:17 +010058 ASSERT_EQ(attrs->size(), 4U);
Mårten Kongstad02751232018-04-27 13:16:32 +020059 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);
Mårten Kongstadb8779022018-11-29 09:53:17 +010062 ASSERT_NE(std::stoul(attrs->at("type_int_boolean")), 0U);
Mårten Kongstad02751232018-04-27 13:16:32 +020063
64 auto fail = xml->FindTag("does-not-exist");
65 ASSERT_THAT(fail, IsNull());
66}
67
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010068} // namespace android::idmap2