text:
#include "source/common/common/hex.h"
#include "source/common/http/utility.h"
#include "source/common/network/io_socket_handle_impl.h"
#include "source/common/network/listener_filter_buffer_impl.h"
#include "source/extensions/filters/listener/http_inspector/http_inspector.h"

#include "test/mocks/api/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/stats/mocks.h"
#include "test/test_common/threadsafe_singleton_injector.h"

#include "gtest/gtest.h"

using testing::_;
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::NiceMock;
using testing::Return;
using testing::ReturnNew;
using testing::ReturnRef;
using testing::SaveArg;

namespace Envoy {
namespace Extensions {
namespace ListenerFilters {
namespace HttpInspector {
namespace {

class HttpInspectorTest : public testing::Test {
public:
  HttpInspectorTest()
      : cfg_(std::make_shared<Config>(store_)),
        io_handle_(
            Network::SocketInterfaceImpl::makePlatformSpecificSocket(42, false, absl::nullopt)) {}
  ~HttpInspectorTest() override { io_handle_->close(); }

  void init() {
    filter_ = std::make_unique<Filter>(cfg_);

    EXPECT_CALL(cb_, socket()).WillRepeatedly(ReturnRef(socket_));
    EXPECT_CALL(socket_, detectedTransportProtocol()).WillRepeatedly(Return("raw_buffer"));
    EXPECT_CALL(cb_, dispatcher()).WillRepeatedly(ReturnRef(dispatcher_));
    EXPECT_CALL(testing::Const(socket_), ioHandle()).WillRepeatedly(ReturnRef(*io_handle_));
    EXPECT_CALL(socket_, ioHandle()).WillRepeatedly(ReturnRef(*io_handle_));
    EXPECT_CALL(dispatcher_,
                createFileEvent_(_, _, Event::PlatformDefaultTriggerType,
                                 Event::FileReadyType::Read | Event::FileReadyType::Closed))
        .WillOnce(
            DoAll(SaveArg<1>(&file_event_callback_), ReturnNew<NiceMock<Event::MockFileEvent>>()));
    buffer_ = std::make_unique<Network::ListenerFilterBufferImpl>(
        *io_handle_, dispatcher_, [](bool) {}, [](Network::ListenerFilterBuffer&) {},
        filter_->maxReadBytes());
  }

  void testHttpInspectMultipleReadsNotFound(absl::string_view header, bool http2 = false) {
    init();
    const std::vector<uint8_t> data = Hex::decode(std::string(header));
    {
      InSequence s;

#ifdef WIN32
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
      if (http2) {
        for (size_t i = 0; i < data.size(); i++) {
          EXPECT_CALL(os_sys_calls_, readv(_, _, _))
              .WillOnce(Invoke(
                  [&data, i](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                    ASSERT(iov->iov_len >= data.size());
                    memcpy(iov->iov_base, data.data() + i, 1);
                    return Api::SysCallSizeResult{ssize_t(1), 0};
                  }))
              .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
        }
      } else {
        for (size_t i = 0; i < header.size(); i++) {
          EXPECT_CALL(os_sys_calls_, readv(_, _, _))
              .WillOnce(Invoke(
                  [&header, i](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                    ASSERT(iov->iov_len >= header.size());
                    memcpy(iov->iov_base, header.data() + i, 1);
                    return Api::SysCallSizeResult{ssize_t(1), 0};
                  }))
              .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
        }
      }
#else
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() {
        return Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN};
      }));
      if (http2) {
        for (size_t i = 1; i <= data.size(); i++) {
          EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
              .WillOnce(Invoke(
                  [&data, i](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                    ASSERT(length >= i);
                    memcpy(buffer, data.data(), i);
                    return Api::SysCallSizeResult{ssize_t(i), 0};
                  }));
        }
      } else {
        for (size_t i = 1; i <= header.size(); i++) {
          EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
              .WillOnce(Invoke([&header, i](os_fd_t, void* buffer, size_t length,
                                            int) -> Api::SysCallSizeResult {
                ASSERT(length >= i);
                memcpy(buffer, header.data(), i);
                return Api::SysCallSizeResult{ssize_t(i), 0};
              }));
        }
      }
#endif
    }
    bool got_continue = false;
    EXPECT_CALL(socket_, setRequestedApplicationProtocols(_)).Times(0);
    auto accepted = filter_->onAccept(cb_);
    EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
    while (!got_continue) {
      file_event_callback_(Event::FileReadyType::Read);
      auto status = filter_->onData(*buffer_);
      if (status == Network::FilterStatus::Continue) {
        got_continue = true;
      }
    }

    EXPECT_EQ(1, cfg_->stats().http_not_found_.value());
  }

  void testHttpInspectMultipleReadsFound(absl::string_view header, absl::string_view alpn) {
    init();
    const std::vector<absl::string_view> alpn_protos{alpn};
    const std::vector<uint8_t> data = Hex::decode(std::string(header));
    {
      InSequence s;

#ifdef WIN32
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
      if (alpn == Http::Utility::AlpnNames::get().Http2c) {
        for (size_t i = 0; i < 24; i++) {
          EXPECT_CALL(os_sys_calls_, readv(_, _, _))
              .WillOnce(Invoke(
                  [&data, i](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                    ASSERT(iov->iov_len >= data.size());
                    memcpy(iov->iov_base, data.data() + i, 1);
                    return Api::SysCallSizeResult{ssize_t(1), 0};
                  }))
              .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
        }
      } else {
        for (size_t i = 0; i < header.size(); i++) {
          EXPECT_CALL(os_sys_calls_, readv(_, _, _))
              .WillOnce(Invoke(
                  [&header, i](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                    ASSERT(iov->iov_len >= header.size());
                    memcpy(iov->iov_base, header.data() + i, 1);
                    return Api::SysCallSizeResult{ssize_t(1), 0};
                  }))
              .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
        }
      }
#else
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() {
        return Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN};
      }));

      if (alpn == Http::Utility::AlpnNames::get().Http2c) {
        for (size_t i = 0; i <= 24; i++) {
          EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
              .WillOnce(Invoke(
                  [&data, i](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                    ASSERT(length >= i);
                    memcpy(buffer, data.data(), i);
                    return Api::SysCallSizeResult{ssize_t(i), 0};
                  }));
        }
      } else {
        for (size_t i = 0; i <= header.size(); i++) {
          EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
              .WillOnce(Invoke([&header, i](os_fd_t, void* buffer, size_t length,
                                            int) -> Api::SysCallSizeResult {
                ASSERT(length >= i);
                memcpy(buffer, header.data(), i);
                return Api::SysCallSizeResult{ssize_t(i), 0};
              }));
        }
      }
#endif
    }

    bool got_continue = false;
    EXPECT_CALL(socket_, setRequestedApplicationProtocols(alpn_protos));
    auto accepted = filter_->onAccept(cb_);
    EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
    while (!got_continue) {
      file_event_callback_(Event::FileReadyType::Read);
      auto status = filter_->onData(*buffer_);
      if (status == Network::FilterStatus::Continue) {
        got_continue = true;
      }
    }
    if (alpn == Http::Utility::AlpnNames::get().Http11) {
      EXPECT_EQ(1, cfg_->stats().http11_found_.value());
    } else if (alpn == Http::Utility::AlpnNames::get().Http10) {
      EXPECT_EQ(1, cfg_->stats().http10_found_.value());
    } else if (alpn == Http::Utility::AlpnNames::get().Http2c) {
      EXPECT_EQ(1, cfg_->stats().http2_found_.value());
    } else {
      EXPECT_EQ(alpn, "unknow alpn");
    }
  }

  void testHttpInspectFound(absl::string_view header, absl::string_view alpn) {
    init();
    std::vector<uint8_t> data = Hex::decode(std::string(header));
#ifdef WIN32
    if (alpn == Http::Utility::AlpnNames::get().Http2c) {
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(
              Invoke([&data](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                ASSERT(iov->iov_len >= data.size());
                memcpy(iov->iov_base, data.data(), data.size());
                return Api::SysCallSizeResult{ssize_t(data.size()), 0};
              }))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    } else {
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(
              Invoke([&header](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                ASSERT(iov->iov_len >= header.size());
                memcpy(iov->iov_base, header.data(), header.size());
                return Api::SysCallSizeResult{ssize_t(header.size()), 0};
              }))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    }
#else
    if (alpn == Http::Utility::AlpnNames::get().Http2c) {
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
          .WillOnce(
              Invoke([&data](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                ASSERT(length >= data.size());
                memcpy(buffer, data.data(), data.size());
                return Api::SysCallSizeResult{ssize_t(data.size()), 0};
              }));
    } else {
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
          .WillOnce(Invoke(
              [&header](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                ASSERT(length >= header.size());
                memcpy(buffer, header.data(), header.size());
                return Api::SysCallSizeResult{ssize_t(header.size()), 0};
              }));
    }
#endif
    const std::vector<absl::string_view> alpn_protos{alpn};

    EXPECT_CALL(socket_, setRequestedApplicationProtocols(alpn_protos));
    auto accepted = filter_->onAccept(cb_);
    EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
    file_event_callback_(Event::FileReadyType::Read);
    auto status = filter_->onData(*buffer_);
    EXPECT_EQ(status, Network::FilterStatus::Continue);
    if (alpn == Http::Utility::AlpnNames::get().Http11) {
      EXPECT_EQ(1, cfg_->stats().http11_found_.value());
    } else if (alpn == Http::Utility::AlpnNames::get().Http10) {
      EXPECT_EQ(1, cfg_->stats().http10_found_.value());
    } else if (alpn == Http::Utility::AlpnNames::get().Http2c) {
      EXPECT_EQ(1, cfg_->stats().http2_found_.value());
    } else {
      EXPECT_EQ(alpn, "unknow alpn");
    }
  }

  void testHttpInspectNotFound(absl::string_view header, bool http2 = false) {
    init();
    std::vector<uint8_t> data = Hex::decode(std::string(header));
#ifdef WIN32
    if (http2) {
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(
              Invoke([&data](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                ASSERT(iov->iov_len >= data.size());
                memcpy(iov->iov_base, data.data(), data.size());
                return Api::SysCallSizeResult{ssize_t(data.size()), 0};
              }))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    } else {
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(
              Invoke([&header](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                ASSERT(iov->iov_len >= header.size());
                memcpy(iov->iov_base, header.data(), header.size());
                return Api::SysCallSizeResult{ssize_t(header.size()), 0};
              }))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    }
#else
    if (http2) {
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
          .WillOnce(
              Invoke([&data](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                ASSERT(length >= data.size());
                memcpy(buffer, data.data(), data.size());
                return Api::SysCallSizeResult{ssize_t(data.size()), 0};
              }));
    } else {
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
          .WillOnce(Invoke(
              [&header](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                ASSERT(length >= header.size());
                memcpy(buffer, header.data(), header.size());
                return Api::SysCallSizeResult{ssize_t(header.size()), 0};
              }));
    }
#endif
    EXPECT_CALL(socket_, setRequestedApplicationProtocols(_)).Times(0);
    auto accepted = filter_->onAccept(cb_);
    EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
    file_event_callback_(Event::FileReadyType::Read);
    auto status = filter_->onData(*buffer_);
    EXPECT_EQ(status, Network::FilterStatus::Continue);
    EXPECT_EQ(1, cfg_->stats().http_not_found_.value());
  }

  NiceMock<Api::MockOsSysCalls> os_sys_calls_;
  TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls_{&os_sys_calls_};
  Stats::IsolatedStoreImpl store_;
  ConfigSharedPtr cfg_;
  std::unique_ptr<Filter> filter_;
  Network::MockListenerFilterCallbacks cb_;
  Network::MockConnectionSocket socket_;
  NiceMock<Event::MockDispatcher> dispatcher_;
  Event::FileReadyCb file_event_callback_;
  Network::IoHandlePtr io_handle_;
  std::unique_ptr<Network::ListenerFilterBufferImpl> buffer_;
};

