1 /*
2  * Copyright (c) 2017, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "Config.hpp"
32 #include "ParameterFramework.hpp"
33 #include "ElementHandle.hpp"
34 #include "Test.hpp"
35 
36 #include <catch.hpp>
37 
38 #include <string>
39 
40 using std::string;
41 
42 namespace parameterFramework
43 {
44 
45 const auto validEnumParameterInstances = Config{
46     &Config::instances,
47     // Default for integers is unsigned/32bits
48     R"(<EnumParameter Name="nominal" Size="8"><ValuePair Literal="on" Numerical="3"/><ValuePair Literal="off" Numerical="0"/></EnumParameter>)"};
49 
50 const auto &invalidEnumParameterParameters = Tests<string>{
51     {"No Size", "<EnumParameter Name='nosize'><ValuePair Literal='on' Numerical='3'/><ValuePair "
52                 "Literal='off' Numerical='0'/></EnumParameter>"}};
53 
54 struct EnumParameterPF : public ParameterFramework
55 {
EnumParameterPFparameterFramework::EnumParameterPF56     EnumParameterPF() : ParameterFramework{std::move(validEnumParameterInstances)} {}
57 };
58 
59 SCENARIO_METHOD(LazyPF, "Invalid EnumParameter types XML structure", "[EnumParameter types]")
60 {
61     for (auto &vec : invalidEnumParameterParameters) {
62         GIVEN ("intentional error: " + vec.title) {
63             create(Config{&Config::instances, vec.payload});
64             THEN ("Start should fail") {
65                 CHECK_THROWS_AS(mPf->start(), Exception);
66             }
67         }
68     }
69 }
70 
71 SCENARIO_METHOD(EnumParameterPF, "EnumParameter types", "[EnumParameter types]")
72 {
73     GIVEN ("A valid XML structure file") {
74         THEN ("Start should succeed") {
75             CHECK_NOTHROW(start());
76             REQUIRE_NOTHROW(setTuningMode(true));
77             string path = "/test/test/nominal";
78 
79             AND_THEN ("Set/Get a EnumParameter type parameter in real value space") {
80 
81                 for (auto &vec : Tests<string>{
82                          {"(not a number)", "foobar"},
83                      }) {
84                     GIVEN ("Invalid value " + vec.title) {
85                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
86                     }
87                 }
88                 for (auto &vec : Tests<string>{
89                          {"(upper limit)", "on"}, {"(lower limit)", "off"},
90                      }) {
91                     GIVEN ("A valid value " + vec.title) {
92                         CHECK_NOTHROW(setParameter(path, vec.payload));
93                         string getValueBack;
94                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
95                         CHECK(getValueBack == vec.payload);
96                     }
97                 }
98             }
99 
100             AND_THEN ("Set/Get a EnumParameter type parameter in raw value space") {
101                 ElementHandle handle{*this, path};
102                 REQUIRE_NOTHROW(setRawValueSpace(true));
103 
104                 for (auto &vec : Tests<string>{{"(too high)", "7"}, {"(too low)", "-1"}}) {
105                     GIVEN ("Invalid value " + vec.title) {
106                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
107                     }
108                 }
109                 for (auto &vec : Tests<string>{
110                          {"(upper limit)", "3"}, {"(lower limit)", "0"},
111                      }) {
112                     GIVEN ("A valid value " + vec.title) {
113                         CHECK_NOTHROW(setParameter(path, vec.payload));
114                         string getValueBack;
115                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
116                         CHECK(getValueBack == vec.payload);
117                     }
118                 }
119 
120                 REQUIRE_NOTHROW(setHexOutputFormat(true));
121 
122                 for (auto &vec : Tests<string>{{"(too high)", "0x7"}, {"(too low)", "0xFF"}}) {
123                     GIVEN ("Invalid value " + vec.title) {
124                         CHECK_THROWS_AS(setParameter(path, vec.payload), Exception);
125                     }
126                 }
127                 for (auto &vec : Tests<string>{
128                          {"(upper limit)", "0x03"}, {"(lower limit)", "0x00"},
129                      }) {
130                     GIVEN ("A valid value " + vec.title) {
131                         CHECK_NOTHROW(setParameter(path, vec.payload));
132                         string getValueBack;
133                         REQUIRE_NOTHROW(getParameter(path, getValueBack));
134                         CHECK(getValueBack == vec.payload);
135                     }
136                 }
137             }
138 
139             AND_THEN ("Set/Get a EnumParameter type parameter in Integer value space") {
140                 ElementHandle handle{*this, path};
141                 /** @FIXME: 'set' operations on a ParameterHandle are silently
142                  * ignored in tuning mode. Does it make sense ? */
143                 REQUIRE_NOTHROW(setTuningMode(false));
144 
145                 for (auto &vec : Tests<int32_t>{
146                          {"(upper limit)", 3}, {"(lower limit)", 0},
147                      }) {
148                     GIVEN ("A valid value " + vec.title) {
149                         CHECK_NOTHROW(handle.setAsSignedInteger(vec.payload));
150                         int32_t getValueBack;
151                         REQUIRE_NOTHROW(handle.getAsSignedInteger(getValueBack));
152                         CHECK(getValueBack == vec.payload);
153                     }
154                 }
155                 for (auto &vec : Tests<int32_t>{
156                          {"(too high)", 13}, {"(too low)", -51},
157                      }) {
158                     GIVEN ("An invalid value " + vec.title) {
159                         CHECK_THROWS_AS(handle.setAsSignedInteger(vec.payload), Exception);
160                     }
161                 }
162             }
163         }
164     }
165 }
166 }
167