Adding element id validity check

Adding checks for element set id functions
This commit is contained in:
Michael Tryby
2019-04-16 17:29:32 -04:00
parent 22a7993c8c
commit 3186ec326c
5 changed files with 41 additions and 7 deletions

View File

@@ -2021,7 +2021,7 @@ int DLLEXPORT EN_setnodeid(EN_Project p, int index, char *newid)
if (index <= 0 || index > net->Nnodes) return 203; if (index <= 0 || index > net->Nnodes) return 203;
n = strlen(newid); n = strlen(newid);
if (n < 1 || n > MAXID) return 209; if (n < 1 || n > MAXID) return 209;
if (strcspn(newid, " ;") < n) return 209; if (!cstr_isvalid(newid)) return 250;
// Check if another node with same name exists // Check if another node with same name exists
if (hashtable_find(net->NodeHashTable, newid) > 0) return 215; if (hashtable_find(net->NodeHashTable, newid) > 0) return 215;
@@ -3208,7 +3208,7 @@ int DLLEXPORT EN_setlinkid(EN_Project p, int index, char *newid)
if (index <= 0 || index > net->Nlinks) return 204; if (index <= 0 || index > net->Nlinks) return 204;
n = strlen(newid); n = strlen(newid);
if (n < 1 || n > MAXID) return 211; if (n < 1 || n > MAXID) return 211;
if (strcspn(newid, " ;") < n) return 211; if (!cstr_isvalid(newid)) return 250;
// Check if another link with same name exists // Check if another link with same name exists
if (hashtable_find(net->LinkHashTable, newid) > 0) return 215; if (hashtable_find(net->LinkHashTable, newid) > 0) return 215;
@@ -4085,7 +4085,12 @@ int DLLEXPORT EN_setpatternid(EN_Project p, int index, char *id)
if (!p->Openflag) return 102; if (!p->Openflag) return 102;
if (index < 1 || index > p->network.Npats) return 205; if (index < 1 || index > p->network.Npats) return 205;
// Check is id name contains invalid characters
if (!cstr_isvalid(id)) return 250;
if (strlen(id) > MAXID) return 250; if (strlen(id) > MAXID) return 250;
for (i = 1; i <= p->network.Npats; i++) for (i = 1; i <= p->network.Npats; i++)
{ {
if (i != index && strcmp(id, p->network.Pattern[i].ID) == 0) return 215; if (i != index && strcmp(id, p->network.Pattern[i].ID) == 0) return 215;
@@ -4352,6 +4357,10 @@ int DLLEXPORT EN_setcurveid(EN_Project p, int index, char *id)
if (!p->Openflag) return 102; if (!p->Openflag) return 102;
if (index < 1 || index > p->network.Ncurves) return 205; if (index < 1 || index > p->network.Ncurves) return 205;
// Check is id name contains invalid characters
if (!cstr_isvalid(id)) return 250;
if (strlen(id) > MAXID) return 250; if (strlen(id) > MAXID) return 250;
for (i = 1; i <= p->network.Ncurves; i++) for (i = 1; i <= p->network.Ncurves; i++)
{ {

View File

@@ -69,6 +69,8 @@ BOOST_FIXTURE_TEST_CASE(test_curve_comments, FixtureOpenClose)
BOOST_FIXTURE_TEST_CASE(test_curve_id_isvalid, FixtureInitClose) BOOST_FIXTURE_TEST_CASE(test_curve_id_isvalid, FixtureInitClose)
{ {
int index;
error = EN_addcurve(ph, "C1"); error = EN_addcurve(ph, "C1");
BOOST_REQUIRE(error == 0); BOOST_REQUIRE(error == 0);
@@ -80,6 +82,10 @@ BOOST_FIXTURE_TEST_CASE(test_curve_id_isvalid, FixtureInitClose)
error = EN_addcurve(ph, "C;2"); error = EN_addcurve(ph, "C;2");
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
EN_getcurveindex(ph, "C1", &index);
error = EN_setcurveid(ph, index, "C;2");
BOOST_REQUIRE(error == 250);
} }

View File

@@ -55,8 +55,10 @@ BOOST_FIXTURE_TEST_CASE(test_adddelete_link, FixtureInitClose)
} }
BOOST_FIXTURE_TEST_CASE(test_link_isvalid, FixtureInitClose) BOOST_FIXTURE_TEST_CASE(test_link_id_isvalid, FixtureInitClose)
{ {
int index;
// Build a network // Build a network
EN_addnode(ph, (char *)"N1", EN_JUNCTION); EN_addnode(ph, (char *)"N1", EN_JUNCTION);
EN_addnode(ph, (char *)"N2", EN_JUNCTION); EN_addnode(ph, (char *)"N2", EN_JUNCTION);
@@ -73,6 +75,10 @@ BOOST_FIXTURE_TEST_CASE(test_link_isvalid, FixtureInitClose)
error = EN_addlink(ph, (char *)"L;2", EN_PIPE, (char *)"N1", (char *)"N2"); error = EN_addlink(ph, (char *)"L;2", EN_PIPE, (char *)"N1", (char *)"N2");
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
EN_getlinkindex(ph, "L1", &index);
error = EN_setlinkid(ph, index, "L;1");
BOOST_REQUIRE(error == 250);
} }
BOOST_AUTO_TEST_CASE(test_setlinktype) BOOST_AUTO_TEST_CASE(test_setlinktype)

View File

@@ -49,17 +49,23 @@ BOOST_FIXTURE_TEST_CASE(test_adddelete_node, FixtureInitClose)
BOOST_FIXTURE_TEST_CASE(test_node_validate_id, FixtureInitClose) BOOST_FIXTURE_TEST_CASE(test_node_validate_id, FixtureInitClose)
{ {
int index;
error = EN_addnode(ph, (char *)"N2", EN_JUNCTION); error = EN_addnode(ph, (char *)"N2", EN_JUNCTION);
BOOST_REQUIRE(error == 0); BOOST_REQUIRE(error == 0);
error = EN_addnode(ph, (char *)"N 2", EN_JUNCTION); error = EN_addnode(ph, (char *)"N 3", EN_JUNCTION);
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
error = EN_addnode(ph, (char *)"N\"2", EN_JUNCTION); error = EN_addnode(ph, (char *)"N\"3", EN_JUNCTION);
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
error = EN_addnode(ph, (char *)"N;2", EN_JUNCTION); error = EN_addnode(ph, (char *)"N;3", EN_JUNCTION);
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
EN_getnodeindex(ph, "N2", &index);
error = EN_setnodeid(ph, index, "N;2");
BOOST_REQUIRE(error = 250);
} }

View File

@@ -147,8 +147,10 @@ BOOST_FIXTURE_TEST_CASE(test_pattern_comments, FixtureOpenClose)
BOOST_CHECK(check_string(comment, (char *)"Time Pattern 1")); BOOST_CHECK(check_string(comment, (char *)"Time Pattern 1"));
} }
BOOST_FIXTURE_TEST_CASE(test_pat_isvalid_id, FixtureInitClose) BOOST_FIXTURE_TEST_CASE(test_pat_id_isvalid, FixtureInitClose)
{ {
int index;
error = EN_addpattern(ph, "P1"); error = EN_addpattern(ph, "P1");
BOOST_REQUIRE(error == 0); BOOST_REQUIRE(error == 0);
@@ -160,6 +162,11 @@ BOOST_FIXTURE_TEST_CASE(test_pat_isvalid_id, FixtureInitClose)
error = EN_addpattern(ph, "P;2"); error = EN_addpattern(ph, "P;2");
BOOST_REQUIRE(error == 250); BOOST_REQUIRE(error == 250);
EN_getpatternindex(ph, "P1", &index);
error = EN_setpatternid(ph, index, "P;1");
BOOST_REQUIRE(error == 250);
} }