TEST_F(HttpInspectorTest, SkipHttpInspectForTLS) {
  filter_ = std::make_unique<Filter>(cfg_);

  EXPECT_CALL(cb_, socket()).WillRepeatedly(ReturnRef(socket_));
  EXPECT_CALL(socket_, ioHandle()).WillRepeatedly(ReturnRef(*io_handle_));
  EXPECT_CALL(socket_, detectedTransportProtocol()).WillRepeatedly(Return("TLS"));
  EXPECT_EQ(filter_->onAccept(cb_), Network::FilterStatus::Continue);
}

TEST_F(HttpInspectorTest, InlineReadInspectHttp10) {
  const absl::string_view header =
      "GET /anything HTTP/1.0\r\nhost: google.com\r\nuser-agent: curl/7.64.0\r\naccept: "
      "*/*\r\nx-forwarded-proto: http\r\nx-request-id: "
      "a52df4a0-ed00-4a19-86a7-80e5049c6c84\r\nx-envoy-expected-rq-timeout-ms: "
      "15000\r\ncontent-length: 0\r\n\r\n";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http10);
}

TEST_F(HttpInspectorTest, InlineReadParseError) {
  const absl::string_view header =
      "NOT_A_LEGAL_PREFIX /anything HTTP/1.0\r\nhost: google.com\r\nuser-agent: "
      "curl/7.64.0\r\naccept: "
      "*/*\r\nx-forwarded-proto: http\r\nx-request-id: "
      "a52df4a0-ed00-4a19-86a7-80e5049c6c84\r\nx-envoy-expected-rq-timeout-ms: "
      "15000\r\ncontent-length: 0\r\n\r\n";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InspectHttp10) {
  const absl::string_view header =
      "GET /anything HTTP/1.0\r\nhost: google.com\r\nuser-agent: curl/7.64.0\r\naccept: "
      "*/*\r\nx-forwarded-proto: http\r\nx-request-id: "
      "a52df4a0-ed00-4a19-86a7-80e5049c6c84\r\nx-envoy-expected-rq-timeout-ms: "
      "15000\r\ncontent-length: 0\r\n\r\n";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http10);
}

TEST_F(HttpInspectorTest, InspectHttp11) {
  const absl::string_view header =
      "GET /anything HTTP/1.1\r\nhost: google.com\r\nuser-agent: curl/7.64.0\r\naccept: "
      "*/*\r\nx-forwarded-proto: http\r\nx-request-id: "
      "a52df4a0-ed00-4a19-86a7-80e5049c6c84\r\nx-envoy-expected-rq-timeout-ms: "
      "15000\r\ncontent-length: 0\r\n\r\n";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http11);
}

TEST_F(HttpInspectorTest, InspectHttp11WithNonEmptyRequestBody) {
  const absl::string_view header =
      "GET /anything HTTP/1.1\r\nhost: google.com\r\nuser-agent: curl/7.64.0\r\naccept: "
      "*/*\r\nx-forwarded-proto: http\r\nx-request-id: "
      "a52df4a0-ed00-4a19-86a7-80e5049c6c84\r\nx-envoy-expected-rq-timeout-ms: "
      "15000\r\ncontent-length: 3\r\n\r\nfoo";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http11);
}

TEST_F(HttpInspectorTest, ExtraSpaceInRequestLine) {
  const absl::string_view header = "GET  /anything  HTTP/1.1\r\n\r\n";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http11);
}

TEST_F(HttpInspectorTest, InvalidHttpMethod) {
  const absl::string_view header = "BAD /anything HTTP/1.1";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InvalidHttpRequestLine) {
  const absl::string_view header = "BAD /anything HTTP/1.1\r\n";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, OldHttpProtocol) {
  const absl::string_view header = "GET /anything HTTP/0.9\r\n";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http10);
}

TEST_F(HttpInspectorTest, InvalidRequestLine) {
  const absl::string_view header = "GET /anything HTTP/1.1 BadRequestLine\r\n";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InvalidRequestLine2) {
  const absl::string_view header = "\r\n\r\n\r\n";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InvalidRequestLine3) {
  const absl::string_view header = "\r\n\r\n\r\n BAD";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InvalidRequestLine4) {
  const absl::string_view header = "\r\nGET /anything HTTP/1.1\r\n";
  testHttpInspectNotFound(header);
}

TEST_F(HttpInspectorTest, InspectHttp2) {
  const std::string header =
      "505249202a20485454502f322e300d0a0d0a534d0d0a0d0a00000c04000000000000041000000000020000000000"
      "00040800000000000fff000100007d010500000001418aa0e41d139d09b8f0000f048860757a4ce6aa660582867a"
      "8825b650c3abb8d2e053032a2f2a408df2b4a7b3c0ec90b22d5d8749ff839d29af4089f2b585ed6950958d279a18"
      "9e03f1ca5582265f59a75b0ac3111959c7e49004908db6e83f4096f2b16aee7f4b17cd65224b22d6765926a4a7b5"
      "2b528f840b60003f";
  testHttpInspectFound(header, Http::Utility::AlpnNames::get().Http2c);
}

TEST_F(HttpInspectorTest, InvalidConnectionPreface) {
  init();

  const std::string header = "505249202a20485454502f322e300d0a";
  std::vector<uint8_t> data = Hex::decode(std::string(header));
#ifdef WIN32
  EXPECT_CALL(os_sys_calls_, readv(_, _, _))
      .WillOnce(Invoke([&data](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
        ASSERT(iov->iov_len >= data.size());
        memcpy(iov->iov_base, data.data(), data.size());
        return Api::SysCallSizeResult{ssize_t(data.size()), 0};
      }))
      .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
#else
  EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
      .WillOnce(
          Invoke([&data](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
            ASSERT(length >= data.size());
            memcpy(buffer, data.data(), data.size());
            return Api::SysCallSizeResult{ssize_t(data.size()), 0};
          }));
#endif
  EXPECT_CALL(socket_, setRequestedApplicationProtocols(_)).Times(0);
  auto accepted = filter_->onAccept(cb_);
  EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
  file_event_callback_(Event::FileReadyType::Read);
  auto status = filter_->onData(*buffer_);
  EXPECT_EQ(status, Network::FilterStatus::StopIteration);
  EXPECT_EQ(0, cfg_->stats().http_not_found_.value());
}

TEST_F(HttpInspectorTest, MultipleReadsHttp2) {
  const std::string header =
      "505249202a20485454502f322e300d0a0d0a534d0d0a0d0a00000c04000000000000041000000000020000000000"
      "00040800000000000fff000100007d010500000001418aa0e41d139d09b8f0000f048860757a4ce6aa660582867a"
      "8825b650c3abb8d2e053032a2f2a408df2b4a7b3c0ec90b22d5d8749ff839d29af4089f2b585ed6950958d279a18"
      "9e03f1ca5582265f59a75b0ac3111959c7e49004908db6e83f4096f2b16aee7f4b17cd65224b22d6765926a4a7b5"
      "2b528f840b60003f";
  testHttpInspectMultipleReadsFound(header, Http::Utility::AlpnNames::get().Http2c);
}

TEST_F(HttpInspectorTest, MultipleReadsHttp2BadPreface) {
  const std::string header = "505249202a20485454502f322e300d0a0d0c";
  testHttpInspectMultipleReadsNotFound(header, true);
}

TEST_F(HttpInspectorTest, MultipleReadsHttp1) {
  const absl::string_view data = "GET /anything HTTP/1.0\r";
  testHttpInspectMultipleReadsFound(data, Http::Utility::AlpnNames::get().Http10);
}

TEST_F(HttpInspectorTest, MultipleReadsHttp1IncompleteBadHeader) {
  const absl::string_view data = "X";
  testHttpInspectMultipleReadsNotFound(data);
}

TEST_F(HttpInspectorTest, MultipleReadsHttp1BadProtocol) {
#ifdef ENVOY_ENABLE_UHV
  // permissive parsing
  return;
#endif

  const std::string valid_header = "GET /index HTTP/1.1\r";
  //  offset:                       0         10
  const std::string truncate_header = valid_header.substr(0, 14).append("\r");
  testHttpInspectMultipleReadsNotFound(truncate_header);
}

TEST_F(HttpInspectorTest, Http1WithLargeRequestLine) {
  // Verify that the http inspector can detect http requests
  // with large request line even when they are split over
  // multiple recv calls.
  init();
  absl::string_view method = "GET", http = "/index HTTP/1.0\r";
  std::string spaces(Config::MAX_INSPECT_SIZE - method.size() - http.size(), ' ');
  const std::string data = absl::StrCat(method, spaces, http);
  {
    InSequence s;
#ifdef WIN32
    EXPECT_CALL(os_sys_calls_, readv(_, _, _))
        .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
#else
    EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() {
      return Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN};
    }));
#endif

    uint64_t num_loops = Config::MAX_INSPECT_SIZE;
#if defined(__has_feature) &&                                                                      \
    ((__has_feature(thread_sanitizer)) || (__has_feature(address_sanitizer)))
    num_loops = 2;
#endif

#ifdef WIN32
    auto ctr = std::make_shared<size_t>(0);
    auto copy_len = std::make_shared<size_t>(1);
    EXPECT_CALL(os_sys_calls_, readv(_, _, _))
        .Times(num_loops)
        .WillRepeatedly(
            Invoke([&data, ctr, copy_len, num_loops](os_fd_t fd, const iovec* iov,
                                                     int iovcnt) -> Api::SysCallSizeResult {
              ASSERT(iov->iov_len >= 1);
              memcpy(iov->iov_base, data.data() + *ctr, 1);
              *ctr += 1;
              return Api::SysCallSizeResult{ssize_t(1), 0};
            }));
