summaryrefslogtreecommitdiffstats
path: root/unittests/clangd/JSONTransportTests.cpp
blob: 23c393462a3146d0741b76df520c7090432a0fd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//===-- JSONTransportTests.cpp  -------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Protocol.h"
#include "Transport.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstdio>

namespace clang {
namespace clangd {
namespace {

// No fmemopen on windows or on versions of MacOS X earlier than 10.13, so we
// can't easily run this test.
#if !(defined(WIN32) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) &&           \
                         __MAC_OS_X_VERSION_MIN_REQUIRED < 101300))

// Fixture takes care of managing the input/output buffers for the transport.
class JSONTransportTest : public ::testing::Test {
  std::string InBuf, OutBuf, MirrorBuf;
  llvm::raw_string_ostream Out, Mirror;
  std::unique_ptr<FILE, int (*)(FILE *)> In;

protected:
  JSONTransportTest() : Out(OutBuf), Mirror(MirrorBuf), In(nullptr, nullptr) {}

  template <typename... Args>
  std::unique_ptr<Transport> transport(std::string InData, bool Pretty,
                                       JSONStreamStyle Style) {
    InBuf = std::move(InData);
    In = {fmemopen(&InBuf[0], InBuf.size(), "r"), &fclose};
    return newJSONTransport(In.get(), Out, &Mirror, Pretty, Style);
  }

  std::string input() const { return InBuf; }
  std::string output() { return Out.str(); }
  std::string input_mirror() { return Mirror.str(); }
};

// Echo is a simple server running on a transport:
//   - logs each message it gets.
//   - when it gets a call, replies to it
//   - when it gets a notification for method "call", makes a call on Target
// Hangs up when it gets an exit notification.
class Echo : public Transport::MessageHandler {
  Transport &Target;
  std::string LogBuf;
  llvm::raw_string_ostream Log;

public:
  Echo(Transport &Target) : Target(Target), Log(LogBuf) {}

  std::string log() { return Log.str(); }

  bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override {
    Log << "Notification " << Method << ": " << Params << "\n";
    if (Method == "call")
      Target.call("echo call", std::move(Params), 42);
    return Method != "exit";
  }

  bool onCall(llvm::StringRef Method, llvm::json::Value Params,
              llvm::json::Value ID) override {
    Log << "Call " << Method << "(" << ID << "): " << Params << "\n";
    if (Method == "err")
      Target.reply(
          ID, llvm::make_error<LSPError>("trouble at mill", ErrorCode(88)));
    else
      Target.reply(ID, std::move(Params));
    return true;
  }

  bool onReply(llvm::json::Value ID,
               llvm::Expected<llvm::json::Value> Params) override {
    if (Params)
      Log << "Reply(" << ID << "): " << *Params << "\n";
    else
      Log << "Reply(" << ID
          << "): error = " << llvm::toString(Params.takeError()) << "\n";
    return true;
  }
};

std::string trim(llvm::StringRef S) { return S.trim().str(); }

// Runs an Echo session using the standard JSON-RPC format we use in production.
TEST_F(JSONTransportTest, StandardDense) {
  auto T = transport(
      "Content-Length: 52\r\n\r\n"
      R"({"jsonrpc": "2.0", "method": "call", "params": 1234})"
      "Content-Length: 46\r\n\r\n"
      R"({"jsonrpc": "2.0", "id": 1234, "result": 5678})"
      "Content-Length: 67\r\n\r\n"
      R"({"jsonrpc": "2.0", "method": "foo", "id": "abcd", "params": "efgh"})"
      "Content-Length: 73\r\n\r\n"
      R"({"jsonrpc": "2.0", "id": "xyz", "error": {"code": 99, "message": "bad!"}})"
      "Content-Length: 68\r\n\r\n"
      R"({"jsonrpc": "2.0", "method": "err", "id": "wxyz", "params": "boom!"})"
      "Content-Length: 36\r\n\r\n"
      R"({"jsonrpc": "2.0", "method": "exit"})",
      /*Pretty=*/false, JSONStreamStyle::Standard);
  Echo E(*T);
  auto Err = T->loop(E);
  EXPECT_FALSE(bool(Err)) << toString(std::move(Err));

  const char *WantLog = R"(
Notification call: 1234
Reply(1234): 5678
Call foo("abcd"): "efgh"
Reply("xyz"): error = 99: bad!
Call err("wxyz"): "boom!"
Notification exit: null
  )";
  EXPECT_EQ(trim(E.log()), trim(WantLog));
  const char *WantOutput =
      "Content-Length: 60\r\n\r\n"
      R"({"id":42,"jsonrpc":"2.0","method":"echo call","params":1234})"
      "Content-Length: 45\r\n\r\n"
      R"({"id":"abcd","jsonrpc":"2.0","result":"efgh"})"
      "Content-Length: 77\r\n\r\n"
      R"({"error":{"code":88,"message":"trouble at mill"},"id":"wxyz","jsonrpc":"2.0"})";
  EXPECT_EQ(output(), WantOutput);
  EXPECT_EQ(trim(input_mirror()), trim(input()));
}

