Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame^] | 19 | #include "TestHelpers.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 20 | #include "gmock/gmock.h" |
| 21 | #include "gtest/gtest.h" |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame^] | 22 | #include "idmap2/Xml.h" |
| 23 | #include "idmap2/ZipFile.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 24 | |
| 25 | using ::testing::IsNull; |
| 26 | using ::testing::NotNull; |
| 27 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 28 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 29 | |
| 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 | |
| 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()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 58 | ASSERT_EQ(attrs->size(), 4U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 62 | ASSERT_NE(std::stoul(attrs->at("type_int_boolean")), 0U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 63 | |
| 64 | auto fail = xml->FindTag("does-not-exist"); |
| 65 | ASSERT_THAT(fail, IsNull()); |
| 66 | } |
| 67 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 68 | } // namespace android::idmap2 |