Skip to content

Commit 4fcb7ac

Browse files
committed
"session Id" and "session version" are an numeric string which accept leading zeros
1 parent fd1fd2f commit 4fcb7ac

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

Development/nmos/sdp_utils.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1624,4 +1624,14 @@ namespace nmos
16241624
{
16251625
details::validate_sdp_parameters(details::format_constraints, sdp_params, details::get_format(sdp_params), details::get_format_parameters(sdp_params), receiver);
16261626
}
1627+
1628+
// Validate the numeric string
1629+
const utility::string_t& valid_numeric_string(const utility::string_t& s)
1630+
{
1631+
if (!std::all_of(s.begin(), s.end(), ::isdigit))
1632+
{
1633+
throw std::invalid_argument("not a numeric string");
1634+
}
1635+
return s;
1636+
}
16271637
}

Development/nmos/sdp_utils.h

+11-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <functional>
66
#include <map>
77
#include <stdexcept>
8-
#include <boost/lexical_cast.hpp>
98
#include "bst/any.h"
109
#include "bst/optional.h"
1110
#include "cpprest/basic_utils.h"
@@ -64,6 +63,11 @@ namespace nmos
6463
// Validate the SDP parameters against a receiver for "video/raw", "audio/L", "video/smpte291" or "video/SMPTE2022-6"
6564
void validate_sdp_parameters(const web::json::value& receiver, const sdp_parameters& sdp_params);
6665

66+
// sdp_parameters helper functions
67+
68+
// Validate the numeric string
69+
const utility::string_t& valid_numeric_string(const utility::string_t& s);
70+
6771
// Format-specific types
6872

6973
struct video_raw_parameters;
@@ -86,30 +90,17 @@ namespace nmos
8690
utility::string_t session_id;
8791
utility::string_t session_version;
8892

89-
origin_t() : session_id(), session_version() {}
93+
origin_t() {}
9094
origin_t(const utility::string_t& user_name, uint64_t session_id, uint64_t session_version)
9195
: user_name(user_name)
92-
, session_id(boost::lexical_cast<utility::string_t>(session_id))
93-
, session_version(boost::lexical_cast<utility::string_t>(session_version))
96+
, session_id(utility::conversions::details::to_string_t(session_id))
97+
, session_version(utility::conversions::details::to_string_t(session_version))
9498
{}
9599
origin_t(const utility::string_t& user_name, const utility::string_t& session_id, const utility::string_t& session_version)
96100
: user_name(user_name)
97-
, session_version(session_version)
98-
{
99-
// validate session_id is a numeric string
100-
if (!std::all_of(session_id.begin(), session_id.end(), ::isdigit))
101-
{
102-
throw std::invalid_argument("session id must be a numeric string");
103-
}
104-
this->session_id = session_id;
105-
106-
// validate session_version is a numeric string
107-
if (!std::all_of(session_version.begin(), session_version.end(), ::isdigit))
108-
{
109-
throw std::invalid_argument("session version must be a numeric string");
110-
}
111-
this->session_version = session_version;
112-
}
101+
, session_id(valid_numeric_string(session_id))
102+
, session_version(valid_numeric_string(session_version))
103+
{}
113104
origin_t(const utility::string_t& user_name, uint64_t session_id_version)
114105
: origin_t{ user_name, session_id_version, session_id_version }
115106
{}

Development/sdp/sdp_grammar.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "sdp/sdp.h"
33

44
#include <stdexcept>
5-
#include <boost/lexical_cast.hpp>
65
#include "bst/regex.h"
76
#include "cpprest/basic_utils.h"
87
#include "cpprest/json_visit.h"
@@ -77,12 +76,7 @@ namespace sdp
7776
inline web::json::value digits2jns(const std::string& s)
7877
{
7978
if (!std::all_of(s.begin(), s.end(), ::isdigit)) throw sdp_parse_error("expected a sequence of digits");
80-
81-
uint64_t v;
82-
std::istringstream is(s);
83-
is >> v;
84-
if (is.fail() || !is.eof()) return s2js(s);
85-
return web::json::value(boost::lexical_cast<utility::string_t>(v));
79+
return s2js(s);
8680
}
8781

