xref: /llvm-project/llvm/unittests/Object/DXContainerTest.cpp (revision 966c40aea663fa2cb575614518fc1b7c528bbae9)
1 //===- DXContainerTest.cpp - Tests for DXContainerFile --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Object/DXContainer.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/BinaryFormat/Magic.h"
12 #include "llvm/Support/MemoryBufferRef.h"
13 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 using namespace llvm::object;
18 
19 template <std::size_t X> MemoryBufferRef getMemoryBuffer(uint8_t Data[X]) {
20   StringRef Obj(reinterpret_cast<char *>(&Data[0]), X);
21   return MemoryBufferRef(Obj, "");
22 }
23 
24 TEST(DXCFile, IdentifyMagic) {
25   StringRef Buffer("DXBC");
26   EXPECT_EQ(identify_magic(Buffer), file_magic::dxcontainer_object);
27 }
28 
29 TEST(DXCFile, ParseHeaderErrors) {
30   uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43};
31   EXPECT_THAT_EXPECTED(
32       DXContainer::create(getMemoryBuffer<4>(Buffer)),
33       FailedWithMessage("Reading structure out of file bounds"));
34 }
35 
36 TEST(DXCFile, ParseHeader) {
37   uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00,
38                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39                       0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
40                       0x70, 0x0D, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00};
41   DXContainer C =
42       llvm::cantFail(DXContainer::create(getMemoryBuffer<32>(Buffer)));
43   EXPECT_TRUE(memcmp(C.getHeader().Magic, "DXBC", 4) == 0);
44   EXPECT_TRUE(memcmp(C.getHeader().FileHash.Digest,
45                      "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0);
46   EXPECT_EQ(C.getHeader().Version.Major, 1u);
47   EXPECT_EQ(C.getHeader().Version.Minor, 0u);
48 }
49