blob: fe79d8f2c5a93ab9188d561d8cc433dd4c384972 [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
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32TEST(XmlTests, Create) {
33 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
34 ASSERT_THAT(zip, NotNull());
35
36 auto data = zip->Uncompress("AndroidManifest.xml");
37 ASSERT_THAT(data, NotNull());
38
39 auto xml = Xml::Create(data->buf, data->size);
40 ASSERT_THAT(xml, NotNull());
41
42 fclose(stderr); // silence expected warnings from libandroidfw
43 const char* not_xml = "foo";
44 auto fail = Xml::Create(reinterpret_cast<const uint8_t*>(not_xml), strlen(not_xml));
45 ASSERT_THAT(fail, IsNull());
46}
47
48TEST(XmlTests, FindTag) {
49 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
50 ASSERT_THAT(zip, NotNull());
51
52 auto data = zip->Uncompress("res/xml/test.xml");
53 ASSERT_THAT(data, NotNull());
54
55 auto xml = Xml::Create(data->buf, data->size);
56 ASSERT_THAT(xml, NotNull());
57
58 auto attrs = xml->FindTag("c");
59 ASSERT_THAT(attrs, NotNull());
Mårten Kongstadb8779022018-11-29 09:53:17 +010060 ASSERT_EQ(attrs->size(), 4U);
Mårten Kongstad02751232018-04-27 13:16:32 +020061 ASSERT_EQ(attrs->at("type_string"), "fortytwo");
62 ASSERT_EQ(std::stoi(attrs->at("type_int_dec")), 42);
63 ASSERT_EQ(std::stoi(attrs->at("type_int_hex")), 42);
Mårten Kongstadb8779022018-11-29 09:53:17 +010064 ASSERT_NE(std::stoul(attrs->at("type_int_boolean")), 0U);
Mårten Kongstad02751232018-04-27 13:16:32 +020065
66 auto fail = xml->FindTag("does-not-exist");
67 ASSERT_THAT(fail, IsNull());
68}
69
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010070} // namespace android::idmap2