Skip to content

Commit 9872eec

Browse files
committed
[#3699] Fixed fromElement
1 parent 8fe5f68 commit 9872eec

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/lib/dhcpsrv/lease.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ Lease::fromElementCommon(const LeasePtr& lease, const data::ConstElementPtr& ele
284284
" or it is not an integer");
285285
}
286286

287-
if ((state->intValue() < 0) || (state->intValue() > Lease::STATE_EXPIRED_RECLAIMED)) {
287+
if ((state->intValue() < 0) || (state->intValue() > Lease::STATE_RELEASED)) {
288288
isc_throw(BadValue, "state " << state->intValue()
289289
<< " must be in range [0.."
290-
<< Lease::STATE_EXPIRED_RECLAIMED << "]");
290+
<< Lease::STATE_RELEASED << "]");
291291
}
292292

293293
lease->state_ = state->intValue();

src/lib/dhcpsrv/tests/lease_unittest.cc

+57
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,32 @@ TEST_F(Lease4Test, fromElement) {
509509
EXPECT_EQ("{ \"foo\": \"bar\" }", lease->getContext()->str());
510510
}
511511

512+
// Verify that a released Lease4 can be created from JSON.
513+
TEST_F(Lease4Test, fromElementReleased) {
514+
// Same as fromElement test at the exception of the state.
515+
std::string json = "{"
516+
"\"client-id\": \"17:34:e2:ff:09:92:54\","
517+
"\"cltt\": 12345678,"
518+
"\"fqdn-fwd\": true,"
519+
"\"fqdn-rev\": true,"
520+
"\"hostname\": \"urania.example.ORG\","
521+
"\"hw-address\": \"08:00:2b:02:3f:4e\","
522+
"\"ip-address\": \"192.0.2.3\","
523+
"\"state\": 3,"
524+
"\"subnet-id\": 789,"
525+
"\"pool-id\": 5,"
526+
"\"user-context\": { \"foo\": \"bar\" },"
527+
"\"valid-lft\": 3600 "
528+
"}";
529+
530+
Lease4Ptr lease;
531+
ASSERT_NO_THROW(lease = Lease4::fromElement(Element::fromJSON(json)));
532+
533+
ASSERT_TRUE(lease);
534+
535+
EXPECT_EQ(Lease::STATE_RELEASED, lease->state_);
536+
}
537+
512538
// Test that specifying invalid values for a lease or not specifying
513539
// mandatory lease parameters causes an error while parsing the lease.
514540
TEST_F(Lease4Test, fromElementInvalidValues) {
@@ -590,6 +616,7 @@ TEST_F(Lease4Test, stateToText) {
590616
EXPECT_EQ("declined", Lease4::statesToText(Lease::STATE_DECLINED));
591617
EXPECT_EQ("expired-reclaimed", Lease4::statesToText(Lease::STATE_EXPIRED_RECLAIMED));
592618
EXPECT_EQ("released", Lease4::statesToText(Lease::STATE_RELEASED));
619+
EXPECT_EQ("unknown (4)", Lease4::statesToText(4));
593620
}
594621

595622
/// @brief Creates an instance of the lease with certain FQDN data.
@@ -1299,6 +1326,35 @@ TEST(Lease6Test, fromElementPD) {
12991326
EXPECT_EQ(400, lease->preferred_lft_);
13001327
}
13011328

1329+
// Verify that a released Lease6 can be created from JSON.
1330+
TEST(Lease6Test, fromElementReleased) {
1331+
// Same as fromElementNA test at the exception of the state.
1332+
std::string json = "{"
1333+
"\"cltt\": 12345678,"
1334+
"\"duid\": \"00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\","
1335+
"\"fqdn-fwd\": false,"
1336+
"\"fqdn-rev\": false,"
1337+
"\"hostname\": \"urania.EXAMPLE.org\","
1338+
"\"hw-address\": \"08:00:2b:02:3f:4e\","
1339+
"\"iaid\": 123456,"
1340+
"\"ip-address\": \"2001:db8::1\","
1341+
"\"preferred-lft\": 400,"
1342+
"\"state\": 3,"
1343+
"\"subnet-id\": 5678,"
1344+
"\"pool-id\": 5,"
1345+
"\"type\": \"IA_NA\","
1346+
"\"user-context\": { \"foobar\": 1234 },"
1347+
"\"valid-lft\": 800"
1348+
"}";
1349+
1350+
Lease6Ptr lease;
1351+
ASSERT_NO_THROW(lease = Lease6::fromElement(Element::fromJSON(json)));
1352+
1353+
ASSERT_TRUE(lease);
1354+
1355+
EXPECT_EQ(Lease::STATE_RELEASED, lease->state_);
1356+
}
1357+
13021358
// Test that specifying invalid values for a lease or not specifying
13031359
// mandatory lease parameters causes an error while parsing the lease.
13041360
TEST(Lease6Test, fromElementInvalidValues) {
@@ -1365,6 +1421,7 @@ TEST(Lease6Test, stateToText) {
13651421
EXPECT_EQ("declined", Lease6::statesToText(Lease::STATE_DECLINED));
13661422
EXPECT_EQ("expired-reclaimed", Lease6::statesToText(Lease::STATE_EXPIRED_RECLAIMED));
13671423
EXPECT_EQ("released", Lease6::statesToText(Lease::STATE_RELEASED));
1424+
EXPECT_EQ("unknown (4)", Lease6::statesToText(4));
13681425
}
13691426

13701427
} // end of anonymous namespace

0 commit comments

Comments
 (0)