Add LinkManager::check_link test.

This commit is contained in:
levlam 2021-05-26 01:21:52 +03:00
parent aaba82f5c5
commit f2ab121fe4
3 changed files with 50 additions and 2 deletions

View File

@ -249,7 +249,7 @@ Result<string> LinkManager::check_link(Slice link) {
Slice query(http_url.query_);
CHECK(query[0] == '/');
if (query[1] == '?') {
if (query.size() > 1 && query[1] == '?') {
query.remove_prefix(1);
}
return PSTRING() << (is_tg ? "tg" : "ton") << "://" << http_url.host_ << query;

View File

@ -4,8 +4,9 @@ cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
set(TD_TEST_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/db.cpp
${CMAKE_CURRENT_SOURCE_DIR}/http.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mtproto.cpp
${CMAKE_CURRENT_SOURCE_DIR}/link.cpp
${CMAKE_CURRENT_SOURCE_DIR}/message_entities.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mtproto.cpp
${CMAKE_CURRENT_SOURCE_DIR}/poll.cpp
${CMAKE_CURRENT_SOURCE_DIR}/secret.cpp
${CMAKE_CURRENT_SOURCE_DIR}/secure_storage.cpp

47
test/link.cpp Normal file
View File

@ -0,0 +1,47 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/LinkManager.h"
#include "td/utils/tests.h"
static void check_link(td::string url, td::string expected) {
auto result = td::LinkManager::check_link(url);
if (result.is_ok()) {
ASSERT_STREQ(expected, result.ok());
} else {
ASSERT_TRUE(expected.empty());
}
}
TEST(Link, check_link) {
check_link("sftp://google.com", "");
check_link("tg://google.com", "tg://google.com/");
check_link("tOn://google", "ton://google/");
check_link("httP://google.com?1#tes", "http://google.com/?1#tes");
check_link("httPs://google.com/?1#tes", "https://google.com/?1#tes");
check_link("tg://google?1#tes", "tg://google?1#tes");
check_link("tg://google/?1#tes", "tg://google?1#tes");
check_link("TG:_", "tg://_/");
check_link("sftp://google.com", "");
check_link("sftp://google.com", "");
check_link("http:google.com", "");
check_link("tg://http://google.com", "");
check_link("tg:http://google.com", "");
check_link("tg:https://google.com", "");
check_link("tg:test@google.com", "");
check_link("tg:google.com:80", "");
check_link("tg:google.com", "tg://google.com/");
check_link("tg:google.com:0", "");
check_link("tg:google.com:a", "");
check_link("tg:[2001:db8:0:0:0:ff00:42:8329]", "");
check_link("tg:127.0.0.1", "tg://127.0.0.1/");
check_link("http://[2001:db8:0:0:0:ff00:42:8329]", "http://[2001:db8:0:0:0:ff00:42:8329]/");
check_link("http://localhost", "");
check_link("http://..", "http://../");
check_link("..", "http://../");
check_link("https://.", "");
}