xref: /llvm-project/llvm/unittests/DebugInfo/CodeView/GUIDFormatTest.cpp (revision 4d65887aac98acd6ed749377f4ea296c5a003d7e)
1 //===- unittest/DebugInfo/CodeView/GUIDFormatTest.cpp - GUID formatting ---===//
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/ADT/SmallVector.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/DebugInfo/CodeView/Formatters.h"
13 #include "llvm/DebugInfo/CodeView/GUID.h"
14 #include "llvm/Support/FormatVariadic.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 using namespace llvm::codeview;
19 
20 // Variant 1 UUIDs, nowadays the most common variant, are encoded in a
21 // big-endian format.
22 // For example, 00112233-4455-6677-8899-aabbccddeeff is encoded as the bytes
23 // 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff
24 //
25 // Variant 2 UUIDs, historically used in Microsoft's COM/OLE libraries, use a
26 // mixed-endian format, whereby the first three components of the UUID are
27 // little-endian, and the last two are big-endian.
28 // For example, 00112233-4455-6677-8899-aabbccddeeff is encoded as the bytes
29 // 33 22 11 00 55 44 77 66 88 99 aa bb cc dd ee ff.
30 //
31 // Note: Only Variant 2 UUIDs are tested.
32 namespace {
33 
34 using GuidPair = std::pair<StringRef, GUID>;
35 using GuidData = SmallVector<GuidPair>;
36 
checkData(GuidData & Data)37 void checkData(GuidData &Data) {
38   for (auto Item : Data) {
39     std::string GuidText(formatv("{0}", Item.second).str());
40     StringRef Scalar(GuidText);
41 
42     // GUID strings are 38 characters long.
43     EXPECT_EQ(Scalar.size(), size_t(38));
44 
45     // GUID must be enclosed in {}
46     EXPECT_EQ(Scalar.front(), '{');
47     EXPECT_EQ(Scalar.back(), '}');
48 
49     Scalar = Scalar.substr(1, Scalar.size() - 2);
50     SmallVector<StringRef, 6> Component;
51     Scalar.split(Component, '-', 5);
52 
53     // GUID must have 5 components.
54     EXPECT_EQ(Component.size(), size_t(5));
55 
56     // GUID components are properly delineated with dashes.
57     EXPECT_EQ(Scalar[8], '-');
58     EXPECT_EQ(Scalar[13], '-');
59     EXPECT_EQ(Scalar[18], '-');
60     EXPECT_EQ(Scalar[23], '-');
61 
62     // GUID only contains hex digits.
63     struct {
64       support::ulittle32_t Data0;
65       support::ulittle16_t Data1;
66       support::ulittle16_t Data2;
67       support::ubig16_t Data3;
68       support::ubig64_t Data4;
69     } G = {};
70     EXPECT_TRUE(to_integer(Component[0], G.Data0, 16));
71     EXPECT_TRUE(to_integer(Component[1], G.Data1, 16));
72     EXPECT_TRUE(to_integer(Component[2], G.Data2, 16));
73     EXPECT_TRUE(to_integer(Component[3], G.Data3, 16));
74     EXPECT_TRUE(to_integer(Component[4], G.Data4, 16));
75 
76     // Check the values are the same.
77     EXPECT_EQ(Scalar, Item.first);
78   }
79 }
80 
TEST(GUIDFormatTest,ValidateFormat)81 TEST(GUIDFormatTest, ValidateFormat) {
82   // Shifting 2 (0x00)
83   GuidData Data = {
84       // Non-zero values in all components.
85       {"11223344-5566-7788-99AA-BBCCDDEEFFAA",
86        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
87         0xdd, 0xee, 0xff, 0xaa}},
88 
89       // Zero values in all components.
90       {"00000000-0000-0000-0000-000000000000",
91        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92         0x00, 0x00, 0x00, 0x00}},
93 
94       // Shift 2 (0x00) across all components
95       {"00003344-5566-7788-99AA-BBCCDDEEFFAA",
96        {0x44, 0x33, 0x00, 0x00, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
97         0xdd, 0xee, 0xff, 0xaa}},
98       {"11000044-5566-7788-99AA-BBCCDDEEFFAA",
99        {0x44, 0x00, 0x00, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
100         0xdd, 0xee, 0xff, 0xaa}},
101       {"11220000-5566-7788-99AA-BBCCDDEEFFAA",
102        {0x00, 0x00, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
103         0xdd, 0xee, 0xff, 0xaa}},
104       {"11223300-0066-7788-99AA-BBCCDDEEFFAA",
105        {0x00, 0x33, 0x22, 0x11, 0x66, 0x00, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
106         0xdd, 0xee, 0xff, 0xaa}},
107       {"11223344-0000-7788-99AA-BBCCDDEEFFAA",
108        {0x44, 0x33, 0x22, 0x11, 0x00, 0x00, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
109         0xdd, 0xee, 0xff, 0xaa}},
110       {"11223344-5500-0088-99AA-BBCCDDEEFFAA",
111        {0x44, 0x33, 0x22, 0x11, 0x00, 0x55, 0x88, 0x00, 0x99, 0xaa, 0xbb, 0xcc,
112         0xdd, 0xee, 0xff, 0xaa}},
113       {"11223344-5566-0000-99AA-BBCCDDEEFFAA",
114        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x00, 0x00, 0x99, 0xaa, 0xbb, 0xcc,
115         0xdd, 0xee, 0xff, 0xaa}},
116       {"11223344-5566-7700-00AA-BBCCDDEEFFAA",
117        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x00, 0x77, 0x00, 0xaa, 0xbb, 0xcc,
118         0xdd, 0xee, 0xff, 0xaa}},
119       {"11223344-5566-7788-0000-BBCCDDEEFFAA",
120        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x00, 0x00, 0xbb, 0xcc,
121         0xdd, 0xee, 0xff, 0xaa}},
122       {"11223344-5566-7788-9900-00CCDDEEFFAA",
123        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0x00, 0x00, 0xcc,
124         0xdd, 0xee, 0xff, 0xaa}},
125       {"11223344-5566-7788-99AA-0000DDEEFFAA",
126        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0x00, 0x00,
127         0xdd, 0xee, 0xff, 0xaa}},
128       {"11223344-5566-7788-99AA-BB0000EEFFAA",
129        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0x00,
130         0x00, 0xee, 0xff, 0xaa}},
131       {"11223344-5566-7788-99AA-BBCC0000FFAA",
132        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
133         0x00, 0x00, 0xff, 0xaa}},
134       {"11223344-5566-7788-99AA-BBCCDD0000AA",
135        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
136         0xdd, 0x00, 0x00, 0xaa}},
137       {"11223344-5566-7788-99AA-BBCCDDEE0000",
138        {0x44, 0x33, 0x22, 0x11, 0x66, 0x55, 0x88, 0x77, 0x99, 0xaa, 0xbb, 0xcc,
139         0xdd, 0xee, 0x00, 0x00}},
140   };
141 
142   checkData(Data);
143 }
144 } // namespace
145