// Runs an Echo session using the "delimited" input and pretty-printed output
// that we use in lit tests.
TEST_F(JSONTransportTest, DelimitedPretty) {
  auto T = transport(R"jsonrpc(
{"jsonrpc": "2.0", "method": "call", "params": 1234}
---
{"jsonrpc": "2.0", "id": 1234, "result": 5678}
---
{"jsonrpc": "2.0", "method": "foo", "id": "abcd", "params": "efgh"}
---
{"jsonrpc": "2.0", "id": "xyz", "error": {"code": 99, "message": "bad!"}}
---
{"jsonrpc": "2.0", "method": "err", "id": "wxyz", "params": "boom!"}
---
{"jsonrpc": "2.0", "method": "exit"}
  )jsonrpc",
                     /*Pretty=*/true, JSONStreamStyle::Delimited);
  Echo E(*T);
  auto Err = T->loop(E);
  EXPECT_FALSE(bool(Err)) << toString(std::move(Err));

  const char *WantLog = R"(
Notification call: 1234
Reply(1234): 5678
Call foo("abcd"): "efgh"
Reply("xyz"): error = 99: bad!
Call err("wxyz"): "boom!"
Notification exit: null
  )";
  EXPECT_EQ(trim(E.log()), trim(WantLog));
  const char *WantOutput = "Content-Length: 77\r\n\r\n"
                           R"({
  "id": 42,
  "jsonrpc": "2.0",
  "method": "echo call",
  "params": 1234
})"
                           "Content-Length: 58\r\n\r\n"
                           R"({
  "id": "abcd",
  "jsonrpc": "2.0",
  "result": "efgh"
})"
                           "Content-Length: 105\r\n\r\n"
                           R"({
  "error": {
    "code": 88,
    "message": "trouble at mill"
  },
  "id": "wxyz",
  "jsonrpc": "2.0"
})";
  EXPECT_EQ(output(), WantOutput);
  EXPECT_EQ(trim(input_mirror()), trim(input()));
}

// IO errors such as EOF ane reported.
// The only successful return from loop() is if a handler returned false.
TEST_F(JSONTransportTest, EndOfFile) {
  auto T = transport("Content-Length: 52\r\n\r\n"
                     R"({"jsonrpc": "2.0", "method": "call", "params": 1234})",
                     /*Pretty=*/false, JSONStreamStyle::Standard);
  Echo E(*T);
  auto Err = T->loop(E);
  EXPECT_EQ(trim(E.log()), "Notification call: 1234");
  EXPECT_TRUE(bool(Err)); // Ran into EOF with no handler signalling done.
  consumeError(std::move(Err));
  EXPECT_EQ(trim(input_mirror()), trim(input()));
}

#endif

} // namespace
} // namespace clangd
} // namespace clang