summaryrefslogtreecommitdiffstats
path: root/bindings/c/Test.cpp
blob: c450e60cbc38c807ab63f5b6c6b90b869d0f5529 (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
/*
 * Copyright (c) 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 "ParameterFramework.h"

#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main()
#include <catch.hpp>

#include <string>
#include <memory>
#include <vector>

#include <cstring>
#include <cerrno>
#include <climits>
extern "C"
{
#include <unistd.h>
}

struct Test
{
    /** @return true if str is empty. */
    bool empty(const char *str)
    {
        REQUIRE(str != NULL);
        return *str == '\0';
    }

    void REQUIRE_FAILURE(bool success)
    {
        THEN("It should be an error") {
            INFO("Previous pfw log: \n" + logLines);
            CAPTURE(pfwGetLastError(pfw));
            CHECK(not success);
            CHECK(not empty(pfwGetLastError(pfw)));
        }
    }

    void REQUIRE_SUCCESS(bool success)
    {
        THEN("It should be a success") {
            INFO("Previous pfw log: \n" + logLines);
            CAPTURE(pfwGetLastError(pfw));
            CHECK(success);
            CHECK(empty(pfwGetLastError(pfw)));
        }
    }

    /** Class to create a temporary file */
    class TmpFile
    {
    public:
        TmpFile(const std::string &content) {
            char tmpName[] = "./tmpPfwUnitTestXXXXXX";
            mFd = mkstemp(tmpName);
            CAPTURE(errno);
            REQUIRE(mFd != -1);
            mPath = tmpName;
            write(mFd, content.c_str(), content.length());
        }
        ~TmpFile() {
            CHECK(close(mFd) != -1);
            unlink(mPath.c_str());
        }
        operator const char *() const { return mPath.c_str(); }
        const std::string &path() const { return mPath; }
    private:
        std::string mPath;
        int mFd;
    };

    /** Log in logLines. */
    static void logCb(void *voidLogLines, PfwLogLevel level, const char *logLine)
    {
        std::string &logLines = *reinterpret_cast<std::string *>(voidLogLines);
        switch(level) {
        case pfwLogWarning:
            logLines += "Warning: ";
            break;
        case pfwLogInfo:
            logLines += "Info: ";
        }
        logLines += logLine;
        logLines += '\n';
    }

    /** Log buffer, will only be display in case of failure */
    std::string logLines;

    /** Pfw handler used in the tests. */
    PfwHandler *pfw;

};

