Browse Source

- change dnstime.{cc,h} to dnssectime.{cc,h}
- change function names from DNSSECTime{To,From}Text to time{To,From}Text
- reject non-numeric characters in time text representations
- reject time values after December 31, 9999
- add unit tests


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1342 e5f2f494-b856-4b98-b285-d166d9295462

Evan Hunt 15 years ago
parent
commit
f1a4e49e97

+ 1 - 1
src/lib/dns/Makefile.am

@@ -23,7 +23,7 @@ libdns_la_SOURCES += base32.h base32.cc
 libdns_la_SOURCES += base64.h base64.cc
 libdns_la_SOURCES += sha1.h sha1.cc
 libdns_la_SOURCES += tsig.h tsig.cc
-libdns_la_SOURCES += dnstime.h dnstime.cc
+libdns_la_SOURCES += dnssectime.h dnssectime.cc
 libdns_la_SOURCES += hex.h hex.cc
 
 rrclass.h: rrclass-placeholder.h

+ 18 - 4
src/lib/dns/dnstime.cc

@@ -33,7 +33,7 @@
 #include <stdio.h>
 #include <time.h>
 
-#include "dnstime.h"
+#include "dnssectime.h"
 
 using namespace std;
 
@@ -45,10 +45,16 @@ enum {
 };
 
 string
-DNSSECTimeToText(const time_t timeval)
+timeToText(const time_t timeval)
 {
     struct tm *t = gmtime(&timeval);
 
+    // gmtime() will keep most values within range, but it can
+    // produce a five-digit year; check for this.
+    if ((t->tm_year + 1900) > 9999) {
+        isc_throw(InvalidTime, "Time value out of range: year > 9999");
+    }
+
     ostringstream oss;
     oss << setfill('0')
         << setw(4) << t->tm_year + 1900
@@ -78,17 +84,25 @@ isLeap(int y) {
 }
 
 time_t
-DNSSECTimeFromText(const string& time_txt)
+timeFromText(const string& time_txt)
 {
     time_t timeval;
+    ostringstream oss;
 
     // first try reading YYYYMMDDHHmmSS format
     int year, month, day, hour, minute, second;
+
+    for (int i = 0; i < time_txt.length(); ++i) {
+        if (!isdigit(time_txt.at(i))) {
+            oss << "Couldn't convert non-numeric time value: " << time_txt;
+            isc_throw(InvalidTime, oss.str().c_str());
+        }
+    }
+
     if (time_txt.length() != DATE_LEN ||
         sscanf(time_txt.c_str(), "%4d%2d%2d%2d%2d%2d",
                &year, &month, &day, &hour, &minute, &second) != 6)
     {
-        ostringstream oss;
         oss << "Couldn't convert time value: " << time_txt;
         isc_throw(InvalidTime, oss.str().c_str());
     }

+ 5 - 5
src/lib/dns/dnstime.h

@@ -14,8 +14,8 @@
 
 // $Id$
 
-#ifndef __DNSTIME_H
-#define __DNSTIME_H 1
+#ifndef __DNSSECTIME_H
+#define __DNSSECTIME_H 1
 
 #include <sys/types.h>
 #include <stdint.h>
@@ -43,14 +43,14 @@ public:
 };
 
 time_t
-DNSSECTimeFromText(const std::string& time_txt);
+timeFromText(const std::string& time_txt);
 
 std::string
-DNSSECTimeToText(const time_t timeval);
+timeToText(const time_t timeval);
 }
 }
 
-#endif  // __DNSTIME_H
+#endif  // __DNSSECTIME_H
 
 // Local Variables: 
 // mode: c++

+ 5 - 5
src/lib/dns/rdata/generic/rrsig_46.cc

@@ -24,7 +24,7 @@
 
 #include "base64.h"
 #include "buffer.h"
-#include "dnstime.h"
+#include "dnssectime.h"
 #include "messagerenderer.h"
 #include "name.h"
 #include "rrtype.h"
@@ -96,8 +96,8 @@ RRSIG::RRSIG(const string& rrsig_str) :
         isc_throw(InvalidRdataText, "RRSIG labels out of range");
     }
 
-    uint32_t timeexpire = DNSSECTimeFromText(expire_txt);
-    uint32_t timeinception = DNSSECTimeFromText(inception_txt);
+    uint32_t timeexpire = timeFromText(expire_txt);
+    uint32_t timeinception = timeFromText(inception_txt);
 
     vector<uint8_t> signature;
     decodeBase64(signaturebuf.str(), signature);
