11fa7c1c4c4195aa95b87b86752770bb7e1ad0f4cthestig@chromium.org// Copyright (c) 2010, Google Inc.
20a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// All rights reserved.
30a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//
40a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// Redistribution and use in source and binary forms, with or without
50a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// modification, are permitted provided that the following conditions are
60a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// met:
70a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//
80a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//     * Redistributions of source code must retain the above copyright
90a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// notice, this list of conditions and the following disclaimer.
100a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//     * Redistributions in binary form must reproduce the above
110a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// copyright notice, this list of conditions and the following disclaimer
120a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// in the documentation and/or other materials provided with the
130a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// distribution.
140a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//     * Neither the name of Google Inc. nor the names of its
150a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// contributors may be used to endorse or promote products derived from
160a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// this software without specific prior written permission.
170a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek//
180a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
200a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
210a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
220a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
230a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
240a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
260a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
270a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
280a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
300a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek// Unit tests for FileID
310a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
323a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org#include <elf.h>
330a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek#include <stdlib.h>
340a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
354e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com#include <string>
364e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com
376cddd17e9db8be3e6c39cb64fcf58e883ca8f19cthestig@chromium.org#include "common/linux/elf_gnu_compat.h"
38aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com#include "common/linux/elfutils.h"
390a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek#include "common/linux/file_id.h"
40c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org#include "common/linux/safe_readlink.h"
418ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek#include "common/linux/synth_elf.h"
428ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek#include "common/test_assembler.h"
43b12089f06d061d1ae393f208d37dc17e003d21c4qsr@chromium.org#include "common/tests/auto_tempdir.h"
444e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com#include "common/using_std_string.h"
450a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek#include "breakpad_googletest_includes.h"
460a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
470a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarekusing namespace google_breakpad;
48aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comusing google_breakpad::ElfClass32;
49aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comusing google_breakpad::ElfClass64;
50c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.orgusing google_breakpad::SafeReadLink;
518ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekusing google_breakpad::synth_elf::ELF;
52ea6e48ee7dc6bb0e553e4ad2cc45093bcbb27cbdted.mielczarek@gmail.comusing google_breakpad::synth_elf::Notes;
538ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekusing google_breakpad::test_assembler::kLittleEndian;
548ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekusing google_breakpad::test_assembler::Section;
55aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comusing ::testing::Types;
568ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
574fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.orgnamespace {
584fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
594fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org// Simply calling Section::Append(size, byte) produces a uninteresting pattern
604fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org// that tends to get hashed to 0000...0000. This populates the section with
614fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org// data to produce better hashes.
624fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.orgvoid PopulateSection(Section* section, int size, int prime_number) {
634fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  for (int i = 0; i < size; i++)
644fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    section->Append(1, (i % prime_number) % 256);
654fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org}
664fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
674fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org}  // namespace
684fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
696fab71bf48984ba57cde91f32ecb43645abd0f52digit@chromium.org#ifndef __ANDROID__
706fab71bf48984ba57cde91f32ecb43645abd0f52digit@chromium.org// This test is disabled on Android: It will always fail, since there is no
716fab71bf48984ba57cde91f32ecb43645abd0f52digit@chromium.org// 'strip' binary installed on test devices.
728ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekTEST(FileIDStripTest, StripSelf) {
738ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // Calculate the File ID of this binary using
748ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // FileID::ElfFileIdentifier, then make a copy of this binary,
758ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // strip it, and ensure that the result is the same.
760a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  char exe_name[PATH_MAX];
77c9fb1f6e7400b2f7198e62e09d7ac0a2aa68c84cbenchan@chromium.org  ASSERT_TRUE(SafeReadLink("/proc/self/exe", exe_name));
780a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
790a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  // copy our binary to a temp file, and strip it
80b12089f06d061d1ae393f208d37dc17e003d21c4qsr@chromium.org  AutoTempDir temp_dir;
814e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com  string templ = temp_dir.path() + "/file-id-unittest";
820a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  char cmdline[4096];
83b12089f06d061d1ae393f208d37dc17e003d21c4qsr@chromium.org  sprintf(cmdline, "cp \"%s\" \"%s\"", exe_name, templ.c_str());
84b732342313268422c0c0794dfb1a82cd17ce9e05ivan.penkov@gmail.com  ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
85b732342313268422c0c0794dfb1a82cd17ce9e05ivan.penkov@gmail.com  sprintf(cmdline, "chmod u+w \"%s\"", templ.c_str());
86b732342313268422c0c0794dfb1a82cd17ce9e05ivan.penkov@gmail.com  ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
87b12089f06d061d1ae393f208d37dc17e003d21c4qsr@chromium.org  sprintf(cmdline, "strip \"%s\"", templ.c_str());
88b732342313268422c0c0794dfb1a82cd17ce9e05ivan.penkov@gmail.com  ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
890a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
900a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  uint8_t identifier1[sizeof(MDGUID)];
910a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  uint8_t identifier2[sizeof(MDGUID)];
920a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  FileID fileid1(exe_name);
930a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  EXPECT_TRUE(fileid1.ElfFileIdentifier(identifier1));
94b12089f06d061d1ae393f208d37dc17e003d21c4qsr@chromium.org  FileID fileid2(templ.c_str());
950a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  EXPECT_TRUE(fileid2.ElfFileIdentifier(identifier2));
960a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  char identifier_string1[37];
970a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  char identifier_string2[37];
980a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  FileID::ConvertIdentifierToString(identifier1, identifier_string1,
990a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek                                    37);
1000a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  FileID::ConvertIdentifierToString(identifier2, identifier_string2,
1010a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek                                    37);
1020a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  EXPECT_STREQ(identifier_string1, identifier_string2);
1030a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek}
1046fab71bf48984ba57cde91f32ecb43645abd0f52digit@chromium.org#endif  // !__ANDROID__
1053a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org
106aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comtemplate<typename ElfClass>
1078ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekclass FileIDTest : public testing::Test {
1088ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarekpublic:
1098ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  void GetElfContents(ELF& elf) {
1108ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    string contents;
1118ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    ASSERT_TRUE(elf.GetContents(&contents));
1122971a050754f48aaa807db47a29e0d0beddbdcf7ivan.penkov@gmail.com    ASSERT_LT(0U, contents.size());
1133a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org
1148ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    elfdata_v.clear();
1158ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end());
1168ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    elfdata = &elfdata_v[0];
1173a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  }
1188ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
1198ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  vector<uint8_t> elfdata_v;
1208ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  uint8_t* elfdata;
1213a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org};
1223a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org
123aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comtypedef Types<ElfClass32, ElfClass64> ElfClasses;
124aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com
125aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comTYPED_TEST_CASE(FileIDTest, ElfClasses);
126aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com
127aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comTYPED_TEST(FileIDTest, ElfClass) {
1283a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  uint8_t identifier[sizeof(MDGUID)];
1293a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  const char expected_identifier_string[] =
1303a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org      "80808080-8080-0000-0000-008080808080";
1313a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  char identifier_string[sizeof(expected_identifier_string)];
1328ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  const size_t kTextSectionSize = 128;
1338ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
134aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
135aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  Section text(kLittleEndian);
1368ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  for (size_t i = 0; i < kTextSectionSize; ++i) {
137aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com    text.D8(i * 3);
1388ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  }
139aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  elf.AddSection(".text", text, SHT_PROGBITS);
140aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  elf.Finish();
141aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  this->GetElfContents(elf);
1428ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
143aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
144aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com                                                      identifier));
1458ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
1468ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  FileID::ConvertIdentifierToString(identifier, identifier_string,
1478ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek                                    sizeof(identifier_string));
1488ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  EXPECT_STREQ(expected_identifier_string, identifier_string);
1498ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek}
1508ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
151aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comTYPED_TEST(FileIDTest, BuildID) {
1528ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  const uint8_t kExpectedIdentifier[sizeof(MDGUID)] =
1538ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1548ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek     0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
1558ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  char expected_identifier_string[] =
1568ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek    "00000000-0000-0000-0000-000000000000";
1578ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  FileID::ConvertIdentifierToString(kExpectedIdentifier,
1588ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek                                    expected_identifier_string,
1598ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek                                    sizeof(expected_identifier_string));
1608ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
1618ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  uint8_t identifier[sizeof(MDGUID)];
1628ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  char identifier_string[sizeof(expected_identifier_string)];
1638ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
164aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
1658ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  Section text(kLittleEndian);
1668ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  text.Append(4096, 0);
167aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  elf.AddSection(".text", text, SHT_PROGBITS);
168ea6e48ee7dc6bb0e553e4ad2cc45093bcbb27cbdted.mielczarek@gmail.com  Notes notes(kLittleEndian);
169ea6e48ee7dc6bb0e553e4ad2cc45093bcbb27cbdted.mielczarek@gmail.com  notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier,
170ea6e48ee7dc6bb0e553e4ad2cc45093bcbb27cbdted.mielczarek@gmail.com                sizeof(kExpectedIdentifier));
171ea6e48ee7dc6bb0e553e4ad2cc45093bcbb27cbdted.mielczarek@gmail.com  elf.AddSection(".note.gnu.build-id", notes, SHT_NOTE);
172aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  elf.Finish();
173aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  this->GetElfContents(elf);
1748ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
175aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
176647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                                                      identifier));
177647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com
178647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  FileID::ConvertIdentifierToString(identifier, identifier_string,
179647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                                    sizeof(identifier_string));
180647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  EXPECT_STREQ(expected_identifier_string, identifier_string);
181647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com}
182647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com
183647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.comTYPED_TEST(FileIDTest, BuildIDPH) {
184647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  const uint8_t kExpectedIdentifier[sizeof(MDGUID)] =
185647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com    {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
186647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com     0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
187647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  char expected_identifier_string[] =
188647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com    "00000000-0000-0000-0000-000000000000";
189647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  FileID::ConvertIdentifierToString(kExpectedIdentifier,
190647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                                    expected_identifier_string,
191647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                                    sizeof(expected_identifier_string));
192647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com
193647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  uint8_t identifier[sizeof(MDGUID)];
194647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  char identifier_string[sizeof(expected_identifier_string)];
195647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com
196647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
197647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  Section text(kLittleEndian);
198647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  text.Append(4096, 0);
199647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  elf.AddSection(".text", text, SHT_PROGBITS);
200647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  Notes notes(kLittleEndian);
201647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  notes.AddNote(0, "Linux",
202647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                reinterpret_cast<const uint8_t *>("\0x42\0x02\0\0"), 4);
203647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier,
204647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com                sizeof(kExpectedIdentifier));
205647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  int note_idx = elf.AddSection(".note", notes, SHT_NOTE);
206647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  elf.AddSegment(note_idx, note_idx, PT_NOTE);
207647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  elf.Finish();
208647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  this->GetElfContents(elf);
209647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com
210647f2b96ea04bdc46d0f315850f72bc0e5fbfd5dted.mielczarek@gmail.com  EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
211aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com                                                      identifier));
2128ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek
2133a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  FileID::ConvertIdentifierToString(identifier, identifier_string,
2143a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org                                    sizeof(identifier_string));
2153a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org  EXPECT_STREQ(expected_identifier_string, identifier_string);
2163a7466663c7b8ddc550a96666c2a14c82d91536fmark@chromium.org}
2174fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
2184fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org// Test to make sure two files with different text sections produce
2194fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org// different hashes when not using a build id.
220aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.comTYPED_TEST(FileIDTest, UniqueHashes) {
2214fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  char identifier_string_1[] =
2224fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    "00000000-0000-0000-0000-000000000000";
2234fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  char identifier_string_2[] =
2244fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    "00000000-0000-0000-0000-000000000000";
2254fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  uint8_t identifier_1[sizeof(MDGUID)];
2264fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  uint8_t identifier_2[sizeof(MDGUID)];
2274fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
2284fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  {
229aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com    ELF elf1(EM_386, TypeParam::kClass, kLittleEndian);
2304fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    Section foo_1(kLittleEndian);
2314fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    PopulateSection(&foo_1, 32, 5);
2324fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf1.AddSection(".foo", foo_1, SHT_PROGBITS);
2334fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    Section text_1(kLittleEndian);
2344fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    PopulateSection(&text_1, 4096, 17);
2354fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf1.AddSection(".text", text_1, SHT_PROGBITS);
2364fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf1.Finish();
237aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com    this->GetElfContents(elf1);
2384fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  }
2394fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
240aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
241aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com                                                      identifier_1));
2424fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  FileID::ConvertIdentifierToString(identifier_1, identifier_string_1,
2434fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org                                    sizeof(identifier_string_1));
2444fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
2454fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  {
246aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com    ELF elf2(EM_386, TypeParam::kClass, kLittleEndian);
2474fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    Section text_2(kLittleEndian);
2484fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    Section foo_2(kLittleEndian);
2494fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    PopulateSection(&foo_2, 32, 5);
2504fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf2.AddSection(".foo", foo_2, SHT_PROGBITS);
2514fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    PopulateSection(&text_2, 4096, 31);
2524fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf2.AddSection(".text", text_2, SHT_PROGBITS);
2534fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org    elf2.Finish();
254aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com    this->GetElfContents(elf2);
2554fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  }
2564fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
257aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com  EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
258aed397d2dba93366fa920051ae1e09b8be6871fbted.mielczarek@gmail.com                                                      identifier_2));
2594fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  FileID::ConvertIdentifierToString(identifier_2, identifier_string_2,
2604fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org                                    sizeof(identifier_string_2));
2614fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org
2624fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org  EXPECT_STRNE(identifier_string_1, identifier_string_2);
2634fd4efe1c615da174abe42f3ca40662bb50763bfthestig@chromium.org}
264