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