#else
    auto ctr = std::make_shared<size_t>(1);
    EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
        .Times(num_loops)
        .WillRepeatedly(Invoke([&data, ctr, num_loops](os_fd_t, void* buffer, size_t length,
                                                       int) -> Api::SysCallSizeResult {
          size_t len = (*ctr);
          if (num_loops == 2) {
            ASSERT(*ctr != 3);
            len = size_t(Config::MAX_INSPECT_SIZE / (3 - (*ctr)));
          }
          ASSERT(length >= len);
          memcpy(buffer, data.data(), len);
          *ctr += 1;
          return Api::SysCallSizeResult{ssize_t(len), 0};
        }));
#endif
  }

  bool got_continue = false;
  const std::vector<absl::string_view> alpn_protos{Http::Utility::AlpnNames::get().Http10};
  EXPECT_CALL(socket_, setRequestedApplicationProtocols(alpn_protos));
  auto accepted = filter_->onAccept(cb_);
  EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
  while (!got_continue) {
    file_event_callback_(Event::FileReadyType::Read);
    auto status = filter_->onData(*buffer_);
    if (status == Network::FilterStatus::Continue) {
      got_continue = true;
    }
  }
  EXPECT_EQ(1, cfg_->stats().http10_found_.value());
}

TEST_F(HttpInspectorTest, Http1WithLargeHeader) {
  init();
  absl::string_view request = "GET /index HTTP/1.0\rfield: ";
  //                           0                  20
  std::string value(Config::MAX_INSPECT_SIZE - request.size(), 'a');
  const std::string data = absl::StrCat(request, value);

  {
    InSequence s;
#ifdef WIN32
    EXPECT_CALL(os_sys_calls_, readv(_, _, _))
        .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    for (size_t i = 0; i < 20; i++) {
      EXPECT_CALL(os_sys_calls_, readv(_, _, _))
          .WillOnce(Invoke(
              [&data, i](os_fd_t fd, const iovec* iov, int iovcnt) -> Api::SysCallSizeResult {
                ASSERT(iov->iov_len >= 20);
                memcpy(iov->iov_base, data.data() + i, 1);
                return Api::SysCallSizeResult{ssize_t(1), 0};
              }))
          .WillOnce(Return(Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN}));
    }
#else
    EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() {
      return Api::SysCallSizeResult{ssize_t(-1), SOCKET_ERROR_AGAIN};
    }));

    for (size_t i = 1; i <= 20; i++) {
      EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK))
          .WillOnce(Invoke(
              [&data, i](os_fd_t, void* buffer, size_t length, int) -> Api::SysCallSizeResult {
                ASSERT(length >= data.size());
                memcpy(buffer, data.data(), i);
                return Api::SysCallSizeResult{ssize_t(i), 0};
              }));
    }
#endif
  }

  bool got_continue = false;
  const std::vector<absl::string_view> alpn_protos{Http::Utility::AlpnNames::get().Http10};
  EXPECT_CALL(socket_, setRequestedApplicationProtocols(alpn_protos));
  auto accepted = filter_->onAccept(cb_);
  EXPECT_EQ(accepted, Network::FilterStatus::StopIteration);
  while (!got_continue) {
    file_event_callback_(Event::FileReadyType::Read);
    auto status = filter_->onData(*buffer_);
    if (status == Network::FilterStatus::Continue) {
      got_continue = true;
    }
  }
  EXPECT_EQ(1, cfg_->stats().http10_found_.value());
}

} // namespace
} // namespace HttpInspector
} // namespace ListenerFilters
} // namespace Extensions
} // namespace Envoy


text:
class Ticker:
    def __init__(self):
        self.to_run = []

    def tick(self, function):
        self.to_run.append(function)
        return function

    def tick_all(self):
        for function in self.to_run:
            function()

default_ticker = Ticker()

redpajama_set_name: RedPajamaGithub


text:
import Vue from 'vue';
import app from '../core/app';
import routy from 'routy';
import clone from 'clone';

/*
 * App Router
 *
 * Initialises and defines route behaviour
 */

var router = new routy.Router(),
    wrap = document.querySelector('[data-role="view"]'),
    view;

export default router;

/*
 * Router behaviour config
 */
router.init = function () {
    router
    .on('change', updateRoute)
    .html5()
    .run();
};

/*
 * Handle route change
 *
 * @param {Object} route
 */
function updateRoute(route) {
    var options = route.route.options,
        controller = options.controller ? options.controller.default : {},
        scope = clone(controller.data || {});

    if (view) { view.$destroy(); }

    wrap.innerHTML = options.template || '';
    app.title = options.title || null;

    view = new Vue({
        data          : scope,
        el            : wrap,
        methods       : controller.methods || {},
        beforeDestroy : controller.beforeDestroy || null
    });

    if (typeof controller.ready === 'function') {
        controller.ready.apply(view, [ route, scope ]);
    }
}


text:
namespace rime {

class Candidate {
 public:
  Candidate() = default;
  Candidate(const string type,
            size_t start,
            size_t end,
            double quality = 0.)
      : type_(type), start_(start), end_(end), quality_(quality) {}
  virtual ~Candidate() = default;

  static an<Candidate>
  GetGenuineCandidate(const an<Candidate>& cand);
  static vector<of<Candidate>>
  GetGenuineCandidates(const an<Candidate>& cand);

  // recognized by translators in learning phase
  const string& type() const { return type_; }
  // [start, end) mark a range in the input that the candidate corresponds to
  size_t start() const { return start_; }
  size_t end() const { return end_; }
  double quality() const { return quality_; }

  // candidate text to commit
  virtual const string& text() const = 0;
  // (optional)
  virtual string comment() const { return string(); }
  // text shown in the preedit area, replacing input string (optional)
  virtual string preedit() const { return string(); }

  void set_type(const string& type) { type_ = type; }
  void set_start(size_t start) { start_ = start; }
  void set_end(size_t end) { end_ = end; }
  void set_quality(double quality) { quality_ = quality; }

 private:
  string type_;
  size_t start_ = 0;
  size_t end_ = 0;
  double quality_ = 0.;
};

using CandidateQueue = list<of<Candidate>>;
using CandidateList = vector<of<Candidate>>;

// useful implimentations

class SimpleCandidate : public Candidate {
 public:
  SimpleCandidate() = default;
  SimpleCandidate(const string type,
                  size_t start,
                  size_t end,
                  const string& text,
                  const string& comment = string(),
                  const string& preedit = string())
      : Candidate(type, start, end),
      text_(text), comment_(comment), preedit_(preedit) {}

  const string& text() const { return text_; }
  string comment() const { return comment_; }
  string preedit() const { return preedit_; }

  void set_text(const string& text) { text_ = text; }
  void set_comment(const string& comment) { comment_ = comment; }
  void set_preedit(const string& preedit) { preedit_ = preedit; }

 protected:
  string text_;
  string comment_;
  string preedit_;
};

class ShadowCandidate : public Candidate {
 public:
  ShadowCandidate(const an<Candidate>& item,
                  const string& type,
                  const string& text = string(),
                  const string& comment = string())
      : Candidate(type, item->start(), item->end(), item->quality()),
        text_(text), comment_(comment),
        item_(item) {}

  const string& text() const {
    return text_.empty() ? item_->text() : text_;
  }
  string comment() const {
    return comment_.empty() ? item_->comment() : comment_;
  }
  string preedit() const {
    return item_->preedit();
  }

  const an<Candidate>& item() const { return item_; }

 protected:
  string text_;
  string comment_;
  an<Candidate> item_;
};

class UniquifiedCandidate : public Candidate {
 public:
  UniquifiedCandidate(const an<Candidate>& item,
                      const string& type,
                      const string& text = string(),
                      const string& comment = string())
      : Candidate(type, item->start(), item->end(), item->quality()),
        text_(text), comment_(comment) {
    Append(item);
  }

  const string& text() const {
    return text_.empty() && !items_.empty() ?
        items_.front()->text() : text_;
  }
  string comment() const {
    return comment_.empty() && !items_.empty() ?
        items_.front()->comment() : comment_;
  }
  string preedit() const {
    return !items_.empty() ? items_.front()->preedit() : string();
  }

  void Append(an<Candidate> item) {
    items_.push_back(item);
    if (quality() < item->quality())
      set_quality(item->quality());
  }
  const CandidateList& items() const { return items_; }

 protected:
  string text_;
  string comment_;
  CandidateList items_;
};

}  // namespace rime

#endif  // RIME_CANDIDATE_H_


text:
module InputParser where

import Data
import Text.ParserCombinators.Parsec
import qualified Text.ParserCombinators.Parsec.Token as P
import Text.ParserCombinators.Parsec.Language(haskellStyle)

parseInput :: String -> String -> Either ParseError [Module]
parseInput = parse inputParser

inputParser :: GenParser Char a [Module]
inputParser =
  do
    whiteSpace
    many comment
    m <- many moduleblock
    many comment
    return m

parseStartpoint :: String -> Either ParseError StartPoint
parseStartpoint = parse startPointParser "Command line"

startPointParser :: GenParser Char a StartPoint
startPointParser =
  do
    mname <- modulename
    char '.'
    nt <- nonterminal
    return (StartPoint nt mname)

comment :: GenParser Char a ()
comment =
  do
    string "/*"
    manyTill anyChar (try (string "*/"))
    whiteSpace

moduleblock :: GenParser Char a Module
moduleblock =
  do
    reserved "module"
    name <- modulename
    reserved "{"
    content <- modulecontent
    reserved "}"
    return (Module name content)

modulename :: GenParser Char a String
modulename = identifier <?> "module name"

modulecontent = many statement

statement :: GenParser Char a Statement
statement =
  do
    s <-
      try abstractStatement
      <|> try importStatement
      <|> try overrideStatement
      <|> rule
    reserved ";"
    return s
  <?> "statement"

abstractStatement :: GenParser Char a Statement
abstractStatement =
  do
    reserved "abstract"
    n <- nonterminal
    return (Abstract n)
  <?> "abstract"

importStatement :: GenParser Char a Statement
importStatement =
  do
    reserved "import"
    mname <- modulename
    iopts <- importOptions []
    return (Import mname iopts)
  <?> "import"

importOptions :: [ImportOption] -> GenParser Char a [ImportOption]
importOptions opts =
  do
    reserved "rename"
    n1 <- nonterminal
    reserved "as"
    n2 <- nonterminal
    importOptions (opts ++ [Rename n1 n2])
  <|>
  do
    reserved "drop"
    n <- nonterminal
    importOptions (opts ++ [Drop n])
  <|>
    return opts

overrideStatement :: GenParser Char a Statement
overrideStatement =
  do
    reserved "override"
    r <- rule
    return (Override r)
  <?> "override"

rule :: GenParser Char a Statement
rule =
  do
    n <- nonterminal
    arrow
    rs <- rightside [] ""
    return (Rule n rs)
  <?> "rule"

{--
FIXME(dum8d0g) :
  attempt to commit a crime against 'good programming style' here
--}
-- rightside :: Parsed rightsides -> Previously parsed thing -> All parsed rs.
rightside :: [[Element]] -> String -> GenParser Char a [[Element]]
rightside xss "" =
  do
    pipe
    rightside (xss ++ [[]]) "pipe"
  <|>
  do
    es <- many1 element
    rightside (xss ++ [es]) "elem"
  <|>
    return (xss ++ [[]])
