Vladimir Marko | 4d2bb1b | 2016-06-24 11:56:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 17 | #include "string_reference.h" |
Vladimir Marko | 4d2bb1b | 2016-06-24 11:56:59 +0100 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | #include "utils/test_dex_file_builder.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | TEST(StringReference, ValueComparator) { |
| 27 | // This is a regression test for the StringReferenceValueComparator using the wrong |
| 28 | // dex file to get the string data from a StringId. We construct two dex files with |
| 29 | // just a single string with the same length but different value. This creates dex |
| 30 | // files that have the same layout, so the byte offset read from the StringId in one |
| 31 | // dex file, when used in the other dex file still points to valid string data, except |
| 32 | // that it's the wrong string. Without the fix the strings would then compare equal. |
| 33 | TestDexFileBuilder builder1; |
| 34 | builder1.AddString("String1"); |
| 35 | std::unique_ptr<const DexFile> dex_file1 = builder1.Build("dummy location 1"); |
| 36 | ASSERT_EQ(1u, dex_file1->NumStringIds()); |
| 37 | ASSERT_STREQ("String1", dex_file1->GetStringData(dex_file1->GetStringId(0))); |
| 38 | StringReference sr1(dex_file1.get(), 0); |
| 39 | |
| 40 | TestDexFileBuilder builder2; |
| 41 | builder2.AddString("String2"); |
| 42 | std::unique_ptr<const DexFile> dex_file2 = builder2.Build("dummy location 2"); |
| 43 | ASSERT_EQ(1u, dex_file2->NumStringIds()); |
| 44 | ASSERT_STREQ("String2", dex_file2->GetStringData(dex_file2->GetStringId(0))); |
| 45 | StringReference sr2(dex_file2.get(), 0); |
| 46 | |
| 47 | StringReferenceValueComparator cmp; |
| 48 | EXPECT_TRUE(cmp(sr1, sr2)); // "String1" < "String2" is true. |
| 49 | EXPECT_FALSE(cmp(sr2, sr1)); // "String2" < "String1" is false. |
| 50 | } |
| 51 | |
| 52 | TEST(StringReference, ValueComparator2) { |
| 53 | const char* const kDexFile1Strings[] = { |
| 54 | "", |
| 55 | "abc", |
| 56 | "abcxyz", |
| 57 | }; |
| 58 | const char* const kDexFile2Strings[] = { |
| 59 | "a", |
| 60 | "abc", |
| 61 | "abcdef", |
| 62 | "def", |
| 63 | }; |
| 64 | const bool expectedCmp12[arraysize(kDexFile1Strings)][arraysize(kDexFile2Strings)] = { |
| 65 | { true, true, true, true }, |
| 66 | { false, false, true, true }, |
| 67 | { false, false, false, true }, |
| 68 | }; |
| 69 | const bool expectedCmp21[arraysize(kDexFile2Strings)][arraysize(kDexFile1Strings)] = { |
| 70 | { false, true, true }, |
| 71 | { false, false, true }, |
| 72 | { false, false, true }, |
| 73 | { false, false, false }, |
| 74 | }; |
| 75 | |
| 76 | TestDexFileBuilder builder1; |
| 77 | for (const char* s : kDexFile1Strings) { |
| 78 | builder1.AddString(s); |
| 79 | } |
| 80 | std::unique_ptr<const DexFile> dex_file1 = builder1.Build("dummy location 1"); |
| 81 | ASSERT_EQ(arraysize(kDexFile1Strings), dex_file1->NumStringIds()); |
| 82 | for (size_t index = 0; index != arraysize(kDexFile1Strings); ++index) { |
| 83 | ASSERT_STREQ(kDexFile1Strings[index], dex_file1->GetStringData(dex_file1->GetStringId(index))); |
| 84 | } |
| 85 | |
| 86 | TestDexFileBuilder builder2; |
| 87 | for (const char* s : kDexFile2Strings) { |
| 88 | builder2.AddString(s); |
| 89 | } |
| 90 | std::unique_ptr<const DexFile> dex_file2 = builder2.Build("dummy location 1"); |
| 91 | ASSERT_EQ(arraysize(kDexFile2Strings), dex_file2->NumStringIds()); |
| 92 | for (size_t index = 0; index != arraysize(kDexFile2Strings); ++index) { |
| 93 | ASSERT_STREQ(kDexFile2Strings[index], dex_file2->GetStringData(dex_file2->GetStringId(index))); |
| 94 | } |
| 95 | |
| 96 | StringReferenceValueComparator cmp; |
| 97 | for (size_t index1 = 0; index1 != arraysize(kDexFile1Strings); ++index1) { |
| 98 | for (size_t index2 = 0; index2 != arraysize(kDexFile2Strings); ++index2) { |
| 99 | StringReference sr1(dex_file1.get(), index1); |
| 100 | StringReference sr2(dex_file2.get(), index2); |
| 101 | EXPECT_EQ(expectedCmp12[index1][index2], cmp(sr1, sr2)) << index1 << " " << index2; |
| 102 | EXPECT_EQ(expectedCmp21[index2][index1], cmp(sr2, sr1)) << index1 << " " << index2; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } // namespace art |