]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/sntp/tests/g_fileHandlingTest.h
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / sntp / tests / g_fileHandlingTest.h
1 #ifndef FILE_HANDLING_TEST_H
2 #define FILE_HANDLING_TEST_H
3
4 #include "g_sntptest.h"
5
6 #include <fstream>
7 #include <string>
8
9 using std::ifstream;
10 using std::string;
11 using std::ios;
12
13 class fileHandlingTest : public sntptest {
14 protected:
15         enum DirectoryType {
16                 INPUT_DIR = 0,
17                 OUTPUT_DIR = 1
18         };
19
20         std::string CreatePath(const char* filename, DirectoryType argument) {
21                 std::string path;
22
23                 if (m_params.size() >= argument + 1) {
24                         path = m_params[argument];
25                 }
26
27                 if (path[path.size()-1] != DIR_SEP && !path.empty()) {
28                         path.append(1, DIR_SEP);
29                 }
30                 path.append(filename);
31
32                 return path;
33         }
34
35         int GetFileSize(ifstream& file) {
36                 int initial = file.tellg();
37
38                 file.seekg(0, ios::end);
39                 int length = file.tellg();
40                 file.seekg(initial);
41
42                 return length;
43         }
44
45         void CompareFileContent(ifstream& expected, ifstream& actual) {
46                 int currentLine = 1;
47                 while (actual.good() && expected.good()) {
48                         string actualLine, expectedLine;
49                         getline(actual, actualLine);
50                         getline(expected, expectedLine);
51                         
52                         EXPECT_EQ(expectedLine, actualLine) << "Comparision failed on line " << currentLine;
53                         currentLine++;
54                 }
55         }
56
57         void ClearFile(const std::string& filename) {
58                 std::ofstream clear(filename.c_str(), ios::trunc);
59                 ASSERT_TRUE(clear.good());
60                 clear.close();
61         }
62 };
63
64 #endif // FILE_HANDLING_TEST_H