File: TestPasswdFile.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (153 lines) | stat: -rw-r--r-- 6,122 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2009- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation
 * nor does it submit to any jurisdiction.
 */

#include <iostream>

#include <boost/test/unit_test.hpp>

#include "ecflow/core/File.hpp"
#include "ecflow/core/PasswdFile.hpp"
#include "ecflow/core/PasswordEncryption.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace ecf;

// #define DEBUG_ME 1

BOOST_AUTO_TEST_SUITE(U_Core)

BOOST_AUTO_TEST_SUITE(T_PasswdFile)

void test_passwd_files(const std::string& directory, bool pass) {
    auto full_path = fs::absolute(directory);

    BOOST_CHECK(fs::exists(full_path));
    BOOST_CHECK(fs::is_directory(full_path));

#if DEBUG_ME
    ECF_TEST_DBG(<< "...In directory: " << full_path.relative_path());
#endif

    fs::directory_iterator end_iter;
    for (fs::directory_iterator dir_itr(full_path); dir_itr != end_iter; ++dir_itr) {
        try {
            fs::path relPath(directory + "/" + dir_itr->path().filename().string());

            // recurse down directories
            if (fs::is_directory(dir_itr->status())) {
                test_passwd_files(relPath.string(), pass);
                continue;
            }
#if DEBUG_ME
            ECF_TEST_DBG(<< "......Parsing file " << relPath.string());
#endif
            PasswdFile theFile;
            std::string errorMsg;
            bool parsedOk = theFile.load(relPath.string(), false /*debug*/, errorMsg);
            if (pass) {
                // Test expected to pass
                BOOST_CHECK_MESSAGE(parsedOk,
                                    "Failed to parse file " << relPath << "\n"
                                                            << errorMsg << "\n"
                                                            << theFile.dump());
            }
            else {
                // test expected to fail
                BOOST_CHECK_MESSAGE(!parsedOk,
                                    "Parse expected to fail for " << relPath << "\n"
                                                                  << errorMsg << "\n"
                                                                  << theFile.dump());
#if DEBUG_ME
                ECF_TEST_DBG(<< errorMsg);
#endif
            }
        }
        catch (const std::exception& ex) {
            ECF_TEST_DBG(<< dir_itr->path().filename() << " " << ex.what());
        }
    }
}

BOOST_AUTO_TEST_CASE(test_parsing_for_good_passwd_files) {
    ECF_NAME_THIS_TEST();

    std::string path = File::test_data("libs/core/test/data/goodPasswdFiles", "libs/core");

    // All the files in this directory are expected to pass
    test_passwd_files(path, true);
}

BOOST_AUTO_TEST_CASE(test_parsing_for_bad_passwd_files) {
    ECF_NAME_THIS_TEST();

    std::string path = File::test_data("libs/core/test/data/badPasswdFiles", "libs/core");

    // All the files in this directory are expected to fail
    test_passwd_files(path, false);
}

BOOST_AUTO_TEST_CASE(test_passwd_empty_file) {
    ECF_NAME_THIS_TEST();

    std::string path = File::test_data("libs/core/test/data/goodPasswdFiles/empty.passwd", "libs/core");

    PasswdFile theFile;
    std::string errorMsg;
    BOOST_CHECK_MESSAGE(theFile.load(path, false, errorMsg), "Failed to parse file " << path << "\n" << errorMsg);

    BOOST_REQUIRE_MESSAGE(theFile.passwds().empty(), "expected empty file ");
    BOOST_REQUIRE_MESSAGE(theFile.get_passwd("fred", "host", "port") == std::string(), "expected empty string");
    BOOST_REQUIRE_MESSAGE(theFile.authenticate("fred", ""),
                          "expected to authenticate. TEST CASE with empty password file");
    BOOST_REQUIRE_MESSAGE(!theFile.authenticate("fred", "passwd"), "expected not to authenticate");
    BOOST_REQUIRE_MESSAGE(!theFile.authenticate("", "passwd"), "expected not to authenticate");
    BOOST_REQUIRE_MESSAGE(!theFile.authenticate("", ""), "expected not to authenticate");
}

BOOST_AUTO_TEST_CASE(test_passwd) {
    ECF_NAME_THIS_TEST();

    std::string path = File::test_data("libs/core/test/data/goodPasswdFiles/ecf.passwd", "libs/core");

    PasswdFile theFile;
    std::string errorMsg;
    BOOST_CHECK_MESSAGE(theFile.load(path, false, errorMsg), "Failed to parse file " << path << "\n" << errorMsg);

    BOOST_CHECK_MESSAGE(theFile.check_at_least_one_user_with_host_and_port("host", "3141"), "expected to pass");
    BOOST_CHECK_MESSAGE(theFile.check_at_least_one_user_with_host_and_port("host3", "3143"), "expected to pass");
    BOOST_CHECK_MESSAGE(theFile.check_at_least_one_user_with_host_and_port("host4", "3145"), "expected to pass");
    BOOST_CHECK_MESSAGE(!theFile.check_at_least_one_user_with_host_and_port("xxxx", "3141"), "expected fail ");
    BOOST_CHECK_MESSAGE(!theFile.check_at_least_one_user_with_host_and_port("host", "13141"), "expected fail ");

    // make sure we find all the users and the access right are correct
    //   4.4.0  # comment
    //
    //   # comment
    //   fred host 3141  x12ggg # comment
    //   fred host3 3143 passwd
    //   fred host4 3145 x12ggg
    //   # comment
    //
    //   jake host 3141  x12ggg
    //   tom host3 3143    x12ggg # sdsdsd

    std::vector<Pass_wd> expected_passwds;
    expected_passwds.emplace_back("fred", "host", "3141", PasswordEncryption::encrypt("x12ggg", "fred"));
    expected_passwds.emplace_back("fred", "host3", "3143", PasswordEncryption::encrypt("passwd", "fred"));
    expected_passwds.emplace_back("fred", "host4", "3145", PasswordEncryption::encrypt("x12ggg", "fred"));
    expected_passwds.emplace_back("jake", "host", "3141", PasswordEncryption::encrypt("x12ggg", "jake"));
    expected_passwds.emplace_back("tom", "host3", "3143", PasswordEncryption::encrypt("x12ggg", "tom"));

    BOOST_REQUIRE_MESSAGE(expected_passwds == theFile.passwds(), "expected passwords to match");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()