rightside xss "elem" =
  do
    pipe
    rightside xss "pipe"
  <|>
    return xss
rightside xss "pipe" =
  do
    es <- many1 element
    rightside (xss ++ [es]) "elem"
  <|>
  do
    pipe
    rightside (xss ++ [[]]) "pipe"
  <|>
    return (xss ++ [[]])

element :: GenParser Char a Element
element = terminal <|> nonterminal

pipe :: GenParser Char a ()
pipe = reserved "|" <?> "pipe"

arrow :: GenParser Char a ()
arrow = reserved "=>" <|> reserved "->"

nonterminal :: GenParser Char a Element
nonterminal =
  do
    n <- identifier
    return (Nonterminal n)
  <?> "nonterminal"

terminal :: GenParser Char a Element
terminal =
  do
    char '\''
    t <- manyTill anyChar (try (char '\''))
    whiteSpace
    return (Terminal t)
  <?> "terminal"

--lexer :: TokenParser a
lexer = P.makeTokenParser haskellDef

--haskellDef :: LanguageDef a
haskellDef = haskellStyle
            { P.reservedNames = [ "module", "abstract", "import", "override",
            "rename", "as", "drop", "=>", "->", "|", ";", "{", "}" ],
              P.caseSensitive = True
            }

whiteSpace :: GenParser Char a ()
whiteSpace = P.whiteSpace lexer

identifier :: GenParser Char a String
identifier = P.identifier lexer

reserved :: String -> GenParser Char a ()
reserved   = P.reserved lexer

-- EOF


text: export { default } from 'ember-flexberry-gis/components/legends/combine-legend';


text:
#include <QDebug>
#include <QModelIndex>
#include <QFile>
#include <QPixmap>

#include <MWidget>
#include <MImageWidget>
#include <MLabel>
#include <MContentItem>

#include "videoslistitemcreator.h"
#include "videoslistmodel.h"
#include "videoslistproxymodel.h"
#include "videoslist.h"

void VideosListItemCreator::updateCell(const QModelIndex &index, MWidget *cell) const
{
    qDebug() << "VideosListItemCreator::updateCell" << index.row();

    if (!cell)
        return;

    if (!index.isValid()) {
        qWarning() << "Invalid ModelIndex inside VideosListItemCreator::updateCell " << index.row();
        return;
    }

    QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
    if (!model) {
        qWarning() << "Invalid proxy model inside VideosListItemCreator::updateCell !!" << index.row();
        return;
    }

    VideosListProxyModel *proxy = qobject_cast<VideosListProxyModel *>(model);
    if (!proxy) {
        qWarning() << "Invalid proxy model inside VideosListItemCreator::updateCell " << index.row();
        return;
    }

    QModelIndex modelIndex = proxy->mapToSource(index);
    VideosListModel *videosListModel = qobject_cast<VideosListModel *> (proxy->sourceModel());

    qDebug() << "Index model is " << index.row() << "Source Model is " << modelIndex.row();
    const bool isFavourite = qvariant_cast<bool>(videosListModel->data(modelIndex, VideosListModel::IsFavoriteRole));

    static QPixmap marker =  (*MTheme::pixmap("icon-m-common-favorite-mark"));

    MContentItem *contentItem = qobject_cast<MContentItem *>(cell);
    QString C1String = modelIndex.data(VideosListModel::CaptionLevel1Role).toString();
    QString C2String = modelIndex.data(VideosListModel::CaptionLevel2Role).toString();
    QPixmap pixmap = modelIndex.data(VideosListModel::ThumbnailRole).value<QPixmap>();
    contentItem->setTitle(C1String);
    contentItem->setSubtitle(C2String);
    contentItem->setPixmap(pixmap);
    contentItem->boundingRect();
    contentItem->setOptionalPixmap(isFavourite ? marker : QPixmap());
}

MWidget *VideosListItemCreator::createCell(const QModelIndex &index, MWidgetRecycler &recycler) const
{
    MContentItem *cell = dynamic_cast<MContentItem *>(recycler.take(MDetailedListItem::staticMetaObject.className()));

    if (cell == NULL)
    {
        qDebug() << "No recycle, create new cell ";
        cell = new MContentItem(MContentItem::IconAndTwoTextLabels);
        cell->setObjectName("VideosItem");
        //cell->imageWidget()->setObjectName("ThumbnailImage");
        //cell->titleLabelWidget()->setObjectName("TitleLabelWidget");
        //cell->setViewType("VideosItemType");
    }

    QMetaObject::invokeMethod(m_videosList, "handleItemCreated", Qt::DirectConnection, Q_ARG(const QModelIndex &, index), Q_ARG(MWidget *, cell));

    updateCell(index, cell);
    return cell;
}


text:
class Relationships::OrganizationUser < ActiveRecord::Base
  # attr_accessible :organization_id, :user_id

  validates_presence_of :organization_id
  validates_presence_of :user_id

  belongs_to :organization, :class_name => "Organization", :foreign_key => "organization_id"
  belongs_to :user, :class_name => "User", :foreign_key => "user_id"
end


text:
// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//	   Copyright (c) 2017 Citrix ShareFile. All rights reserved.
// </auto-generated>
// ------------------------------------------------------------------------------

package com.citrix.sharefile.api.models;

import java.io.InputStream;
import java.util.ArrayList;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;

import com.google.gson.annotations.SerializedName;
import com.citrix.sharefile.api.*;
import com.citrix.sharefile.api.enumerations.*;
import com.citrix.sharefile.api.models.*;

public class SFStoragePolicy extends SFPolicy {

	@SerializedName("Settings")
	private SFStorageSettings Settings;

		/**
		* The settings managed for this Policy
		*/
	public SFStorageSettings getSettings() {
		return this.Settings;
	}

		/**
		* The settings managed for this Policy
		*/
	public void setSettings(SFStorageSettings settings) {
		this.Settings = settings;
	}

}


text:
<?php

$tabs ='
[
{"caption":"Field", "fields": [
{"field":"field","caption":"Fieldname"},
{"field":"caption","caption":"Caption"},
{"field":"description","caption":"Description","inputTVtype":"textarea"},
{"field":"description_is_code","caption":"Description is Code","inputTVtype":"listbox","description":"Display the description as Form-code. MODX-tags gets parsed there.","inputOptionValues":"no==0||yes==1"},
{"field":"inputTV","caption":"Input TV"},
{"field":"inputTVtype","caption":"Input TV type"},
{"field":"validation","caption":"Validation","description":"Example: required"},
{"field":"configs","caption":"Configs","inputTVtype":"textarea","description":"this can be used either for a migx-TV-configname or for input-properties of any TV-type as json-string"},
{"field":"restrictive_condition","caption":"Restrictive Condition (MODX tags)","inputTVtype":"textarea","description":"An empty result will show this field"},
{"field":"display","caption":"Display","inputTVtype":"listbox","inputOptionValues":"yes==||no==none"}
]},
{"caption":"Mediasources", "fields": [
{"field":"sourceFrom","caption":"source From","inputTVtype":"listbox","inputOptionValues":"config||tv||migx"},
{"field":"sources","caption":"Sources","inputTVtype":"migx","configs":"migxfieldsources"}
]},
{"caption":"Input Options", "fields": [
{"field":"inputOptionValues","caption":"Input Option Values"},
{"field":"default","caption":"Default Value"},
{"field":"useDefaultIfEmpty","caption":"Use Default if Empty","inputTVtype":"listbox","inputOptionValues":"no==0||yes==1","description":"for example, when working with real TVs of resources, you would set this to yes, otherwise, the default-value is only used for new items"}
]}
]
';

$columns = '
[
{
  "header": "ID"
, "width": "10"
, "dataIndex": "MIGX_id"
},
{
  "header": "Fieldname"
, "width": "10"
, "dataIndex": "field"
},
{
  "header": "Caption"
, "width": "10"
, "dataIndex": "caption"
},
{
  "header": "Input TV"
, "width": "10"
, "dataIndex": "inputTV"
}]
';

$this->customconfigs['win_id']= 'migxformtabfields';
$this->customconfigs['tabs']= $this->modx->fromJson($tabs);
$this->customconfigs['columns'] = $this->modx->fromJson($columns);


text:
class PathgeoTwitter:
    import sys
    from datetime import datetime

    '''
        createXLSX: convert tweets array into xlsx file
        input
            1. *tweets (array)
            2. *cols (array): which columns in tweets you want to export
            3. *outputPath (String)
            4. *fileName (String): with XLSX extension, such as "test.xlsx"
            5. ?keyword (string)
            6. ?sheetTitle (string)
        return filepath (string)
    '''
    def createXLSX(self, tweets, cols, outputPath, fileName, keyword=None, sheetTitle='Tweets'):
        from openpyxl import Workbook
        from BeautifulSoup import BeautifulSoup as BS

        try:
            book = Workbook()
            sheet = book.get_active_sheet()

            sheet.title = sheetTitle

            #create columns
            for indx, col in enumerate(cols):
                sheet.cell(row=0, column=indx).value = col.upper()

                #read tweets
            for rowIndx, tweet in enumerate(tweets):
                for colIndx, col in enumerate(cols):
                    if col not in tweet:
                        continue

                    val = ''

                    if col in ('urls', 'hashtags'):
                        if 'entities' in tweet and col in tweet['entities'] and tweet['entities'][col]:
                            if type(tweet['entities'][col][0]) in (str, unicode):
                                val = ', '.join(tweet['entities'][col])
                            elif col == 'urls':
                                val = ', '.join(map(lambda item: item['expanded_url'], tweet['entities'][col]))
                            elif col == 'hashtags':
                                val = ', '.join(map(lambda item: item['text'], tweet['entities'][col]))

                    if col == 'is_retweet':
                        val = '' if 'retweeted_id' not in tweet and 'user' not in tweet else bool(tweet.get('retweeted_id', None))

                    if col == 'retweeted_id':
                        val = tweet.get('retweeted_id', '')

                    if col == 'retweet_count':
                        val = tweet.get('retweet_count', '')

                    if col == 'time_zone' and 'user' in tweet:
                        val = tweet['user'].get('time_zone', '')

                    if col == 'followers_count' and 'user' in tweet:
                        val = tweet['user'].get('followers_count', '')

                    if col == 'friends_count' and 'user' in tweet:
                        val = tweet['user'].get('friends_count', '')

                    if col == 'statuses_count' and 'user' in tweet:
                        val = tweet['user'].get('statuses_count', '')

                    if col == 'language':
                        val = tweet.get('lang', None)
                        val = val or tweet.get('iso_language_code', None)

                    if col == 'location':
                        if 'location' in tweet:
                            val = tweet[col]
                        elif 'user' in tweet and 'location' in tweet['user']:
                            val = tweet['user']['location']

                    if col == 'from_user':
                        if 'from_user' in tweet:
                            val = tweet[col]
                        elif 'user' in tweet and type(tweet['user']) is dict and 'screen_name' in tweet['user']:
                            val = tweet['user']['screen_name']

                    if col == 'from_user_name':
                        if 'from_user_name' in tweet:
                            val = tweet[col]
                        elif 'user' in tweet and type(tweet['user']) is dict and 'name' in tweet['user']:
                            val = tweet['user']['name']

                    if col == "keyword":
                        val = keyword
                    elif col == "city":
                        val = ', '.join([item['name'] for item in tweet['search_info']['search_areas']])
                    elif col == "geo" and tweet['geo']:
                        val = "%f,%f" % (tweet['geo']['coordinates'][0], tweet['geo']['coordinates'][1])
                    elif col in ("created_at", "created_at_local"):
                        val = str(tweet[col])
                    elif col == 'source':
                        #strip away tags with BeautifulSoup
                        val = BS(tweet[col]).text
                    elif col in tweet:
                        val = tweet[col]

                    if type(val) not in (list, dict) and col not in ("_id", 'search_info', 'entities'):
                        sheet.cell(row=rowIndx+1, column=colIndx).value = val

            book.save(outputPath+"\\"+fileName)
            return outputPath+"\\"+fileName

        except Exception, e:
            import traceback
            print str(e)
            print str(traceback.print_exc())


