| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest> |
| 30 | #include <QObject> |
| 31 | #include <QProcessEnvironment> |
| 32 | |
| 33 | class tst_QProcessEnvironment: public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | private slots: |
| 37 | void operator_eq(); |
| 38 | void clearAndIsEmpty(); |
| 39 | void insert(); |
| 40 | void emptyNull(); |
| 41 | void toStringList(); |
| 42 | void keys(); |
| 43 | void insertEnv(); |
| 44 | |
| 45 | void caseSensitivity(); |
| 46 | void systemEnvironment(); |
| 47 | void putenv(); |
| 48 | }; |
| 49 | |
| 50 | void tst_QProcessEnvironment::operator_eq() |
| 51 | { |
| 52 | QProcessEnvironment e1; |
| 53 | QCOMPARE(e1, e1); |
| 54 | e1.clear(); |
| 55 | QCOMPARE(e1, e1); |
| 56 | |
| 57 | e1 = QProcessEnvironment(); |
| 58 | QProcessEnvironment e2; |
| 59 | QCOMPARE(e1, e2); |
| 60 | |
| 61 | e1.clear(); |
| 62 | QCOMPARE(e1, e2); |
| 63 | |
| 64 | e2.clear(); |
| 65 | QCOMPARE(e1, e2); |
| 66 | |
| 67 | e1.insert(name: "FOO" , value: "bar" ); |
| 68 | QVERIFY(e1 != e2); |
| 69 | |
| 70 | e2.insert(name: "FOO" , value: "bar" ); |
| 71 | QCOMPARE(e1, e2); |
| 72 | |
| 73 | e2.insert(name: "FOO" , value: "baz" ); |
| 74 | QVERIFY(e1 != e2); |
| 75 | } |
| 76 | |
| 77 | void tst_QProcessEnvironment::clearAndIsEmpty() |
| 78 | { |
| 79 | QProcessEnvironment e; |
| 80 | e.insert(name: "FOO" , value: "bar" ); |
| 81 | QVERIFY(!e.isEmpty()); |
| 82 | e.clear(); |
| 83 | QVERIFY(e.isEmpty()); |
| 84 | } |
| 85 | |
| 86 | void tst_QProcessEnvironment::insert() |
| 87 | { |
| 88 | QProcessEnvironment e; |
| 89 | e.insert(name: "FOO" , value: "bar" ); |
| 90 | QVERIFY(!e.isEmpty()); |
| 91 | QVERIFY(e.contains("FOO" )); |
| 92 | QCOMPARE(e.value("FOO" ), QString("bar" )); |
| 93 | |
| 94 | e.remove(name: "FOO" ); |
| 95 | QVERIFY(!e.contains("FOO" )); |
| 96 | QVERIFY(e.value("FOO" ).isNull()); |
| 97 | |
| 98 | e.clear(); |
| 99 | QVERIFY(!e.contains("FOO" )); |
| 100 | } |
| 101 | |
| 102 | void tst_QProcessEnvironment::emptyNull() |
| 103 | { |
| 104 | QProcessEnvironment e; |
| 105 | |
| 106 | e.insert(name: "FOO" , value: "" ); |
| 107 | QVERIFY(e.contains("FOO" )); |
| 108 | QVERIFY(e.value("FOO" ).isEmpty()); |
| 109 | QVERIFY(!e.value("FOO" ).isNull()); |
| 110 | |
| 111 | e.insert(name: "FOO" , value: QString()); |
| 112 | QVERIFY(e.contains("FOO" )); |
| 113 | QVERIFY(e.value("FOO" ).isEmpty()); |
| 114 | // don't test if it's NULL, since we shall not make a guarantee |
| 115 | |
| 116 | e.remove(name: "FOO" ); |
| 117 | QVERIFY(!e.contains("FOO" )); |
| 118 | } |
| 119 | |
| 120 | void tst_QProcessEnvironment::toStringList() |
| 121 | { |
| 122 | QProcessEnvironment e; |
| 123 | QVERIFY(e.isEmpty()); |
| 124 | QVERIFY(e.toStringList().isEmpty()); |
| 125 | |
| 126 | e.insert(name: "FOO" , value: "bar" ); |
| 127 | QStringList result = e.toStringList(); |
| 128 | QVERIFY(!result.isEmpty()); |
| 129 | QCOMPARE(result.length(), 1); |
| 130 | QCOMPARE(result.at(0), QString("FOO=bar" )); |
| 131 | |
| 132 | e.clear(); |
| 133 | e.insert(name: "BAZ" , value: "" ); |
| 134 | result = e.toStringList(); |
| 135 | QCOMPARE(result.at(0), QString("BAZ=" )); |
| 136 | |
| 137 | e.insert(name: "FOO" , value: "bar" ); |
| 138 | e.insert(name: "A" , value: "bc" ); |
| 139 | e.insert(name: "HELLO" , value: "World" ); |
| 140 | result = e.toStringList(); |
| 141 | QCOMPARE(result.length(), 4); |
| 142 | |
| 143 | // order is not specified, so use contains() |
| 144 | QVERIFY(result.contains("FOO=bar" )); |
| 145 | QVERIFY(result.contains("BAZ=" )); |
| 146 | QVERIFY(result.contains("A=bc" )); |
| 147 | QVERIFY(result.contains("HELLO=World" )); |
| 148 | } |
| 149 | |
| 150 | void tst_QProcessEnvironment::keys() |
| 151 | { |
| 152 | QProcessEnvironment e; |
| 153 | QVERIFY(e.isEmpty()); |
| 154 | QVERIFY(e.keys().isEmpty()); |
| 155 | |
| 156 | e.insert(name: "FOO" , value: "bar" ); |
| 157 | QStringList result = e.keys(); |
| 158 | QCOMPARE(result.length(), 1); |
| 159 | QCOMPARE(result.at(0), QString("FOO" )); |
| 160 | |
| 161 | e.clear(); |
| 162 | e.insert(name: "BAZ" , value: "" ); |
| 163 | result = e.keys(); |
| 164 | QCOMPARE(result.at(0), QString("BAZ" )); |
| 165 | |
| 166 | e.insert(name: "FOO" , value: "bar" ); |
| 167 | e.insert(name: "A" , value: "bc" ); |
| 168 | e.insert(name: "HELLO" , value: "World" ); |
| 169 | result = e.keys(); |
| 170 | QCOMPARE(result.length(), 4); |
| 171 | |
| 172 | // order is not specified, so use contains() |
| 173 | QVERIFY(result.contains("FOO" )); |
| 174 | QVERIFY(result.contains("BAZ" )); |
| 175 | QVERIFY(result.contains("A" )); |
| 176 | QVERIFY(result.contains("HELLO" )); |
| 177 | } |
| 178 | |
| 179 | void tst_QProcessEnvironment::insertEnv() |
| 180 | { |
| 181 | QProcessEnvironment e; |
| 182 | e.insert(name: "FOO" , value: "bar" ); |
| 183 | e.insert(name: "A" , value: "bc" ); |
| 184 | e.insert(name: "Hello" , value: "World" ); |
| 185 | |
| 186 | QProcessEnvironment e2; |
| 187 | e2.insert(name: "FOO2" , value: "bar2" ); |
| 188 | e2.insert(name: "A2" , value: "bc2" ); |
| 189 | e2.insert(name: "Hello" , value: "Another World" ); |
| 190 | |
| 191 | e.insert(e: e2); |
| 192 | QStringList keys = e.keys(); |
| 193 | QCOMPARE(keys.length(), 5); |
| 194 | |
| 195 | QCOMPARE(e.value("FOO" ), QString("bar" )); |
| 196 | QCOMPARE(e.value("A" ), QString("bc" )); |
| 197 | QCOMPARE(e.value("Hello" ), QString("Another World" )); |
| 198 | QCOMPARE(e.value("FOO2" ), QString("bar2" )); |
| 199 | QCOMPARE(e.value("A2" ), QString("bc2" )); |
| 200 | |
| 201 | QProcessEnvironment e3; |
| 202 | e3.insert(name: "FOO2" , value: "bar2" ); |
| 203 | e3.insert(name: "A2" , value: "bc2" ); |
| 204 | e3.insert(name: "Hello" , value: "Another World" ); |
| 205 | |
| 206 | e3.insert(e: e3); // mustn't deadlock |
| 207 | |
| 208 | QCOMPARE(e3, e2); |
| 209 | } |
| 210 | |
| 211 | void tst_QProcessEnvironment::caseSensitivity() |
| 212 | { |
| 213 | QProcessEnvironment e; |
| 214 | e.insert(name: "foo" , value: "bar" ); |
| 215 | |
| 216 | #ifdef Q_OS_WIN |
| 217 | // Windows is case-insensitive, but case-preserving |
| 218 | QVERIFY(e.contains("foo" )); |
| 219 | QVERIFY(e.contains("FOO" )); |
| 220 | QVERIFY(e.contains("FoO" )); |
| 221 | |
| 222 | QCOMPARE(e.value("foo" ), QString("bar" )); |
| 223 | QCOMPARE(e.value("FOO" ), QString("bar" )); |
| 224 | QCOMPARE(e.value("FoO" ), QString("bar" )); |
| 225 | |
| 226 | // Per Windows, this overwrites the value, but keeps the name's original capitalization |
| 227 | e.insert("Foo" , "Bar" ); |
| 228 | |
| 229 | QStringList list = e.toStringList(); |
| 230 | QCOMPARE(list.length(), 1); |
| 231 | QCOMPARE(list.at(0), QString("foo=Bar" )); |
| 232 | #else |
| 233 | // otherwise, it's case sensitive |
| 234 | QVERIFY(e.contains("foo" )); |
| 235 | QVERIFY(!e.contains("FOO" )); |
| 236 | |
| 237 | e.insert(name: "FOO" , value: "baz" ); |
| 238 | QVERIFY(e.contains("FOO" )); |
| 239 | QCOMPARE(e.value("FOO" ), QString("baz" )); |
| 240 | QCOMPARE(e.value("foo" ), QString("bar" )); |
| 241 | |
| 242 | QStringList list = e.toStringList(); |
| 243 | QCOMPARE(list.length(), 2); |
| 244 | QVERIFY(list.contains("foo=bar" )); |
| 245 | QVERIFY(list.contains("FOO=baz" )); |
| 246 | #endif |
| 247 | } |
| 248 | |
| 249 | void tst_QProcessEnvironment::systemEnvironment() |
| 250 | { |
| 251 | static const char envname[] = "THIS_ENVIRONMENT_VARIABLE_HOPEFULLY_DOESNT_EXIST" ; |
| 252 | QByteArray path = qgetenv(varName: "PATH" ); |
| 253 | QByteArray nonexistant = qgetenv(varName: envname); |
| 254 | QProcessEnvironment system = QProcessEnvironment::systemEnvironment(); |
| 255 | |
| 256 | QVERIFY(nonexistant.isNull()); |
| 257 | |
| 258 | // all other system have environments |
| 259 | if (path.isEmpty()) |
| 260 | QFAIL("Could not find the PATH environment variable -- please correct the test environment" ); |
| 261 | |
| 262 | QVERIFY(system.contains("PATH" )); |
| 263 | QCOMPARE(system.value("PATH" ), QString::fromLocal8Bit(path)); |
| 264 | |
| 265 | QVERIFY(!system.contains(envname)); |
| 266 | |
| 267 | #ifdef Q_OS_WIN |
| 268 | // check case-insensitive too |
| 269 | QVERIFY(system.contains("path" )); |
| 270 | QCOMPARE(system.value("path" ), QString::fromLocal8Bit(path)); |
| 271 | |
| 272 | QVERIFY(!system.contains(QString(envname).toLower())); |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | void tst_QProcessEnvironment::putenv() |
| 277 | { |
| 278 | static const char envname[] = "WE_RE_SETTING_THIS_ENVIRONMENT_VARIABLE" ; |
| 279 | static bool testRan = false; |
| 280 | |
| 281 | if (testRan) |
| 282 | QFAIL("You cannot run this test more than once, since we modify the environment" ); |
| 283 | testRan = true; |
| 284 | |
| 285 | QByteArray valBefore = qgetenv(varName: envname); |
| 286 | if (!valBefore.isNull()) |
| 287 | QFAIL("The environment variable we set in the environment is already set! -- please correct the test environment" ); |
| 288 | QProcessEnvironment eBefore = QProcessEnvironment::systemEnvironment(); |
| 289 | |
| 290 | qputenv(varName: envname, value: "Hello, World" ); |
| 291 | QByteArray valAfter = qgetenv(varName: envname); |
| 292 | QCOMPARE(valAfter, QByteArray("Hello, World" )); |
| 293 | |
| 294 | QProcessEnvironment eAfter = QProcessEnvironment::systemEnvironment(); |
| 295 | |
| 296 | QVERIFY(!eBefore.contains(envname)); |
| 297 | QVERIFY(eAfter.contains(envname)); |
| 298 | QCOMPARE(eAfter.value(envname), QString("Hello, World" )); |
| 299 | |
| 300 | # ifdef Q_OS_WIN |
| 301 | // check case-insensitive too |
| 302 | QString lower = envname; |
| 303 | lower = lower.toLower(); |
| 304 | QVERIFY(!eBefore.contains(lower)); |
| 305 | QVERIFY(eAfter.contains(lower)); |
| 306 | QCOMPARE(eAfter.value(lower), QString("Hello, World" )); |
| 307 | # endif |
| 308 | } |
| 309 | |
| 310 | QTEST_MAIN(tst_QProcessEnvironment) |
| 311 | |
| 312 | #include "tst_qprocessenvironment.moc" |
| 313 | |