|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtTest> |
|
43 #include <QObject> |
|
44 #include <QProcessEnvironment> |
|
45 |
|
46 // Note: |
|
47 // in cross-platform tests, ALWAYS use UPPERCASE variable names |
|
48 // That's because on Windows, the variables are uppercased |
|
49 |
|
50 class tst_QProcessEnvironment: public QObject |
|
51 { |
|
52 Q_OBJECT |
|
53 private slots: |
|
54 void operator_eq(); |
|
55 void clearAndIsEmpty(); |
|
56 void insert(); |
|
57 void emptyNull(); |
|
58 void toStringList(); |
|
59 |
|
60 void caseSensitivity(); |
|
61 void systemEnvironment(); |
|
62 void putenv(); |
|
63 }; |
|
64 |
|
65 void tst_QProcessEnvironment::operator_eq() |
|
66 { |
|
67 QProcessEnvironment e1; |
|
68 QVERIFY(e1 == e1); |
|
69 e1.clear(); |
|
70 QVERIFY(e1 == e1); |
|
71 |
|
72 e1 = QProcessEnvironment(); |
|
73 QProcessEnvironment e2; |
|
74 QVERIFY(e1 == e2); |
|
75 |
|
76 e1.clear(); |
|
77 QVERIFY(e1 != e2); |
|
78 |
|
79 e2.clear(); |
|
80 |
|
81 QVERIFY(e1 == e2); |
|
82 } |
|
83 |
|
84 void tst_QProcessEnvironment::clearAndIsEmpty() |
|
85 { |
|
86 QProcessEnvironment e; |
|
87 e.insert("FOO", "bar"); |
|
88 QVERIFY(!e.isEmpty()); |
|
89 e.clear(); |
|
90 QVERIFY(e.isEmpty()); |
|
91 } |
|
92 |
|
93 void tst_QProcessEnvironment::insert() |
|
94 { |
|
95 QProcessEnvironment e; |
|
96 e.insert("FOO", "bar"); |
|
97 QVERIFY(!e.isEmpty()); |
|
98 QVERIFY(e.contains("FOO")); |
|
99 QCOMPARE(e.value("FOO"), QString("bar")); |
|
100 |
|
101 e.remove("FOO"); |
|
102 QVERIFY(!e.contains("FOO")); |
|
103 QVERIFY(e.value("FOO").isNull()); |
|
104 |
|
105 e.clear(); |
|
106 QVERIFY(!e.contains("FOO")); |
|
107 } |
|
108 |
|
109 void tst_QProcessEnvironment::emptyNull() |
|
110 { |
|
111 QProcessEnvironment e; |
|
112 |
|
113 e.insert("FOO", ""); |
|
114 QVERIFY(e.contains("FOO")); |
|
115 QVERIFY(e.value("FOO").isEmpty()); |
|
116 QVERIFY(!e.value("FOO").isNull()); |
|
117 |
|
118 e.insert("FOO", QString()); |
|
119 QVERIFY(e.contains("FOO")); |
|
120 QVERIFY(e.value("FOO").isEmpty()); |
|
121 // don't test if it's NULL, since we shall not make a guarantee |
|
122 |
|
123 e.remove("FOO"); |
|
124 QVERIFY(!e.contains("FOO")); |
|
125 } |
|
126 |
|
127 void tst_QProcessEnvironment::toStringList() |
|
128 { |
|
129 QProcessEnvironment e; |
|
130 QVERIFY(e.isEmpty()); |
|
131 QVERIFY(e.toStringList().isEmpty()); |
|
132 |
|
133 e.insert("FOO", "bar"); |
|
134 QStringList result = e.toStringList(); |
|
135 QVERIFY(!result.isEmpty()); |
|
136 QCOMPARE(result.length(), 1); |
|
137 QCOMPARE(result.at(0), QString("FOO=bar")); |
|
138 |
|
139 e.clear(); |
|
140 e.insert("BAZ", ""); |
|
141 result = e.toStringList(); |
|
142 QCOMPARE(result.at(0), QString("BAZ=")); |
|
143 |
|
144 e.insert("FOO", "bar"); |
|
145 e.insert("A", "bc"); |
|
146 e.insert("HELLO", "World"); |
|
147 result = e.toStringList(); |
|
148 QCOMPARE(result.length(), 4); |
|
149 |
|
150 // order is not specified, so use contains() |
|
151 QVERIFY(result.contains("FOO=bar")); |
|
152 QVERIFY(result.contains("BAZ=")); |
|
153 QVERIFY(result.contains("A=bc")); |
|
154 QVERIFY(result.contains("HELLO=World")); |
|
155 } |
|
156 |
|
157 void tst_QProcessEnvironment::caseSensitivity() |
|
158 { |
|
159 QProcessEnvironment e; |
|
160 e.insert("foo", "bar"); |
|
161 |
|
162 #ifdef Q_OS_WIN |
|
163 // on Windows, it's uppercased |
|
164 QVERIFY(e.contains("foo")); |
|
165 QVERIFY(e.contains("FOO")); |
|
166 QVERIFY(e.contains("FoO")); |
|
167 |
|
168 QCOMPARE(e.value("foo"), QString("bar")); |
|
169 QCOMPARE(e.value("FOO"), QString("bar")); |
|
170 QCOMPARE(e.value("FoO"), QString("bar")); |
|
171 |
|
172 QStringList list = e.toStringList(); |
|
173 QCOMPARE(list.at(0), QString("FOO=bar")); |
|
174 #else |
|
175 // otherwise, it's case sensitive |
|
176 QVERIFY(e.contains("foo")); |
|
177 QVERIFY(!e.contains("FOO")); |
|
178 |
|
179 e.insert("FOO", "baz"); |
|
180 QVERIFY(e.contains("FOO")); |
|
181 QCOMPARE(e.value("FOO"), QString("baz")); |
|
182 QCOMPARE(e.value("foo"), QString("bar")); |
|
183 |
|
184 QStringList list = e.toStringList(); |
|
185 QVERIFY(list.contains("foo=bar")); |
|
186 QVERIFY(list.contains("FOO=baz")); |
|
187 #endif |
|
188 } |
|
189 |
|
190 void tst_QProcessEnvironment::systemEnvironment() |
|
191 { |
|
192 static const char envname[] = "THIS_ENVIRONMENT_VARIABLE_HOPEFULLY_DOESNT_EXIST"; |
|
193 QByteArray path = qgetenv("PATH"); |
|
194 QByteArray nonexistant = qgetenv(envname); |
|
195 QProcessEnvironment system = QProcessEnvironment::systemEnvironment(); |
|
196 |
|
197 QVERIFY(nonexistant.isNull()); |
|
198 |
|
199 #ifdef Q_WS_WINCE |
|
200 // Windows CE has no environment |
|
201 QVERIFY(path.isEmpty()); |
|
202 QVERIFY(!system.contains("PATH")); |
|
203 QVERIFY(system.isEmpty()); |
|
204 #else |
|
205 // all other system have environments |
|
206 if (path.isEmpty()) |
|
207 QFAIL("Could not find the PATH environment variable -- please correct the test environment"); |
|
208 |
|
209 QVERIFY(system.contains("PATH")); |
|
210 QCOMPARE(system.value("PATH"), QString::fromLocal8Bit(path)); |
|
211 |
|
212 QVERIFY(!system.contains(envname)); |
|
213 |
|
214 # ifdef Q_OS_WIN |
|
215 // check case-insensitive too |
|
216 QVERIFY(system.contains("path")); |
|
217 QCOMPARE(system.value("path"), QString::fromLocal8Bit(path)); |
|
218 |
|
219 QVERIFY(!system.contains(QString(envname).toLower())); |
|
220 # endif |
|
221 #endif |
|
222 } |
|
223 |
|
224 void tst_QProcessEnvironment::putenv() |
|
225 { |
|
226 #ifdef Q_WS_WINCE |
|
227 QSKIP("Windows CE has no environment", SkipAll); |
|
228 #else |
|
229 static const char envname[] = "WE_RE_SETTING_THIS_ENVIRONMENT_VARIABLE"; |
|
230 static bool testRan = false; |
|
231 |
|
232 if (testRan) |
|
233 QFAIL("You cannot run this test more than once, since we modify the environment"); |
|
234 testRan = true; |
|
235 |
|
236 QByteArray valBefore = qgetenv(envname); |
|
237 if (!valBefore.isNull()) |
|
238 QFAIL("The environment variable we set in the environment is already set! -- please correct the test environment"); |
|
239 QProcessEnvironment eBefore = QProcessEnvironment::systemEnvironment(); |
|
240 |
|
241 qputenv(envname, "Hello, World"); |
|
242 QByteArray valAfter = qgetenv(envname); |
|
243 if (valAfter != "Hello, World") |
|
244 QSKIP("Could not test: qputenv did not do its job", SkipAll); |
|
245 |
|
246 QProcessEnvironment eAfter = QProcessEnvironment::systemEnvironment(); |
|
247 |
|
248 QVERIFY(!eBefore.contains(envname)); |
|
249 QVERIFY(eAfter.contains(envname)); |
|
250 QCOMPARE(eAfter.value(envname), QString("Hello, World")); |
|
251 |
|
252 # ifdef Q_OS_WIN |
|
253 // check case-insensitive too |
|
254 QString lower = envname; |
|
255 lower = lower.toLower(); |
|
256 QVERIFY(!eBefore.contains(lower)); |
|
257 QVERIFY(eAfter.contains(lower)); |
|
258 QCOMPARE(eAfter.value(lower), QString("Hello, World")); |
|
259 # endif |
|
260 #endif |
|
261 } |
|
262 |
|
263 QTEST_MAIN(tst_QProcessEnvironment) |
|
264 |
|
265 #include "tst_qprocessenvironment.moc" |