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   {
26     StringRef Buffer("DXBC");
27     EXPECT_EQ(identify_magic(Buffer), file_magic::dxcontainer_object);
28   }
29   {
30     StringRef Buffer("DXBCBlahBlahBlah");
31     EXPECT_EQ(identify_magic(Buffer), file_magic::dxcontainer_object);
32   }
33 }
34 
35 TEST(DXCFile, ParseHeaderErrors) {
36   uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43};
37   EXPECT_THAT_EXPECTED(
38       DXContainer::create(getMemoryBuffer<4>(Buffer)),
39       FailedWithMessage("Reading structure out of file bounds"));
40 }
41 
42 TEST(DXCFile, ParseHeader) {
43   uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00,
44                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45                       0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
46                       0x70, 0x0D, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00};
47   DXContainer C =
48       llvm::cantFail(DXContainer::create(getMemoryBuffer<32>(Buffer)));
49   EXPECT_TRUE(memcmp(C.getHeader().Magic, "DXBC", 4) == 0);
50   EXPECT_TRUE(memcmp(C.getHeader().FileHash.Digest,
51                      "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0);
52   EXPECT_EQ(C.getHeader().Version.Major, 1u);
53   EXPECT_EQ(C.getHeader().Version.Minor, 0u);
54 }
55