text:
/*************************************************************************************************/

#ifndef GIL_JPEG_IO_H
#define GIL_JPEG_IO_H

/// \file
/// \brief  Support for reading and writing JPEG files
///         Requires libjpeg
/// \author Hailin Jin and Lubomir Bourdev \n
///         Adobe Systems Incorporated
/// \date   2005-2007 \n Last updated September 24, 2006

#include <cstdio>
#include <algorithm>
#include <string>
#include <boost/static_assert.hpp>
#include <boost/shared_ptr.hpp>
extern "C" {
#include <jpeglib.h>
}
#include "io_error.hpp"
#include "jpeg_io_private.hpp"

namespace boost { namespace gil {

/// \ingroup JPEG_IO
/// \brief Determines whether the given view type is supported for reading
template <typename View>
struct jpeg_read_support {
    BOOST_STATIC_CONSTANT(bool,is_supported=
                          (detail::jpeg_read_support_private<typename channel_type<View>::type,
                                                             typename color_space_type<View>::type>::is_supported));
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
                          (detail::jpeg_read_support_private<typename channel_type<View>::type,
                                                             typename color_space_type<View>::type>::color_type));
    BOOST_STATIC_CONSTANT(bool, value=is_supported);
};

/// \ingroup JPEG_IO
/// \brief Returns the width and height of the JPEG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
inline point2<std::ptrdiff_t> jpeg_read_dimensions(const char* filename) {
    detail::jpeg_reader m(filename);
    return m.get_dimensions();
}

/// \ingroup JPEG_IO
/// \brief Returns the width and height of the JPEG file at the specified location.
/// Throws std::ios_base::failure if the location does not correspond to a valid JPEG file
inline point2<std::ptrdiff_t> jpeg_read_dimensions(const std::string& filename) {
    return jpeg_read_dimensions(filename.c_str());
}

/// \ingroup JPEG_IO
/// \brief Loads the image specified by the given jpeg image file name into the given view.
/// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not
/// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
template <typename View>
inline void jpeg_read_view(const char* filename,const View& view) {
    BOOST_STATIC_ASSERT(jpeg_read_support<View>::is_supported);

    detail::jpeg_reader m(filename);
    m.apply(view);
}

/// \ingroup JPEG_IO
/// \brief Loads the image specified by the given jpeg image file name into the given view.
template <typename View>
inline void jpeg_read_view(const std::string& filename,const View& view) {
    jpeg_read_view(filename.c_str(),view);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it.
/// Triggers a compile assert if the image color space or channel depth are not supported by the JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its color space or channel depth are not
/// compatible with the ones specified by Image
template <typename Image>
inline void jpeg_read_image(const char* filename,Image& im) {
    BOOST_STATIC_ASSERT(jpeg_read_support<typename Image::view_t>::is_supported);

    detail::jpeg_reader m(filename);
    m.read_image(im);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, and loads the pixels into it.
template <typename Image>
inline void jpeg_read_image(const std::string& filename,Image& im) {
    jpeg_read_image(filename.c_str(),im);
}

/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
template <typename View,typename CC>
inline void jpeg_read_and_convert_view(const char* filename,const View& view,CC cc) {
    detail::jpeg_reader_color_convert<CC> m(filename,cc);
    m.apply(view);
}

/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
/// Throws std::ios_base::failure if the file is not a valid JPEG file, or if its dimensions don't match the ones of the view.
template <typename View>
inline void jpeg_read_and_convert_view(const char* filename,const View& view) {
    detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
    m.apply(view);
}

/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
template <typename View,typename CC>
inline void jpeg_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
    jpeg_read_and_convert_view(filename.c_str(),view);
}

/// \ingroup JPEG_IO
/// \brief Loads and color-converts the image specified by the given jpeg image file name into the given view.
template <typename View>
inline void jpeg_read_and_convert_view(const std::string& filename,const View& view) {
    jpeg_read_and_convert_view(filename.c_str(),view);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid JPEG file
template <typename Image,typename CC>
inline void jpeg_read_and_convert_image(const char* filename,Image& im,CC cc) {
    detail::jpeg_reader_color_convert<CC> m(filename,cc);
    m.read_image(im);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
/// Throws std::ios_base::failure if the file is not a valid JPEG file
template <typename Image>
inline void jpeg_read_and_convert_image(const char* filename,Image& im) {
    detail::jpeg_reader_color_convert<default_color_converter> m(filename,default_color_converter());
    m.read_image(im);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
template <typename Image,typename CC>
inline void jpeg_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
    jpeg_read_and_convert_image(filename.c_str(),im);
}

/// \ingroup JPEG_IO
/// \brief Allocates a new image whose dimensions are determined by the given jpeg image file, loads and color-converts the pixels into it.
template <typename Image>
inline void jpeg_read_and_convert_image(const std::string& filename,Image& im) {
    jpeg_read_and_convert_image(filename.c_str(),im);
}

/// \ingroup JPEG_IO
/// \brief Determines whether the given view type is supported for writing
template <typename View>
struct jpeg_write_support {
    BOOST_STATIC_CONSTANT(bool,is_supported=
                          (detail::jpeg_write_support_private<typename channel_type<View>::type,
                                                              typename color_space_type<View>::type>::is_supported));
    BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=
                          (detail::jpeg_write_support_private<typename channel_type<View>::type,
                                                             typename color_space_type<View>::type>::color_type));
    BOOST_STATIC_CONSTANT(bool, value=is_supported);
};

/// \ingroup JPEG_IO
/// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
/// Triggers a compile assert if the view color space and channel depth are not supported by the JPEG library or by the I/O extension.
/// Throws std::ios_base::failure if it fails to create the file.
template <typename View>
inline void jpeg_write_view(const char* filename,const View& view,int quality=100) {
    BOOST_STATIC_ASSERT(jpeg_write_support<View>::is_supported);

    detail::jpeg_writer m(filename);
    m.apply(view,quality);
}

/// \ingroup JPEG_IO
/// \brief Saves the view to a jpeg file specified by the given jpeg image file name.
template <typename View>
inline void jpeg_write_view(const std::string& filename,const View& view,int quality=100) {
    jpeg_write_view(filename.c_str(),view,quality);
}

} }  // namespace boost::gil

#endif


text:
package org.openkilda.integration.exception;

public class IntegrationException extends RuntimeException {

    private static final long serialVersionUID = 9177586156625415602L;

    public IntegrationException() {
        super();
    }

    public IntegrationException(final String errorMessage) {
        super(errorMessage);
    }

    public IntegrationException(final String errorMessage, final Throwable e) {
        super(errorMessage, e);
    }

    public IntegrationException(final Throwable e) {
        super(e);
    }
}


text:
module QA
  RSpec.describe 'Verify', :docker, :runner do
    describe 'Pipeline creation and processing' do
      let(:executor) { "qa-runner-#{Time.now.to_i}" }
      let(:max_wait) { 30 }

      let(:project) do
        Resource::Project.fabricate_via_api! do |project|
          project.name = 'project-with-pipeline'
        end
      end

      let!(:runner) do
        Resource::Runner.fabricate! do |runner|
          runner.project = project
          runner.name = executor
          runner.tags = [executor]
        end
      end

      after do
        runner.remove_via_api!
      end

      it 'users creates a pipeline which gets processed', :smoke, status_issue: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/391' do
        Flow::Login.sign_in

        Resource::Repository::Commit.fabricate_via_api! do |commit|
          commit.project = project
          commit.commit_message = 'Add .gitlab-ci.yml'
          commit.add_files(
            [
              {
                file_path: '.gitlab-ci.yml',
                content: <<~YAML
                  test-success:
                    tags:
                      - #{executor}
                    script: echo 'OK'

                  test-failure:
                    tags:
                      - #{executor}
                    script:
                      - echo 'FAILURE'
                      - exit 1

                  test-tags:
                    tags:
                     - invalid
                    script: echo 'NOOP'

                  test-artifacts:
                    tags:
                      - #{executor}
                    script: mkdir my-artifacts; echo "CONTENTS" > my-artifacts/artifact.txt
                    artifacts:
                      paths:
                      - my-artifacts/
                YAML
              }
            ]
          )
        end.project.visit!

        Page::Project::Menu.perform(&:click_ci_cd_pipelines)
        Page::Project::Pipeline::Index.perform(&:click_on_latest_pipeline)

        {
          'test-success': :passed,
          'test-failure': :failed,
          'test-tags': :pending,
          'test-artifacts': :passed
        }.each do |job, status|
          Page::Project::Pipeline::Show.perform do |pipeline|
            pipeline.click_job(job)
          end

          Page::Project::Job::Show.perform do |show|
            expect(show).to public_send("be_#{status}")
            show.click_element(:pipeline_path, Page::Project::Pipeline::Show)
          end
        end
      end
    end
  end
end


text:
from lino.api import dd, _

class ResidenceTypes(dd.ChoiceList):
    verbose_name = _("Resident register")
    verbose_name_plural = _("Resident registers")

add = ResidenceTypes.add_item
add('1', _("Register of citizens"))
add('2', _("Register of foreigners"))
add('3', _("Waiting register"))

class BeIdCardTypes(dd.ChoiceList):