@@ -164,8 +164,8 @@ RRSIG::~RRSIG()
 string
 RRSIG::toText() const
 {
-    string expire = DNSSECTimeToText(impl_->timeexpire_);
-    string inception = DNSSECTimeToText(impl_->timeinception_);
+    string expire = timeToText(impl_->timeexpire_);
+    string inception = timeToText(impl_->timeinception_);
 
     return (impl_->covered_.toText() +
             " " + boost::lexical_cast<string>(static_cast<int>(impl_->algorithm_))

+ 1 - 1
src/lib/dns/tests/Makefile.am

@@ -10,7 +10,7 @@ run_unittests_SOURCES += buffer_unittest.cc name_unittest.cc
 run_unittests_SOURCES += messagerenderer_unittest.cc
 run_unittests_SOURCES += rrclass_unittest.cc rrtype_unittest.cc
 run_unittests_SOURCES += rrttl_unittest.cc
-run_unittests_SOURCES += dnstime_unittest.cc
+run_unittests_SOURCES += dnssectime_unittest.cc
 run_unittests_SOURCES += rdata_unittest.h rdata_unittest.cc
 run_unittests_SOURCES += rdata_in_a_unittest.cc rdata_in_aaaa_unittest.cc
 run_unittests_SOURCES += rdata_ns_unittest.cc rdata_soa_unittest.cc

+ 68 - 0
src/lib/dns/tests/dnssectime_unittest.cc

@@ -0,0 +1,68 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <string>
+
+#include <time.h>
+
+#include <dns/dnssectime.h>
+
+#include <gtest/gtest.h>
+
+using namespace std;
+using namespace isc::dns;
+
+namespace {
+
+TEST(DNSSECTimeTest, fromText)
+{
+    // These are bogus and should be rejected
+    EXPECT_THROW(timeFromText("2011 101120000"), InvalidTime);
+    EXPECT_THROW(timeFromText("201101011200-0"), InvalidTime);
+
+    // Short length
+    EXPECT_THROW(timeFromText("20100223"), InvalidTime);
+
+    // Leap year checks
+    EXPECT_THROW(timeFromText("20110229120000"), InvalidTime);
+    EXPECT_THROW(timeFromText("21000229120000"), InvalidTime);
+    EXPECT_NO_THROW(timeFromText("20000229120000"));
+    EXPECT_NO_THROW(timeFromText("20120229120000"));
+
+    // unusual case: this implementation allows SS=60 for "leap seconds"
+    EXPECT_NO_THROW(timeFromText("20110101120060"));
+
+    // Out of range parameters
+    EXPECT_THROW(timeFromText("19100223214617"), InvalidTime); // YY<1970
+    EXPECT_THROW(timeFromText("20110001120000"), InvalidTime); // MM=00
+    EXPECT_THROW(timeFromText("20111301120000"), InvalidTime); // MM=13
+    EXPECT_THROW(timeFromText("20110100120000"), InvalidTime); // DD=00
+    EXPECT_THROW(timeFromText("20110132120000"), InvalidTime); // DD=32
+    EXPECT_THROW(timeFromText("20110431120000"), InvalidTime); // 'Apr31'
+    EXPECT_THROW(timeFromText("20110101250000"), InvalidTime); // HH=25
+    EXPECT_THROW(timeFromText("20110101126000"), InvalidTime); // mm=60
+    EXPECT_THROW(timeFromText("20110101120061"), InvalidTime); // SS=61
+}
+
+TEST(DNSSECTimeTest, toText)
+{
+    EXPECT_EQ("19700101000000", timeToText(0));
+    EXPECT_EQ("20100311233000", timeToText(1268350200));
+
+    // Jan 1, Year 10,000.  What should we do?
+    EXPECT_THROW(timeToText(253402300800), InvalidTime);
+}
+}

+ 0 - 67
src/lib/dns/tests/dnstime_unittest.cc

@@ -1,67 +0,0 @@
-// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-// $Id$
-
-#include <string>
-
-#include <time.h>
-
-#include <dns/dnstime.h>
-
-#include <gtest/gtest.h>
-
-using namespace std;
-using namespace isc::dns;
-
-namespace {
-
-TEST(DNSTimeTest, fromText)
-{
-    // We need test cases after overwrapping.
-    //DNSSECTimeFromText("99991231235959");
-
-    // These are bogus and should be rejected
-    //EXPECT_THROW(DNSSECTimeFromText("2011 101120000"), InvalidTime);
-    //EXPECT_THROW(DNSSECTimeFromText("201101011200-0"), InvalidTime);
-    // Short length
-    EXPECT_THROW(DNSSECTimeFromText("20100223"), InvalidTime);
-
-    // Leap year checks
-    EXPECT_THROW(DNSSECTimeFromText("20110229120000"), InvalidTime);
-    EXPECT_THROW(DNSSECTimeFromText("21000229120000"), InvalidTime);
-    EXPECT_NO_THROW(DNSSECTimeFromText("20000229120000"));
-    EXPECT_NO_THROW(DNSSECTimeFromText("20120229120000"));
-
-    // unusual case: this implementation allows SS=60 for "leap seconds"
-    EXPECT_NO_THROW(DNSSECTimeFromText("20110101120060"));
-
-    // Out of range parameters
-    EXPECT_THROW(DNSSECTimeFromText("19100223214617"), InvalidTime); // YY<1970
-    EXPECT_THROW(DNSSECTimeFromText("20110001120000"), InvalidTime); // MM=00
-    EXPECT_THROW(DNSSECTimeFromText("20111301120000"), InvalidTime); // MM=13
-    EXPECT_THROW(DNSSECTimeFromText("20110100120000"), InvalidTime); // DD=00
-    EXPECT_THROW(DNSSECTimeFromText("20110132120000"), InvalidTime); // DD=32
-    EXPECT_THROW(DNSSECTimeFromText("20110431120000"), InvalidTime); // 'Apr31'
-    EXPECT_THROW(DNSSECTimeFromText("20110101250000"), InvalidTime); // HH=25
-    EXPECT_THROW(DNSSECTimeFromText("20110101126000"), InvalidTime); // mm=60
-    EXPECT_THROW(DNSSECTimeFromText("20110101120061"), InvalidTime); // SS=61
-}
-
-TEST(DNSTimeTest, fromTime)
-{
-    // Jan 1, Year 10,000.  What should we do?
-    //EXPECT_EQ("", DNSSECTimeToText(253402300800));
-}
-}

+ 1 - 1
src/lib/dns/tests/rdata_rrsig_unittest.cc

@@ -16,7 +16,7 @@
 
 #include <dns/base64.h>
 #include <dns/buffer.h>
-#include <dns/dnstime.h>
+#include <dns/dnssectime.h>
 #include <dns/messagerenderer.h>
 #include <dns/rdata.h>
 #include <dns/rdataclass.h>