12
12
#include < testutils/test_to_element.h>
13
13
14
14
#include < boost/lexical_cast.hpp>
15
+ #include < fstream>
15
16
16
17
using namespace isc ;
17
18
using namespace isc ::data;
@@ -80,18 +81,21 @@ void checkStringValue(const ConstElementPtr& element,
80
81
// / @param name is the value to compare against key's name_.
81
82
// / @param algorithm is the string value to compare against key's algorithm.
82
83
// / @param secret is the value to compare against key's secret.
84
+ // / @param secret_file is the file name where the secret can be found.
85
+ // / @param digestbits is the minimum truncated length in bits.
83
86
// /
84
87
// / @return returns true if there is a match across the board, otherwise it
85
88
// / returns false.
86
89
bool checkKey (TSIGKeyInfoPtr key, const std::string& name,
87
90
const std::string& algorithm, const std::string& secret,
88
- uint32_t digestbits = 0 ) {
91
+ std::string secret_file, uint32_t digestbits = 0 ) {
89
92
// Return value, assume its a match.
90
93
return (((key) &&
91
94
(key->getName () == name) &&
92
- (key->getAlgorithm () == algorithm) &&
95
+ (key->getAlgorithm () == algorithm) &&
93
96
(key->getDigestbits () == digestbits) &&
94
- (key->getSecret () == secret) &&
97
+ (key->getSecret () == secret) &&
98
+ (key->getSecretFile () == secret_file) &&
95
99
(key->getTSIGKey ())));
96
100
}
97
101
@@ -232,7 +236,8 @@ class D2SimpleParserTest : public ::testing::Test {
232
236
// / ensure any D2 object(s) that were created by a prior invocation are
233
237
// / destroyed. This permits parsing to be conducted more than once
234
238
// / in the same test.
235
- virtual void reset (){};
239
+ virtual void reset () {
240
+ }
236
241
237
242
// / @brief Adds default values to the given element tree
238
243
// /
@@ -303,7 +308,8 @@ class TSIGKeyInfoParserTest : public D2SimpleParserTest {
303
308
public:
304
309
// / @brief Constructor
305
310
TSIGKeyInfoParserTest ()
306
- : D2SimpleParserTest(D2ParserContext::PARSER_TSIG_KEY) {
311
+ : D2SimpleParserTest(D2ParserContext::PARSER_TSIG_KEY),
312
+ test_file_name_ (TEST_DATA_BUILDDIR " /sf-test" ) {
307
313
}
308
314
309
315
// / @brief Free up the keys created by parsing
@@ -313,6 +319,7 @@ class TSIGKeyInfoParserTest : public D2SimpleParserTest {
313
319
314
320
// / @brief Destructor
315
321
virtual ~TSIGKeyInfoParserTest () {
322
+ static_cast <void >(remove (test_file_name_.c_str ()));
316
323
reset ();
317
324
};
318
325
@@ -340,8 +347,10 @@ class TSIGKeyInfoParserTest : public D2SimpleParserTest {
340
347
341
348
// / @brief Retains the TSIGKeyInfo created by a successful parsing
342
349
TSIGKeyInfoPtr key_;
343
- };
344
350
351
+ // / @brief Secret file name.
352
+ std::string test_file_name_;
353
+ };
345
354
346
355
// / @brief Test fixture class for testing TSIGKeyInfo list parsing.
347
356
class TSIGKeyInfoListParserTest : public D2SimpleParserTest {
@@ -599,6 +608,7 @@ class DdnsDomainListParserTest : public DdnsDomainParserTest {
599
608
// / 1. Name cannot be blank.
600
609
// / 2. Algorithm cannot be blank.
601
610
// / 3. Secret cannot be blank.
611
+ // / 4. Secret file cannot be invalid when specified.
602
612
TEST_F (TSIGKeyInfoParserTest, invalidEntry) {
603
613
604
614
// Name cannot be blank.
@@ -643,8 +653,59 @@ TEST_F(TSIGKeyInfoParserTest, invalidEntry) {
643
653
" }" ;
644
654
PARSE_FAIL (config, " Cannot make D2TsigKey: non-zero bits left over"
645
655
" bogus (<string>:1:1)" );
646
- }
647
656
657
+ // Secret file must exist.
658
+ config = " {"
659
+ " \" name\" : \" d2_key_one\" , "
660
+ " \" algorithm\" : \" HMAC-MD5\" , "
661
+ " \" digest-bits\" : 120 , "
662
+ " \" secret-file\" : \" /does/not/exist\" "
663
+ " }" ;
664
+ PARSE_FAIL (config, " tsig-key : Expected a file at path "
665
+ " '/does/not/exist' (<string>:1:91)" );
666
+
667
+ // Secret file must be a regular file.
668
+ config = " {"
669
+ " \" name\" : \" d2_key_one\" , "
670
+ " \" algorithm\" : \" HMAC-MD5\" , "
671
+ " \" digest-bits\" : 120 , "
672
+ " \" secret-file\" : \" /\" "
673
+ " }" ;
674
+ PARSE_FAIL (config, " tsig-key : Expected '/' to be a regular file "
675
+ " (<string>:1:91)" );
676
+
677
+ // Secret file must not be empty.
678
+ std::ofstream fs (test_file_name_.c_str (),
679
+ std::ofstream::out | std::ofstream::trunc);
680
+ ASSERT_TRUE (fs.is_open ());
681
+ fs.close ();
682
+
683
+ config = " {"
684
+ " \" name\" : \" d2_key_one\" , "
685
+ " \" algorithm\" : \" HMAC-MD5\" , "
686
+ " \" digest-bits\" : 120 , "
687
+ " \" secret-file\" : \" " ;
688
+ config += test_file_name_ + " \" }" ;
689
+ std::string expected = " tsig-key : Expected '" ;
690
+ expected += test_file_name_ + " ' to not be empty (<string>:1:91)" ;
691
+ PARSE_FAIL (config, expected);
692
+
693
+ // Secret file content must be valid.
694
+ fs = std::ofstream (test_file_name_.c_str (),
695
+ std::ofstream::out | std::ofstream::trunc);
696
+ ASSERT_TRUE (fs.is_open ());
697
+ fs << " bogus" ;
698
+ fs.close ();
699
+
700
+ config = " {"
701
+ " \" name\" : \" d2_key_one\" , "
702
+ " \" algorithm\" : \" HMAC-MD5\" , "
703
+ " \" digest-bits\" : 120 , "
704
+ " \" secret-file\" : \" " ;
705
+ config += test_file_name_ + " \" }" ;
706
+ PARSE_FAIL (config, " Cannot make D2TsigKey: non-zero bits left over"
707
+ " bogus (<string>:1:1)" );
708
+ }
648
709
649
710
// / @brief Verifies that TSIGKeyInfo parsing creates a proper TSIGKeyInfo
650
711
// / when given a valid combination of entries.
@@ -662,7 +723,36 @@ TEST_F(TSIGKeyInfoParserTest, validEntry) {
662
723
663
724
// Verify the key contents.
664
725
EXPECT_TRUE (checkKey (key_, " d2_key_one" , " HMAC-MD5" ,
665
- " dGhpcyBrZXkgd2lsbCBtYXRjaA==" , 120 ));
726
+ " dGhpcyBrZXkgd2lsbCBtYXRjaA==" , " " , 120 ));
727
+
728
+ // Verify unparsing.
729
+ runToElementTest<TSIGKeyInfo>(config, *key_);
730
+ }
731
+
732
+ // / @brief Verifies that TSIGKeyInfo parsing creates a proper TSIGKeyInfo
733
+ // / when given a valid secret file.
734
+ TEST_F (TSIGKeyInfoParserTest, validSecretFile) {
735
+ // Valid entries for TSIG key, all items are required.
736
+ std::string config = " {"
737
+ " \" name\" : \" d2_key_one\" , "
738
+ " \" algorithm\" : \" HMAC-MD5\" , "
739
+ " \" digest-bits\" : 120 , "
740
+ " \" secret-file\" : \" " ;
741
+ config += test_file_name_ + " \" }" ;
742
+ // Create and fill the secret file.
743
+ std::ofstream fs (test_file_name_.c_str (),
744
+ std::ofstream::out | std::ofstream::trunc);
745
+ ASSERT_TRUE (fs.is_open ());
746
+ fs << " dGhpcyBrZXkgd2lsbCBtYXRjaA==" ;
747
+ fs.close ();
748
+ // Verify that it parses.
749
+ PARSE_OK (config);
750
+ ASSERT_TRUE (key_);
751
+
752
+ // Verify the key contents.
753
+ EXPECT_TRUE (checkKey (key_, " d2_key_one" , " HMAC-MD5" ,
754
+ " dGhpcyBrZXkgd2lsbCBtYXRjaA==" ,
755
+ test_file_name_, 120 ));
666
756
667
757
// Verify unparsing.
668
758
runToElementTest<TSIGKeyInfo>(config, *key_);
@@ -770,7 +860,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
770
860
771
861
// Verify the key contents.
772
862
EXPECT_TRUE (checkKey (key, " key1" , TSIGKeyInfo::HMAC_MD5_STR,
773
- ref_secret, 80 ));
863
+ ref_secret, " " , 80 ));
774
864
775
865
// Find the 2nd key and retrieve it.
776
866
gotit = keys_->find (" key2" );
@@ -779,7 +869,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
779
869
780
870
// Verify the key contents.
781
871
EXPECT_TRUE (checkKey (key, " key2" , TSIGKeyInfo::HMAC_SHA1_STR,
782
- ref_secret, 80 ));
872
+ ref_secret, " " , 80 ));
783
873
784
874
// Find the 3rd key and retrieve it.
785
875
gotit = keys_->find (" key3" );
@@ -788,7 +878,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
788
878
789
879
// Verify the key contents.
790
880
EXPECT_TRUE (checkKey (key, " key3" , TSIGKeyInfo::HMAC_SHA256_STR,
791
- ref_secret, 128 ));
881
+ ref_secret, " " , 128 ));
792
882
793
883
// Find the 4th key and retrieve it.
794
884
gotit = keys_->find (" key4" );
@@ -797,7 +887,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
797
887
798
888
// Verify the key contents.
799
889
EXPECT_TRUE (checkKey (key, " key4" , TSIGKeyInfo::HMAC_SHA224_STR,
800
- ref_secret, 112 ));
890
+ ref_secret, " " , 112 ));
801
891
802
892
// Find the 5th key and retrieve it.
803
893
gotit = keys_->find (" key5" );
@@ -806,7 +896,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
806
896
807
897
// Verify the key contents.
808
898
EXPECT_TRUE (checkKey (key, " key5" , TSIGKeyInfo::HMAC_SHA384_STR,
809
- ref_secret, 192 ));
899
+ ref_secret, " " , 192 ));
810
900
811
901
// Find the 6th key and retrieve it.
812
902
gotit = keys_->find (" key6" );
@@ -815,7 +905,7 @@ TEST_F(TSIGKeyInfoListParserTest, validTSIGKeyList) {
815
905
816
906
// Verify the key contents.
817
907
EXPECT_TRUE (checkKey (key, " key6" , TSIGKeyInfo::HMAC_SHA512_STR,
818
- ref_secret, 256 ));
908
+ ref_secret, " " , 256 ));
819
909
}
820
910
821
911
// / @brief Tests the enforcement of data validation when parsing DnsServerInfos.
0 commit comments