    required_roles = dd.login_required(dd.SiteStaff)
    verbose_name = _("eID card type")
    verbose_name_plural = _("eID card types")
    old2new = {'1' : '01', '6': '06'}

add = BeIdCardTypes.add_item
# add('1', _("Belgian citizen"), "belgian_citizen")
add('01', _("Belgian citizen"), "belgian_citizen")
# ,de=u"Belgischer Staatsbürger",fr=u"Citoyen belge"),
add('06', _("Kids card (< 12 year)"), "kids_card")
#add('06', _("Kids card (< 12 year)"), "kids_card0")
#,de=u"Kind unter 12 Jahren"),

#~ add('8', _("Habilitation"))
#,fr=u"Habilitation",nl=u"Machtiging")

add('11', _("Foreigner card A"), "foreigner_a")
        #~ nl=u"Bewijs van inschrijving in het vreemdelingenregister - Tijdelijk verblijf",
        #~ fr=u"Certificat d'inscription au registre des étrangers - Séjour temporaire",
        #~ de=u"Ausländerkarte A Bescheinigung der Eintragung im Ausländerregister - Vorübergehender Aufenthalt",
add('12', _("Foreigner card B"), "foreigner_b")
        #~ nl=u"Bewijs van inschrijving in het vreemdelingenregister",
        #~ fr=u"Certificat d'inscription au registre des étrangers",
        #~ de=u"Ausländerkarte B (Bescheinigung der Eintragung im Ausländerregister)",
add('13', _("Foreigner card C"), "foreigner_c")
        #~ nl=u"Identiteitskaart voor vreemdeling",
        #~ fr=u"Carte d'identité d'étranger",
        #~ de=u"C (Personalausweis für Ausländer)",
add('14', _("Foreigner card D"), "foreigner_d")
        #~ nl=u"EG - langdurig ingezetene",
        #~ fr=u"Résident de longue durée - CE",
        #~ de=u"Daueraufenthalt - EG",
add('15', _("Foreigner card E"), "foreigner_e")
        #~ nl=u"Verklaring van inschrijving",
        #~ fr=u"Attestation d'enregistrement",
        #~ de=u"Anmeldebescheinigung",
add('16', _("Foreigner card E+"), "foreigner_e_plus")
        # Document ter staving van duurzaam verblijf van een EU onderdaan
add('17', _("Foreigner card F"), "foreigner_f")
        #~ nl=u"Verblijfskaart van een familielid van een burger van de Unie",
        #~ fr=u"Carte de séjour de membre de la famille d'un citoyen de l'Union",
        #~ de=u"Aufenthaltskarte für Familienangehörige eines Unionsbürgers",
add('18', _("Foreigner card F+"), "foreigner_f_plus")

# This is not an electronic card, but it makes sense to add it to this
# list e.g. for Lino Avanti:
# FR: Attestation d'immatriculation (Carte orange)
# DE: Eintragungsbescheinigung (Orange Karte)
add('99', _("Registration certificate (Orange card)"), "orange_card")


text:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rectangle_fragment">
    <solid android:color="@color/fragment_googlemap_accent_color" />
</shape>


text:
namespace features {

// All features in alphabetical order. The features should be documented
// alongside the definition of their values in the .cc file.

extern const base::Feature kChromeLabs;

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
extern const base::Feature kChromeTipsInMainMenu;

extern const base::Feature kChromeTipsInMainMenuNewBadge;
#endif

extern const base::Feature kChromeWhatsNewUI;
extern const base::FeatureParam<bool> kChromeWhatsNewUIFeedbackButton;

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
extern const base::Feature kChromeWhatsNewInMainMenuNewBadge;
#endif

extern const base::Feature kCommander;

#if !defined(ANDROID)
extern const base::Feature kAccessCodeCastUI;
#endif

extern const base::Feature kEvDetailsInPageInfo;

extern const base::Feature kExtensionsMenuAccessControl;

extern const base::Feature kForceSignInReauth;

extern const base::Feature kProminentDarkModeActiveTabTitle;

extern const base::Feature kReadLaterNewBadgePromo;

extern const base::Feature kReadLaterAddFromDialog;

extern const base::Feature kScrollableTabStrip;
extern const char kMinimumTabWidthFeatureParameterName[];

extern const base::Feature kScrollableTabStripButtons;

// TODO(pbos): Once kReadLater is cleaned up on Desktop, move definition into
// ui_features.cc. This is currently temporarily in reading_list_switches.h.
extern const base::Feature kSidePanel;
extern const base::Feature kSidePanelDragAndDrop;

#if BUILDFLAG(ENABLE_SIDE_SEARCH)
extern const base::Feature kSideSearch;
extern const base::Feature kSideSearchClearCacheWhenClosed;
extern const base::Feature kSideSearchFeedback;
extern const base::Feature kSideSearchStatePerTab;
#endif  // BUILDFLAG(ENABLE_SIDE_SEARCH)

extern const base::Feature kSyncConfirmationUpdatedText;

extern const base::Feature kTabGroupsAutoCreate;

extern const base::Feature kTabGroupsCollapseFreezing;

extern const base::Feature kTabGroupsNewBadgePromo;

extern const base::Feature kTabGroupsSave;

extern const base::Feature kTabHoverCardImages;

// These parameters control how long the hover card system waits before
// requesting a preview image from a tab where no preview image is available.
// Values are in ms.
extern const char kTabHoverCardImagesNotReadyDelayParameterName[];
extern const char kTabHoverCardImagesLoadingDelayParameterName[];
extern const char kTabHoverCardImagesLoadedDelayParameterName[];

// Determines how long to wait during a hover card slide transition before a
// placeholder image is displayed via crossfade.
// -1: disable crossfade entirely
//  0: show placeholder immediately
//  1: show placeholder when the card lands on the new tab
//  between 0 and 1: show at a percentage of transition
//
// Note: crossfade is automatically disabled if animations are disabled at the
// OS level (e.g. for accessibility).
extern const char kTabHoverCardImagesCrossfadePreviewAtParameterName[];

// Adds an amount of time (in ms) to the show delay when tabs are max width -
// typically when there are less than 5 or 6 tabs in a browser window.
extern const char kTabHoverCardAdditionalMaxWidthDelay[];

// When set to 1, reverses the order of elements in the hover card, so that
// the title and site are on bottom and the tab status and preview image are
// on top. 0 is the default layout.
extern const char kTabHoverCardAlternateFormat[];

extern const base::Feature kTabOutlinesInLowContrastThemes;

extern const base::Feature kTabSearchChevronIcon;

extern const base::Feature kTabSearchFeedback;

extern const base::Feature kTabSearchFuzzySearch;

extern const char kTabSearchSearchThresholdName[];

// Setting this to true will ignore the distance parameter when finding matches.
// This means that it will not matter where in the string the pattern occurs.
extern const base::FeatureParam<bool> kTabSearchSearchIgnoreLocation;

extern const base::Feature kTabSearchMediaTabs;

// Determines how close the match must be to the beginning of the string. Eg a
// distance of 100 and threshold of 0.8 would require a perfect match to be
// within 80 characters of the beginning of the string.
extern const base::FeatureParam<int> kTabSearchSearchDistance;

// This determines how strong the match should be for the item to be included in
// the result set. Eg a threshold of 0.0 requires a perfect match, 1.0 would
// match anything. Permissible values are [0.0, 1.0].
extern const base::FeatureParam<double> kTabSearchSearchThreshold;

// These are the hardcoded minimum and maximum search threshold values for
// |kTabSearchSearchThreshold|.
constexpr double kTabSearchSearchThresholdMin = 0.0;
constexpr double kTabSearchSearchThresholdMax = 1.0;

// Controls the weight associated with a tab's title for filtering and ordering
// list items.
extern const base::FeatureParam<double> kTabSearchTitleWeight;

// Controls the weight associated with a tab's hostname when filering and
// odering list items.
extern const base::FeatureParam<double> kTabSearchHostnameWeight;

// Controls the weight associated with a tab's group title filering and
// odering list items
extern const base::FeatureParam<double> kTabSearchGroupTitleWeight;

// Whether to move the active tab to the bottom of the list.
extern const base::FeatureParam<bool> kTabSearchMoveActiveTabToBottom;

extern const base::Feature kTabSearchRecentlyClosed;

// Default number of recently closed entries to display by default when no
// search text is provided.
extern const base::FeatureParam<int>
    kTabSearchRecentlyClosedDefaultItemDisplayCount;

// A threshold of recently closed tabs after which to stop adding recently
// closed item data to the profile data payload should the minimum display
// count have been met.
extern const base::FeatureParam<int> kTabSearchRecentlyClosedTabCountThreshold;

// Determines how screenshots of the toolbar uses Software or Hardware drawing.
// Works on Android 10+.
extern const base::Feature kToolbarUseHardwareBitmapDraw;

extern const base::Feature kUnifiedSidePanel;

extern const base::Feature kWebUIBubblePerProfilePersistence;

#if !defined(ANDROID)
extern const base::Feature kWebUIBrandingUpdate;
#endif

extern const base::Feature kWebUIDownloadShelf;

extern const base::Feature kWebUITabStrip;

// Controls whether the context menu is shown on a touch press or a touch
// tap gesture on the WebUI Tab Strip.
extern const base::Feature kWebUITabStripContextMenuAfterTap;

extern const base::Feature kWebUIFeedback;

#if defined(OS_CHROMEOS)
extern const base::Feature kChromeOSTabSearchCaptionButton;
#endif

// Cocoa to views migration.
#if defined(OS_MAC)
extern const base::Feature kLocationPermissionsExperiment;

extern const base::Feature kViewsFirstRunDialog;
extern const base::Feature kViewsTaskManager;
extern const base::Feature kViewsJSAppModalDialog;

int GetLocationPermissionsExperimentBubblePromptLimit();
int GetLocationPermissionsExperimentLabelPromptLimit();
#endif

#if defined(OS_WIN)
extern const base::Feature kWin10TabSearchCaptionButton;
#endif

}  // namespace features

#endif  // CHROME_BROWSER_UI_UI_FEATURES_H_


text:
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

using Moq;
using NUnit.Framework;

using set.web.Data.Services;
using set.web.Helpers;
using set.web.Models;
using set.web.test.Shared;
using set.web.test.Shared.Builders;

namespace set.web.test.Behaviour
{
    [TestFixture]
    public class VisitorBehaviourTests : BaseBehaviourTest
    {
        [Test]
        public async void any_visitor_can_create_user_account()
        {
            //arrange
            var userService = new Mock<IUserService>();
            userService.Setup(x => x.Create(ValidUserModel, ConstHelper.User))
                       .Returns(Task.FromResult(true));

            var authService = new Mock<IAuthService>();
            authService.Setup(x => x.SignIn(ValidUserModel.Id, ValidUserModel.Name, ValidUserModel.Email, ConstHelper.User, true));

            //act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                                                 .WithAuthService(authService.Object)
                                                 .Build();

            var result = await sut.New(ValidUserModel);

            ////assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom<RedirectResult>(result);

            userService.Verify(x => x.Create(ValidUserModel, ConstHelper.User), Times.Once);
            authService.Verify(x => x.SignIn(ValidUserModel.Id, ValidUserModel.Name, ValidUserModel.Email, ConstHelper.User, true), Times.Once);

            sut.AssertPostAttribute(ACTION_NEW, new[] { typeof(UserModel) });
            sut.AssertAllowAnonymousAttribute(ACTION_NEW, new[] { typeof(UserModel) });
        }

