-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2840bb0
commit 645b35b
Showing
8 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "pfs/parsers.hpp" | ||
#include "pfs/utils.hpp" | ||
|
||
namespace pfs { | ||
namespace impl { | ||
namespace parsers { | ||
|
||
net_device parse_net_device_line(const std::string& line) | ||
{ | ||
// Example: | ||
// clang-format off | ||
// Inter-| Receive | Transmit | ||
// face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed | ||
// lo: 27267010 48045 0 0 0 0 0 0 27267010 48045 0 0 0 0 0 0 | ||
// eth0: 335754274 58179 0 0 0 0 0 0 9805218 48519 0 0 0 0 0 0 | ||
// clang-format on | ||
|
||
enum token { | ||
NAME = 0, | ||
RX_BYTES = 1, | ||
RX_PACKETS = 2, | ||
RX_ERRORS = 3, | ||
RX_DROP = 4, | ||
RX_FIFO = 5, | ||
RX_FRAME = 6, | ||
RX_COMPRESSED = 7, | ||
RX_MULTICAST = 8, | ||
TX_BYTES = 9, | ||
TX_PACKETS = 10, | ||
TX_ERRORS = 11, | ||
TX_DROP = 12, | ||
TX_FIFO = 13, | ||
TX_COLLS = 14, | ||
TX_CARRIER = 15, | ||
TX_COMPRESSED = 16, | ||
COUNT | ||
}; | ||
|
||
auto tokens = utils::split(line); | ||
if (tokens.size() != COUNT) | ||
{ | ||
throw parser_error("Corrupted net device line - Wrong number of tokens", | ||
line); | ||
} | ||
|
||
try { | ||
net_device dev; | ||
|
||
dev.name = tokens[NAME]; | ||
dev.name.pop_back(); // Remove ':'; | ||
|
||
utils::stot(tokens[RX_BYTES], dev.rx_bytes, utils::base::decimal); | ||
utils::stot(tokens[RX_PACKETS], dev.rx_packets, utils::base::decimal); | ||
utils::stot(tokens[RX_ERRORS], dev.rx_errors, utils::base::decimal); | ||
utils::stot(tokens[RX_DROP], dev.rx_drop, utils::base::decimal); | ||
utils::stot(tokens[RX_FIFO], dev.rx_fifo, utils::base::decimal); | ||
utils::stot(tokens[RX_FRAME], dev.rx_frame, utils::base::decimal); | ||
utils::stot(tokens[RX_COMPRESSED], dev.rx_compressed, utils::base::decimal); | ||
utils::stot(tokens[RX_MULTICAST], dev.rx_multicast, utils::base::decimal); | ||
utils::stot(tokens[TX_BYTES], dev.tx_bytes, utils::base::decimal); | ||
utils::stot(tokens[TX_PACKETS], dev.tx_packets, utils::base::decimal); | ||
utils::stot(tokens[TX_ERRORS], dev.tx_errors, utils::base::decimal); | ||
utils::stot(tokens[TX_DROP], dev.tx_drop, utils::base::decimal); | ||
utils::stot(tokens[TX_FIFO], dev.tx_fifo, utils::base::decimal); | ||
utils::stot(tokens[TX_COLLS], dev.tx_colls, utils::base::decimal); | ||
utils::stot(tokens[TX_CARRIER], dev.tx_carrier, utils::base::decimal); | ||
utils::stot(tokens[TX_COMPRESSED], dev.tx_compressed, utils::base::decimal); | ||
|
||
return dev; | ||
} | ||
catch (const std::invalid_argument& ex) | ||
{ | ||
throw parser_error("Corrupted net device - Invalid argument", line); | ||
} | ||
catch (const std::out_of_range& ex) | ||
{ | ||
throw parser_error("Corrupted net device - Out of range", line); | ||
} | ||
} | ||
|
||
} // namespace parsers | ||
} // namespace impl | ||
} // namespace pfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <sstream> | ||
|
||
#include "catch.hpp" | ||
#include "test_utils.hpp" | ||
|
||
#include "pfs/parsers.hpp" | ||
|
||
using namespace pfs::impl::parsers; | ||
|
||
TEST_CASE("Parse corrupted net device", "[net][net_device]") | ||
{ | ||
|
||
SECTION("Missing token") | ||
{ | ||
std::string line = | ||
"lo: 27267010 48045 0 0 0 0 0 0 " | ||
"27267010 48045 0 0 0 0 0"; | ||
|
||
REQUIRE_THROWS_AS(parse_net_device_line(line), pfs::parser_error); | ||
} | ||
|
||
SECTION("Extra token") | ||
{ | ||
std::string line = | ||
"lo: 27267010 48045 0 0 0 0 0 0 " | ||
"27267010 48045 0 0 0 0 0 0 0"; | ||
|
||
REQUIRE_THROWS_AS(parse_net_device_line(line), pfs::parser_error); | ||
} | ||
|
||
} | ||
|
||
TEST_CASE("Parse net device", "[net][net_device]") | ||
{ | ||
|
||
pfs::net_device expected; | ||
|
||
std::string line = | ||
"eth0: 335754274 58179 1 2 3 4 5 " | ||
"6 9805218 48519 11 12 13 14 15 16"; | ||
|
||
expected.name = "eth0"; | ||
expected.rx_bytes = 335754274; | ||
expected.rx_packets = 58179; | ||
expected.rx_errors = 1; | ||
expected.rx_drop = 2; | ||
expected.rx_fifo = 3; | ||
expected.rx_frame = 4; | ||
expected.rx_compressed = 5; | ||
expected.rx_multicast = 6; | ||
expected.tx_bytes = 9805218; | ||
expected.tx_packets = 48519; | ||
expected.tx_errors = 11; | ||
expected.tx_drop = 12; | ||
expected.tx_fifo = 13; | ||
expected.tx_colls = 14; | ||
expected.tx_carrier = 15; | ||
expected.tx_compressed = 16; | ||
|
||
auto device = parse_net_device_line(line); | ||
|
||
REQUIRE(device.name == expected.name); | ||
REQUIRE(device.rx_bytes == expected.rx_bytes); | ||
REQUIRE(device.rx_packets == expected.rx_packets); | ||
REQUIRE(device.rx_errors == expected.rx_errors); | ||
REQUIRE(device.rx_drop == expected.rx_drop); | ||
REQUIRE(device.rx_fifo == expected.rx_fifo); | ||
REQUIRE(device.rx_compressed == expected.rx_compressed); | ||
REQUIRE(device.rx_multicast == expected.rx_multicast); | ||
REQUIRE(device.tx_bytes == expected.tx_bytes); | ||
REQUIRE(device.tx_packets == expected.tx_packets); | ||
REQUIRE(device.tx_errors == expected.tx_errors); | ||
REQUIRE(device.tx_drop == expected.tx_drop); | ||
REQUIRE(device.tx_fifo == expected.tx_fifo); | ||
REQUIRE(device.tx_colls == expected.tx_colls); | ||
REQUIRE(device.tx_carrier == expected.tx_carrier); | ||
REQUIRE(device.tx_compressed == expected.tx_compressed); | ||
|
||
} |