Skip to content
Repos

mologram

C++ 5.3 KB102 lines Opens in site

Directory Raw Download

// TapiService contract (track D): the SPEC "Parameter values" rules are enforced before any network use, unknown values are
// rejected and never guessed, and no method answers "not implemented". SPEC 6.4, FR-04, FR-12, FR-20, FR-24, FR-40, FR-50.
#include <filesystem>
#include <fstream>
#include <future>

#include "tttest.hpp"
#include "mologram/tapi/TapiService.hpp"

using namespace mologram;
using js::Object;
using js::Value;

namespace {

struct TmpDir {
  std::string path;
  TmpDir() { char t[] = "/tmp/mologram-tapicontract-XXXXXX"; path = mkdtemp(t); }
  ~TmpDir() { std::error_code ec; std::filesystem::remove_all(path, ec); }
};

RpcResult callSync(ITelegramService& s, const std::string& m, Object p = {}) {
  auto pr = std::make_shared<std::promise<RpcResult>>();
  auto fu = pr->get_future();
  s.call(m, Value(std::move(p)), [pr](const RpcResult& r) { pr->set_value(r); });
  if (fu.wait_for(std::chrono::seconds(10)) != std::future_status::ready) return RpcResult::failure("test", "no reply");
  return fu.get();
}

TapiService::Options options(const TmpDir& d) {
  TapiService::Options o;
  o.apiId = 1; o.apiHash = "h"; o.phone = "p"; o.sessionPath = d.path + "/s.session"; o.cacheDir = d.path + "/cache";
  return o;
}

}  // namespace

TEST_CASE("no TapiService method answers 'not implemented': the stub file is gone and the phrase appears nowhere in src/tapi", "[tapi][contract][unit]") {
  std::error_code ec;
  CHECK(!std::filesystem::exists(std::string(MOLOGRAM_SOURCE_DIR) + "/src/tapi/TapiPending.cpp", ec));
  int hits = 0;
  for (auto& e : std::filesystem::recursive_directory_iterator(std::string(MOLOGRAM_SOURCE_DIR) + "/src/tapi")) {
    if (!e.is_regular_file()) continue;
    std::ifstream in(e.path());
    std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    if (text.find("not implemented in this build") != std::string::npos) hits++;
  }
  CHECK_EQ(hits, 0);
}

TEST_CASE("text contract: only plain or html, entities and format are not combined", "[tapi][contract][unit]") {
  TmpDir d;
  TapiService s(options(d));
  s.setAuthorizedForTest(true);
  auto code = [&](const std::string& m, Object p) { return callSync(s, m, p).error.code; };
  const std::string bad = ErrorCode::BadParams;
  CHECK_EQ(code("messages.send", {{"peer", Value("user:1")}, {"text", Value("x")}, {"format", Value("markdown")}}), bad);
  js::Array ents;
  ents.push_back(Value(Object{{"type", Value("custom_emoji")}, {"offset", Value(0)}, {"length", Value(1)}, {"document_id", Value("5")}}));
  CHECK_EQ(code("messages.send", {{"peer", Value("user:1")}, {"text", Value("x")}, {"format", Value("html")}, {"entities", Value(ents)}}), bad);
  CHECK_EQ(code("messages.edit", {{"peer", Value("user:1")}, {"id", Value("5")}, {"text", Value("x")}, {"format", Value("md")}}), bad);
}

TEST_CASE("search filters and polls: unknown values and impossible quizzes are rejected", "[tapi][contract][unit]") {
  TmpDir d;
  TapiService s(options(d));
  s.setAuthorizedForTest(true);
  auto code = [&](const std::string& m, Object p) { return callSync(s, m, p).error.code; };
  const std::string bad = ErrorCode::BadParams;
  CHECK_EQ(code("messages.search", {{"query", Value("a")}, {"filter", Value("everything")}}), bad);
  js::Array two{Value("a"), Value("b")};
  CHECK_EQ(code("messages.send_poll", {{"peer", Value("user:1")}, {"question", Value("q")}, {"options", Value(two)}, {"quiz", Value(true)}}), bad);
  CHECK_EQ(code("messages.send_poll", {{"peer", Value("user:1")}, {"question", Value("q")}, {"options", Value(two)}, {"quiz", Value(true)}, {"correct", Value(5)}}), bad);
}

TEST_CASE("privacy and notifications: unknown keys, rules and scopes are rejected, read_time has no contacts rule", "[tapi][contract][privacy][unit]") {
  TmpDir d;
  TapiService s(options(d));
  s.setAuthorizedForTest(true);
  auto code = [&](const std::string& m, Object p) { return callSync(s, m, p).error.code; };
  const std::string bad = ErrorCode::BadParams;
  CHECK_EQ(code("account.set_privacy", {{"key", Value("last_seen")}, {"rule", Value("friends")}}), bad);
  CHECK_EQ(code("account.set_privacy", {{"key", Value("shoe_size")}, {"rule", Value("nobody")}}), bad);
  CHECK_EQ(code("account.set_privacy", {{"key", Value("read_time")}, {"rule", Value("contacts")}}), bad);
  CHECK_EQ(code("account.privacy", {{"key", Value("shoe_size")}}), bad);
  CHECK_EQ(code("account.set_notifications", {{"scope", Value("everything")}, {"muted", Value(true)}}), bad);
}

TEST_CASE("stickers, bots, contacts, members and login: bad input is rejected before the network", "[tapi][contract][unit]") {
  TmpDir d;
  TapiService s(options(d));
  s.setAuthorizedForTest(true);
  auto code = [&](const std::string& m, Object p) { return callSync(s, m, p).error.code; };
  const std::string bad = ErrorCode::BadParams;
  CHECK_EQ(code("messages.send_sticker", {{"peer", Value("user:1")}, {"sticker", Value("12345")}}), bad);  // never listed
  CHECK_EQ(code("stickers.set", {{"short_name", Value("")}}), bad);
  CHECK_EQ(code("bot.inline_send", {{"peer", Value("user:1")}, {"query_id", Value("")}, {"result_id", Value("r")}}), bad);
  js::Array noPhone{Value(Object{{"name", Value("A")}})};
  CHECK_EQ(code("contacts.import", {{"contacts", Value(noPhone)}}), bad);
  CHECK_EQ(code("chat.add_members", {{"peer", Value("chat:5")}, {"users", Value(js::Array{})}}), bad);
  CHECK_EQ(code("auth.resend_code", {}), bad);  // no phone number was sent yet
}
31.4ms