summaryrefslogtreecommitdiff
path: root/upstream/test/test-platform/TestPlatform.cpp
blob: d0909395d6c20f321a49554e531929e8fe1f1913 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * Copyright (c) 2011-2015, Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <errno.h>
#include <convert.hpp>
#include <sstream>
#include "TestPlatform.h"

using std::string;

CTestPlatform::CTestPlatform(const string &strClass, std::string bindAddress)
    : mParameterMgrPlatformConnector(strClass), mLogger(), mRemoteProcessorServer(bindAddress)
{
    mParameterMgrPlatformConnector.setLogger(&mLogger);
}

CTestPlatform::~CTestPlatform()
{
}

CTestPlatform::CommandReturn CTestPlatform::exit(const IRemoteCommand & /*command*/,
                                                 string & /*strResult*/)
{
    mRemoteProcessorServer.stop();

    return CTestPlatform::CCommandHandler::EDone;
}

bool CTestPlatform::run(std::string &strError)
{
    // Start remote processor server
    if (!mRemoteProcessorServer.start(strError)) {

        strError = "TestPlatform: Unable to start remote processor server: " + strError;
        return false;
    }

    CCommandHandler commandHandler(this);

    // Add command parsers
    commandHandler.addCommandParser("exit", &CTestPlatform::exit, 0, "", "Exit TestPlatform");
    commandHandler.addCommandParser("createExclusiveSelectionCriterionFromStateList",
                                    &CTestPlatform::createExclusiveSelectionCriterionFromStateList,
                                    2, "<name> <stateList>",
                                    "Create inclusive selection criterion from state name list");
    commandHandler.addCommandParser("createInclusiveSelectionCriterionFromStateList",
                                    &CTestPlatform::createInclusiveSelectionCriterionFromStateList,
                                    2, "<name> <stateList>",
                                    "Create exclusive selection criterion from state name list");

    commandHandler.addCommandParser("createExclusiveSelectionCriterion",
                                    &CTestPlatform::createExclusiveSelectionCriterion, 2,
                                    "<name> <nbStates>", "Create inclusive selection criterion");
    commandHandler.addCommandParser("createInclusiveSelectionCriterion",
                                    &CTestPlatform::createInclusiveSelectionCriterion, 2,
                                    "<name> <nbStates>", "Create exclusive selection criterion");

    commandHandler.addCommandParser("start", &CTestPlatform::startParameterMgr, 0, "",
                                    "Start ParameterMgr");

    commandHandler.addCommandParser("setCriterionState", &CTestPlatform::setCriterionState, 2,
                                    "<name> <state>",
                                    "Set the current state of a selection criterion");
    commandHandler.addCommandParser(
        "applyConfigurations", &CTestPlatform::applyConfigurations, 0, "",
        "Apply configurations selected by current selection criteria states");

    commandHandler.addCommandParser(
        "setFailureOnMissingSubsystem",
        &CTestPlatform::setter<&CParameterMgrPlatformConnector::setFailureOnMissingSubsystem>, 1,
        "true|false", "Set policy for missing subsystems, "
                      "either abort start or fallback on virtual subsystem.");
    commandHandler.addCommandParser(
        "getMissingSubsystemPolicy",
        &CTestPlatform::getter<&CParameterMgrPlatformConnector::getFailureOnMissingSubsystem>, 0,
        "", "Get policy for missing subsystems, "
            "either abort start or fallback on virtual subsystem.");

    commandHandler.addCommandParser(
        "setFailureOnFailedSettingsLoad",
        &CTestPlatform::setter<&CParameterMgrPlatformConnector::setFailureOnFailedSettingsLoad>, 1,
        "true|false",
        "Set policy for failed settings load, either abort start or continue without domains.");
    commandHandler.addCommandParser(
        "getFailedSettingsLoadPolicy",
        &CTestPlatform::getter<&CParameterMgrPlatformConnector::getFailureOnFailedSettingsLoad>, 0,
        "", "Get policy for failed settings load, either abort start or continue without domains.");

    commandHandler.addCommandParser(
        "setValidateSchemasOnStart",
        &CTestPlatform::setter<&CParameterMgrPlatformConnector::setValidateSchemasOnStart>, 1,
        "true|false", "Set policy for schema validation based on .xsd files (false by default).");
    commandHandler.addCommandParser(
        "getValidateSchemasOnStart",
        &CTestPlatform::getter<&CParameterMgrPlatformConnector::getValidateSchemasOnStart>, 0, "",
        "Get policy for schema validation based on .xsd files.");

    commandHandler.addCommandParser("getSchemaUri", &CTestPlatform::getSchemaUri, 0, "",
                                    "Get the directory where schemas can be found.");
    commandHandler.addCommandParser("setSchemaUri", &CTestPlatform::setSchemaUri, 1, "<directory>",
                                    "Set the directory where schemas can be found.");

    return mRemoteProcessorServer.process(commandHandler);
}

