mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-10-02 15:00:02 +00:00
Add unit test for utf8 sanitization.
This commit is contained in:
parent
13e46ab1e6
commit
d449781c40
7 changed files with 2125 additions and 1 deletions
BIN
test/data/UTF-8-test.txt
Normal file
BIN
test/data/UTF-8-test.txt
Normal file
Binary file not shown.
1902
test/data/UTF-8-test.txt.c
Normal file
1902
test/data/UTF-8-test.txt.c
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,23 +1,32 @@
|
|||
project(tawashi_unittest CXX)
|
||||
project(tawashi_unittest CXX C)
|
||||
|
||||
find_package(GLIB 2.20 REQUIRED)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wpedantic -Wconversion -Werror")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_DEBUG} -Wall -Wpedantic -Wconversion -Werror")
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
check.cpp
|
||||
fake_cgi_env.cpp
|
||||
test_ini_file.cpp
|
||||
test_settings_bag.cpp
|
||||
test_index_response.cpp
|
||||
test_invalid_utf8_post.cpp
|
||||
../data/UTF-8-test.txt.c
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/Catch/single_include
|
||||
PRIVATE ${TAWASHI_GEN_INCLUDE_DIR}
|
||||
PRIVATE ${GLIB_INCLUDE_DIRS}
|
||||
)
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE tawashi_implem
|
||||
PRIVATE duckhandy
|
||||
PRIVATE ${GLIB_LIBRARIES}
|
||||
)
|
||||
|
||||
add_test(
|
||||
|
|
72
test/unit/test_invalid_utf8_post.cpp
Normal file
72
test/unit/test_invalid_utf8_post.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* Copyright 2017, Michele Santullo
|
||||
* This file is part of "tawashi".
|
||||
*
|
||||
* "tawashi" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "tawashi" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "cgi_post.hpp"
|
||||
#include "cgi_env.hpp"
|
||||
#include "sanitized_utf8.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <fstream>
|
||||
#include <ciso646>
|
||||
#include <glib.h>
|
||||
|
||||
extern "C" {
|
||||
extern const unsigned char UTF_8_test_txt[];
|
||||
extern const unsigned int UTF_8_test_txt_len;
|
||||
} //extern C
|
||||
|
||||
TEST_CASE ("Retrieve and sanitize invalid an invalid utf-8 text", "[utf8][security]") {
|
||||
using tawashi::cgi::PostMapType;
|
||||
|
||||
auto content_length = std::string("CONTENT_LENGTH=") + std::to_string(UTF_8_test_txt_len);
|
||||
const std::string invalid_text_prefix("invalid_text=");
|
||||
std::string invalid_text;
|
||||
invalid_text.reserve(invalid_text_prefix.size() + UTF_8_test_txt_len);
|
||||
invalid_text = "invalid_text=";
|
||||
std::copy(reinterpret_cast<const char*>(UTF_8_test_txt), reinterpret_cast<const char*>(UTF_8_test_txt) + UTF_8_test_txt_len, std::back_inserter(invalid_text));
|
||||
|
||||
std::istringstream iss;
|
||||
iss >> std::noskipws;
|
||||
iss.str(std::move(invalid_text));
|
||||
|
||||
const char* const fake_env[] = {
|
||||
content_length.c_str(),
|
||||
nullptr
|
||||
};
|
||||
|
||||
tawashi::cgi::Env env(fake_env);
|
||||
const PostMapType& post_data = read_post(iss, env);
|
||||
|
||||
CHECK(g_utf8_validate(post_data.at("invalid_text").data(), post_data.at("invalid_text").size(), nullptr));
|
||||
|
||||
//std::istringstream iss_expected;
|
||||
//iss_expected >> std::noskipws;
|
||||
//iss_expected.str(std::string(reinterpret_cast<const char*>(libreoffice_UTF_8_test_txt), libreoffice_UTF_8_test_txt_len));
|
||||
//std::string expected_line;
|
||||
//std::istringstream iss_obtained;
|
||||
//iss_obtained >> std::noskipws;
|
||||
//iss_obtained.str(post_data.at("invalid_text"));
|
||||
//for (std::string line; std::getline(iss_obtained, line); ) {
|
||||
// std::getline(iss_expected, expected_line);
|
||||
// std::cout << '"' << line << "\n\"" << expected_line << "\"\n";
|
||||
// REQUIRE(line == expected_line);
|
||||
//}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue