summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/smf/test/testsmfutils.H
blob: d8ad2fb8a80ab98a0fff93215066e0c28202d985 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/smf/test/testsmfutils.H $                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2018                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef __TESTSMFUTILS_H
#define __TESTSMFUTILS_H

#include <cxxtest/TestSuite.H>

#include <targeting/common/target.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/util.H>
#include <errl/errlmanager.H>
#include <hbotcompid.H>
#include <secureboot/secure_reasoncodes.H>
#include <secureboot/smf_utils.H>
#include <errl/errlmanager.H>
#include <vfs/vfs.H>

class SmfUtilsTestSuite : public CxxTest::TestSuite
{
private:
    bool cv_isSmfLoaded;

public:

    SmfUtilsTestSuite() : cv_isSmfLoaded(false)
    {
        errlHndl_t l_errl = nullptr;
        l_errl = VFS::module_load("libsmf.so");
        if(l_errl)
        {
            TS_FAIL("SmfUtilsTestSuite(): could not load libsmf.so");
            errlCommit(l_errl, CXXTEST_COMP_ID);
            cv_isSmfLoaded = false;
        }
        else
        {
            cv_isSmfLoaded = true;
        }
    }

    ~SmfUtilsTestSuite()
    {
        if(cv_isSmfLoaded)
        {
            errlHndl_t l_errl = nullptr;
            l_errl = VFS::module_unload("libsmf.so");
            if(l_errl)
            {
                TS_FAIL("~SmfUtilsTestSuite(): could not unload libsmf.so");
                errlCommit(l_errl, CXXTEST_COMP_ID);
            }
        }
    }

    /**
     * @brief Test isSmfEnabled helper function
     */
    void testIsSmfEnabled() const
    {
        TS_INFO(ENTER_MRK "testIsSmfEnabled");

        do {
        if(!cv_isSmfLoaded)
        {
            break;
        }

        TARGETING::Target* l_sys = nullptr;
        TARGETING::targetService().getTopLevelTarget(l_sys);
        assert(l_sys, "The top level target is nullptr!");
        l_sys->setAttr<TARGETING::ATTR_SMF_ENABLED>(true);

        bool l_smfEnabled = SECUREBOOT::SMF::isSmfEnabled();
        if(l_smfEnabled != true)
        {
            TS_FAIL("testIsSmfEnabled: expected SMF to be enabled, but it's disabled");
        }

        l_sys->setAttr<TARGETING::ATTR_SMF_ENABLED>(false);
        l_smfEnabled = SECUREBOOT::SMF::isSmfEnabled();
        if(l_smfEnabled != false)
        {
            TS_FAIL("testIsSmfEnabled: expected SMF to be disabled, but it's enabled");
        }

        } while (0);

        TS_INFO(EXIT_MRK "testIsSmfEnabled");
    }

    /**
     * @brief Test checkRiskLevelForSmf helper function
     */
    void testCheckRiskLevelForSmf() const
    {
        TS_INFO(ENTER_MRK "testCheckRiskLevelForSmf");
        errlHndl_t l_errl = nullptr;

        do {
        if(!cv_isSmfLoaded)
        {
            break;
        }

        TARGETING::Target* l_masterProc = nullptr;

        l_errl = TARGETING::targetService()
                                 .queryMasterProcChipTargetHandle(l_masterProc);
        if(l_errl)
        {
            TS_FAIL("testCheckRiskLevelForSmf: could not get master proc");
            errlCommit(l_errl, CXXTEST_COMP_ID);
            break;
        }

        if(l_masterProc->getAttr<TARGETING::ATTR_MODEL>() ==
            TARGETING::MODEL_AXONE)
        {
            // SMF is enabled by default on Axone
            l_errl = SECUREBOOT::SMF::checkRiskLevelForSmf();
            if(l_errl)
            {
                TS_FAIL("testCheckRiskLevelForSmf: checkRiskLevelForSmf returned an error for Axone; plid = 0x%x", l_errl->plid());
                errlCommit(l_errl, CXXTEST_COMP_ID);
            }
        }
        else // NIMBUS or CUMULUS
        {
            TARGETING::Target* l_sys = nullptr;
            TARGETING::targetService().getTopLevelTarget(l_sys);
            l_sys->setAttr<TARGETING::ATTR_RISK_LEVEL>(
                  TARGETING::UTIL::P9N23_P9C13_NATIVE_SMF_RUGBY_FAVOR_SECURITY);
            l_errl = SECUREBOOT::SMF::checkRiskLevelForSmf();
            if(l_errl)
            {
                TS_FAIL("testCheckRiskLevelForSmf: checkRiskLevelForSmf returned an error for CUMULUS/NIMBUS; plid = 0x%x", l_errl->plid());
                errlCommit(l_errl, CXXTEST_COMP_ID);
            }

            l_sys->setAttr<TARGETING::ATTR_RISK_LEVEL>(
                                        TARGETING::UTIL::P9N22_P9N23_JAVA_PERF);
            l_errl = SECUREBOOT::SMF::checkRiskLevelForSmf();
            if(!l_errl)
            {
                TS_FAIL("testCheckRiskLevelForSmf: expected error was not returned from checkRiskLevelForSmf");
            }
            else
            {
                if(l_errl->reasonCode() != SECUREBOOT::RC_RISK_LEVEL_TOO_LOW)
                {
                    TS_FAIL("testCheckRiskLevelForSmf: incorrect reason code returned from checkRiskLevelForSmf. Expected 0x%x; actual 0x%x", SECUREBOOT::RC_RISK_LEVEL_TOO_LOW, l_errl->reasonCode());
                }
                errlCommit(l_errl, CXXTEST_COMP_ID);
            }
        }
        } while(0);

        TS_INFO(EXIT_MRK "testCheckRiskLevelForSmf");
    }
};

#endif
OpenPOWER on IntegriCloud