//////////////// Remote command parsers
/// Selection Criterion
CTestPlatform::CommandReturn CTestPlatform::createExclusiveSelectionCriterionFromStateList(
    const IRemoteCommand &remoteCommand, string &strResult)
{
    return createExclusiveSelectionCriterionFromStateList(remoteCommand.getArgument(0),
                                                          remoteCommand, strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

CTestPlatform::CommandReturn CTestPlatform::createInclusiveSelectionCriterionFromStateList(
    const IRemoteCommand &remoteCommand, string &strResult)
{
    return createInclusiveSelectionCriterionFromStateList(remoteCommand.getArgument(0),
                                                          remoteCommand, strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

CTestPlatform::CommandReturn CTestPlatform::createExclusiveSelectionCriterion(
    const IRemoteCommand &remoteCommand, string &strResult)
{
    return createExclusiveSelectionCriterion(
               remoteCommand.getArgument(0),
               strtoul(remoteCommand.getArgument(1).c_str(), nullptr, 0), strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

CTestPlatform::CommandReturn CTestPlatform::createInclusiveSelectionCriterion(
    const IRemoteCommand &remoteCommand, string &strResult)
{
    return createInclusiveSelectionCriterion(
               remoteCommand.getArgument(0),
               strtoul(remoteCommand.getArgument(1).c_str(), nullptr, 0), strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

CTestPlatform::CommandReturn CTestPlatform::startParameterMgr(
    const IRemoteCommand & /*remoteCommand*/, string &strResult)
{
    return mParameterMgrPlatformConnector.start(strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

template <CTestPlatform::setter_t setFunction>
CTestPlatform::CommandReturn CTestPlatform::setter(const IRemoteCommand &remoteCommand,
                                                   string &strResult)
{
    const string &strAbort = remoteCommand.getArgument(0);

    bool bFail;

    if (!convertTo(strAbort, bFail)) {
        return CTestPlatform::CCommandHandler::EShowUsage;
    }

    return (mParameterMgrPlatformConnector.*setFunction)(bFail, strResult)
               ? CTestPlatform::CCommandHandler::EDone
               : CTestPlatform::CCommandHandler::EFailed;
}

template <CTestPlatform::getter_t getFunction>
CTestPlatform::CommandReturn CTestPlatform::getter(const IRemoteCommand & /*command*/,
                                                   string &strResult)
{
    strResult = (mParameterMgrPlatformConnector.*getFunction)() ? "true" : "false";

    return CTestPlatform::CCommandHandler::ESucceeded;
}

CTestPlatform::CommandReturn CTestPlatform::getSchemaUri(const IRemoteCommand & /*remotecommand*/,
                                                         string &result)
{
    result = mParameterMgrPlatformConnector.getSchemaUri();
    return CTestPlatform::CCommandHandler::EDone;
}

CTestPlatform::CommandReturn CTestPlatform::setSchemaUri(const IRemoteCommand &remotecommand,
                                                         string & /*result*/)
{
    mParameterMgrPlatformConnector.setSchemaUri(remotecommand.getArgument(0));
    return CTestPlatform::CCommandHandler::EDone;
}

CTestPlatform::CommandReturn CTestPlatform::setCriterionState(const IRemoteCommand &remoteCommand,
                                                              string &strResult)
{

    bool bSuccess;

    uint32_t state;

    if (convertTo(remoteCommand.getArgument(1), state)) {
        // Sucessfull conversion, set criterion state by numerical state
        bSuccess = setCriterionState(remoteCommand.getArgument(0), state, strResult);

    } else {
        // Conversion failed, set criterion state by lexical state
        bSuccess = setCriterionStateByLexicalSpace(remoteCommand, strResult);
    }

    return bSuccess ? CTestPlatform::CCommandHandler::EDone
                    : CTestPlatform::CCommandHandler::EFailed;
}

CTestPlatform::CommandReturn CTestPlatform::applyConfigurations(const IRemoteCommand & /*command*/,
                                                                string & /*strResult*/)
{
    mParameterMgrPlatformConnector.applyConfigurations();

    return CTestPlatform::CCommandHandler::EDone;
}

//////////////// Remote command handlers

bool CTestPlatform::createExclusiveSelectionCriterionFromStateList(
    const string &strName, const IRemoteCommand &remoteCommand, string &strResult)
{
    ISelectionCriterionTypeInterface *pCriterionType =
        mParameterMgrPlatformConnector.createSelectionCriterionType(false);

    assert(pCriterionType != nullptr);

    size_t nbStates = remoteCommand.getArgumentCount() - 1;

    for (size_t state = 0; state < nbStates; state++) {

        const std::string &strValue = remoteCommand.getArgument(state + 1);

        // FIXME state type vs addValuePair params
        if (!pCriterionType->addValuePair(int(state), strValue, strResult)) {

            strResult = "Unable to add value: " + strValue + ": " + strResult;

            return false;
        }
    }

    mParameterMgrPlatformConnector.createSelectionCriterion(strName, pCriterionType);

    return true;
}

bool CTestPlatform::createInclusiveSelectionCriterionFromStateList(
    const string &strName, const IRemoteCommand &remoteCommand, string &strResult)
{
    ISelectionCriterionTypeInterface *pCriterionType =
        mParameterMgrPlatformConnector.createSelectionCriterionType(true);

    assert(pCriterionType != nullptr);

    size_t nbStates = remoteCommand.getArgumentCount() - 1;

    for (size_t state = 0; state < nbStates; state++) {

        const std::string &strValue = remoteCommand.getArgument(state + 1);

        if (!pCriterionType->addValuePair(0x1 << state, strValue, strResult)) {

            strResult = "Unable to add value: " + strValue + ": " + strResult;

            return false;
        }
    }

    mParameterMgrPlatformConnector.createSelectionCriterion(strName, pCriterionType);

    return true;
}

bool CTestPlatform::createExclusiveSelectionCriterion(const string &strName, size_t nbStates,
                                                      string &strResult)
{
    ISelectionCriterionTypeInterface *pCriterionType =
        mParameterMgrPlatformConnector.createSelectionCriterionType(false);

    for (size_t state = 0; state < nbStates; state++) {

        std::ostringstream ostrValue;

        ostrValue << "State_";
        ostrValue << state;

        // FIXME state type vs addValuePair params
        if (!pCriterionType->addValuePair(int(state), ostrValue.str(), strResult)) {

            strResult = "Unable to add value: " + ostrValue.str() + ": " + strResult;

            return false;
        }
    }

    mParameterMgrPlatformConnector.createSelectionCriterion(strName, pCriterionType);

    return true;
}

bool CTestPlatform::createInclusiveSelectionCriterion(const string &strName, size_t nbStates,
                                                      string &strResult)
{
    ISelectionCriterionTypeInterface *pCriterionType =
        mParameterMgrPlatformConnector.createSelectionCriterionType(true);

    for (size_t state = 0; state < nbStates; state++) {

        std::ostringstream ostrValue;

        ostrValue << "State_0x";
        ostrValue << (0x1 << state);

        if (!pCriterionType->addValuePair(0x1 << state, ostrValue.str(), strResult)) {

            strResult = "Unable to add value: " + ostrValue.str() + ": " + strResult;

            return false;
        }
    }

    mParameterMgrPlatformConnector.createSelectionCriterion(strName, pCriterionType);

    return true;
}

bool CTestPlatform::setCriterionState(const string &strName, uint32_t uiState, string &strResult)
{
    ISelectionCriterionInterface *pCriterion =
        mParameterMgrPlatformConnector.getSelectionCriterion(strName);

    if (!pCriterion) {

        strResult = "Unable to retrieve selection criterion: " + strName;

        return false;
    }

    pCriterion->setCriterionState(uiState);

    return true;
}

bool CTestPlatform::setCriterionStateByLexicalSpace(const IRemoteCommand &remoteCommand,
                                                    string &strResult)
{

    // Get criterion name
    std::string strCriterionName = remoteCommand.getArgument(0);

    ISelectionCriterionInterface *pCriterion =
        mParameterMgrPlatformConnector.getSelectionCriterion(strCriterionName);

    if (!pCriterion) {

        strResult = "Unable to retrieve selection criterion: " + strCriterionName;

        return false;
    }

    // Get criterion type
    const ISelectionCriterionTypeInterface *pCriterionType = pCriterion->getCriterionType();

    // Get substate number, the first argument (index 0) is the criterion name
    size_t nbSubStates = remoteCommand.getArgumentCount() - 1;

    // Check that exclusive criterion has only one substate
    if (!pCriterionType->isTypeInclusive() && nbSubStates != 1) {

        strResult = "Exclusive criterion " + strCriterionName + " can only have one state";

        return false;
    }

    /// Translate lexical state to numerical state
    int iNumericalState = 0;
    size_t lexicalSubStateIndex;

    // Parse lexical substates
    std::string strLexicalState = "";

    for (lexicalSubStateIndex = 1; lexicalSubStateIndex <= nbSubStates; lexicalSubStateIndex++) {
        /*
         * getNumericalValue method from ISelectionCriterionTypeInterface strip his parameter
         * first parameter based on | sign. In case that the user uses multiple parameters
         * to set InclusiveCriterion value, we aggregate all desired values to be sure
         * they will be handled correctly.
         */
        if (lexicalSubStateIndex != 1) {
            strLexicalState += "|";
        }
        strLexicalState += remoteCommand.getArgument(lexicalSubStateIndex);
    }

    // Translate lexical to numerical substate
    if (!pCriterionType->getNumericalValue(strLexicalState, iNumericalState)) {

        strResult = "Unable to find lexical state \"" + strLexicalState + "\" in criteria " +
                    strCriterionName;

        return false;
    }

    // Set criterion new state
    pCriterion->setCriterionState(iNumericalState);

    return true;
}