From 4efb441008e361042c943410df486cebddf44586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 4 Nov 2014 12:45:01 +0100 Subject: [PATCH 1/5] Run tests using http requests We now start a local HTTP server on port 2900 that serves the test fixtures. This is the same port we also use in the JS test suite fixes #532 --- test/fixtures/fixture_request.cpp | 72 ------------------------------- test/headless.cpp | 64 +++++++++++++++++++++------ test/suite | 2 +- test/test.gyp | 2 - 4 files changed, 52 insertions(+), 88 deletions(-) delete mode 100644 test/fixtures/fixture_request.cpp diff --git a/test/fixtures/fixture_request.cpp b/test/fixtures/fixture_request.cpp deleted file mode 100644 index cc01760ae..000000000 --- a/test/fixtures/fixture_request.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - - -const std::string base_directory = []{ - std::string fn = __FILE__; - fn.erase(fn.find_last_of("/")); - fn.erase(fn.find_last_of("/")); - fn.erase(fn.find_last_of("/")); - return fn + "/test/suite/"; -}(); - -namespace mbgl { - -void HTTPRequestBaton::start(const util::ptr &baton) { - assert(uv_thread_self() == baton->thread_id); - - std::string clean_url = util::percentDecode(baton->path); - if (clean_url.find("local://") == 0) { - clean_url = base_directory + clean_url.substr(8); - } - - baton->response = std::make_unique(); - FILE *file = fopen(clean_url.c_str(),"rb"); - if (file != NULL) { - fseek(file, 0, SEEK_END); - const size_t size = ftell(file); - fseek(file, 0, SEEK_SET); - baton->response->data.resize(size); - const size_t read = fread(&baton->response->data[0], 1, size, file); - fclose(file); - - if (read == size) { - baton->response->code = 200; - baton->type = HTTPResponseType::Successful; - } else { - baton->response->code = 500; - baton->type = HTTPResponseType::PermanentError; - baton->response->message = "Read bytes differed from file size"; - } - } else { - baton->type = HTTPResponseType::PermanentError; - baton->response->code = 404; - } - - uv_async_send(baton->async); -} - -void HTTPRequestBaton::stop(const util::ptr &/*baton*/) { - fprintf(stderr, "HTTP request cannot be canceled because it is answered immediately"); - abort(); -} - -namespace platform { - -std::string defaultCacheDatabase() { - // Disables the cache. - return ""; -} - -} - -} diff --git a/test/headless.cpp b/test/headless.cpp index 4605d7f28..98aee6beb 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -15,22 +15,58 @@ #include "./fixtures/fixture_log.hpp" #include +#include +#include -const std::string base_directory = []{ - std::string fn = __FILE__; - fn.erase(fn.find_last_of("/")); - fn.erase(fn.find_last_of("/")); - return fn + "/test/suite/"; -}(); +std::string base_directory; -auto display_ = std::make_shared(); + +class ServerEnvironment : public ::testing::Environment { +public: + virtual void SetUp() { + pid = fork(); + if (pid < 0) { + throw std::runtime_error("Cannot create web server"); + } else if (pid == 0) { + int ret = execve((base_directory + "bin/server.py").c_str(), nullptr, nullptr); + // This call should not return. In case execve failed, we exit anyway. + if (ret < 0) { + fprintf(stderr, "Failed to start server: %s\n", strerror(errno)); + } + exit(0); + } else { + display = std::make_shared(); + } + } + virtual void TearDown() { + ASSERT_TRUE(pid); + kill(pid, SIGHUP); + } + + std::shared_ptr display; + +private: + pid_t pid = 0; +}; + + +ServerEnvironment* env = nullptr; + + +GTEST_API_ int main(int argc, char *argv[]) { + base_directory = std::string(dirname(argv[0])) + "/" + dirname(const_cast(__FILE__)) + "/suite/"; + testing::InitGoogleTest(&argc, argv); + env = new ServerEnvironment(); + ::testing::AddGlobalTestEnvironment(env); + return RUN_ALL_TESTS(); +} class HeadlessTest : public ::testing::TestWithParam {}; TEST_P(HeadlessTest, render) { using namespace mbgl; - const std::string &base = GetParam(); + const std::string& base = GetParam(); std::string style = util::read_file(base_directory + "tests/" + base + "/style.json"); std::string info = util::read_file(base_directory + "tests/" + base + "/info.json"); @@ -55,8 +91,10 @@ TEST_P(HeadlessTest, render) { Log::Set(); for (auto it = infoDoc.MemberBegin(), end = infoDoc.MemberEnd(); it != end; it++) { - const std::string name { it->name.GetString(), it->name.GetStringLength() }; - const rapidjson::Value &value = it->value; + const std::string name { + it->name.GetString(), it->name.GetStringLength() + }; + const rapidjson::Value& value = it->value; ASSERT_TRUE(value.IsObject()); if (value.HasMember("center")) ASSERT_TRUE(value["center"].IsArray()); @@ -72,16 +110,16 @@ TEST_P(HeadlessTest, render) { std::vector classes; if (value.HasMember("classes")) { - const rapidjson::Value &js_classes = value["classes"]; + const rapidjson::Value& js_classes = value["classes"]; ASSERT_TRUE(js_classes.IsArray()); for (rapidjson::SizeType i = 0; i < js_classes.Size(); i++) { - const rapidjson::Value &js_class = js_classes[i]; + const rapidjson::Value& js_class = js_classes[i]; ASSERT_TRUE(js_class.IsString()); classes.push_back({ js_class.GetString(), js_class.GetStringLength() }); } } - HeadlessView view(display_); + HeadlessView view(env->display); Map map(view); map.setStyleJSON(style, base_directory); diff --git a/test/suite b/test/suite index 1651a3df4..8f03d5dab 160000 --- a/test/suite +++ b/test/suite @@ -1 +1 @@ -Subproject commit 1651a3df444ff83c98f6eff00679d6b84dc8845e +Subproject commit 8f03d5dabfb7ec6447696b25691794280f399477 diff --git a/test/test.gyp b/test/test.gyp index 8ae475d1c..96e0917a5 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -162,9 +162,7 @@ 'product_name': 'test_headless', 'type': 'executable', 'sources': [ - './main.cpp', './headless.cpp', - './fixtures/fixture_request.cpp', './fixtures/fixture_log.cpp', ], 'conditions': [ From b6c10e44c08fb7cdb16f8be80fc29e8054eafc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 4 Nov 2014 13:58:31 +0100 Subject: [PATCH 2/5] pass array with a single nullptr instead of nullptr for compatibility --- test/headless.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/headless.cpp b/test/headless.cpp index 98aee6beb..26aff177f 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -28,7 +28,7 @@ public: if (pid < 0) { throw std::runtime_error("Cannot create web server"); } else if (pid == 0) { - int ret = execve((base_directory + "bin/server.py").c_str(), nullptr, nullptr); + int ret = execve((base_directory + "bin/server.py").c_str(), { nullptr }, { nullptr }); // This call should not return. In case execve failed, we exit anyway. if (ret < 0) { fprintf(stderr, "Failed to start server: %s\n", strerror(errno)); From 75889d812402ac2b75d891576a1c37cb43c8eaf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 4 Nov 2014 06:44:50 -0800 Subject: [PATCH 3/5] replace strftime_l with our custom formatter --- include/mbgl/util/time.hpp | 6 ++++++ platform/default/http_request_baton_curl.cpp | 14 ++++---------- src/util/time.cpp | 13 +++++++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/mbgl/util/time.hpp b/include/mbgl/util/time.hpp index 9d61edf75..e8afbeff6 100644 --- a/include/mbgl/util/time.hpp +++ b/include/mbgl/util/time.hpp @@ -1,7 +1,9 @@ #ifndef MBGL_UTIL_TIME #define MBGL_UTIL_TIME +#include #include +#include namespace mbgl { @@ -15,6 +17,10 @@ namespace util { // comparisons between different timestamps produce meaningful values. timestamp now(); + +// Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT" +std::string rfc1123(std::time_t time); + } // used for time conversions diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp index 0c3d2b43f..80eed2767 100644 --- a/platform/default/http_request_baton_curl.cpp +++ b/platform/default/http_request_baton_curl.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -9,8 +10,6 @@ #include #include #include -#include -#include // This file contains code from http://curl.haxx.se/libcurl/c/multi-uv.html: @@ -369,9 +368,6 @@ void start_request(void *const ptr) { util::ptr &baton = *baton_guard.get(); assert(baton); - // Create a C locale - static locale_t locale = newlocale(LC_ALL_MASK, nullptr, nullptr); - CURL *handle = nullptr; if (!handles.empty()) { handle = handles.front(); @@ -390,11 +386,9 @@ void start_request(void *const ptr) { const std::string header = std::string("If-None-Match: ") + baton->response->etag; context->headers = curl_slist_append(context->headers, header.c_str()); } else if (baton->response->modified) { - const time_t modified = baton->response->modified; - struct tm *timeinfo = std::gmtime(&modified); - char buffer[64]; - strftime_l(buffer, 64, "If-Modified-Since: %a, %d %b %Y %H:%M:%S GMT", timeinfo, locale); - context->headers = curl_slist_append(context->headers, buffer); + const std::string time = std::string("If-Modified-Since: ") + + util::rfc1123(baton->response->modified); + context->headers = curl_slist_append(context->headers, time.c_str()); } } diff --git a/src/util/time.cpp b/src/util/time.cpp index 4cd8077f9..1953975b1 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -8,5 +8,18 @@ timestamp now() { return uv_hrtime(); } +static const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + +std::string rfc1123(std::time_t time) { + std::tm info; + gmtime_r(&time, &info); + char buffer[30]; + snprintf(buffer, 30, "%s, %02d %s %4d %02d:%02d:%02d GMT", week[info.tm_wday], info.tm_mday, + months[info.tm_mon], 1900 + info.tm_year, info.tm_hour, info.tm_min, info.tm_sec); + return buffer; +} + } } From 67eae5724a8e1caa36ca492ebe5f7e0d01b42ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 4 Nov 2014 06:45:06 -0800 Subject: [PATCH 4/5] use execv and pass non-inline argument for gcc --- test/headless.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/headless.cpp b/test/headless.cpp index 26aff177f..361f3a9fc 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -28,7 +28,8 @@ public: if (pid < 0) { throw std::runtime_error("Cannot create web server"); } else if (pid == 0) { - int ret = execve((base_directory + "bin/server.py").c_str(), { nullptr }, { nullptr }); + char *arg[] = { nullptr }; + int ret = execv((base_directory + "bin/server.py").c_str(), arg); // This call should not return. In case execve failed, we exit anyway. if (ret < 0) { fprintf(stderr, "Failed to start server: %s\n", strerror(errno)); From 885d61e5def90678c89235e7b79750375f180220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 4 Nov 2014 06:45:21 -0800 Subject: [PATCH 5/5] use dirname() on copies of the parameter only glibc's dirname() modifies the parameter --- test/headless.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/headless.cpp b/test/headless.cpp index 361f3a9fc..657bab4c3 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -55,7 +55,11 @@ ServerEnvironment* env = nullptr; GTEST_API_ int main(int argc, char *argv[]) { - base_directory = std::string(dirname(argv[0])) + "/" + dirname(const_cast(__FILE__)) + "/suite/"; + // Note: glibc's dirname() **modifies** the argument and can't handle static strings. + std::string argv0 { argv[0] }; argv0 = dirname(const_cast(argv0.c_str())); + std::string file { __FILE__ }; file = dirname(const_cast(file.c_str())); + base_directory = argv0 + "/" + file + "/suite/"; + testing::InitGoogleTest(&argc, argv); env = new ServerEnvironment(); ::testing::AddGlobalTestEnvironment(env);