gcontini
2020-02-10 205c71575c272a685011c641a33d26cf66cf60a0
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
#define BOOST_TEST_MODULE network_test
#include <string>
#include <iostream>
#include <boost/test/unit_test.hpp>
 
#include <licensecc_properties.h>
#include <licensecc_properties_test.h>
#include "../../src/library/base/StringUtils.h"
#include "../../src/library/os/network.hpp"
#include "../../src/library/os/execution_environment.hpp"
 
namespace license {
namespace os {
namespace test {
 
using namespace license::os;
using namespace std;
 
BOOST_AUTO_TEST_CASE(read_network_adapters) {
    std::vector<license::os::OsAdapterInfo> adapters;
    // we can suppose every test environment  other than docker has at least one network interface (it's hard to
    // download this source code)
    FUNCTION_RETURN result = getAdapterInfos(adapters);
    ExecutionEnvironment exec_env;
    if (result != FUNC_RET_OK && exec_env.is_docker()) {
        BOOST_TEST_MESSAGE("detected docker environment, not having network interfaces is normal here");
        return;
    }
    BOOST_CHECK_EQUAL(result, FUNC_RET_OK);
    for (auto& it : adapters) {
        cout << "Interface found: " << string(it.description) << endl;
        BOOST_CHECK_GT(strlen(it.description), 0);
        // lo mac address is always 0 but it has ip
        // other interfaces may not be connected
        if (string(it.description) == "lo") {
            BOOST_FAIL("loopback adapters shouldn't appear");
        } else {
            // check mac address are not all zero
            bool mac_is_0 = true;
            for (int j = 0; j < 6; j++) {
                mac_is_0 = mac_is_0 || (it.mac_address[j] != 0);
            }
            BOOST_CHECK_MESSAGE(mac_is_0, "Mac address for interface " << it.description << " is 0");
        }
    }
}
 
}  // namespace test
}  // namespace os
}  // namespace license