        [Test]
        public async void any_visitor_can_send_feedback()
        {
            //arrange
            var feedbackService = new Mock<IFeedbackService>();
            feedbackService.Setup(x => x.CreateFeedback(ValidContactMessageModel.Message, ValidContactMessageModel.Email))
                           .Returns(Task.FromResult(true));

            //act
            var sut = new FeedbackControllerBuilder().WithFeedbackService(feedbackService.Object)
                                                     .BuildWithMockControllerContext(ValidUserModel.Id, ValidUserModel.Name, ValidUserModel.Email, ValidUserModel.RoleName);

            var result = await sut.New(ValidContactMessageModel.Message);

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom<JsonResult>(result);

            feedbackService.Verify(x => x.CreateFeedback(ValidContactMessageModel.Message, ValidContactMessageModel.Email), Times.Once);

            sut.AssertPostAttributeWithOutAntiForgeryToken(ACTION_NEW, new[] { typeof(string) });
            sut.AssertAllowAnonymousAttribute(ACTION_NEW, new[] { typeof(string) });
        }

        [Test]
        public async void any_visitor_can_send_contact_message()
        {
            //arrange
            var feedbackService = new Mock<IFeedbackService>();
            feedbackService.Setup(x => x.CreateContactMessage(ValidContactMessageModel.Subject, ValidContactMessageModel.Email, ValidContactMessageModel.Message))
                           .Returns(Task.FromResult(true));

            //act
            var sut = new HomeControllerBuilder().WithFeedbackService(feedbackService.Object)
                                                 .Build();
            var result = await sut.Contact(ValidContactMessageModel);

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom<ViewResult>(result);

            feedbackService.Verify(x => x.CreateContactMessage(ValidContactMessageModel.Subject, ValidContactMessageModel.Email, ValidContactMessageModel.Message), Times.Once);

            sut.AssertPostAttribute(ACTION_CONTACT, new[] { typeof(ContactMessageModel) });
            sut.AssertAllowAnonymousAttribute(ACTION_CONTACT, new[] { typeof(ContactMessageModel) });
        }

        [Test]
        public void any_visitor_can_change_language()
        {
            //arrange
            var controllerContext = new Mock<ControllerContext>();

            var httpContext = new Mock<HttpContextBase>();

            var httpRequest = new Mock<HttpRequestBase>();
            var httpResponse = new Mock<HttpResponseBase>();

            controllerContext.Setup(x => x.HttpContext).Returns(httpContext.Object);

            httpContext.Setup(x => x.Request).Returns(httpRequest.Object);
            httpContext.Setup(x => x.Response).Returns(httpResponse.Object);

            httpResponse.Setup(x => x.SetCookie(It.IsAny<HttpCookie>()));

            //act
            var sut = new LangControllerBuilder().Build();
            sut.ControllerContext = controllerContext.Object;

            var view = sut.Change(ConstHelper.tr);

            //assert
            Assert.NotNull(view);

            sut.AssertGetAttribute(ACTION_CHANGE, new[] { typeof(string) });
            sut.AssertAllowAnonymousAttribute(ACTION_CHANGE, new[] { typeof(string) });

            httpResponse.Verify(x => x.SetCookie(It.IsAny<HttpCookie>()), Times.AtLeastOnce);
        }

        [Test]
        public async void any_visitor_can_search()
        {
            //arrange
            const string text = "search_text";

            var searchService = new Mock<ISearchService>();
            searchService.Setup(x => x.Query(text)).Returns(Task.FromResult(new List<SearchResult> { new SearchResult { Name = text } }));

            //act
            var sut = new SearchControllerBuilder().WithSearchService(searchService.Object)
                                                   .Build();

            var result = await sut.Query(text);

            //assert
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom<JsonResult>(result);

            sut.AssertGetAttribute(ACTION_QUERY, new[] { typeof(string) });
            sut.AssertAllowAnonymousAttribute(ACTION_QUERY, new[] { typeof(string) });
        }
    }
}


text:
namespace ash {

AssistantSuggestionsModel::AssistantSuggestionsModel() = default;

AssistantSuggestionsModel::~AssistantSuggestionsModel() = default;

void AssistantSuggestionsModel::AddObserver(
    AssistantSuggestionsModelObserver* observer) const {
  observers_.AddObserver(observer);
}

void AssistantSuggestionsModel::RemoveObserver(
    AssistantSuggestionsModelObserver* observer) const {
  observers_.RemoveObserver(observer);
}

const assistant::AssistantSuggestion*
AssistantSuggestionsModel::GetSuggestionById(
    const base::UnguessableToken& id) const {
  for (auto& conversation_starter : conversation_starters_) {
    if (conversation_starter.id == id)
      return &conversation_starter;
  }
  for (auto& onboarding_suggestion : onboarding_suggestions_) {
    if (onboarding_suggestion.id == id)
      return &onboarding_suggestion;
  }
  return nullptr;
}

void AssistantSuggestionsModel::SetConversationStarters(
    std::vector<AssistantSuggestion>&& conversation_starters) {
  conversation_starters_ = std::move(conversation_starters);
  NotifyConversationStartersChanged();
}

const std::vector<assistant::AssistantSuggestion>&
AssistantSuggestionsModel::GetConversationStarters() const {
  return conversation_starters_;
}

void AssistantSuggestionsModel::SetOnboardingSuggestions(
    std::vector<AssistantSuggestion>&& onboarding_suggestions) {
  onboarding_suggestions_ = std::move(onboarding_suggestions);
  NotifyOnboardingSuggestionsChanged();
}

const std::vector<assistant::AssistantSuggestion>&
AssistantSuggestionsModel::GetOnboardingSuggestions() const {
  return onboarding_suggestions_;
}

void AssistantSuggestionsModel::NotifyConversationStartersChanged() {
  for (AssistantSuggestionsModelObserver& observer : observers_)
    observer.OnConversationStartersChanged(conversation_starters_);
}

void AssistantSuggestionsModel::NotifyOnboardingSuggestionsChanged() {
  for (AssistantSuggestionsModelObserver& observer : observers_)
    observer.OnOnboardingSuggestionsChanged(onboarding_suggestions_);
}

}  // namespace ash


text:
'use strict';

const authentication = require('../../../../config/middlewares/authentication');
const AdminEventsController = require('../../../controllers/admin/events');
const eventsMiddleware = require('../../../../config/middlewares/events');

module.exports = function(api) {
  api.delete('/admin/events/:id', authentication.requireLoggedInUser, eventsMiddleware.findAccessibleEvent, AdminEventsController.destroy);
};


text:
\documentclass{article}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tabularx}
\usepackage{multirow}
\pagestyle{fancy}
\lhead{Anirudhan J Rajagopalan --- ajr619 --- N18824115}

\begin{document}

\title{Web Search Engines --- Homework 3}
\date{March 28, 2016}
\author{Anirudhan J Rajagopalan\\ N18824115\\ ajr619}
\maketitle

\newpage

\section{Problem 1}

The decision of the two judges can be summarized as:
\begin{center}
  \begin{tabular}{cc|c|c|}
    \cline{3-4}
    & & \multicolumn{2}{c|}{Judge 2} \\ \cline{3-4}
    & & Yes & No \\ \cline{1-4}
    \multicolumn{1}{|c}{\multirow{2}{*}{Judge 1}} &
    \multicolumn{1}{|c|}{Yes} & 2 & 4 \\ \cline{2-4}
    \multicolumn{1}{|c}{} &
    \multicolumn{1}{|c|}{No} & 4 & 2 \\ \cline{1-4}
  \end{tabular}
\end{center}

For the above condition, we get
\begin{align*}
  P(A) = \frac{2 + 2}{12}
\end{align*}

\begin{align*}
  P(nonrelevant) = \frac{6 + 6}{12 + 12} = 0.5
\end{align*}

\begin{align*}
  P(relevant) = \frac{6 + 6}{12 + 12} = 0.5
\end{align*}

\begin{align*}
  P(E) = & {P(NR)}^{2} + {P(R)}^{2} \\
  =& 0.5^{2} + 0.5^{2} \\
  =& 0.5
\end{align*}

\subsection{$kappa$ statistic}

\begin{align*}
  \kappa =& \frac{P(A) - P(E)}{1 - P(E)} \\
  =& \frac{0.33 - 0.5}{1 - 0.5} \\
  =& -0.36
\end{align*}

