1 // Copyright (C) 2013-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 /// @file
8 /// @brief Marker file callout library
9 ///
10 /// This is the source of a test library for the DHCP parser and configuration
11 /// tests.
12 ///
13 /// To check that they libraries are loaded and unloaded correctly, the load
14 /// and unload functions in this library maintain two marker files - the load
15 /// marker file and the unload marker file.  The functions append a single
16 /// line to the file, creating the file if need be.  In this way, the test code
17 /// can determine whether the load/unload functions have been run and, if so,
18 /// in what order.
19 ///
20 /// The additional marker file is created for the dhcp4_srv_configured hook
21 /// point. It records the library number and the names of the parameters
22 /// provided to the callout.
23 ///
24 /// This file is the common library file for the tests.  It will not compile
25 /// by itself - it is included into each callout library which specifies the
26 /// missing constant LIBRARY_NUMBER before the inclusion.
27 
28 #include <config.h>
29 #include <hooks/hooks.h>
30 #include "marker_file.h"
31 
32 #include <fstream>
33 
34 extern "C" {
35 
36 /// @brief Append digit to marker file
37 ///
38 /// If the marker file does not exist, create it.  Then append the single
39 /// digit (given by the constant LIBRARY_NUMBER) defined earlier to it and
40 /// close the file.
41 ///
42 /// @param name Name of the file to open
43 ///
44 /// @return 0 on success, non-zero on error.
45 int
appendDigit(const char * name)46 appendDigit(const char* name) {
47     // Open the file and check if successful.
48     std::fstream file(name, std::fstream::out | std::fstream::app);
49     if (!file.good()) {
50         return (1);
51     }
52 
53     // Add the library number to it and close.
54     file << LIBRARY_NUMBER;
55     file.close();
56 
57     return (0);
58 }
59 
60 /// @brief Append argument name passed to the callout to a marker file.
61 ///
62 /// @param file_name Name of the file to open.
63 /// @param parameter Parameter name.
64 ///
65 /// @return 0 on success, non-zero on error.
appendArgument(const char * file_name,const char * argument)66 int appendArgument(const char* file_name, const char* argument) {
67     // Open the file and check if successful.
68     std::fstream file(file_name, std::fstream::out | std::fstream::app);
69     if (!file.good()) {
70         return (1);
71     }
72 
73     // Add the library number to it and close.
74     file << argument;
75     file.close();
76 
77     return (0);
78 }
79 
80 // Framework functions
81 int
version()82 version() {
83     return (KEA_HOOKS_VERSION);
84 }
85 
86 int
load(isc::hooks::LibraryHandle &)87 load(isc::hooks::LibraryHandle&) {
88     return (appendDigit(LOAD_MARKER_FILE));
89 }
90 
91 int
unload()92 unload() {
93     return (appendDigit(UNLOAD_MARKER_FILE));
94 }
95 
96 };
97