TEST_CASE_METHOD(Test, "Parameter-framework c api use") {
    // Create criteria
    const char *letterList[] = {"a", "b", "c", NULL};
    const char *numberList[] = {"1", "2", "3", NULL};
    const PfwCriterion criteria[] = {
        {"inclusiveCrit", true, letterList},
        {"exclusiveCrit", false, numberList},
    };
    size_t criterionNb = sizeof(criteria)/sizeof(criteria[0]);
    PfwLogger logger = {&logLines, logCb};

    // Create valid pfw config file
    const char *intParameterPath = "/test/system/integer";
    const char *stringParameterPath = "/test/system/string";
    TmpFile system("<?xml version='1.0' encoding='UTF-8'?>\
        <Subsystem Name='system' Type='Virtual' Endianness='Little'>\
            <ComponentLibrary/>\
            <InstanceDefinition>\
                <IntegerParameter Name='integer' Size='32' Signed='true' Max='100'/>\
                <StringParameter Name='string' MaxLength='9'/>\
            </InstanceDefinition>\
        </Subsystem>");
    TmpFile libraries("<?xml version='1.0' encoding='UTF-8'?>\
        <SystemClass Name='test'>\
            <SubsystemInclude Path='" + system.path() + "'/>\
        </SystemClass>");
    TmpFile config("<?xml version='1.0' encoding='UTF-8'?>\
        <ParameterFrameworkConfiguration\
            SystemClassName='test' TuningAllowed='false'>\
            <SubsystemPlugins/>\
            <StructureDescriptionFileLocation Path='" + libraries.path() + "'/>\
        </ParameterFrameworkConfiguration>");

    GIVEN("A created parameter framework") {
        pfw = pfwCreate();
        REQUIRE(pfw != NULL);

        THEN("Error message should be empty") {
            CHECK(empty(pfwGetLastError(pfw)));
        }

        WHEN("The pfw is started without an handler") {
            CHECK(not pfwStart(NULL, config, criteria, criterionNb, &logger));
        }
        WHEN("The pfw is started without a config path") {
            REQUIRE_FAILURE(pfwStart(pfw, NULL, criteria, criterionNb, &logger));
        }
        WHEN("The pfw is started without an existent file") {
            REQUIRE_FAILURE(pfwStart(pfw, "/doNotExist", criteria, criterionNb, &logger));
        }

        WHEN("The pfw is started without a criteria list") {
            REQUIRE_FAILURE(pfwStart(pfw, config, NULL, criterionNb, &logger));
        }
        WHEN("The pfw is started with duplicated criterion value") {
            const PfwCriterion duplicatedCriteria[] = {
                {"duplicated name", true, letterList},
                {"duplicated name", false, numberList},
            };
            REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 2, &logger));
        }
        WHEN("The pfw is started with duplicated criterion value state") {
            const char * values[] = {"a", "a", NULL};
            const PfwCriterion duplicatedCriteria[] = {{"name", true, values}};

            WHEN("Using test logger") {
                REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 1, &logger));
            }
            WHEN("Using default logger") {
                // Test coverage of default logger warning
                REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 1, NULL));
            }
        }
        WHEN("The pfw is started with NULL name criterion") {
            const PfwCriterion duplicatedCriteria[] = {{NULL, true, letterList}};
            REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 1, &logger));
        }
        WHEN("The pfw is started with NULL criterion state list") {
            const PfwCriterion duplicatedCriteria[] = {{"name", true, NULL}};
            REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 1, &logger));
        }
        GIVEN("A criteria with lots of values")
        {
            // Build a criterion with as many value as there is bits in int.
            std::vector<char> names(sizeof(int) * CHAR_BIT + 1, 'a');
            names.back() = '\0';
            std::vector<const char *> values(names.size());
            for(size_t i = 0; i < values.size(); ++i) {
                values[i] = &names[i];
            }
            values.back() = NULL;
            /* The pfw c api requires criterion values to be a NULL terminated
             * array of string. Each string is a pointer to a NULL terminated
             * array of char. The pfw requires each string to be different
             * from all others, ie strcmp(values[i], values[j]) != 0 for any
             * i j.
             *
             * In order to generate easily an array of different strings,
             * instantiate one string (names) big enough
             * (@see PfwCriterion::values).
             * Then instantiate an array of pointer (values),
             * each pointing to a different position in the previously
             * created string.
             *
             * Representation of the names and values vectors.
             *
             * n = number of bit in an int
             *            <--- n+1 elements --->
             * names    = |a|a|a|a|...|a|a|a|\0|
             *             ^ ^             ^
             * values[0] = ´ |             |
             * values[1] = --´             |
             * ...                         |
             * values[n - 1] =  -----------´
             * values[n] = NULL
             *
             */
            const PfwCriterion duplicatedCriteria[] = {{"name", true, &values[0]}};

            WHEN("The pfw is started with a too long criterion state list") {
                REQUIRE_FAILURE(pfwStart(pfw, config, duplicatedCriteria, 1, &logger));
            }
            WHEN("The pfw is started with max length criterion state list") {
                values[values.size() - 2] = NULL; // Hide last value
                REQUIRE_SUCCESS(pfwStart(pfw, config, duplicatedCriteria, 1, &logger));
            }
        }

        WHEN("The pfw is started with zero criteria") {
            REQUIRE_SUCCESS(pfwStart(pfw, config, criteria, 0, &logger));
        }

        WHEN("The pfw is started twice a pfw") {
            REQUIRE_SUCCESS(pfwStart(pfw, config, criteria, criterionNb, &logger));
            REQUIRE_FAILURE(pfwStart(pfw, config, criteria, criterionNb, &logger));
        }

        WHEN("The pfw is started without a logger callback") {
            PfwLogger noLog = { NULL, NULL };
            REQUIRE_SUCCESS(pfwStart(pfw, config, criteria, criterionNb, &noLog));
        }
        WHEN("The pfw is started with default logger") {
            REQUIRE_SUCCESS(pfwStart(pfw, config, criteria, criterionNb, NULL));
        }

        WHEN("Get criterion of a stopped pfw") {
            int value;
            REQUIRE_FAILURE(pfwGetCriterion(pfw, criteria[0].name, &value));
        }
        WHEN("Set criterion of a stopped pfw") {
            REQUIRE_FAILURE(pfwSetCriterion(pfw, criteria[0].name, 1));
        }
        WHEN("Commit criteria of a stopped pfw") {
            REQUIRE_FAILURE(pfwApplyConfigurations(pfw));
        }

        WHEN("Bind parameter with a stopped pfw") {
            REQUIRE(pfwBindParameter(pfw, intParameterPath) == NULL);
        }

        WHEN("The pfw is started correctly")
        {
            REQUIRE_SUCCESS(pfwStart(pfw, config, criteria, criterionNb, &logger));
            int value;

            WHEN("Get criterion without an handle") {
                REQUIRE(not pfwGetCriterion(NULL, criteria[0].name, &value));
            }
            WHEN("Get criterion without a name") {
                REQUIRE_FAILURE(pfwGetCriterion(pfw, NULL, &value));
            }
            WHEN("Get criterion without an output value") {
                REQUIRE_FAILURE(pfwGetCriterion(pfw, criteria[0].name, NULL));
            }
            WHEN("Get not existing criterion") {
                REQUIRE_FAILURE(pfwGetCriterion(pfw, "Do not exist", &value));
            }
            THEN("All criterion should value 0") {
                for(size_t i = 0; i < criterionNb; ++i) {
                    const char *criterionName = criteria[i].name;
                    CAPTURE(criterionName);
                    REQUIRE_SUCCESS(pfwGetCriterion(pfw, criterionName, &value));
                    REQUIRE(value == 0);
                }
            }

            WHEN("Set criterion without an handle") {
                REQUIRE(not pfwSetCriterion(NULL, criteria[0].name, 1));
            }
            WHEN("Set criterion without a name") {
                REQUIRE_FAILURE(pfwSetCriterion(pfw, NULL, 2));
            }
            WHEN("Set not existing criterion") {
                REQUIRE_FAILURE(pfwSetCriterion(pfw, "Do not exist", 3));
            }
            WHEN("Set criterion value") {
                for(size_t i = 0; i < criterionNb; ++i) {
                    const char *criterionName = criteria[i].name;
                    CAPTURE(criterionName);
                    REQUIRE_SUCCESS(pfwSetCriterion(pfw, criterionName, 3));
                }
                THEN("Get criterion value should return what was set") {
                    for(size_t i = 0; i < criterionNb; ++i) {
                        const char *criterionName = criteria[i].name;
                        CAPTURE(criterionName);
                        REQUIRE_SUCCESS(pfwGetCriterion(pfw, criterionName, &value));
                        REQUIRE(value == 3);
                    }
                }
            }
            WHEN("Commit criteria without a pfw") {
                REQUIRE(not pfwApplyConfigurations(NULL));
            }
            WHEN("Commit criteria of a started pfw") {
                REQUIRE_SUCCESS(pfwApplyConfigurations(pfw));
            }

            WHEN("Bind parameter without a pfw") {
                REQUIRE(pfwBindParameter(NULL, intParameterPath) == NULL);
            }
            WHEN("Bind parameter without a path") {
                REQUIRE_FAILURE(pfwBindParameter(pfw, NULL) != NULL);
            }
            WHEN("Bind a non existing parameter") {
                REQUIRE_FAILURE(pfwBindParameter(pfw, "do/not/exist") != NULL);
            }

            WHEN("Set an int parameter without a parameter handle") {
                REQUIRE(not pfwSetIntParameter(NULL, value));
            }
            WHEN("Get an int parameter without a parameter handle") {
                REQUIRE(not pfwGetIntParameter(NULL, &value));
            }

            GIVEN("An integer parameter handle") {
                PfwParameterHandler *param = pfwBindParameter(pfw, intParameterPath);
                REQUIRE_SUCCESS(param != NULL);

                WHEN("Get an int parameter without an output value") {
                    REQUIRE_FAILURE(pfwGetIntParameter(param, NULL));
                }

                WHEN("Set parameter out of range") {
                    REQUIRE_FAILURE(pfwSetIntParameter(param, 101));
                }

                WHEN("Set parameter") {
                    REQUIRE_SUCCESS(pfwSetIntParameter(param, 11));
                    THEN("Get parameter should return what was set") {
                        REQUIRE_SUCCESS(pfwGetIntParameter(param, &value));
                        REQUIRE(value == 11);
                    }
                }

                pfwUnbindParameter(param);
            }

            GIVEN("An string parameter handle") {
                PfwParameterHandler *param = pfwBindParameter(pfw, stringParameterPath);
                REQUIRE_SUCCESS(param != NULL);

                WHEN("Get an int parameter without an output value") {
                    REQUIRE_FAILURE(pfwGetStringParameter(param, NULL));
                }

                WHEN("Set parameter out of range") {
                    REQUIRE_FAILURE(pfwSetStringParameter(param, "ko_1234567"));
                }

                WHEN("Set parameter") {
                    const char *value;
                    REQUIRE_SUCCESS(pfwSetStringParameter(param, "ok"));
                    THEN("Get parameter should return what was set") {
                        REQUIRE_SUCCESS(pfwGetStringParameter(param, &value));
                        REQUIRE(value == std::string("ok"));
                        pfwFree((void *)value);
                    }
                }

                pfwUnbindParameter(param);
            }
        }

        pfwDestroy(pfw);
    }
}

SCENARIO("Get last error without a pfw") {
    THEN("Should return NULL") {
        CHECK(pfwGetLastError(NULL) == NULL);
    }
}