\subsection{Relevant only if both judges agree}
\#Relevant documents retrieved = 1 \\
\#retrieved documents = 5 \\
\#relevant documents = 4 \\
\subsubsection{Precision}
\begin{align*}
  Precision =& \frac{\#relevant documents retrieved}{\#retrieved documents} \\
  =& \frac{1}{5} \\
  =& 0.2
\end{align*}

\subsubsection{Recall}
\begin{align*}
  Recall =& \frac{\#relevant documents retrieved}{\#relevant documents}\\
  =& \frac{1}{4} \\
  =& 0.25
\end{align*}

\subsubsection{$F_{1}$ --- score}
\begin{align*}
  F_{1} =& \frac{2PR}{P+R} \\
  =& \frac{2*0.2*0.25}{0.2+0.25} \\
  =& 0.222
\end{align*}

\subsection{Relevant if either judge thinks relevant}
\#Relevant documents retrieved = 5 \\
\#retrieved documents = 5 \\
\#relevant documents = 10 \\

\subsubsection{Precision}
\begin{align*}
  Precision =& \frac{\#relevant documents retrieved}{\#retrieved documents} \\
  =& \frac{5}{5} \\
  =& 1
\end{align*}

\subsubsection{Recall}
\begin{align*}
  Recall =& \frac{\#relevant documents retrieved}{\#relevant documents}\\
  =& \frac{5}{10} \\
  =& 0.5
\end{align*}

\subsubsection{$F_{1}$ --- score}
\begin{align*}
  F_{1} =& \frac{2PR}{P+R} \\
  =& \frac{2 * 1 * 0.5}{1 + 0.5} \\
  =& 0.667
\end{align*}

\section{Problem 2}
\subsection{How to evaluate an image search engine}
Evaluating any search engine involves defining what ``relevancy'' is with respect to the particular audience the search engine is designed to serve.
The relevancy of a search engine might be dependent on how the input is provided.  Incase of image search, the search can be done by text or by providing images.

\subsubsection{Image input}
When a search is based on image input, the returned images should be similar to the input image.  This will depend on various descriptors of an image such as brightness, contrast, colors and so on.

Suppose we upload an image of a patch of tiger skin, the first relevant answer can be a sofa which has stripes similar to that of a tiger.  This result is acceptable because the sofa's pattern matches the input image's pattern.

\subsubsection{Text input}
When an image search is triggered by textual input, the result should be an image which best describes the textual content given by the user.
The most important way in which we can check for relevancy is to verify if objects representing each and every search term is present in the result images.

Suppose an user searches for ``Monkey swinging from a tree''.  The first result should have a monkey, and a tree and possibily the monkey swinging.  The second result can have a moneky, a tree and a swing and so on.

Suppose, the user searches for ``tiger skin'', the first relevant answer should be an image which shows tiger skin and not a sofa which is covered by a cloth similar to tiger skin.

\subsection{How evaluating image search differs from textual search}
As discussed above, evaluating image search differs from textual search mainly because of the type of inputs that can be provided to the system.

Also the conecpt of relevancy can get confusing when we deal with images \& text, especially when the input is an image.  An image search for ``August Rush'' can return the posters of movie \textit{August rush} which is quite relevant for people looking for the movie.  But it can also return images of a festival which happens in August and attracts huge amount of people.  This may or maynot be relevant to the user who tries to search for images using textual content.

\section{Problem 3}
Given a relation between Gold standard and custom clustering as follows:
\begin{center}
  \begin{tabular}{llll}
      & A & B & C \\
    X & 10 & 40 & 20 \\
    Y & 50 & 10 & 20 \\
  \end{tabular}
\end{center}

\subsection{Purity of cluster}

For the given example
\begin{align*}
  \Omega = & {X, Y} \\
  \mathbb{C} = & {A, B, C}
\end{align*}

The purity is given by
\begin{align*}
  purity(\Omega, \mathbb{C}) = & \frac{1}{N} \sum_{k} \max_{j} |\omega_{k} \cap C_{j}|
\end{align*}

We interpret $\omega_{k}$ as the set of documents in $\omega_{k}$ and $ c_{j} $ as the set of documents in $c_{j}$.

Therefore the purity for the given cluster combination is given by

\begin{align*}
  purity(\Omega, \mathbb{C}) =& \frac{1}{150} ( 40 + 50 )\\
  =& \frac{90}{150}\\
  =& 0.6
\end{align*}

\subsection{Computation for TP, TN, FP, FN, P, R, $F_{\beta}$ }
\subsubsection{TP --- True Positives}
Pairs $ \alpha -  \beta $ which are in the same cluster in both the gold standard and in the algorithmic clustering.

The True Positives are given by
\begin{align*}
  TP =& \frac{10 * 9}{2} + \frac{40 * 39}{2} + \frac{20 * 19}{2} + \frac{50 * 49}{2} + \frac{10 * 9}{2} + \frac{20 * 19}{2} \\
  =& 20*39 + 25*49 + 20*19 + 10*9 \\
  =& 2475
\end{align*}

\subsubsection{FP --- False Positives}
Pairs $ \alpha -  \beta $ which are in the same cluster in the algorithmic cluster but different clusters in the gold standard.

The False Positives are given by
\begin{align*}
  FP =& 10*40 + 10*20 + 40*20 + 50*10 + 50*20 + 10*20 \\
  =& 400 + 200 + 800 + 500 + 1000 + 200 \\
  =& 3100
\end{align*}

\subsubsection{FN --- False Negatives}
Pairs $ \alpha -  \beta $ which are in the same cluster in the gold standard, but different algorithmic clusters.

The False Negatives are given by
\begin{align*}
  FN =& 10*50 + 40*10 + 20*20 \\
  =& 500 + 400 + 400 \\
  =& 1300
\end{align*}

\subsubsection{TN --- True Negatives}
Pairs $ \alpha -  \beta $ that are in different clusters in both the gold standard and the algorithmic clustering.

The True Negatives are given by
\begin{align*}
  TN =& 10*10 + 10*20 + 40*50 + 40*20 + 20*50 + 20*10 \\
  =& 100 + 200 + 2000 + 800 + 1000 + 200 \\
  =& 4300
\end{align*}

\subsection{Rand index}
\begin{align*}
  RI = & \frac{TP + TN}{TP + FP + FN + TN} \\
  =& \frac{6775}{11175} \\
  =& 0.606
\end{align*}

\subsection{Precision}
\begin{align*}
  Precision =& \frac{TP}{TP+FP} \\
  =& \frac{2475}{2475 + 3100}\\
  =& \frac{2475}{5575} \\
  =& 0.4439
\end{align*}

\subsection{Recall}
\begin{align*}
  Recall =& \frac{TP}{TP + FN} \\
  =& \frac{2475}{2475 + 1300} \\
  =& \frac{2475}{3775} \\
  =& 0.6556
\end{align*}

\subsection{F score when $\beta$=1}
\begin{align*}
  F_{\beta} =& \frac{(\beta^{2} + 1) * P * R}{\beta^{2}P + R} \\
  =& \frac{2PR}{P+R} \\
  =& \frac{2 * 0.4439 * 0.6556}{0.4439 + 0.6556} \\
  =& \frac{0.5820}{1.0995} \\
  =& 0.5293
\end{align*}

\end{document}


text:
package com.ctrip.framework.apollo.portal.listener;

import com.ctrip.framework.apollo.common.constants.ReleaseOperation;
import com.ctrip.framework.apollo.portal.component.ConfigReleaseWebhookNotifier;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.component.emailbuilder.GrayPublishEmailBuilder;
import com.ctrip.framework.apollo.portal.component.emailbuilder.MergeEmailBuilder;
import com.ctrip.framework.apollo.portal.component.emailbuilder.NormalPublishEmailBuilder;
import com.ctrip.framework.apollo.portal.component.emailbuilder.RollbackEmailBuilder;
import com.ctrip.framework.apollo.portal.entity.bo.Email;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.service.ReleaseHistoryService;
import com.ctrip.framework.apollo.portal.spi.EmailService;
import com.ctrip.framework.apollo.portal.spi.MQService;
import com.ctrip.framework.apollo.tracer.Tracer;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class ConfigPublishListener {

  private final ReleaseHistoryService releaseHistoryService;
  private final EmailService emailService;
  private final NormalPublishEmailBuilder normalPublishEmailBuilder;
  private final GrayPublishEmailBuilder grayPublishEmailBuilder;
  private final RollbackEmailBuilder rollbackEmailBuilder;
  private final MergeEmailBuilder mergeEmailBuilder;
  private final PortalConfig portalConfig;
  private final MQService mqService;
  private final ConfigReleaseWebhookNotifier configReleaseWebhookNotifier;

  private ExecutorService executorService;

  public ConfigPublishListener(
      final ReleaseHistoryService releaseHistoryService,
      final EmailService emailService,
      final NormalPublishEmailBuilder normalPublishEmailBuilder,
      final GrayPublishEmailBuilder grayPublishEmailBuilder,
      final RollbackEmailBuilder rollbackEmailBuilder,
      final MergeEmailBuilder mergeEmailBuilder,
      final PortalConfig portalConfig,
      final MQService mqService,
      final ConfigReleaseWebhookNotifier configReleaseWebhookNotifier) {
    this.releaseHistoryService = releaseHistoryService;
    this.emailService = emailService;
    this.normalPublishEmailBuilder = normalPublishEmailBuilder;
    this.grayPublishEmailBuilder = grayPublishEmailBuilder;
    this.rollbackEmailBuilder = rollbackEmailBuilder;
    this.mergeEmailBuilder = mergeEmailBuilder;
    this.portalConfig = portalConfig;
    this.mqService = mqService;
    this.configReleaseWebhookNotifier = configReleaseWebhookNotifier;
  }

  @PostConstruct
  public void init() {
    executorService = Executors.newSingleThreadExecutor(ApolloThreadFactory.create("ConfigPublishNotify", true));
  }

  @EventListener
  public void onConfigPublish(ConfigPublishEvent event) {
    executorService.submit(new ConfigPublishNotifyTask(event.getConfigPublishInfo()));
  }

  private class ConfigPublishNotifyTask implements Runnable {

    private final ConfigPublishEvent.ConfigPublishInfo publishInfo;

    ConfigPublishNotifyTask(ConfigPublishEvent.ConfigPublishInfo publishInfo) {
      this.publishInfo = publishInfo;
    }

    @Override
    public void run() {
      ReleaseHistoryBO releaseHistory = getReleaseHistory();
      if (releaseHistory == null) {
        Tracer.logError("Load release history failed", null);
        return;
      }

      this.sendPublishWebHook(releaseHistory);

      sendPublishEmail(releaseHistory);

      sendPublishMsg(releaseHistory);
    }

    private ReleaseHistoryBO getReleaseHistory() {
      Env env = publishInfo.getEnv();

      int operation = publishInfo.isMergeEvent() ? ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER :
                      publishInfo.isRollbackEvent() ? ReleaseOperation.ROLLBACK :
                      publishInfo.isNormalPublishEvent() ? ReleaseOperation.NORMAL_RELEASE :
                      publishInfo.isGrayPublishEvent() ? ReleaseOperation.GRAY_RELEASE : -1;

      if (operation == -1) {
        return null;
      }

      if (publishInfo.isRollbackEvent()) {
        return releaseHistoryService
            .findLatestByPreviousReleaseIdAndOperation(env, publishInfo.getPreviousReleaseId(), operation);
      }
      return releaseHistoryService.findLatestByReleaseIdAndOperation(env, publishInfo.getReleaseId(), operation);

    }

    /**
    * webhook send
    *
    * @param releaseHistory {@link ReleaseHistoryBO}
    */
    private void sendPublishWebHook(ReleaseHistoryBO releaseHistory) {
      Env env = publishInfo.getEnv();

      String[] webHookUrls = portalConfig.webHookUrls();
      if (!portalConfig.webHookSupportedEnvs().contains(env) || webHookUrls == null) {
        return;
      }

      configReleaseWebhookNotifier.notify(webHookUrls, env, releaseHistory);
    }

    private void sendPublishEmail(ReleaseHistoryBO releaseHistory) {
      Env env = publishInfo.getEnv();

      if (!portalConfig.emailSupportedEnvs().contains(env)) {
        return;
      }

      int realOperation = releaseHistory.getOperation();

      Email email = null;
      try {
        email = buildEmail(env, releaseHistory, realOperation);
      } catch (Throwable e) {
        Tracer.logError("build email failed.", e);
      }

      if (email != null) {
        emailService.send(email);
      }
    }

    private void sendPublishMsg(ReleaseHistoryBO releaseHistory) {
      mqService.sendPublishMsg(publishInfo.getEnv(), releaseHistory);
    }

    private Email buildEmail(Env env, ReleaseHistoryBO releaseHistory, int operation) {
      switch (operation) {
        case ReleaseOperation.GRAY_RELEASE: {
          return grayPublishEmailBuilder.build(env, releaseHistory);
        }
        case ReleaseOperation.NORMAL_RELEASE: {
          return normalPublishEmailBuilder.build(env, releaseHistory);
        }
        case ReleaseOperation.ROLLBACK: {
          return rollbackEmailBuilder.build(env, releaseHistory);
        }
        case ReleaseOperation.GRAY_RELEASE_MERGE_TO_MASTER: {
          return mergeEmailBuilder.build(env, releaseHistory);
        }
        default:
          return null;
      }
    }
  }

}


text:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Net = require("net");
const port = Number(process.argv[2]);
const server = Net.createServer((socket) => {
    socket.pipe(process.stdout);
    process.stdin.pipe(socket);
});
server.listen(port);
//# sourceMappingURL=DebugAdapter.js.map