Skip to content

Commit

Permalink
vlanmgrd will crashed if there has incorrect type value on cfg_db.json (
Browse files Browse the repository at this point in the history
  • Loading branch information
linemma authored and lguohan committed Apr 25, 2019
1 parent 624c599 commit 7a1b5a9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cfgmgr/vlanmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ void VlanMgr::doVlanTask(Consumer &consumer)
}

int vlan_id;
vlan_id = stoi(key.substr(4));
try
{
vlan_id = stoi(key.substr(4));
}
catch (...)
{
SWSS_LOG_ERROR("Invalid key format. Not a number after 'Vlan' prefix: %s", key.c_str());
it = consumer.m_toSync.erase(it);
continue;
}

string vlan_alias, port_alias;
string op = kfvOp(t);
Expand Down
65 changes: 65 additions & 0 deletions tests/test_vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def remove_vlan_member(self, vlan, interface):
tbl._del("Vlan" + vlan + "|" + interface)
time.sleep(1)

def check_syslog(self, dvs, marker, err_log, vlan_str, expected_cnt):
(exitcode, num) = dvs.runcmd(['sh', '-c', "awk \'/%s/,ENDFILE {print;}\' /var/log/syslog | grep vlanmgrd | grep \"%s\" | grep -i %s | wc -l" % (marker, err_log, vlan_str)])
assert num.strip() == str(expected_cnt)

def test_VlanAddRemove(self, dvs, testlog):
self.setup_db(dvs)

Expand Down Expand Up @@ -186,3 +190,64 @@ def test_MultipleVlan(self, dvs, testlog):
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN")
vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id]
assert len(vlan_entries) == 0

@pytest.mark.parametrize("test_input, expected", [
(["Vla", "2"], 0),
(["VLAN", "3"], 0),
(["vlan", "4"], 0),
(["Vlan", "5"], 1),
])
def test_AddVlanWithIncorrectKeyPrefix(self, dvs, testlog, test_input, expected):
self.setup_db(dvs)
marker = dvs.add_log_marker()
vlan_prefix = test_input[0]
vlan = test_input[1]

# create vlan
tbl = swsscommon.Table(self.cdb, "VLAN")
fvs = swsscommon.FieldValuePairs([("vlanid", vlan)])
tbl.set(vlan_prefix + vlan, fvs)
time.sleep(1)

# check asic database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN")
vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id]
assert len(vlan_entries) == expected

if len(vlan_entries) == 0:
# check error log
self.check_syslog(dvs, marker, "Invalid key format. No 'Vlan' prefix:", vlan_prefix+vlan, 1)
else:
#remove vlan
self.remove_vlan(vlan)

@pytest.mark.parametrize("test_input, expected", [
(["Vlan", "abc"], 0),
(["Vlan", "a3"], 0),
(["Vlan", ""], 0),
(["Vlan", "5"], 1),
])
def test_AddVlanWithIncorrectValueType(self, dvs, testlog, test_input, expected):
self.setup_db(dvs)
marker = dvs.add_log_marker()
vlan_prefix = test_input[0]
vlan = test_input[1]

# create vlan
tbl = swsscommon.Table(self.cdb, "VLAN")
fvs = swsscommon.FieldValuePairs([("vlanid", vlan)])
tbl.set(vlan_prefix + vlan, fvs)
time.sleep(1)

# check asic database
tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN")
vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id]
assert len(vlan_entries) == expected

if len(vlan_entries) == 0:
# check error log
self.check_syslog(dvs, marker, "Invalid key format. Not a number after \'Vlan\' prefix:", vlan_prefix+vlan, 1)
else:
#remove vlan
self.remove_vlan(vlan)

0 comments on commit 7a1b5a9

Please sign in to comment.