| 1 | //===--- MarshallingTests.cpp ------------------------------------*- C++-*-===// |
| 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 "../TestTU.h" |
| 10 | #include "Index.pb.h" |
| 11 | #include "TestFS.h" |
| 12 | #include "index/Index.h" |
| 13 | #include "index/Ref.h" |
| 14 | #include "index/Relation.h" |
| 15 | #include "index/Serialization.h" |
| 16 | #include "index/Symbol.h" |
| 17 | #include "index/SymbolID.h" |
| 18 | #include "index/SymbolLocation.h" |
| 19 | #include "index/SymbolOrigin.h" |
| 20 | #include "index/remote/marshalling/Marshalling.h" |
| 21 | #include "clang/Index/IndexSymbol.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/Twine.h" |
| 25 | #include "llvm/Support/Error.h" |
| 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/StringSaver.h" |
| 28 | #include "gmock/gmock.h" |
| 29 | #include "gtest/gtest.h" |
| 30 | #include <cstring> |
| 31 | |
| 32 | namespace clang { |
| 33 | namespace clangd { |
| 34 | namespace remote { |
| 35 | namespace { |
| 36 | |
| 37 | using llvm::sys::path::convert_to_slash; |
| 38 | |
| 39 | const char *testPathURI(llvm::StringRef Path, |
| 40 | llvm::UniqueStringSaver &Strings) { |
| 41 | auto URI = URI::createFile(AbsolutePath: testPath(File: Path)); |
| 42 | return Strings.save(S: URI.toString()).begin(); |
| 43 | } |
| 44 | |
| 45 | clangd::Symbol createSymbol(llvm::StringRef PathPrefix, |
| 46 | llvm::UniqueStringSaver &Strings) { |
| 47 | clangd::Symbol Sym; |
| 48 | Sym.ID = llvm::cantFail(ValOrErr: SymbolID::fromStr("057557CEBF6E6B2D" )); |
| 49 | |
| 50 | index::SymbolInfo Info; |
| 51 | Info.Kind = index::SymbolKind::Function; |
| 52 | Info.SubKind = index::SymbolSubKind::AccessorGetter; |
| 53 | Info.Lang = index::SymbolLanguage::CXX; |
| 54 | Info.Properties = static_cast<index::SymbolPropertySet>( |
| 55 | index::SymbolProperty::TemplateSpecialization); |
| 56 | Sym.SymInfo = Info; |
| 57 | |
| 58 | Sym.Name = Strings.save(S: "Foo" ); |
| 59 | Sym.Scope = Strings.save(S: "llvm::foo::bar::" ); |
| 60 | |
| 61 | clangd::SymbolLocation Location; |
| 62 | Location.Start.setLine(1); |
| 63 | Location.Start.setColumn(15); |
| 64 | Location.End.setLine(3); |
| 65 | Location.End.setColumn(121); |
| 66 | Location.FileURI = testPathURI(Path: PathPrefix.str() + "Definition.cpp" , Strings); |
| 67 | Sym.Definition = Location; |
| 68 | |
| 69 | Location.Start.setLine(42); |
| 70 | Location.Start.setColumn(31); |
| 71 | Location.End.setLine(20); |
| 72 | Location.End.setColumn(400); |
| 73 | Location.FileURI = testPathURI(Path: PathPrefix.str() + "Declaration.h" , Strings); |
| 74 | Sym.CanonicalDeclaration = Location; |
| 75 | |
| 76 | Sym.References = 9000; |
| 77 | Sym.Origin = clangd::SymbolOrigin::Static; |
| 78 | Sym.Signature = Strings.save(S: "(int X, char Y, Type T)" ); |
| 79 | Sym.TemplateSpecializationArgs = Strings.save(S: "<int, char, bool, Type>" ); |
| 80 | Sym.CompletionSnippetSuffix = |
| 81 | Strings.save(S: "({1: int X}, {2: char Y}, {3: Type T})" ); |
| 82 | Sym.Documentation = Strings.save(S: "This is my amazing Foo constructor!" ); |
| 83 | Sym.ReturnType = Strings.save(S: "Foo" ); |
| 84 | |
| 85 | Sym.Flags = clangd::Symbol::SymbolFlag::IndexedForCodeCompletion; |
| 86 | |
| 87 | return Sym; |
| 88 | } |
| 89 | |
| 90 | TEST(RemoteMarshallingTest, URITranslation) { |
| 91 | llvm::BumpPtrAllocator Arena; |
| 92 | llvm::UniqueStringSaver Strings(Arena); |
| 93 | Marshaller ProtobufMarshaller( |
| 94 | testPath(File: "remote/machine/projects/llvm-project/" ), |
| 95 | testPath(File: "home/my-projects/llvm-project/" )); |
| 96 | clangd::Ref Original; |
| 97 | Original.Location.FileURI = |
| 98 | testPathURI(Path: "remote/machine/projects/llvm-project/clang-tools-extra/" |
| 99 | "clangd/unittests/remote/MarshallingTests.cpp" , |
| 100 | Strings); |
| 101 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Original); |
| 102 | ASSERT_TRUE(bool(Serialized)); |
| 103 | EXPECT_EQ(Serialized->location().file_path(), |
| 104 | "clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp" ); |
| 105 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 106 | ASSERT_TRUE(bool(Deserialized)); |
| 107 | EXPECT_STREQ(Deserialized->Location.FileURI, |
| 108 | testPathURI("home/my-projects/llvm-project/clang-tools-extra/" |
| 109 | "clangd/unittests/remote/MarshallingTests.cpp" , |
| 110 | Strings)); |
| 111 | |
| 112 | // Can't have empty paths. |
| 113 | *Serialized->mutable_location()->mutable_file_path() = std::string(); |
| 114 | Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 115 | EXPECT_FALSE(bool(Deserialized)); |
| 116 | llvm::consumeError(Err: Deserialized.takeError()); |
| 117 | |
| 118 | clangd::Ref WithInvalidURI; |
| 119 | // Invalid URI results in serialization failure. |
| 120 | WithInvalidURI.Location.FileURI = "This is not a URI" ; |
| 121 | auto DeserializedRef = ProtobufMarshaller.toProtobuf(From: WithInvalidURI); |
| 122 | EXPECT_FALSE(bool(DeserializedRef)); |
| 123 | llvm::consumeError(Err: DeserializedRef.takeError()); |
| 124 | |
| 125 | // Can not use URIs with scheme different from "file". |
| 126 | auto UnittestURI = |
| 127 | URI::create(AbsolutePath: testPath(File: "project/lib/HelloWorld.cpp" ), Scheme: "unittest" ); |
| 128 | ASSERT_TRUE(bool(UnittestURI)); |
| 129 | WithInvalidURI.Location.FileURI = |
| 130 | Strings.save(S: UnittestURI->toString()).begin(); |
| 131 | auto DeserializedSymbol = ProtobufMarshaller.toProtobuf(From: WithInvalidURI); |
| 132 | EXPECT_FALSE(bool(DeserializedSymbol)); |
| 133 | llvm::consumeError(Err: DeserializedSymbol.takeError()); |
| 134 | |
| 135 | // Paths transmitted over the wire can not be absolute, they have to be |
| 136 | // relative. |
| 137 | Ref WithAbsolutePath; |
| 138 | *WithAbsolutePath.mutable_location()->mutable_file_path() = |
| 139 | "/usr/local/user/home/HelloWorld.cpp" ; |
| 140 | Deserialized = ProtobufMarshaller.fromProtobuf(Message: WithAbsolutePath); |
| 141 | EXPECT_FALSE(bool(Deserialized)); |
| 142 | llvm::consumeError(Err: Deserialized.takeError()); |
| 143 | } |
| 144 | |
| 145 | TEST(RemoteMarshallingTest, SymbolSerialization) { |
| 146 | llvm::BumpPtrAllocator Arena; |
| 147 | llvm::UniqueStringSaver Strings(Arena); |
| 148 | |
| 149 | clangd::Symbol Sym = createSymbol(PathPrefix: "home/" , Strings); |
| 150 | Marshaller ProtobufMarshaller(testPath(File: "home/" ), testPath(File: "home/" )); |
| 151 | |
| 152 | // Check that symbols are exactly the same if the path to indexed project is |
| 153 | // the same on indexing machine and the client. |
| 154 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 155 | ASSERT_TRUE(bool(Serialized)); |
| 156 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 157 | ASSERT_TRUE(bool(Deserialized)); |
| 158 | // Origin is overwritten when deserializing. |
| 159 | Sym.Origin = SymbolOrigin::Remote; |
| 160 | EXPECT_EQ(toYAML(Sym), toYAML(*Deserialized)); |
| 161 | // Serialized paths are relative and have UNIX slashes. |
| 162 | EXPECT_EQ(convert_to_slash(Serialized->definition().file_path(), |
| 163 | llvm::sys::path::Style::posix), |
| 164 | Serialized->definition().file_path()); |
| 165 | EXPECT_TRUE( |
| 166 | llvm::sys::path::is_relative(Serialized->definition().file_path())); |
| 167 | |
| 168 | // Missing definition is OK. |
| 169 | Sym.Definition = clangd::SymbolLocation(); |
| 170 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 171 | ASSERT_TRUE(bool(Serialized)); |
| 172 | ASSERT_TRUE(bool(ProtobufMarshaller.fromProtobuf(*Serialized))); |
| 173 | |
| 174 | // Relative path is absolute. |
| 175 | *Serialized->mutable_canonical_declaration()->mutable_file_path() = |
| 176 | convert_to_slash(path: "/path/to/Declaration.h" ); |
| 177 | Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 178 | EXPECT_FALSE(bool(Deserialized)); |
| 179 | llvm::consumeError(Err: Deserialized.takeError()); |
| 180 | |
| 181 | // Fail with an invalid URI. |
| 182 | Sym.Definition.FileURI = "Not A URI" ; |
| 183 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 184 | EXPECT_FALSE(bool(Serialized)); |
| 185 | llvm::consumeError(Err: Serialized.takeError()); |
| 186 | |
| 187 | // Schemes other than "file" can not be used. |
| 188 | auto UnittestURI = URI::create(AbsolutePath: testPath(File: "home/SomePath.h" ), Scheme: "unittest" ); |
| 189 | ASSERT_TRUE(bool(UnittestURI)); |
| 190 | Sym.Definition.FileURI = Strings.save(S: UnittestURI->toString()).begin(); |
| 191 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 192 | EXPECT_FALSE(bool(Serialized)); |
| 193 | llvm::consumeError(Err: Serialized.takeError()); |
| 194 | |
| 195 | // Passing root that is not prefix of the original file path. |
| 196 | Sym.Definition.FileURI = testPathURI(Path: "home/File.h" , Strings); |
| 197 | // Check that the symbol is valid and passing the correct path works. |
| 198 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 199 | ASSERT_TRUE(bool(Serialized)); |
| 200 | Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 201 | ASSERT_TRUE(bool(Deserialized)); |
| 202 | EXPECT_STREQ(Deserialized->Definition.FileURI, |
| 203 | testPathURI("home/File.h" , Strings)); |
| 204 | // Fail with a wrong root. |
| 205 | Marshaller WrongMarshaller(testPath(File: "nothome/" ), testPath(File: "home/" )); |
| 206 | Serialized = WrongMarshaller.toProtobuf(From: Sym); |
| 207 | EXPECT_FALSE(Serialized); |
| 208 | llvm::consumeError(Err: Serialized.takeError()); |
| 209 | } |
| 210 | |
| 211 | TEST(RemoteMarshallingTest, RefSerialization) { |
| 212 | clangd::Ref Ref; |
| 213 | Ref.Kind = clangd::RefKind::Spelled | clangd::RefKind::Declaration; |
| 214 | |
| 215 | llvm::BumpPtrAllocator Arena; |
| 216 | llvm::UniqueStringSaver Strings(Arena); |
| 217 | |
| 218 | clangd::SymbolLocation Location; |
| 219 | Location.Start.setLine(124); |
| 220 | Location.Start.setColumn(21); |
| 221 | Location.End.setLine(3213); |
| 222 | Location.End.setColumn(541); |
| 223 | Location.FileURI = testPathURI( |
| 224 | Path: "llvm-project/llvm/clang-tools-extra/clangd/Protocol.h" , Strings); |
| 225 | Ref.Location = Location; |
| 226 | Ref.Container = llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000001" )); |
| 227 | |
| 228 | Marshaller ProtobufMarshaller(testPath(File: "llvm-project/" ), |
| 229 | testPath(File: "llvm-project/" )); |
| 230 | |
| 231 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Ref); |
| 232 | ASSERT_TRUE(bool(Serialized)); |
| 233 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 234 | ASSERT_TRUE(bool(Deserialized)); |
| 235 | EXPECT_EQ(toYAML(Ref), toYAML(*Deserialized)); |
| 236 | } |
| 237 | |
| 238 | TEST(RemoteMarshallingTest, IncludeHeaderURIs) { |
| 239 | llvm::BumpPtrAllocator Arena; |
| 240 | llvm::UniqueStringSaver Strings(Arena); |
| 241 | |
| 242 | clangd::Symbol Sym = createSymbol(PathPrefix: "remote/" , Strings); |
| 243 | |
| 244 | clangd::Symbol::IncludeHeaderWithReferences ; |
| 245 | // Add only valid headers. |
| 246 | Header.IncludeHeader = |
| 247 | Strings.save(S: URI::createFile(AbsolutePath: testPath(File: "project/Header.h" )).toString()); |
| 248 | Header.References = 21; |
| 249 | Sym.IncludeHeaders.push_back(Elt: Header); |
| 250 | Header.IncludeHeader = Strings.save(S: "<iostream>" ); |
| 251 | Header.References = 100; |
| 252 | Sym.IncludeHeaders.push_back(Elt: Header); |
| 253 | Header.IncludeHeader = Strings.save(S: "\"cstdio\"" ); |
| 254 | Header.References = 200; |
| 255 | Sym.IncludeHeaders.push_back(Elt: Header); |
| 256 | |
| 257 | Marshaller ProtobufMarshaller(testPath(File: "" ), testPath(File: "" )); |
| 258 | |
| 259 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 260 | ASSERT_TRUE(bool(Serialized)); |
| 261 | EXPECT_EQ(static_cast<size_t>(Serialized->headers_size()), |
| 262 | Sym.IncludeHeaders.size()); |
| 263 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 264 | ASSERT_TRUE(bool(Deserialized)); |
| 265 | Sym.Origin = SymbolOrigin::Remote; |
| 266 | EXPECT_EQ(toYAML(Sym), toYAML(*Deserialized)); |
| 267 | |
| 268 | // This is an absolute path to a header: can not be transmitted over the wire. |
| 269 | Header.IncludeHeader = Strings.save(S: testPath(File: "project/include/Common.h" )); |
| 270 | Header.References = 42; |
| 271 | Sym.IncludeHeaders.push_back(Elt: Header); |
| 272 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 273 | EXPECT_FALSE(bool(Serialized)); |
| 274 | llvm::consumeError(Err: Serialized.takeError()); |
| 275 | |
| 276 | // Remove last invalid header. |
| 277 | Sym.IncludeHeaders.pop_back(); |
| 278 | // This is not a valid header: can not be transmitted over the wire; |
| 279 | Header.IncludeHeader = Strings.save(S: "NotAHeader" ); |
| 280 | Header.References = 5; |
| 281 | Sym.IncludeHeaders.push_back(Elt: Header); |
| 282 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 283 | EXPECT_FALSE(bool(Serialized)); |
| 284 | llvm::consumeError(Err: Serialized.takeError()); |
| 285 | |
| 286 | // Try putting an invalid header into already serialized symbol. |
| 287 | Sym.IncludeHeaders.pop_back(); |
| 288 | Serialized = ProtobufMarshaller.toProtobuf(From: Sym); |
| 289 | ASSERT_TRUE(bool(Serialized)); |
| 290 | HeaderWithReferences ; |
| 291 | InvalidHeader.set_header(convert_to_slash(path: "/absolute/path/Header.h" )); |
| 292 | InvalidHeader.set_references(9000); |
| 293 | *Serialized->add_headers() = InvalidHeader; |
| 294 | Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 295 | EXPECT_FALSE(bool(Deserialized)); |
| 296 | llvm::consumeError(Err: Deserialized.takeError()); |
| 297 | } |
| 298 | |
| 299 | TEST(RemoteMarshallingTest, LookupRequestSerialization) { |
| 300 | clangd::LookupRequest Request; |
| 301 | Request.IDs.insert(V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000001" ))); |
| 302 | Request.IDs.insert(V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000002" ))); |
| 303 | |
| 304 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 305 | |
| 306 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 307 | EXPECT_EQ(static_cast<unsigned>(Serialized.ids_size()), Request.IDs.size()); |
| 308 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 309 | ASSERT_TRUE(bool(Deserialized)); |
| 310 | EXPECT_EQ(Deserialized->IDs, Request.IDs); |
| 311 | } |
| 312 | |
| 313 | TEST(RemoteMarshallingTest, LookupRequestFailingSerialization) { |
| 314 | clangd::LookupRequest Request; |
| 315 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 316 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 317 | Serialized.add_ids("Invalid Symbol ID" ); |
| 318 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 319 | EXPECT_FALSE(bool(Deserialized)); |
| 320 | llvm::consumeError(Err: Deserialized.takeError()); |
| 321 | } |
| 322 | |
| 323 | TEST(RemoteMarshallingTest, FuzzyFindRequestSerialization) { |
| 324 | clangd::FuzzyFindRequest Request; |
| 325 | Request.ProximityPaths = {testPath(File: "local/Header.h" ), |
| 326 | testPath(File: "local/subdir/OtherHeader.h" ), |
| 327 | testPath(File: "remote/File.h" ), "Not a Path." }; |
| 328 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 329 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 330 | EXPECT_EQ(Serialized.proximity_paths_size(), 2); |
| 331 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 332 | ASSERT_TRUE(bool(Deserialized)); |
| 333 | EXPECT_THAT(Deserialized->ProximityPaths, |
| 334 | testing::ElementsAre(testPath("remote/Header.h" ), |
| 335 | testPath("remote/subdir/OtherHeader.h" ))); |
| 336 | } |
| 337 | |
| 338 | TEST(RemoteMarshallingTest, RefsRequestSerialization) { |
| 339 | clangd::RefsRequest Request; |
| 340 | Request.IDs.insert(V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000001" ))); |
| 341 | Request.IDs.insert(V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000002" ))); |
| 342 | |
| 343 | Request.Limit = 9000; |
| 344 | Request.Filter = RefKind::Spelled | RefKind::Declaration; |
| 345 | |
| 346 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 347 | |
| 348 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 349 | EXPECT_EQ(static_cast<unsigned>(Serialized.ids_size()), Request.IDs.size()); |
| 350 | EXPECT_EQ(Serialized.limit(), Request.Limit); |
| 351 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 352 | ASSERT_TRUE(bool(Deserialized)); |
| 353 | EXPECT_EQ(Deserialized->IDs, Request.IDs); |
| 354 | ASSERT_TRUE(Deserialized->Limit); |
| 355 | EXPECT_EQ(*Deserialized->Limit, Request.Limit); |
| 356 | EXPECT_EQ(Deserialized->Filter, Request.Filter); |
| 357 | } |
| 358 | |
| 359 | TEST(RemoteMarshallingTest, RefsRequestFailingSerialization) { |
| 360 | clangd::RefsRequest Request; |
| 361 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 362 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 363 | Serialized.add_ids("Invalid Symbol ID" ); |
| 364 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 365 | EXPECT_FALSE(bool(Deserialized)); |
| 366 | llvm::consumeError(Err: Deserialized.takeError()); |
| 367 | } |
| 368 | |
| 369 | TEST(RemoteMarshallingTest, RelationsRequestSerialization) { |
| 370 | clangd::RelationsRequest Request; |
| 371 | Request.Subjects.insert( |
| 372 | V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000001" ))); |
| 373 | Request.Subjects.insert( |
| 374 | V: llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000002" ))); |
| 375 | |
| 376 | Request.Limit = 9000; |
| 377 | Request.Predicate = RelationKind::BaseOf; |
| 378 | |
| 379 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 380 | |
| 381 | auto Serialized = ProtobufMarshaller.toProtobuf(From: Request); |
| 382 | EXPECT_EQ(static_cast<unsigned>(Serialized.subjects_size()), |
| 383 | Request.Subjects.size()); |
| 384 | EXPECT_EQ(Serialized.limit(), Request.Limit); |
| 385 | EXPECT_EQ(static_cast<RelationKind>(Serialized.predicate()), |
| 386 | Request.Predicate); |
| 387 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 388 | ASSERT_TRUE(bool(Deserialized)); |
| 389 | EXPECT_EQ(Deserialized->Subjects, Request.Subjects); |
| 390 | ASSERT_TRUE(Deserialized->Limit); |
| 391 | EXPECT_EQ(*Deserialized->Limit, Request.Limit); |
| 392 | EXPECT_EQ(Deserialized->Predicate, Request.Predicate); |
| 393 | } |
| 394 | |
| 395 | TEST(RemoteMarshallingTest, RelationsRequestFailingSerialization) { |
| 396 | RelationsRequest Serialized; |
| 397 | Serialized.add_subjects("ZZZZZZZZZZZZZZZZ" ); |
| 398 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 399 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: &Serialized); |
| 400 | EXPECT_FALSE(bool(Deserialized)); |
| 401 | llvm::consumeError(Err: Deserialized.takeError()); |
| 402 | } |
| 403 | |
| 404 | TEST(RemoteMarshallingTest, RelationsSerializion) { |
| 405 | llvm::BumpPtrAllocator Arena; |
| 406 | llvm::UniqueStringSaver Strings(Arena); |
| 407 | |
| 408 | clangd::Symbol Sym = createSymbol(PathPrefix: "remote/" , Strings); |
| 409 | SymbolID ID = llvm::cantFail(ValOrErr: SymbolID::fromStr("0000000000000002" )); |
| 410 | Marshaller ProtobufMarshaller(testPath(File: "remote/" ), testPath(File: "local/" )); |
| 411 | auto Serialized = ProtobufMarshaller.toProtobuf(Subject: ID, Object: Sym); |
| 412 | ASSERT_TRUE(bool(Serialized)); |
| 413 | auto Deserialized = ProtobufMarshaller.fromProtobuf(Message: *Serialized); |
| 414 | ASSERT_TRUE(bool(Deserialized)); |
| 415 | EXPECT_THAT(Deserialized->first, ID); |
| 416 | EXPECT_THAT(Deserialized->second.ID, Sym.ID); |
| 417 | } |
| 418 | |
| 419 | TEST(RemoteMarshallingTest, RelativePathToURITranslation) { |
| 420 | Marshaller ProtobufMarshaller(/*RemoteIndexRoot=*/"" , |
| 421 | /*LocalIndexRoot=*/testPath(File: "home/project/" )); |
| 422 | auto URIString = ProtobufMarshaller.relativePathToURI(RelativePath: "lib/File.cpp" ); |
| 423 | ASSERT_TRUE(bool(URIString)); |
| 424 | // RelativePath can not be absolute. |
| 425 | URIString = ProtobufMarshaller.relativePathToURI(RelativePath: "/lib/File.cpp" ); |
| 426 | EXPECT_FALSE(bool(URIString)); |
| 427 | llvm::consumeError(Err: URIString.takeError()); |
| 428 | // RelativePath can not be empty. |
| 429 | URIString = ProtobufMarshaller.relativePathToURI(RelativePath: std::string()); |
| 430 | EXPECT_FALSE(bool(URIString)); |
| 431 | llvm::consumeError(Err: URIString.takeError()); |
| 432 | } |
| 433 | |
| 434 | TEST(RemoteMarshallingTest, URIToRelativePathTranslation) { |
| 435 | llvm::BumpPtrAllocator Arena; |
| 436 | llvm::UniqueStringSaver Strings(Arena); |
| 437 | Marshaller ProtobufMarshaller(/*RemoteIndexRoot=*/testPath(File: "remote/project/" ), |
| 438 | /*LocalIndexRoot=*/"" ); |
| 439 | auto RelativePath = ProtobufMarshaller.uriToRelativePath( |
| 440 | URI: testPathURI(Path: "remote/project/lib/File.cpp" , Strings)); |
| 441 | ASSERT_TRUE(bool(RelativePath)); |
| 442 | // RemoteIndexRoot has to be a prefix of the file path. |
| 443 | Marshaller WrongMarshaller( |
| 444 | /*RemoteIndexRoot=*/testPath(File: "remote/other/project/" ), |
| 445 | /*LocalIndexRoot=*/"" ); |
| 446 | RelativePath = WrongMarshaller.uriToRelativePath( |
| 447 | URI: testPathURI(Path: "remote/project/lib/File.cpp" , Strings)); |
| 448 | EXPECT_FALSE(bool(RelativePath)); |
| 449 | llvm::consumeError(Err: RelativePath.takeError()); |
| 450 | } |
| 451 | |
| 452 | } // namespace |
| 453 | } // namespace remote |
| 454 | } // namespace clangd |
| 455 | } // namespace clang |
| 456 | |