From c58c8568d31deca7c3ccf403986a266ee676c0c0 Mon Sep 17 00:00:00 2001 From: Pawel Olejniczak Date: Wed, 9 Jun 2021 13:42:57 +0200 Subject: [PATCH] [CP-143] Set 204 code for responses with empty body Body field was always added to response json, even if it was containing only null. Now empty body is not attached to response json, and status code for such message is set to 204. Additionaly some minor cleanup in service desktop was done. --- .../service-desktop/ServiceDesktop.cpp | 2 +- .../service-desktop/endpoints/Context.hpp | 19 +++-- .../service-desktop/endpoints/DBHelper.hpp | 4 +- .../service-desktop/endpoints/Endpoint.hpp | 4 +- .../calendarEvents/CalendarEventsEndpoint.hpp | 4 +- .../calendarEvents/CalendarEventsHelper.cpp | 12 +-- .../calendarEvents/CalendarEventsHelper.hpp | 10 +-- .../endpoints/calllog/CalllogEndpoint.hpp | 4 +- .../endpoints/calllog/CalllogHelper.cpp | 6 +- .../endpoints/calllog/CalllogHelper.hpp | 2 +- .../endpoints/contacts/ContactHelper.cpp | 14 ++-- .../endpoints/contacts/ContactHelper.hpp | 6 +- .../endpoints/contacts/ContactsEndpoint.hpp | 2 +- .../developerMode/DeveloperModeHelper.cpp | 30 +++++--- .../developerMode/DeveloperModeHelper.hpp | 6 +- .../deviceInfo/DeviceInfoEndpoint.hpp | 4 +- .../endpoints/factoryReset/FactoryReset.cpp | 76 +++++++++---------- .../endpoints/factoryReset/FactoryReset.hpp | 4 +- .../filesystem/FilesystemEndpoint.cpp | 9 +-- .../filesystem/FilesystemEndpoint.hpp | 4 +- .../endpoints/messages/MessageHelper.cpp | 16 ++-- .../endpoints/restore/RestoreEndpoint.hpp | 2 +- .../security/SecurityEndpointHelper.cpp | 6 +- .../endpoints/update/UpdateEndpoint.hpp | 4 +- .../endpoints/update/UpdateMuditaOS.cpp | 19 +++-- .../endpoints/update/UpdateMuditaOS.hpp | 6 +- .../endpoints/update/UpdateOSTypes.hpp | 8 +- .../service-desktop/parser/HttpEnums.hpp | 4 +- .../service-desktop/tests/unittest.cpp | 3 +- test/pytest/service-desktop/test_security.py | 4 +- 30 files changed, 153 insertions(+), 141 deletions(-) diff --git a/module-services/service-desktop/ServiceDesktop.cpp b/module-services/service-desktop/ServiceDesktop.cpp index 08a4a6dbf6fced6bdafaaf3a626e5d4f1bbbacad..8a36ec33fc541d493be37ce585e96ef048cd035f 100644 --- a/module-services/service-desktop/ServiceDesktop.cpp +++ b/module-services/service-desktop/ServiceDesktop.cpp @@ -176,7 +176,7 @@ sys::ReturnCodes ServiceDesktop::InitHandler() } if (updateOsMsg != nullptr && updateOsMsg->messageType == updateos::UpdateMessageType::UpdateNow) { - LOG_DEBUG("ServiceDesktop::DataReceivedHandler file:%s uuuid:%" PRIu32 "", + LOG_DEBUG("ServiceDesktop::DataReceivedHandler file:%s uuid:%" PRIu32 "", updateOsMsg->updateStats.updateFile.c_str(), updateOsMsg->updateStats.uuid); diff --git a/module-services/service-desktop/endpoints/Context.hpp b/module-services/service-desktop/endpoints/Context.hpp index 231954d51e2e7be84e812dba7e716e5cc3c84860..12a64a7aa6bdc82d45761587a3396bfca1852963 100644 --- a/module-services/service-desktop/endpoints/Context.hpp +++ b/module-services/service-desktop/endpoints/Context.hpp @@ -4,6 +4,7 @@ #pragma once #include +#include #include "ResponseContext.hpp" namespace parserFSM @@ -40,7 +41,7 @@ namespace parserFSM auto validate() -> void { - if (body.is_object() == false) { + if (!body.is_object()) { body = json11::Json(); } if (static_cast(endpoint) > lastEndpoint) { @@ -83,16 +84,22 @@ namespace parserFSM virtual auto createSimpleResponse(const std::string &entryTitle = json::entries) -> std::string { - json11::Json responseJson = json11::Json::object{{json::endpoint, static_cast(getEndpoint())}, - {json::status, static_cast(responseContext.status)}, - {json::uuid, getUuid()}, - {json::body, responseContext.body}}; + json11::Json::object contextJsonObject = + json11::Json::object{{json::endpoint, static_cast(getEndpoint())}, + {json::status, static_cast(responseContext.status)}, + {json::uuid, getUuid()}}; + if (!responseContext.body.is_null()) { + contextJsonObject[json::body] = responseContext.body; + } + + const json11::Json responseJson{std::move(contextJsonObject)}; + return buildResponseStr(responseJson.dump().size(), responseJson.dump()); } auto setResponse(endpoint::ResponseContext r) { - responseContext = r; + responseContext = std::move(r); } auto setResponseStatus(http::Code status) diff --git a/module-services/service-desktop/endpoints/DBHelper.hpp b/module-services/service-desktop/endpoints/DBHelper.hpp index 9f103619b080ccd536ba765bf50ddd89e2524df4..a56b13be604f2e0d9d8851030427835ae11dc411 100644 --- a/module-services/service-desktop/endpoints/DBHelper.hpp +++ b/module-services/service-desktop/endpoints/DBHelper.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -22,7 +22,7 @@ namespace parserFSM virtual auto updateDBEntry(Context &context) -> sys::ReturnCodes = 0; virtual auto deleteDBEntry(Context &context) -> sys::ReturnCodes = 0; - DBHelper(sys::Service *_ownerServicePtr) : ownerServicePtr(_ownerServicePtr){}; + explicit DBHelper(sys::Service *_ownerServicePtr) : ownerServicePtr(_ownerServicePtr){}; virtual ~DBHelper() = default; protected: diff --git a/module-services/service-desktop/endpoints/Endpoint.hpp b/module-services/service-desktop/endpoints/Endpoint.hpp index 5f53555c4f504e53d3d88f34649d20eabc11dbaf..ab9235cc4d33feb9693288570f99a4991a893c93 100644 --- a/module-services/service-desktop/endpoints/Endpoint.hpp +++ b/module-services/service-desktop/endpoints/Endpoint.hpp @@ -19,7 +19,7 @@ namespace parserFSM class Endpoint { public: - Endpoint(sys::Service *_ownerServicePtr) : ownerServicePtr(_ownerServicePtr){}; + explicit Endpoint(sys::Service *_ownerServicePtr) : ownerServicePtr(_ownerServicePtr){}; virtual ~Endpoint() = default; virtual auto handle(parserFSM::Context &context) -> void = 0; auto c_str() -> const char * @@ -28,7 +28,7 @@ namespace parserFSM } protected: - std::string debugName = ""; + std::string debugName; sys::Service *ownerServicePtr = nullptr; }; diff --git a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsEndpoint.hpp b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsEndpoint.hpp index 778f4a0557b45279e4defb426488b970ab7f0840..556839e8f976d45ce9ed0165868b0650858a75e6 100644 --- a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsEndpoint.hpp +++ b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsEndpoint.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -14,7 +14,7 @@ class CalendarEventsEndpoint : public parserFSM::Endpoint std::unique_ptr helper; public: - CalendarEventsEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) + explicit CalendarEventsEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) { helper = std::make_unique(ownerServicePtr); } diff --git a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.cpp b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.cpp index 172516219271bfd2ac9e8840838bc60a5a56ffbf..a3e412afe2ef055abd921298482e24fa184c60f3 100644 --- a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.cpp +++ b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.cpp @@ -83,7 +83,7 @@ namespace parserFSM } // namespace parserFSM using namespace parserFSM; -auto CalendarEventsHelper::isICalEventValid(ICalEvent icalEvent) const -> bool +auto CalendarEventsHelper::isICalEventValid(const ICalEvent &icalEvent) const -> bool { if (!icalEvent.event.isValid) { LOG_ERROR("Ical event invalid!"); @@ -196,7 +196,7 @@ auto CalendarEventsHelper::icalEventFrom(const EventsRecord &record) const -> IC return ICalEvent{event, alarm, rrule}; } -auto CalendarEventsHelper::eventJsonObjectFrom(EventsRecord record) const -> json11::Json +auto CalendarEventsHelper::eventJsonObjectFrom(const EventsRecord &record) const -> json11::Json { auto icalEvent = icalEventFrom(record); if (!isICalEventValid(icalEvent)) { @@ -325,7 +325,7 @@ auto CalendarEventsHelper::eventsRecordFrom(ICalEvent &icalEvent) const -> Event return record; } -auto CalendarEventsHelper::ICalEventFromJson(json11::Json eventObj) const -> ICalEvent +auto CalendarEventsHelper::ICalEventFromJson(const json11::Json &eventObj) const -> ICalEvent { ICalEvent icalEvent; icalEvent.event.setUID(eventObj[json::calendar::event::uid].string_value()); @@ -358,7 +358,7 @@ auto CalendarEventsHelper::createDBEntry(Context &context) -> sys::ReturnCodes const auto eventsJsonObj = context.getBody(); const auto eventsJsonArray = eventsJsonObj[json::calendar::events].array_items(); bool ret = true; - for (auto event : eventsJsonArray) { + for (const auto &event : eventsJsonArray) { auto icalEvent = ICalEventFromJson(event); @@ -415,7 +415,7 @@ auto CalendarEventsHelper::updateDBEntry(Context &context) -> sys::ReturnCodes auto eventsJsonObj = context.getBody(); bool ret = true; - for (auto event : eventsJsonObj[json::calendar::events].array_items()) { + for (const auto &event : eventsJsonObj[json::calendar::events].array_items()) { auto icalEvent = ICalEventFromJson(event); if (!isICalEventValid(icalEvent) || icalEvent.event.getUID().empty()) { @@ -429,7 +429,7 @@ auto CalendarEventsHelper::updateDBEntry(Context &context) -> sys::ReturnCodes auto listener = std::make_unique( [](db::QueryResult *result, Context context) { if (auto EventResult = dynamic_cast(result)) { - context.setResponseStatus(EventResult->getResult() ? http::Code::OK + context.setResponseStatus(EventResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; diff --git a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.hpp b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.hpp index 1a9716c9e5e1c1469730c976c2df89a21db793bd..6246777ca0a4de69323f882e0a8fd367ed90614a 100644 --- a/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.hpp +++ b/module-services/service-desktop/endpoints/calendarEvents/CalendarEventsHelper.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -29,12 +29,12 @@ namespace parserFSM [[nodiscard]] auto repeatFrom(RecurrenceRule &rrule) const -> Repeat; [[nodiscard]] auto eventsRecordFrom(ICalEvent &icalEvent) const -> EventsRecord; - [[nodiscard]] auto eventJsonObjectFrom(EventsRecord record) const -> json11::Json; - [[nodiscard]] auto ICalEventFromJson(json11::Json eventObj) const -> ICalEvent; - [[nodiscard]] auto isICalEventValid(ICalEvent event) const -> bool; + [[nodiscard]] auto eventJsonObjectFrom(const EventsRecord &record) const -> json11::Json; + [[nodiscard]] auto ICalEventFromJson(const json11::Json &eventObj) const -> ICalEvent; + [[nodiscard]] auto isICalEventValid(const ICalEvent &event) const -> bool; public: - CalendarEventsHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr) + explicit CalendarEventsHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr) {} auto createDBEntry(Context &context) -> sys::ReturnCodes override; diff --git a/module-services/service-desktop/endpoints/calllog/CalllogEndpoint.hpp b/module-services/service-desktop/endpoints/calllog/CalllogEndpoint.hpp index b3a78730d66fb85f5957f2103c9f94385eba46a8..9100702cafb60db9b391074d18bea02520f1b9a6 100644 --- a/module-services/service-desktop/endpoints/calllog/CalllogEndpoint.hpp +++ b/module-services/service-desktop/endpoints/calllog/CalllogEndpoint.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -26,7 +26,7 @@ class CalllogEndpoint : public parserFSM::Endpoint std::unique_ptr helper; public: - CalllogEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) + explicit CalllogEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) { debugName = "CalllogEndpoint"; helper = std::make_unique(ownerServicePtr); diff --git a/module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp b/module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp index a2ca374a05ea1ed8fdb8262427192ae7a50f0818..5448c613471b5fce5376e89ba8f6c5c88d310d4f 100644 --- a/module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp +++ b/module-services/service-desktop/endpoints/calllog/CalllogHelper.cpp @@ -47,7 +47,7 @@ auto CalllogHelper::requestDataFromDB(Context &context) -> sys::ReturnCodes auto recordsPtr = std::make_unique>(contactResult->getRecords()); json11::Json::array calllogArray; - for (auto record : *recordsPtr.get()) { + for (const auto &record : *recordsPtr) { calllogArray.emplace_back(CalllogHelper::to_json(record)); } @@ -102,7 +102,7 @@ auto CalllogHelper::getCalllogByContactID(Context &context) -> sys::ReturnCodes auto records = calllogResult->getResults(); json11::Json::array calllogArray; - for (auto record : records) { + for (const auto &record : records) { calllogArray.emplace_back(CalllogHelper::to_json(record)); } @@ -133,7 +133,7 @@ auto CalllogHelper::deleteDBEntry(Context &context) -> sys::ReturnCodes [](db::QueryResult *result, Context context) { if (auto calllogResult = dynamic_cast(result)) { - context.setResponseStatus(calllogResult->getResults() ? http::Code::OK + context.setResponseStatus(calllogResult->getResults() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; diff --git a/module-services/service-desktop/endpoints/calllog/CalllogHelper.hpp b/module-services/service-desktop/endpoints/calllog/CalllogHelper.hpp index 692d814b16093908faaca422b6c76d7fd696483a..9a721fafa604d1a728f4a12c07255c0746d7e36e 100644 --- a/module-services/service-desktop/endpoints/calllog/CalllogHelper.hpp +++ b/module-services/service-desktop/endpoints/calllog/CalllogHelper.hpp @@ -27,7 +27,7 @@ namespace parserFSM class CalllogHelper : public DBHelper { public: - CalllogHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr){}; + explicit CalllogHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr){}; auto createDBEntry(Context &context) -> sys::ReturnCodes override; auto requestDataFromDB(Context &context) -> sys::ReturnCodes override; diff --git a/module-services/service-desktop/endpoints/contacts/ContactHelper.cpp b/module-services/service-desktop/endpoints/contacts/ContactHelper.cpp index 242df628d587f78c4a15fa459ba49f8a09e4f031..034c32f6a5ec439e6f5f88794fb2756435598d0e 100644 --- a/module-services/service-desktop/endpoints/contacts/ContactHelper.cpp +++ b/module-services/service-desktop/endpoints/contacts/ContactHelper.cpp @@ -30,11 +30,11 @@ using namespace parserFSM; -auto ContactHelper::to_json(ContactRecord record) -> json11::Json +auto ContactHelper::to_json(const ContactRecord &record) -> json11::Json { auto numberArray = json11::Json::array(); - for (auto number : record.numbers) { + for (const auto &number : record.numbers) { numberArray.emplace_back(number.number.getEntered().c_str()); } @@ -48,7 +48,7 @@ auto ContactHelper::to_json(ContactRecord record) -> json11::Json return recordEntry; } -auto ContactHelper::from_json(json11::Json contactJSON) -> ContactRecord +auto ContactHelper::from_json(const json11::Json &contactJSON) -> ContactRecord { auto newRecord = ContactRecord(); newRecord.primaryName = UTF8(contactJSON[json::contacts::primaryName].string_value()); @@ -56,7 +56,7 @@ auto ContactHelper::from_json(json11::Json contactJSON) -> ContactRecord newRecord.alternativeName = UTF8(contactJSON[json::contacts::alternativeName].string_value()); newRecord.address = UTF8(contactJSON[json::contacts::address].string_value()); - for (auto num : contactJSON[json::contacts::numbers].array_items()) { + for (const auto &num : contactJSON[json::contacts::numbers].array_items()) { utils::PhoneNumber phoneNumber(num.string_value()); auto contactNum = ContactRecord::Number(phoneNumber.get(), phoneNumber.toE164(), ContactNumberType ::CELL); newRecord.numbers.push_back(contactNum); @@ -91,7 +91,7 @@ auto ContactHelper::requestDataFromDB(Context &context) -> sys::ReturnCodes context.setTotalCount(contactResult->getAllLength()); json11::Json::array contactsArray; - for (const auto &record : *recordsPtr.get()) { + for (const auto &record : *recordsPtr) { contactsArray.emplace_back(ContactHelper::to_json(record)); } @@ -215,7 +215,7 @@ auto ContactHelper::updateDBEntry(Context &context) -> sys::ReturnCodes [](db::QueryResult *result, Context context) { if (auto contactResult = dynamic_cast(result)) { - context.setResponseStatus(contactResult->getResult() ? http::Code::OK + context.setResponseStatus(contactResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); @@ -242,7 +242,7 @@ auto ContactHelper::deleteDBEntry(Context &context) -> sys::ReturnCodes [](db::QueryResult *result, Context context) { if (auto contactResult = dynamic_cast(result)) { - context.setResponseStatus(contactResult->getResult() ? http::Code::OK + context.setResponseStatus(contactResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); diff --git a/module-services/service-desktop/endpoints/contacts/ContactHelper.hpp b/module-services/service-desktop/endpoints/contacts/ContactHelper.hpp index d66508e9f90485ee6682e12f1d8c84e8028114a0..e9cec56a91cb031597230f66566f249c26b0db96 100644 --- a/module-services/service-desktop/endpoints/contacts/ContactHelper.hpp +++ b/module-services/service-desktop/endpoints/contacts/ContactHelper.hpp @@ -28,7 +28,7 @@ namespace parserFSM { public: - ContactHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr) + explicit ContactHelper(sys::Service *_ownerServicePtr) : DBHelper(_ownerServicePtr) {} auto createDBEntry(Context &context) -> sys::ReturnCodes override; @@ -38,8 +38,8 @@ namespace parserFSM auto requestCount(Context &context) -> sys::ReturnCodes; auto requestContactByID(Context &context) -> sys::ReturnCodes; - static auto to_json(ContactRecord record) -> json11::Json; - static auto from_json(json11::Json contactJSON) -> ContactRecord; + static auto to_json(const ContactRecord &record) -> json11::Json; + static auto from_json(const json11::Json &contactJSON) -> ContactRecord; }; namespace json::contacts diff --git a/module-services/service-desktop/endpoints/contacts/ContactsEndpoint.hpp b/module-services/service-desktop/endpoints/contacts/ContactsEndpoint.hpp index ae3a7601856a4f8a09d5fe327c1ed2aab2744867..d947c203a0480b7aa61560f579c73c283e4a39cd 100644 --- a/module-services/service-desktop/endpoints/contacts/ContactsEndpoint.hpp +++ b/module-services/service-desktop/endpoints/contacts/ContactsEndpoint.hpp @@ -28,7 +28,7 @@ class ContactsEndpoint : public parserFSM::Endpoint std::unique_ptr helper; public: - ContactsEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) + explicit ContactsEndpoint(sys::Service *_ownerServicePtr) : Endpoint(_ownerServicePtr) { debugName = "ContactsEndpoint"; helper = std::make_unique(ownerServicePtr); diff --git a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp index e361757d569236c83fda95af7227be84bb883713..af813ee7cf9a1376e437c2395b9ccf9ff1022e6f 100644 --- a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp +++ b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.cpp @@ -84,18 +84,21 @@ auto DeveloperModeHelper::processPut(Context &context) -> ProcessResult path.service = service::name::db; path.scope = settings::SettingsScope::Global; auto msg = std::make_shared(std::move(path), std::move(value)); - code = toCode(owner->bus.sendUnicast(std::move(msg), service::name::db)); + code = owner->bus.sendUnicast(std::move(msg), service::name::db) ? http::Code::NoContent + : http::Code::InternalServerError; + return {sent::no, endpoint::ResponseContext{.status = code}}; } else if (body[json::developerMode::changeSim].is_number()) { int simSelected = body[json::developerMode::changeSim].int_value(); requestSimChange(simSelected); - code = toCode(true); + code = http::Code::NoContent; return {sent::no, endpoint::ResponseContext{.status = code}}; } else if (body[json::developerMode::changeCellularStateCmd].is_number()) { int cellularState = body[json::developerMode::changeCellularStateCmd].int_value(); - code = toCode(requestCellularPowerStateChange(cellularState)); + code = requestCellularPowerStateChange(cellularState) ? http::Code::NoContent : http::Code::InternalServerError; + return {sent::no, endpoint::ResponseContext{.status = code}}; } else if (body[json::developerMode::smsCommand].is_string()) { @@ -118,7 +121,8 @@ auto DeveloperModeHelper::processPut(Context &context) -> ProcessResult else if (body[json::developerMode::phoneLockCodeEnabled].is_bool()) { auto phoneLockState = body[json::developerMode::phoneLockCodeEnabled].bool_value(); auto msg = std::make_shared(phoneLockState); - code = toCode(owner->bus.sendUnicast(std::move(msg), "ApplicationManager")); + code = owner->bus.sendUnicast(std::move(msg), "ApplicationManager") ? http::Code::NoContent + : http::Code::InternalServerError; } else if (auto switchData = body[json::developerMode::switchApplication].object_items(); !switchData.empty()) { auto msg = std::make_shared( @@ -126,13 +130,16 @@ auto DeveloperModeHelper::processPut(Context &context) -> ProcessResult switchData[json::developerMode::switchData::applicationName].string_value(), switchData[json::developerMode::switchData::windowName].string_value(), nullptr); - code = toCode(owner->bus.sendUnicast(std::move(msg), "ApplicationManager")); + code = owner->bus.sendUnicast(std::move(msg), "ApplicationManager") ? http::Code::NoContent + : http::Code::InternalServerError; } else if (auto switchData = body[json::developerMode::switchWindow].object_items(); !switchData.empty()) { auto msg = std::make_shared( switchData[json::developerMode::switchData::windowName].string_value(), "", nullptr); - code = toCode(owner->bus.sendUnicast( - std::move(msg), switchData[json::developerMode::switchData::applicationName].string_value())); + code = owner->bus.sendUnicast(std::move(msg), + switchData[json::developerMode::switchData::applicationName].string_value()) + ? http::Code::NoContent + : http::Code::InternalServerError; } else { @@ -157,7 +164,7 @@ auto DeveloperModeHelper::processGet(Context &context) -> ProcessResult return {sent::no, std::move(response)}; } else if (keyValue == json::developerMode::cellularStateInfo) { - if (requestServiceStateInfo(owner) == false) { + if (!requestServiceStateInfo(owner)) { return {sent::no, endpoint::ResponseContext{.status = http::Code::NotAcceptable}}; } else { @@ -165,7 +172,7 @@ auto DeveloperModeHelper::processGet(Context &context) -> ProcessResult } } else if (keyValue == json::developerMode::cellularSleepModeInfo) { - if (requestCellularSleepModeInfo(owner) == false) { + if (!requestCellularSleepModeInfo(owner)) { return {sent::no, endpoint::ResponseContext{.status = http::Code::NotAcceptable}}; } else { @@ -179,7 +186,6 @@ auto DeveloperModeHelper::processGet(Context &context) -> ProcessResult else { return {sent::no, endpoint::ResponseContext{.status = http::Code::BadRequest}}; } - return {sent::no, std::nullopt}; } auto DeveloperModeHelper::getKeyCode(int val) noexcept -> bsp::KeyCodes @@ -247,7 +253,7 @@ bool DeveloperModeHelper::sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::S gui::InputEvent event(key, state, static_cast(keyCode)); LOG_INFO("Sending %s", event.str().c_str()); - auto message = std::make_shared(std::move(event)); + auto message = std::make_shared(event); return owner->bus.sendUnicast(std::move(message), service::name::evt_manager); } @@ -275,7 +281,7 @@ bool DeveloperModeHelper::requestCellularPowerStateChange(const int cellularStat } return res; } -auto DeveloperModeHelper::smsRecordFromJson(json11::Json msgJson) -> SMSRecord +auto DeveloperModeHelper::smsRecordFromJson(const json11::Json &msgJson) -> SMSRecord { auto record = SMSRecord(); diff --git a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp index 188645a63b3e9f3c9614836365419e35f0fab46a..60cd7134dd57db21b6ba017c9e2733a75ec92d03 100644 --- a/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp +++ b/module-services/service-desktop/endpoints/developerMode/DeveloperModeHelper.hpp @@ -26,9 +26,9 @@ namespace parserFSM static auto getKeyCode(int val) noexcept -> bsp::KeyCodes; bool sendKeypress(bsp::KeyCodes keyCode, gui::InputEvent::State state); - void requestSimChange(const int simSelected); - auto smsRecordFromJson(json11::Json msgJson) -> SMSRecord; - bool requestCellularPowerStateChange(const int simSelected); + void requestSimChange(int simSelected); + auto smsRecordFromJson(const json11::Json &msgJson) -> SMSRecord; + bool requestCellularPowerStateChange(int simSelected); bool requestServiceStateInfo(sys::Service *serv); bool requestCellularSleepModeInfo(sys::Service *serv); auto prepareSMS(Context &context) -> ProcessResult; diff --git a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.hpp b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.hpp index 9159c555c15698d210c929c12dec22cb67217391..7b343cc07b709b2dbf1541048f650548b0ee07dd 100644 --- a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.hpp +++ b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -23,7 +23,7 @@ class DeviceInfoEndpoint : public parserFSM::Endpoint { public: - DeviceInfoEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) + explicit DeviceInfoEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) { debugName = "DeviceInfoEndpoint"; } diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp index 012f6ee839dc3946319aa45fc657e862dc61b2b8..53f68c3ab2bdc65254653899ef4e71fbb45a653c 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp @@ -27,7 +27,7 @@ namespace FactoryReset inline constexpr auto copy_buf = 8192 * 4; } // namespace - static bool CopyFile(std::string sourcefile, std::string targetfile); + static bool CopyFile(const std::string &sourcefile, const std::string &targetfile); static int recurseDepth = 0; static const int max_recurse_depth = 120; /* 120 is just an arbitrary value of max number of recursive calls. @@ -39,8 +39,8 @@ namespace FactoryReset { LOG_INFO("Restoring factory state started..."); - recurseDepth = 0; - const auto userOSPath = purefs::dir::getUserDiskPath(); + recurseDepth = 0; + const auto userOSPath = purefs::dir::getUserDiskPath(); if (std::filesystem::is_directory(userOSPath.c_str()) && std::filesystem::is_empty(userOSPath.c_str())) { LOG_ERROR("Restoring factory state aborted"); @@ -71,7 +71,7 @@ namespace FactoryReset for (const auto &ext : selectedFileExt) { if (f.path().extension() == ext) { auto removeStatus = std::filesystem::remove(f.path()); - if (removeStatus == false) { + if (!removeStatus) { LOG_ERROR("Error deleting file %s, aborting...", f.path().c_str()); returnStatus = false; } @@ -85,42 +85,42 @@ namespace FactoryReset return returnStatus; } - bool DeleteDirContent(std::string dir) + bool DeleteDirContent(const std::string &dir) { for (auto &direntry : std::filesystem::directory_iterator(dir.c_str())) { - if ((direntry.path().string().compare(".") != 0) && (direntry.path().string().compare("..") != 0) && - (direntry.path().string().compare("...") != 0)) { - - std::string delpath = dir; - delpath += "/"; - delpath += direntry.path().string().c_str(); - - if (std::filesystem::is_directory(direntry)) { - if (direntry.path().string().compare(purefs::dir::getFactoryOSPath()) != 0) { - LOG_INFO("FactoryReset: recursively deleting dir %s...", delpath.c_str()); - try { - std::filesystem::remove_all(delpath.c_str()); - } - catch (const std::filesystem::filesystem_error &e) { - LOG_ERROR("FactoryReset: error deleting dir %s, aborting...", delpath.c_str()); - return false; - } + if (!((direntry.path().string() != ".") && (direntry.path().string() != "..") && + (direntry.path().string() != "..."))) { + continue; + } + std::string delpath = dir; + delpath += "/"; + delpath += direntry.path().string(); + + if (std::filesystem::is_directory(direntry)) { + if (direntry.path().string() != purefs::dir::getFactoryOSPath()) { + LOG_INFO("FactoryReset: recursively deleting dir %s...", delpath.c_str()); + try { + std::filesystem::remove_all(delpath.c_str()); } - } - else { - LOG_INFO("FactoryReset: deleting file %s...", delpath.c_str()); - if (std::filesystem::remove(delpath.c_str())) { - LOG_ERROR("FactoryReset: error deleting file %s, aborting...", delpath.c_str()); + catch (const std::filesystem::filesystem_error &e) { + LOG_ERROR("FactoryReset: error deleting dir %s, aborting...", delpath.c_str()); return false; } } } + else { + LOG_INFO("FactoryReset: deleting file %s...", delpath.c_str()); + if (std::filesystem::remove(delpath.c_str())) { + LOG_ERROR("FactoryReset: error deleting file %s, aborting...", delpath.c_str()); + return false; + } + } } return true; } - bool CopyDirContent(std::string sourcedir, std::string targetdir) + bool CopyDirContent(const std::string &sourcedir, const std::string &targetdir) { if (recurseDepth >= max_recurse_depth) { LOG_ERROR("FactoryReset: recurse level %d (too high), error assumed, skipping restore of dir %s", @@ -132,18 +132,18 @@ namespace FactoryReset const auto factoryOSPath = purefs::dir::getFactoryOSPath(); for (auto &direntry : std::filesystem::directory_iterator(sourcedir.c_str())) { - if ((direntry.path().string().compare(".") == 0) || (direntry.path().string().compare("..") == 0) || - (direntry.path().string().compare("...") == 0)) { + if ((direntry.path().string() == ".") || (direntry.path().string() == "..") || + (direntry.path().string() == "...")) { continue; } std::string sourcepath = sourcedir; sourcepath += "/"; - sourcepath += direntry.path().string().c_str(); + sourcepath += direntry.path().string(); std::string targetpath = targetdir; targetpath += "/"; - targetpath += direntry.path().string().c_str(); + targetpath += direntry.path().string(); if ((sourcepath.size() >= max_filepath_length) || (targetpath.size() >= max_filepath_length)) { LOG_ERROR("FactoryReset: path length (source or target) exceeds system limit of %d", @@ -153,7 +153,7 @@ namespace FactoryReset } if (std::filesystem::is_directory(direntry)) { - if (targetpath.compare(factoryOSPath.c_str()) == 0) { + if (targetpath == factoryOSPath) { continue; } @@ -172,7 +172,7 @@ namespace FactoryReset recurseDepth++; - if (CopyDirContent(sourcepath, targetpath) != true) { + if (!CopyDirContent(sourcepath, targetpath)) { recurseDepth--; return false; } @@ -182,7 +182,7 @@ namespace FactoryReset else { LOG_INFO("FactoryReset: restoring file %s into %s...", sourcepath.c_str(), targetpath.c_str()); - if (CopyFile(sourcepath, targetpath) != true) { + if (!CopyFile(sourcepath, targetpath)) { return false; } } @@ -191,7 +191,7 @@ namespace FactoryReset return true; } - static bool CopyFile(std::string sourcefile, std::string targetfile) + static bool CopyFile(const std::string &sourcefile, const std::string &targetfile) { bool ret = true; auto lamb = [](std::FILE *stream) { std::fclose(stream); }; @@ -199,10 +199,10 @@ namespace FactoryReset std::unique_ptr sf(std::fopen(sourcefile.c_str(), "r"), lamb); std::unique_ptr tf(std::fopen(targetfile.c_str(), "w"), lamb); - if ((sf.get() != nullptr) && (tf.get() != nullptr)) { + if (sf && tf) { std::unique_ptr buffer(new unsigned char[copy_buf]); - if (buffer.get() != nullptr) { + if (buffer) { uint32_t loopcount = (std::filesystem::file_size(sourcefile) / copy_buf) + 1u; uint32_t readsize = copy_buf; diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp index 336e208326fe21446fb819cd1e2c3324ad9a8858..085a52443f162ae16b5510d34b1443c9725dc4bc 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.hpp @@ -15,6 +15,6 @@ namespace FactoryReset { bool Run(sys::Service *ownerService); bool DeleteSelectedUserFiles(const std::filesystem::path &userOSPath); - bool DeleteDirContent(std::string dir); - bool CopyDirContent(std::string sourcedir, std::string targetdir); + bool DeleteDirContent(const std::string &dir); + bool CopyDirContent(const std::string &sourcedir, const std::string &targetdir); } // namespace FactoryReset diff --git a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp index 2213f4b4c5868c86e76c4fd89cb0a3042200138c..a909df085e93792e864d92ea308d0ee8cc5a9e9d 100644 --- a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp @@ -19,18 +19,13 @@ auto FilesystemEndpoint::handle(Context &context) -> void break; } } -static bool isWritable(const fs::path file) +static bool isWritable(const fs::path &file) { auto lamb = [](std::FILE *stream) { std::fclose(stream); }; std::unique_ptr sf(std::fopen(file.c_str(), "w"), lamb); - if (sf.get() != nullptr) { - return true; - } - else { - return false; - } + return static_cast(sf); } auto FilesystemEndpoint::run(Context &context) -> sys::ReturnCodes diff --git a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.hpp b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.hpp index b972d1ba4bbdea55ef0b5da19c33956e7f8441c7..c2875a5bf7190e54da83e0f41095f5bfc01279b7 100644 --- a/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.hpp +++ b/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -9,7 +9,7 @@ class FilesystemEndpoint : public parserFSM::Endpoint { public: - FilesystemEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) + explicit FilesystemEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) {} auto handle(parserFSM::Context &context) -> void override; auto run(parserFSM::Context &context) -> sys::ReturnCodes; diff --git a/module-services/service-desktop/endpoints/messages/MessageHelper.cpp b/module-services/service-desktop/endpoints/messages/MessageHelper.cpp index 0e6be20ab75a1ab31122b0799ce5851b60b53266..9571067455d0e087ce14d0c503532d9bdb8af1fe 100644 --- a/module-services/service-desktop/endpoints/messages/MessageHelper.cpp +++ b/module-services/service-desktop/endpoints/messages/MessageHelper.cpp @@ -165,7 +165,7 @@ namespace parserFSM auto MessageHelper::createSMS(Context &context) -> sys::ReturnCodes { - context.setResponseStatus(http::Code::InternalServerError); + context.setResponseStatus(http::Code::NotImplemented); MessageHandler::putToSendQueue(context.createSimpleResponse()); return sys::ReturnCodes::Success; } @@ -182,7 +182,7 @@ namespace parserFSM [=](db::QueryResult *result, Context context) { if (auto smsTemplateResult = dynamic_cast(result)) { - context.setResponseStatus(smsTemplateResult->getResults() ? http::Code::OK + context.setResponseStatus(smsTemplateResult->getResults() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; @@ -238,7 +238,7 @@ namespace parserFSM [=](db::QueryResult *result, Context context) { if (auto smsTemplateResult = dynamic_cast(result)) { - context.setResponseStatus(smsTemplateResult->getResult() ? http::Code::OK + context.setResponseStatus(smsTemplateResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; @@ -271,7 +271,7 @@ namespace parserFSM [=](db::QueryResult *result, Context context) { if (auto smsTemplateResult = dynamic_cast(result)) { - context.setResponseStatus(smsTemplateResult->getResult() ? http::Code::OK + context.setResponseStatus(smsTemplateResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; @@ -303,7 +303,7 @@ namespace parserFSM [=](db::QueryResult *result, Context context) { if (auto smsTemplateResult = dynamic_cast(result)) { - context.setResponseStatus(smsTemplateResult->getResults() ? http::Code::OK + context.setResponseStatus(smsTemplateResult->getResults() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; @@ -337,7 +337,7 @@ namespace parserFSM auto theResults = threadsResults->getResults(); threadsArray.reserve(theResults.size()); for (auto &record : theResults) { - threadsArray.emplace_back(MessageHelper::toJson(std::move(record))); + threadsArray.emplace_back(MessageHelper::toJson(record)); } context.setResponseBody(std::move(threadsArray)); context.setTotalCount(threadsResults->getTotalCount()); @@ -384,7 +384,7 @@ namespace parserFSM [=](db::QueryResult *result, Context context) { if (auto threadResult = dynamic_cast(result)) { - context.setResponseStatus(threadResult->getResult() ? http::Code::OK + context.setResponseStatus(threadResult->getResult() ? http::Code::NoContent : http::Code::InternalServerError); MessageHandler::putToSendQueue(context.createSimpleResponse()); return true; @@ -404,7 +404,7 @@ namespace parserFSM auto MessageHelper::deleteThread(Context &context) -> sys::ReturnCodes { - context.setResponseStatus(http::Code::InternalServerError); + context.setResponseStatus(http::Code::NotImplemented); MessageHandler::putToSendQueue(context.createSimpleResponse()); return sys::ReturnCodes::Success; } diff --git a/module-services/service-desktop/endpoints/restore/RestoreEndpoint.hpp b/module-services/service-desktop/endpoints/restore/RestoreEndpoint.hpp index 8ae4f126bda85e779efb29cadd550c2ebd5d4e51..5ca92f0c9bdfe5762aab558eb480e4e42693c4f2 100644 --- a/module-services/service-desktop/endpoints/restore/RestoreEndpoint.hpp +++ b/module-services/service-desktop/endpoints/restore/RestoreEndpoint.hpp @@ -20,7 +20,7 @@ namespace sys class RestoreEndpoint : public parserFSM::Endpoint { public: - RestoreEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) + explicit RestoreEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) { debugName = "RestoreEndpoint"; } diff --git a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp index dfd3aac93461be39b874bec91bffcc15c07ab956..03e8bfa90b41e727c256b31dac3411c624fe54f0 100644 --- a/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp +++ b/module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp @@ -44,7 +44,7 @@ auto SecurityEndpointHelper::processStatus(Context &context) -> http::Code preventBlockingDevice(); } - return security == EndpointSecurity::Allow ? http::Code::OK : http::Code::Forbidden; + return security == EndpointSecurity::Allow ? http::Code::NoContent : http::Code::Forbidden; } auto SecurityEndpointHelper::passCodeArrayToVecOfInts(const json11::Json::array &passCode) -> std::vector @@ -73,7 +73,9 @@ auto SecurityEndpointHelper::processConfiguration(Context &context) -> http::Cod if (passCode.size() == PasscodeLength) { try { auto msg = std::make_shared(passCodeArrayToVecOfInts(passCode)); - status = toCode(owner->bus.sendUnicast(std::move(msg), app::manager::ApplicationManager::ServiceName)); + status = owner->bus.sendUnicast(std::move(msg), app::manager::ApplicationManager::ServiceName) + ? http::Code::NoContent + : http::Code::InternalServerError; } catch (const std::exception &e) { LOG_ERROR("Passcode decoding exception: %s", e.what()); diff --git a/module-services/service-desktop/endpoints/update/UpdateEndpoint.hpp b/module-services/service-desktop/endpoints/update/UpdateEndpoint.hpp index 1e3972fa37c2bda819250b74ebb7a73e69e28872..09db4ec68b72fe4fb80d8f4576219d275ca5b8c9 100644 --- a/module-services/service-desktop/endpoints/update/UpdateEndpoint.hpp +++ b/module-services/service-desktop/endpoints/update/UpdateEndpoint.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -24,7 +24,7 @@ class UpdateEndpoint : public parserFSM::Endpoint { public: - UpdateEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) + explicit UpdateEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) { debugName = "UpdateEndpoint"; } diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp index 610d886d250a746a7cf7307724b45227b7968a70..c6b898cd4cce9d7a550bf482f9e8881b822660cf 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp @@ -53,7 +53,7 @@ UpdateMuditaOS::UpdateMuditaOS(ServiceDesktop *ownerService) : owner(ownerServic } updateos::UpdateError UpdateMuditaOS::setUpdateFile(const std::filesystem::path &updatesOSPath, - fs::path updateFileToUse) + const fs::path &updateFileToUse) { if (isUpdateToBeAborted()) { setUpdateAbortFlag(false); @@ -338,7 +338,7 @@ updateos::UpdateError UpdateMuditaOS::updateBootloader() unsigned long UpdateMuditaOS::getExtractedFileCRC32(const std::string &filePath) { - for (auto file : filesInUpdatePackage) { + for (const auto &file : filesInUpdatePackage) { if (file.fileName == filePath) { return file.fileCRC32; } @@ -448,7 +448,7 @@ updateos::UpdateError UpdateMuditaOS::updateBootJSON() auto *fpCRC = std::fopen(bootJSONAbsoulte.c_str(), "w"); if (fpCRC != nullptr) { - std::array crcBuf; + std::array crcBuf{}; snprintf(crcBuf.data(), crcBuf.size(), "%lX", bootJSONAbsoulteCRC); std::fwrite(crcBuf.data(), 1, boot::consts::crc_char_size, fpCRC); std::fclose(fpCRC); @@ -544,7 +544,7 @@ updateos::UpdateError UpdateMuditaOS::cleanupAfterUpdate() try { mtar_close(&updateTar); - if (std::remove(updateFile.c_str())) { + if (std::remove(updateFile.c_str()) != 0) { return informError(updateos::UpdateError::CantRemoveUpdateFile, "Failed to delete %s", updateFile.c_str()); } } @@ -561,10 +561,12 @@ updateos::UpdateError UpdateMuditaOS::cleanupAfterUpdate() const fs::path UpdateMuditaOS::getUpdateTmpChild(const fs::path &childPath) { - if (childPath.string().rfind("./", 0) == 0) + if (childPath.string().rfind("./", 0) == 0) { return updateTempDirectory / childPath.string().substr(2); - else + } + else { return updateTempDirectory / childPath; + } } updateos::UpdateError UpdateMuditaOS::prepareTempDirForUpdate(const std::filesystem::path &temporaryPath, @@ -872,8 +874,9 @@ void UpdateMuditaOS::informUpdateWindow() auto msgToSend = std::make_shared(updateos::UpdateMessageType::UpdateNow, file); msgToSend->updateStats.versionInformation = UpdateMuditaOS::getVersionInfoFromFile(file); msgToSend->updateStats.status = status; - if (owner) + if (owner != nullptr) { owner->bus.sendUnicast(msgToSend, app::name_desktop); + } } void UpdateMuditaOS::storeRunStatusInDB() @@ -902,7 +905,7 @@ void UpdateMuditaOS::storeRunStatusInDB() } } - if (statusRunFound == false) { + if (!statusRunFound) { // if our element was not found, insert it tempTable.emplace_back(updateRunStatus); } diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp index 56a387d50d997335a6fe29518884cebf31d2befd..ac41abf830276f3bdcaae1229b4adc8e2f126d10 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp @@ -31,7 +31,7 @@ struct FileInfo class UpdateMuditaOS : public updateos::UpdateStats { public: - UpdateMuditaOS(ServiceDesktop *ownerService); + explicit UpdateMuditaOS(ServiceDesktop *ownerService); updateos::UpdateError runUpdate(); updateos::UpdateError prepareTempDirForUpdate(const std::filesystem::path &temporaryPath, @@ -42,13 +42,13 @@ class UpdateMuditaOS : public updateos::UpdateStats updateos::UpdateError updateBootloader(); updateos::UpdateError prepareRoot(); updateos::UpdateError updateBootJSON(); - updateos::UpdateError setUpdateFile(const std::filesystem::path &updatesOSPath, fs::path updateFileToUse); + updateos::UpdateError setUpdateFile(const std::filesystem::path &updatesOSPath, const fs::path &updateFileToUse); updateos::UpdateError cleanupAfterUpdate(); updateos::UpdateError updateUserData(); updateos::UpdateError informError(updateos::UpdateError errorCode, const char *format, ...); void informDebug(const char *format, ...); - void informUpdate(const updateos::UpdateState statusCode, const char *format, ...); + void informUpdate(updateos::UpdateState statusCode, const char *format, ...); updateos::UpdateError writeBootloader(fs::path bootloaderFile); diff --git a/module-services/service-desktop/endpoints/update/UpdateOSTypes.hpp b/module-services/service-desktop/endpoints/update/UpdateOSTypes.hpp index 61f0fa446c3de4244acfb86967f990c090208e8e..4a6c827bbb29141fc7a00ad0c1a27159630ee21d 100644 --- a/module-services/service-desktop/endpoints/update/UpdateOSTypes.hpp +++ b/module-services/service-desktop/endpoints/update/UpdateOSTypes.hpp @@ -6,8 +6,6 @@ #include -namespace fs = std::filesystem; - namespace fs = std::filesystem; namespace updateos { @@ -97,7 +95,7 @@ namespace updateos uint32_t currentExtractedBytes = 0; uint32_t fileExtractedSize = 0; uint32_t uuid = 0; - std::string messageText = ""; + std::string messageText; updateos::UpdateState status; json11::Json versionInformation; }; @@ -113,8 +111,8 @@ namespace updateos { return json11::Json::object{{updateos::settings::startTime, std::to_string(startTime)}, {updateos::settings::endTime, std::to_string(endTime)}, - {updateos::settings::finishedState, (int)finishedState}, - {updateos::settings::finishedError, (int)finishedError}, + {updateos::settings::finishedState, static_cast(finishedState)}, + {updateos::settings::finishedError, static_cast(finishedError)}, {updateos::settings::fromVersion, fromVersion}, {updateos::settings::toVersion, toVersion}}; } diff --git a/module-services/service-desktop/parser/HttpEnums.hpp b/module-services/service-desktop/parser/HttpEnums.hpp index 904552d397061c82c111c470c134f7083cb0c809..1b28fa417a38b97583872f1833f43e8d542d0a0d 100644 --- a/module-services/service-desktop/parser/HttpEnums.hpp +++ b/module-services/service-desktop/parser/HttpEnums.hpp @@ -13,12 +13,14 @@ namespace parserFSM::http { OK = 200, Accepted = 202, + NoContent = 204, SeeOther = 303, BadRequest = 400, Forbidden = 403, NotFound = 404, NotAcceptable = 406, - InternalServerError = 500 + InternalServerError = 500, + NotImplemented = 501 }; /*! Enum class for the HTTP methods. diff --git a/module-services/service-desktop/tests/unittest.cpp b/module-services/service-desktop/tests/unittest.cpp index 295c1e1a4103782137dcc173b24481341d80742d..ffba1020710d1a9a7e7d257c7a3486dd6d3eb1f2 100644 --- a/module-services/service-desktop/tests/unittest.cpp +++ b/module-services/service-desktop/tests/unittest.cpp @@ -226,8 +226,7 @@ TEST_CASE("Context class test") REQUIRE(context.getMethod() == http::Method::get); REQUIRE(context.getUuid() == 12345); REQUIRE(context.getEndpoint() == EndpointType::contacts); - REQUIRE(context.createSimpleResponse() == - R"(#000000059{"body": null, "endpoint": 7, "status": 200, "uuid": 12345})"); + REQUIRE(context.createSimpleResponse() == R"(#000000045{"endpoint": 7, "status": 200, "uuid": 12345})"); context.setResponseBody(context.getBody()); REQUIRE(context.createSimpleResponse() == diff --git a/test/pytest/service-desktop/test_security.py b/test/pytest/service-desktop/test_security.py index c9afcba3500c9fa2eae847ce48bae3a5f5fcec72..cc6bbfe3cedf08a81eccceb32d190d0d7d0616c8 100644 --- a/test/pytest/service-desktop/test_security.py +++ b/test/pytest/service-desktop/test_security.py @@ -12,8 +12,8 @@ from harness.interface.defs import status def test_security_phone_unlocked(harness): body = {} - ret = harness.endpoint_request("deviceInfo", "get", body) - assert ret["status"] == status["OK"] + ret = harness.endpoint_request("usbSecurity", "get", body) + assert ret["status"] == status["NoContent"] @pytest.mark.service_desktop_test