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