8882
// find the first delimiter in str, beginning at pos, and return the substring from pos to the delimiter (or end)

Development/sdp/test/sdp_test.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ m=video 000000000000000000050020 RTP/AVP 0096
396396
)";
397397

398398
const std::string expected_sdp = R"(v=0
399-
o=- 7 987654321098765432 IN IP4 192.0.2.0
399+
o=- 007 0987654321098765432 IN IP4 192.0.2.0
400400
s=Leading zeros
401401
b=AS:9876543210
402402
t=0 0
@@ -577,10 +577,21 @@ BST_TEST_CASE(testSdpSessionId)
577577
{
578578
const std::string before = "v=0\r\no=- ";
579579
const std::string after = " 42 IN IP4 10.0.0.1\r\ns= \r\nt=0 0\r\n";
580-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "0" + after));
581-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "007" + after));
582-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "18446744073709551615" + after)); // session id to UINT64_MAX
583-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "184467440737095516150" + after)); // session id greater than UINT64_MAX
580+
581+
const std::vector<std::pair<utility::string_t, std::string>> session_id_params = {
582+
{U("1"), before + "1" + after},
583+
{U("00000000000000000000"), before + "00000000000000000000" + after},
584+
{U("0018446744073709551615"), before + "0018446744073709551615" + after},
585+
{U("001844674407370955161500"), before + "001844674407370955161500" + after}
586+
};
587+
for (const auto& session_id_param : session_id_params)
588+
{
589+
auto session_description = sdp::parse_session_description(session_id_param.second);
590+
auto origin = sdp::fields::origin(session_description);
591+
auto session_id = sdp::fields::session_id(origin);
592+
BST_CHECK_EQUAL(session_id_param.first, session_id);
593+
}
594+
584595
// an invalid session id results in "sdp parse error - expected a sequence of digits at line 2"
585596
BST_REQUIRE_THROW(sdp::parse_session_description(before + "foo" + after), std::runtime_error);
586597
BST_REQUIRE_THROW(sdp::parse_session_description(before + "0foo" + after), std::runtime_error);
@@ -593,10 +604,21 @@ BST_TEST_CASE(testSdpSessionVersion)
593604
{
594605
const std::string before = "v=0\r\no=- 42 ";
595606
const std::string after = " IN IP4 10.0.0.1\r\ns= \r\nt=0 0\r\n";
596-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "0" + after));
597-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "007" + after));
598-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "18446744073709551615" + after)); // session version to UINT64_MAX
599-
BST_REQUIRE_NO_THROW(sdp::parse_session_description(before + "184467440737095516150" + after)); // session version greater than UINT64_MAX
607+
608+
const std::vector<std::pair<utility::string_t, std::string>> session_version_params = {
609+
{U("1"), before + "1" + after},
610+
{U("00000000000000000000"), before + "00000000000000000000" + after},
611+
{U("0018446744073709551615"), before + "0018446744073709551615" + after},
612+
{U("001844674407370955161500"), before + "001844674407370955161500" + after}
613+
};
614+
for (const auto& session_version_param : session_version_params)
615+
{
616+
auto session_description = sdp::parse_session_description(session_version_param.second);
617+
auto origin = sdp::fields::origin(session_description);
618+
auto session_version = sdp::fields::session_version(origin);
619+
BST_CHECK_EQUAL(session_version_param.first, session_version);
620+
}
621+
600622
// an invalid session version results in "sdp parse error - expected a sequence of digits at line 2"
601623
BST_REQUIRE_THROW(sdp::parse_session_description(before + "foo" + after), std::runtime_error);
602624
BST_REQUIRE_THROW(sdp::parse_session_description(before + "0foo" + after), std::runtime_error);

0 commit comments

Comments
 (0)