summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeServerLib/testServerBuffering.cpp
blob: 6f22940678ad6fef13011bc37f76f3000c729ee2 (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
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "cmConnection.h"
#include "cmServerConnection.h"

void print_error(const std::vector<std::string>& input,
                 const std::vector<std::string>& output)
{
  std::cerr << "Responses don't equal input messages input." << std::endl;
  std::cerr << "Responses: " << std::endl;

  for (auto& msg : output) {
    std::cerr << "'" << msg << "'" << std::endl;
  }

  std::cerr << "Input messages" << std::endl;
  for (auto& msg : input) {
    std::cerr << "'" << msg << "'" << std::endl;
  }
}

std::string trim_newline(const std::string& _buffer)
{
  auto buffer = _buffer;
  while (!buffer.empty() && (buffer.back() == '\n' || buffer.back() == '\r')) {
    buffer.pop_back();
  }
  return buffer;
}

int testServerBuffering(int, char** const)
{
  std::vector<std::string> messages = {
    "{ \"test\": 10}", "{ \"test\": { \"test2\": false} }",
    "{ \"test\": [1, 2, 3] }",
    "{ \"a\": { \"1\": {}, \n\n\n \"2\":[] \t\t\t\t}}"
  };

  std::string fullMessage;
  for (auto& msg : messages) {
    fullMessage += "[== \"CMake Server\" ==[\n";
    fullMessage += msg;
    fullMessage += "\n]== \"CMake Server\" ==]\n";
  }

  // The buffering strategy should cope with any fragmentation, including
  // just getting the characters one at a time.
  auto bufferingStrategy =
    std::unique_ptr<cmConnectionBufferStrategy>(new cmServerBufferStrategy);
  std::vector<std::string> response;
  std::string rawBuffer;
  for (auto& messageChar : fullMessage) {
    rawBuffer += messageChar;
    std::string packet = bufferingStrategy->BufferMessage(rawBuffer);
    do {
      if (!packet.empty() && packet != "\r\n") {
        response.push_back(trim_newline(packet));
      }
      packet = bufferingStrategy->BufferMessage(rawBuffer);
    } while (!packet.empty());
  }

  if (response != messages) {
    print_error(messages, response);
    return 1;
  }

  // We should also be able to deal with getting a bunch at once
  response.clear();
  std::string packet = bufferingStrategy->BufferMessage(fullMessage);
  do {
    if (!packet.empty() && packet != "\r\n") {
      response.push_back(trim_newline(packet));
    }
    packet = bufferingStrategy->BufferMessage(fullMessage);
  } while (!packet.empty());

  if (response != messages) {
    print_error(messages, response);
    return 1;
  }

  return 0;
}