diff --git a/Makefile b/Makefile index 4b500ed..2587695 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,15 @@ jsoncpp_cflags := $(shell pkg-config --cflags jsoncpp) jsoncpp_libs := $(shell pkg-config --libs jsoncpp) +curl_cflags := $(shell pkg-config --cflags libcurl) +curl_libs := $(shell pkg-config --libs libcurl) +ssl_cflags := $(shell pkg-config --cflags openssl) +ssl_libs := $(shell pkg-config --libs openssl) CFLAGS = -pthread -O2 -Wall -Werror -pedantic -Wno-long-long \ - -Wno-variadic-macros -Isrc $(jsoncpp_cflags) -upl_libs = -pthread $(jsoncpp_libs) -lcurl -lssl + -Wno-variadic-macros -Isrc \ + $(jsoncpp_cflags) $(curl_cflags) $(ssl_cflags) +upl_libs = -pthread $(jsoncpp_libs) $(curl_libs) $(ssl_libs) ext_libs = $(jsoncpp_libs) rfc_libs = $(jsoncpp_libs) diff --git a/src/CouchDB.cxx b/src/CouchDB.cxx index 8b564e7..554b6f7 100644 --- a/src/CouchDB.cxx +++ b/src/CouchDB.cxx @@ -134,7 +134,7 @@ void Database::save_doc(Json::Value &doc) { response = server.curl.put(doc_url, json_doc); } - catch (EZ::HTTPResponse e) + catch (EZ::HTTPResponse &e) { /* Catch HTTP 409 Resource Conflict */ diff --git a/src/RFC3339.cxx b/src/RFC3339.cxx index 7274b16..5a1f066 100644 --- a/src/RFC3339.cxx +++ b/src/RFC3339.cxx @@ -21,7 +21,7 @@ bool validate_rfc3339(const string &rfc3339) { rfc3339_to_timestamp(rfc3339); } - catch (InvalidFormat e) + catch (InvalidFormat &e) { return false; } diff --git a/src/UKHASExtractor.cxx b/src/UKHASExtractor.cxx index a7bcd93..a640163 100644 --- a/src/UKHASExtractor.cxx +++ b/src/UKHASExtractor.cxx @@ -66,7 +66,7 @@ void UKHASExtractor::push(char b, enum push_flags flags) { mgr->data(crude_parse()); } - catch (runtime_error e) + catch (runtime_error &e) { mgr->status("UKHAS Extractor: crude parse failed: " + string(e.what())); @@ -386,7 +386,7 @@ Json::Value UKHASExtractor::crude_parse() attempt_settings(data, (*it), checksum_name, parts); return data; } - catch (runtime_error e) + catch (runtime_error &e) { errors.push_back(e.what()); } diff --git a/src/Uploader.cxx b/src/Uploader.cxx index 78b3568..101cf90 100644 --- a/src/Uploader.cxx +++ b/src/Uploader.cxx @@ -175,7 +175,7 @@ string Uploader::payload_telemetry(const string &data, database.save_doc(doc); return doc_id; } - catch (CouchDB::Conflict e) + catch (CouchDB::Conflict &e) { for (int attempts = 0; attempts < max_merge_attempts; attempts++) { @@ -191,7 +191,7 @@ string Uploader::payload_telemetry(const string &data, return doc_id; } - catch (CouchDB::Conflict e) + catch (CouchDB::Conflict &e) { continue; } diff --git a/src/UploaderThread.cxx b/src/UploaderThread.cxx index fa6ec9b..f2687c3 100644 --- a/src/UploaderThread.cxx +++ b/src/UploaderThread.cxx @@ -224,12 +224,12 @@ void *UploaderThread::run() { break; } - catch (runtime_error e) + catch (runtime_error &e) { caught_exception(e); continue; } - catch (invalid_argument e) + catch (invalid_argument &e) { caught_exception(e); continue; diff --git a/tests/test_rfc3339.py b/tests/test_rfc3339.py index fd87c70..62e4820 100644 --- a/tests/test_rfc3339.py +++ b/tests/test_rfc3339.py @@ -33,7 +33,13 @@ class ProxyFunction(object): args = [self.name] + list(args) self.p.stdin.write(json.dumps(args)) self.p.stdin.write("\n") - return json.loads(self.p.stdout.readline()) + response, thing = json.loads(self.p.stdout.readline()) + if response == "return": + return thing + elif response == "time_t error": + raise ValueError("timestamp out of range for platform time_t") + else: + raise Exception(thing) class ProxyFunctionModule(object): @@ -210,13 +216,20 @@ class TestTimestampToRFC3339UTCOffset(object): class TestTimestampToRFC3339LocalOffsetLondon(object): def setup(self): - environ = os.environ.copy() - environ["TZ"] = "Europe/London" + self.old = os.environ.get("TZ", None) + os.environ["TZ"] = "Europe/London" + time.tzset() - self.mod = ProxyFunctionModule("tests/rfc3339", environ=environ) + self.mod = ProxyFunctionModule("tests/rfc3339", environ=os.environ) self.func = self.mod.timestamp_to_rfc3339_localoffset def teardown(self): + if self.old is None: + del os.environ["TZ"] + else: + os.environ["TZ"] = self.old + time.tzset() + self.mod.close() def test_simple_cases(self): @@ -244,13 +257,20 @@ class TestTimestampToRFC3339LocalOffsetLondon(object): class TestTimestampToRFC3339LocalOffsetNewYork(object): def setup(self): - environ = os.environ.copy() - environ["TZ"] = "America/New_York" + self.old = os.environ.get("TZ", None) + os.environ["TZ"] = "America/New_York" + time.tzset() - self.mod = ProxyFunctionModule("tests/rfc3339", environ=environ) + self.mod = ProxyFunctionModule("tests/rfc3339", environ=os.environ) self.func = self.mod.timestamp_to_rfc3339_localoffset def teardown(self): + if self.old is None: + del os.environ["TZ"] + else: + os.environ["TZ"] = self.old + time.tzset() + self.mod.close() def test_simple_cases(self): diff --git a/tests/test_rfc3339_main.cxx b/tests/test_rfc3339_main.cxx index 23ec96c..23af1cf 100644 --- a/tests/test_rfc3339_main.cxx +++ b/tests/test_rfc3339_main.cxx @@ -36,10 +36,13 @@ int main(int argc, char **argv) } } -void reply(const Json::Value &what) +void reply(const Json::Value &arg1, const Json::Value &arg2) { + Json::Value response(Json::arrayValue); + response.append(arg1); + response.append(arg2); Json::FastWriter writer; - cout << writer.write(what); + cout << writer.write(response); } void handle_command(const Json::Value &command) @@ -58,18 +61,29 @@ void handle_command(const Json::Value &command) int_arg = command[1u].asLargestInt(); } - if (command_name == "validate_rfc3339") - reply(RFC3339::validate_rfc3339(string_arg)); - else if (command_name == "rfc3339_to_timestamp") - reply(RFC3339::rfc3339_to_timestamp(string_arg)); - else if (command_name == "timestamp_to_rfc3339_utcoffset") - reply(RFC3339::timestamp_to_rfc3339_utcoffset(int_arg)); - else if (command_name == "timestamp_to_rfc3339_localoffset") - reply(RFC3339::timestamp_to_rfc3339_localoffset(int_arg)); - else if (command_name == "now_to_rfc3339_utcoffset") - reply(RFC3339::now_to_rfc3339_utcoffset()); - else if (command_name == "now_to_rfc3339_localoffset") - reply(RFC3339::now_to_rfc3339_localoffset()); - else - throw runtime_error("Command not found"); + try + { + if (command_name == "validate_rfc3339") + reply("return", RFC3339::validate_rfc3339(string_arg)); + else if (command_name == "rfc3339_to_timestamp") + reply("return", RFC3339::rfc3339_to_timestamp(string_arg)); + else if (command_name == "timestamp_to_rfc3339_utcoffset") + reply("return", RFC3339::timestamp_to_rfc3339_utcoffset(int_arg)); + else if (command_name == "timestamp_to_rfc3339_localoffset") + reply("return", RFC3339::timestamp_to_rfc3339_localoffset(int_arg)); + else if (command_name == "now_to_rfc3339_utcoffset") + reply("return", RFC3339::now_to_rfc3339_utcoffset()); + else if (command_name == "now_to_rfc3339_localoffset") + reply("return", RFC3339::now_to_rfc3339_localoffset()); + else + throw runtime_error("Command not found"); + } + catch (out_of_range &e) + { + reply("time_t error", false); + } + catch (exception &e) + { + reply("exception", e.what()); + } } diff --git a/tests/test_uploader.py b/tests/test_uploader.py index f374c10..bf5fb60 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -14,6 +14,7 @@ import uuid import copy import random import xml.etree.cElementTree as ET +import urllib class ProxyException: def __init__(self, name, what=None): @@ -252,7 +253,7 @@ class MockHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): e = self.server.expect_queue.popleft() self.compare(e["method"], self.command, "method") - self.compare(e["path"], self.path, "path") + self.compare(e["path"], urllib.unquote(self.path), "path") expect_100_header = self.headers.getheader('expect') expect_100 = expect_100_header and \ @@ -740,13 +741,10 @@ class TestCPPConnector: fake_view_response = \ {"total_rows": len(rows), "offset": 0, "rows": rows} - # cURL is a little overzealous with its escape(): _ is replaced - # with %5F. This should be fine - self.callbacks.advance_time(1925) view_time = self.callbacks.time_project(1925) - view_path = "_design/flight/_view/end%5Fstart%5Fincluding%5Fpayloads" - options = "include%5Fdocs=true&startkey=%5B{0}%5D".format(view_time) + view_path = "_design/flight/_view/end_start_including_payloads" + options = "include_docs=true&startkey=[{0}]".format(view_time) self.couchdb.expect_request( path=self.db_path + view_path + "?" + options, @@ -766,8 +764,8 @@ class TestCPPConnector: fake_view_response = \ {"total_rows": len(rows), "offset": 0, "rows": rows} - view_path = "_design/payload%5Fconfiguration/_view/name%5Ftime%5Fcreated" - options = "include%5Fdocs=true" + view_path = "_design/payload_configuration/_view/name_time_created" + options = "include_docs=true" self.couchdb.expect_request( path=self.db_path + view_path + "?" + options, diff --git a/tests/test_uploader_main.cxx b/tests/test_uploader_main.cxx index 2258f33..e4e3697 100644 --- a/tests/test_uploader_main.cxx +++ b/tests/test_uploader_main.cxx @@ -140,14 +140,14 @@ int main(int argc, char **argv) else report_result("return"); } - catch (runtime_error e) + catch (runtime_error &e) { if (e.what() == string("invalid command name")) throw; report_result("error", "runtime_error", e.what()); } - catch (invalid_argument e) + catch (invalid_argument &e) { report_result("error", "invalid_argument", e.what()); }