1 // Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 #include <exceptions/exceptions.h>
9 #include <gtest/gtest.h>
10 #include <cc/data.h>
11 #include <fstream>
12 
13 using namespace isc;
14 using namespace isc::data;
15 
16 namespace {
17 
18 /// @brief Test class for testing Daemon class
19 class DataFileTest : public ::testing::Test {
20 public:
21 
22     /// @brief writes specified text to a file
23     ///
24     /// That is an auxiliary function used in fileRead() tests.
25     ///
26     /// @param content text to be written to disk
writeFile(const std::string & content)27     void writeFile(const std::string& content) {
28         // Write sample content to disk
29         static_cast<void>(remove(TEMP_FILE));
30         std::ofstream write_me(TEMP_FILE);
31         EXPECT_TRUE(write_me.is_open());
32         write_me << content;
33         write_me.close();
34     }
35 
36     /// destructor
~DataFileTest()37     ~DataFileTest() {
38         static_cast<void>(remove(TEMP_FILE));
39     }
40 
41     /// Name of the temporary file
42     static const char* TEMP_FILE;
43 };
44 
45 /// Temporary file name used in some tests
46 const char* DataFileTest::TEMP_FILE="temp-file.json";
47 
48 // Test checks whether a text file can be read from disk.
TEST_F(DataFileTest,readFileMultiline)49 TEST_F(DataFileTest, readFileMultiline) {
50 
51     const char* no_endline = "{ \"abc\": 123 }";
52     const char* with_endline = "{\n \"abc\":\n 123\n }\n";
53 
54     // That's what we expect
55     ElementPtr exp = Element::fromJSON(no_endline);
56 
57     // Write sample content to disk
58     writeFile(no_endline);
59 
60     // Check that the read content is correct
61     EXPECT_TRUE(exp->equals(*Element::fromJSONFile(TEMP_FILE)));
62 
63     // Write sample content to disk
64     writeFile(with_endline);
65 
66     // Check that the read content is correct
67     EXPECT_TRUE(exp->equals(*Element::fromJSONFile(TEMP_FILE)));
68 }
69 
70 // Test checks whether comments in file are ignored as expected.
TEST_F(DataFileTest,readFileComments)71 TEST_F(DataFileTest, readFileComments) {
72     const char* commented_content = "# This is a comment\n"
73         "{ \"abc\":\n"
74         "# a comment comment\n"
75         "1 }\n";
76 
77     // That's what we expect
78     ElementPtr exp = Element::fromJSON("{ \"abc\": 1 }");
79 
80     // Write sample content to disk
81     writeFile(commented_content);
82 
83     // Check that the read will fail (without comment elimination)
84     EXPECT_THROW(Element::fromJSONFile(TEMP_FILE), JSONError);
85 
86     // Check that the read content is correct (with comment elimination)
87     EXPECT_NO_THROW(Element::fromJSONFile(TEMP_FILE, true));
88     EXPECT_TRUE(exp->equals(*Element::fromJSONFile(TEMP_FILE, true)));
89 }
90 
91 // This test checks that missing file will generate an exception.
TEST_F(DataFileTest,readFileError)92 TEST_F(DataFileTest, readFileError) {
93 
94     // Check that the read content is correct
95     EXPECT_THROW(Element::fromJSONFile("no-such-file.txt"), isc::InvalidOperation);
96 }
97 
98 };
99