2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 22:15:23 +00:00

[#1680] all tokens must not throw on empty string

This commit is contained in:
Razvan Becheriu
2021-03-17 02:43:59 +02:00
parent cd32079830
commit 2c5ac0a501
2 changed files with 122 additions and 32 deletions

View File

@@ -765,13 +765,7 @@ TEST_F(TokenTest, addressToText) {
TokenPtr address((new TokenIpAddressToText())); TokenPtr address((new TokenIpAddressToText()));
std::vector<uint8_t> bytes; std::vector<uint8_t> bytes;
std::string value; std::string value = "10.0.0.1";
values_.push(value);
// Invalid data size fails.
EXPECT_THROW(address->evaluate(*pkt4_, values_), EvalTypeError);
value = "10.0.0.1";
values_.push(value); values_.push(value);
// Invalid data size fails. // Invalid data size fails.
@@ -794,6 +788,17 @@ TEST_F(TokenTest, addressToText) {
// Check that the evaluation put its value on the values stack. // Check that the evaluation put its value on the values stack.
ASSERT_EQ(2, values_.size()); ASSERT_EQ(2, values_.size());
values_.push(std::string());
EXPECT_NO_THROW(address->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(3, values_.size());
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check IPv6 address // Check IPv6 address
EXPECT_EQ(11, values_.top().size()); EXPECT_EQ(11, values_.top().size());
EXPECT_EQ("2001:db8::1", values_.top()); EXPECT_EQ("2001:db8::1", values_.top());
@@ -824,23 +829,7 @@ TEST_F(TokenTest, integerToText) {
TokenPtr uint32token((new TokenUInt32ToText())); TokenPtr uint32token((new TokenUInt32ToText()));
std::vector<uint8_t> bytes; std::vector<uint8_t> bytes;
std::string value; std::string value = "0123456789";
// Invalid data size fails.
values_.push(value);
EXPECT_THROW(int8token->evaluate(*pkt4_, values_), EvalTypeError);
values_.push(value);
EXPECT_THROW(int16token->evaluate(*pkt4_, values_), EvalTypeError);
values_.push(value);
EXPECT_THROW(int32token->evaluate(*pkt4_, values_), EvalTypeError);
values_.push(value);
EXPECT_THROW(uint8token->evaluate(*pkt4_, values_), EvalTypeError);
values_.push(value);
EXPECT_THROW(uint16token->evaluate(*pkt4_, values_), EvalTypeError);
values_.push(value);
EXPECT_THROW(uint32token->evaluate(*pkt4_, values_), EvalTypeError);
value = "0123456789";
// Invalid data size fails. // Invalid data size fails.
values_.push(value); values_.push(value);
@@ -900,6 +889,74 @@ TEST_F(TokenTest, integerToText) {
// Check that the evaluation put its value on the values stack. // Check that the evaluation put its value on the values stack.
ASSERT_EQ(6, values_.size()); ASSERT_EQ(6, values_.size());
value = "";
values_.push(value);
EXPECT_NO_THROW(int8token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(7, values_.size());
values_.push(value);
EXPECT_NO_THROW(int16token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(8, values_.size());
values_.push(value);
EXPECT_NO_THROW(int32token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(9, values_.size());
values_.push(value);
EXPECT_NO_THROW(uint8token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(10, values_.size());
values_.push(value);
EXPECT_NO_THROW(uint16token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(11, values_.size());
values_.push(value);
EXPECT_NO_THROW(uint32token->evaluate(*pkt4_, values_));
// Check that the evaluation put its value on the values stack.
ASSERT_EQ(12, values_.size());
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check empty data
EXPECT_EQ(0, values_.top().size());
EXPECT_EQ("", values_.top());
values_.pop();
// Check uint32 // Check uint32
EXPECT_EQ(10, values_.top().size()); EXPECT_EQ(10, values_.top().size());
EXPECT_EQ("4294967295", values_.top()); EXPECT_EQ("4294967295", values_.top());

View File

@@ -113,10 +113,13 @@ TokenIpAddressToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
values.pop();
uint8_t size = op.size(); if (!(size = op.size())) {
return;
}
values.pop();
if ((size != sizeof(uint32_t)) && (size != INET_ADDRSTRLEN)) { if ((size != sizeof(uint32_t)) && (size != INET_ADDRSTRLEN)) {
isc_throw(EvalTypeError, "Can not convert to valid address."); isc_throw(EvalTypeError, "Can not convert to valid address.");
@@ -144,10 +147,15 @@ TokenInt8ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(int8_t)) { if (size != sizeof(int8_t)) {
isc_throw(EvalTypeError, "Can not convert to valid int8."); isc_throw(EvalTypeError, "Can not convert to valid int8.");
} }
@@ -168,10 +176,15 @@ TokenInt16ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(int16_t)) { if (size != sizeof(int16_t)) {
isc_throw(EvalTypeError, "Can not convert to valid int16."); isc_throw(EvalTypeError, "Can not convert to valid int16.");
} }
@@ -194,10 +207,15 @@ TokenInt32ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(int32_t)) { if (size != sizeof(int32_t)) {
isc_throw(EvalTypeError, "Can not convert to valid int32."); isc_throw(EvalTypeError, "Can not convert to valid int32.");
} }
@@ -220,10 +238,15 @@ TokenUInt8ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(uint8_t)) { if (size != sizeof(uint8_t)) {
isc_throw(EvalTypeError, "Can not convert to valid uint8."); isc_throw(EvalTypeError, "Can not convert to valid uint8.");
} }
@@ -244,10 +267,15 @@ TokenUInt16ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(uint16_t)) { if (size != sizeof(uint16_t)) {
isc_throw(EvalTypeError, "Can not convert to valid uint16."); isc_throw(EvalTypeError, "Can not convert to valid uint16.");
} }
@@ -270,10 +298,15 @@ TokenUInt32ToText::evaluate(Pkt& /*pkt*/, ValueStack& values) {
isc_throw(EvalBadStack, "Incorrect empty stack."); isc_throw(EvalBadStack, "Incorrect empty stack.");
} }
size_t size;
string op = values.top(); string op = values.top();
if (!(size = op.size())) {
return;
}
values.pop(); values.pop();
if (op.size() != sizeof(uint32_t)) { if (size != sizeof(uint32_t)) {
isc_throw(EvalTypeError, "Can not convert to valid uint32."); isc_throw(EvalTypeError, "Can not convert to valid uint32.");
} }