| 1 |
//===-- flang/unittests/Runtime/CommandTest.cpp ---------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "flang/Runtime/command.h" |
| 10 |
#include "gmock/gmock.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
#include "flang/Runtime/descriptor.h" |
| 13 |
#include "flang/Runtime/main.h" |
| 14 |
#include <cstdlib> |
| 15 |
|
| 16 |
using namespace Fortran::runtime; |
| 17 |
|
| 18 |
template <std::size_t n = 64> |
| 19 |
static OwningPtr<Descriptor> CreateEmptyCharDescriptor() { |
| 20 |
OwningPtr<Descriptor> descriptor{Descriptor::Create( |
| 21 |
sizeof(char), n, nullptr, 0, nullptr, CFI_attribute_allocatable)}; |
| 22 |
if (descriptor->Allocate() != 0) { |
| 23 |
return nullptr; |
| 24 |
} |
| 25 |
return descriptor; |
| 26 |
} |
| 27 |
|
| 28 |
static OwningPtr<Descriptor> CharDescriptor(const char *value) { |
| 29 |
std::size_t n{std::strlen(value)}; |
| 30 |
OwningPtr<Descriptor> descriptor{Descriptor::Create( |
| 31 |
sizeof(char), n, nullptr, 0, nullptr, CFI_attribute_allocatable)}; |
| 32 |
if (descriptor->Allocate() != 0) { |
| 33 |
return nullptr; |
| 34 |
} |
| 35 |
std::memcpy(descriptor->OffsetElement(), value, n); |
| 36 |
return descriptor; |
| 37 |
} |
| 38 |
|
| 39 |
template <int kind = sizeof(std::int64_t)> |
| 40 |
static OwningPtr<Descriptor> EmptyIntDescriptor() { |
| 41 |
OwningPtr<Descriptor> descriptor{Descriptor::Create(TypeCategory::Integer, |
| 42 |
kind, nullptr, 0, nullptr, CFI_attribute_allocatable)}; |
| 43 |
if (descriptor->Allocate() != 0) { |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
return descriptor; |
| 47 |
} |
| 48 |
|
| 49 |
class CommandFixture : public ::testing::Test { |
| 50 |
protected: |
| 51 |
CommandFixture(int argc, const char *argv[]) { |
| 52 |
RTNAME(ProgramStart)(argc, argv, {}, {}); |
| 53 |
} |
| 54 |
|
| 55 |
std::string GetPaddedStr(const char *text, std::size_t len) const { |
| 56 |
std::string res{text}; |
| 57 |
assert(res.length() <= len && "No room to pad"); |
| 58 |
res.append(len - res.length(), ' '); |
| 59 |
return res; |
| 60 |
} |
| 61 |
|
| 62 |
void CheckDescriptorEqStr( |
| 63 |
const Descriptor *value, const std::string &expected) const { |
| 64 |
ASSERT_NE(value, nullptr); |
| 65 |
EXPECT_EQ(std::strncmp(value->OffsetElement(), expected.c_str(), |
| 66 |
value->ElementBytes()), |
| 67 |
0) |
| 68 |
<< "expected: " << expected << "\n" |
| 69 |
<< "value: " |
| 70 |
<< std::string{value->OffsetElement(), value->ElementBytes()}; |
| 71 |
} |
| 72 |
|
| 73 |
template <typename INT_T = std::int64_t> |
| 74 |
void CheckDescriptorEqInt( |
| 75 |
const Descriptor *value, const INT_T expected) const { |
| 76 |
if (expected != -1) { |
| 77 |
ASSERT_NE(value, nullptr); |
| 78 |
EXPECT_EQ(*value->OffsetElement<INT_T>(), expected); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
template <typename RuntimeCall> |
| 83 |
void CheckValue(RuntimeCall F, const char *expectedValue, |
| 84 |
std::int64_t expectedLength = -1, std::int32_t expectedStatus = 0, |
| 85 |
const char *expectedErrMsg = "shouldn't change") const { |
| 86 |
OwningPtr<Descriptor> value{CreateEmptyCharDescriptor()}; |
| 87 |
ASSERT_NE(value, nullptr); |
| 88 |
|
| 89 |
OwningPtr<Descriptor> length{ |
| 90 |
expectedLength == -1 ? nullptr : EmptyIntDescriptor()}; |
| 91 |
|
| 92 |
OwningPtr<Descriptor> errmsg{CharDescriptor(expectedErrMsg)}; |
| 93 |
ASSERT_NE(errmsg, nullptr); |
| 94 |
|
| 95 |
std::string expectedValueStr{ |
| 96 |
GetPaddedStr(expectedValue, value->ElementBytes())}; |
| 97 |
|
| 98 |
EXPECT_EQ(F(value.get(), length.get(), errmsg.get()), expectedStatus); |
| 99 |
CheckDescriptorEqStr(value.get(), expectedValueStr); |
| 100 |
CheckDescriptorEqInt(length.get(), expectedLength); |
| 101 |
CheckDescriptorEqStr(errmsg.get(), expectedErrMsg); |
| 102 |
} |
| 103 |
|
| 104 |
void CheckArgumentValue(const char *expectedValue, int n) const { |
| 105 |
SCOPED_TRACE(n); |
| 106 |
SCOPED_TRACE("Checking argument:"); |
| 107 |
CheckValue( |
| 108 |
[&](const Descriptor *value, const Descriptor *length, |
| 109 |
const Descriptor *errmsg) { |
| 110 |
return RTNAME(GetCommandArgument)(n, value, length, errmsg); |
| 111 |
}, |
| 112 |
expectedValue, std::strlen(expectedValue)); |
| 113 |
} |
| 114 |
|
| 115 |
void CheckCommandValue(const char *args[], int n) const { |
| 116 |
SCOPED_TRACE("Checking command:"); |
| 117 |
ASSERT_GE(n, 1); |
| 118 |
std::string expectedValue{args[0]}; |
| 119 |
for (int i = 1; i < n; i++) { |
| 120 |
expectedValue += " " + std::string{args[i]}; |
| 121 |
} |
| 122 |
CheckValue( |
| 123 |
[&](const Descriptor *value, const Descriptor *length, |
| 124 |
const Descriptor *errmsg) { |
| 125 |
return RTNAME(GetCommand)(value, length, errmsg); |
| 126 |
}, |
| 127 |
expectedValue.c_str(), expectedValue.size()); |
| 128 |
} |
| 129 |
|
| 130 |
void CheckEnvVarValue( |
| 131 |
const char *expectedValue, const char *name, bool trimName = true) const { |
| 132 |
SCOPED_TRACE(name); |
| 133 |
SCOPED_TRACE("Checking environment variable"); |
| 134 |
CheckValue( |
| 135 |
[&](const Descriptor *value, const Descriptor *, |
| 136 |
const Descriptor *errmsg) { |
| 137 |
return RTNAME(EnvVariableValue)(*CharDescriptor(name), value, |
| 138 |
trimName, errmsg, /*sourceFile=*/nullptr, /*line=*/0); |
| 139 |
}, |
| 140 |
expectedValue); |
| 141 |
} |
| 142 |
|
| 143 |
void CheckMissingEnvVarValue(const char *name, bool trimName = true) const { |
| 144 |
SCOPED_TRACE(name); |
| 145 |
SCOPED_TRACE("Checking missing environment variable"); |
| 146 |
|
| 147 |
ASSERT_EQ(nullptr, std::getenv(name)) |
| 148 |
<< "Environment variable " << name << " not expected to exist"; |
| 149 |
|
| 150 |
OwningPtr<Descriptor> nameDescriptor{CharDescriptor(name)}; |
| 151 |
EXPECT_EQ(0, RTNAME(EnvVariableLength)(*nameDescriptor, trimName)); |
| 152 |
CheckValue( |
| 153 |
[&](const Descriptor *value, const Descriptor *, |
| 154 |
const Descriptor *errmsg) { |
| 155 |
return RTNAME(EnvVariableValue)(*nameDescriptor, value, trimName, |
| 156 |
errmsg, /*sourceFile=*/nullptr, /*line=*/0); |
| 157 |
}, |
| 158 |
"", -1, 1, "Missing environment variable"); |
| 159 |
} |
| 160 |
|
| 161 |
void CheckMissingArgumentValue(int n, const char *errStr = nullptr) const { |
| 162 |
OwningPtr<Descriptor> value{CreateEmptyCharDescriptor()}; |
| 163 |
ASSERT_NE(value, nullptr); |
| 164 |
|
| 165 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 166 |
ASSERT_NE(length, nullptr); |
| 167 |
|
| 168 |
OwningPtr<Descriptor> err{errStr ? CreateEmptyCharDescriptor() : nullptr}; |
| 169 |
|
| 170 |
EXPECT_GT( |
| 171 |
RTNAME(GetCommandArgument)(n, value.get(), length.get(), err.get()), 0); |
| 172 |
|
| 173 |
std::string spaces(value->ElementBytes(), ' '); |
| 174 |
CheckDescriptorEqStr(value.get(), spaces); |
| 175 |
|
| 176 |
CheckDescriptorEqInt(length.get(), 0); |
| 177 |
|
| 178 |
if (errStr) { |
| 179 |
std::string paddedErrStr(GetPaddedStr(errStr, err->ElementBytes())); |
| 180 |
CheckDescriptorEqStr(err.get(), paddedErrStr); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void CheckMissingCommandValue(const char *errStr = nullptr) const { |
| 185 |
OwningPtr<Descriptor> value{CreateEmptyCharDescriptor()}; |
| 186 |
ASSERT_NE(value, nullptr); |
| 187 |
|
| 188 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 189 |
ASSERT_NE(length, nullptr); |
| 190 |
|
| 191 |
OwningPtr<Descriptor> err{errStr ? CreateEmptyCharDescriptor() : nullptr}; |
| 192 |
|
| 193 |
EXPECT_GT(RTNAME(GetCommand)(value.get(), length.get(), err.get()), 0); |
| 194 |
|
| 195 |
std::string spaces(value->ElementBytes(), ' '); |
| 196 |
CheckDescriptorEqStr(value.get(), spaces); |
| 197 |
|
| 198 |
CheckDescriptorEqInt(length.get(), 0); |
| 199 |
|
| 200 |
if (errStr) { |
| 201 |
std::string paddedErrStr(GetPaddedStr(errStr, err->ElementBytes())); |
| 202 |
CheckDescriptorEqStr(err.get(), paddedErrStr); |
| 203 |
} |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
class NoArgv : public CommandFixture { |
| 208 |
protected: |
| 209 |
NoArgv() : CommandFixture(0, nullptr) {} |
| 210 |
}; |
| 211 |
|
| 212 |
// TODO: Test other intrinsics with this fixture. |
| 213 |
|
| 214 |
TEST_F(NoArgv, GetCommand) { CheckMissingCommandValue(); } |
| 215 |
|
| 216 |
static const char *commandOnlyArgv[]{"aProgram"}; |
| 217 |
class ZeroArguments : public CommandFixture { |
| 218 |
protected: |
| 219 |
ZeroArguments() : CommandFixture(1, commandOnlyArgv) {} |
| 220 |
}; |
| 221 |
|
| 222 |
TEST_F(ZeroArguments, ArgumentCount) { EXPECT_EQ(0, RTNAME(ArgumentCount)()); } |
| 223 |
|
| 224 |
TEST_F(ZeroArguments, GetCommandArgument) { |
| 225 |
CheckMissingArgumentValue(-1); |
| 226 |
CheckArgumentValue(commandOnlyArgv[0], 0); |
| 227 |
CheckMissingArgumentValue(1); |
| 228 |
} |
| 229 |
|
| 230 |
TEST_F(ZeroArguments, GetCommand) { CheckCommandValue(commandOnlyArgv, 1); } |
| 231 |
|
| 232 |
static const char *oneArgArgv[]{"aProgram", "anArgumentOfLength20"}; |
| 233 |
class OneArgument : public CommandFixture { |
| 234 |
protected: |
| 235 |
OneArgument() : CommandFixture(2, oneArgArgv) {} |
| 236 |
}; |
| 237 |
|
| 238 |
TEST_F(OneArgument, ArgumentCount) { EXPECT_EQ(1, RTNAME(ArgumentCount)()); } |
| 239 |
|
| 240 |
TEST_F(OneArgument, GetCommandArgument) { |
| 241 |
CheckMissingArgumentValue(-1); |
| 242 |
CheckArgumentValue(oneArgArgv[0], 0); |
| 243 |
CheckArgumentValue(oneArgArgv[1], 1); |
| 244 |
CheckMissingArgumentValue(2); |
| 245 |
} |
| 246 |
|
| 247 |
TEST_F(OneArgument, GetCommand) { CheckCommandValue(oneArgArgv, 2); } |
| 248 |
|
| 249 |
static const char *severalArgsArgv[]{ |
| 250 |
"aProgram", "16-char-long-arg", "", "-22-character-long-arg", "o"}; |
| 251 |
class SeveralArguments : public CommandFixture { |
| 252 |
protected: |
| 253 |
SeveralArguments() |
| 254 |
: CommandFixture(sizeof(severalArgsArgv) / sizeof(*severalArgsArgv), |
| 255 |
severalArgsArgv) {} |
| 256 |
}; |
| 257 |
|
| 258 |
TEST_F(SeveralArguments, ArgumentCount) { |
| 259 |
EXPECT_EQ(4, RTNAME(ArgumentCount)()); |
| 260 |
} |
| 261 |
|
| 262 |
TEST_F(SeveralArguments, GetCommandArgument) { |
| 263 |
CheckArgumentValue(severalArgsArgv[0], 0); |
| 264 |
CheckArgumentValue(severalArgsArgv[1], 1); |
| 265 |
CheckArgumentValue(severalArgsArgv[3], 3); |
| 266 |
CheckArgumentValue(severalArgsArgv[4], 4); |
| 267 |
} |
| 268 |
|
| 269 |
TEST_F(SeveralArguments, NoArgumentValue) { |
| 270 |
// Make sure we don't crash if the 'value', 'length' and 'error' parameters |
| 271 |
// aren't passed. |
| 272 |
EXPECT_GT(RTNAME(GetCommandArgument)(2), 0); |
| 273 |
EXPECT_EQ(RTNAME(GetCommandArgument)(1), 0); |
| 274 |
EXPECT_GT(RTNAME(GetCommandArgument)(-1), 0); |
| 275 |
} |
| 276 |
|
| 277 |
TEST_F(SeveralArguments, MissingArguments) { |
| 278 |
CheckMissingArgumentValue(-1, "Invalid argument number"); |
| 279 |
CheckMissingArgumentValue(2, "Missing argument"); |
| 280 |
CheckMissingArgumentValue(5, "Invalid argument number"); |
| 281 |
CheckMissingArgumentValue(5); |
| 282 |
} |
| 283 |
|
| 284 |
TEST_F(SeveralArguments, ArgValueTooShort) { |
| 285 |
OwningPtr<Descriptor> tooShort{CreateEmptyCharDescriptor<15>()}; |
| 286 |
ASSERT_NE(tooShort, nullptr); |
| 287 |
EXPECT_EQ(RTNAME(GetCommandArgument)(1, tooShort.get()), -1); |
| 288 |
CheckDescriptorEqStr(tooShort.get(), severalArgsArgv[1]); |
| 289 |
|
| 290 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 291 |
ASSERT_NE(length, nullptr); |
| 292 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor()}; |
| 293 |
ASSERT_NE(errMsg, nullptr); |
| 294 |
|
| 295 |
EXPECT_EQ( |
| 296 |
RTNAME(GetCommandArgument)(1, tooShort.get(), length.get(), errMsg.get()), |
| 297 |
-1); |
| 298 |
|
| 299 |
CheckDescriptorEqInt(length.get(), 16); |
| 300 |
std::string expectedErrMsg{ |
| 301 |
GetPaddedStr("Value too short", errMsg->ElementBytes())}; |
| 302 |
CheckDescriptorEqStr(errMsg.get(), expectedErrMsg); |
| 303 |
} |
| 304 |
|
| 305 |
TEST_F(SeveralArguments, ArgErrMsgTooShort) { |
| 306 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor<3>()}; |
| 307 |
EXPECT_GT(RTNAME(GetCommandArgument)(-1, nullptr, nullptr, errMsg.get()), 0); |
| 308 |
CheckDescriptorEqStr(errMsg.get(), "Inv"); |
| 309 |
} |
| 310 |
|
| 311 |
TEST_F(SeveralArguments, GetCommand) { |
| 312 |
CheckMissingCommandValue(); |
| 313 |
CheckMissingCommandValue("Missing argument"); |
| 314 |
} |
| 315 |
|
| 316 |
TEST_F(SeveralArguments, CommandErrMsgTooShort) { |
| 317 |
OwningPtr<Descriptor> value{CreateEmptyCharDescriptor()}; |
| 318 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 319 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor<3>()}; |
| 320 |
|
| 321 |
EXPECT_GT(RTNAME(GetCommand)(value.get(), length.get(), errMsg.get()), 0); |
| 322 |
|
| 323 |
std::string spaces(value->ElementBytes(), ' '); |
| 324 |
CheckDescriptorEqStr(value.get(), spaces); |
| 325 |
CheckDescriptorEqInt(length.get(), 0); |
| 326 |
CheckDescriptorEqStr(errMsg.get(), "Mis"); |
| 327 |
} |
| 328 |
|
| 329 |
TEST_F(SeveralArguments, GetCommandCanTakeNull) { |
| 330 |
EXPECT_GT(RTNAME(GetCommand)(nullptr, nullptr, nullptr), 0); |
| 331 |
} |
| 332 |
|
| 333 |
static const char *onlyValidArgsArgv[]{ |
| 334 |
"aProgram", "-f", "has/a/few/slashes", "has\\a\\few\\backslashes"}; |
| 335 |
class OnlyValidArguments : public CommandFixture { |
| 336 |
protected: |
| 337 |
OnlyValidArguments() |
| 338 |
: CommandFixture(sizeof(onlyValidArgsArgv) / sizeof(*onlyValidArgsArgv), |
| 339 |
onlyValidArgsArgv) {} |
| 340 |
}; |
| 341 |
|
| 342 |
TEST_F(OnlyValidArguments, GetCommand) { |
| 343 |
CheckCommandValue(onlyValidArgsArgv, 4); |
| 344 |
} |
| 345 |
|
| 346 |
TEST_F(OnlyValidArguments, CommandValueTooShort) { |
| 347 |
OwningPtr<Descriptor> tooShort{CreateEmptyCharDescriptor<50>()}; |
| 348 |
ASSERT_NE(tooShort, nullptr); |
| 349 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 350 |
ASSERT_NE(length, nullptr); |
| 351 |
|
| 352 |
EXPECT_EQ(RTNAME(GetCommand)(tooShort.get(), length.get(), nullptr), -1); |
| 353 |
|
| 354 |
CheckDescriptorEqStr( |
| 355 |
tooShort.get(), "aProgram -f has/a/few/slashes has\\a\\few\\backslashe"); |
| 356 |
CheckDescriptorEqInt(length.get(), 51); |
| 357 |
|
| 358 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor()}; |
| 359 |
ASSERT_NE(errMsg, nullptr); |
| 360 |
|
| 361 |
EXPECT_EQ(-1, RTNAME(GetCommand)(tooShort.get(), nullptr, errMsg.get())); |
| 362 |
|
| 363 |
std::string expectedErrMsg{ |
| 364 |
GetPaddedStr("Value too short", errMsg->ElementBytes())}; |
| 365 |
CheckDescriptorEqStr(errMsg.get(), expectedErrMsg); |
| 366 |
} |
| 367 |
|
| 368 |
TEST_F(OnlyValidArguments, GetCommandCanTakeNull) { |
| 369 |
EXPECT_EQ(0, RTNAME(GetCommand)(nullptr, nullptr, nullptr)); |
| 370 |
|
| 371 |
OwningPtr<Descriptor> value{CreateEmptyCharDescriptor()}; |
| 372 |
ASSERT_NE(value, nullptr); |
| 373 |
OwningPtr<Descriptor> length{EmptyIntDescriptor()}; |
| 374 |
ASSERT_NE(length, nullptr); |
| 375 |
|
| 376 |
EXPECT_EQ(0, RTNAME(GetCommand)(value.get(), nullptr, nullptr)); |
| 377 |
CheckDescriptorEqStr(value.get(), |
| 378 |
GetPaddedStr("aProgram -f has/a/few/slashes has\\a\\few\\backslashes", |
| 379 |
value->ElementBytes())); |
| 380 |
|
| 381 |
EXPECT_EQ(0, RTNAME(GetCommand)(nullptr, length.get(), nullptr)); |
| 382 |
CheckDescriptorEqInt(length.get(), 51); |
| 383 |
} |
| 384 |
|
| 385 |
TEST_F(OnlyValidArguments, GetCommandShortLength) { |
| 386 |
OwningPtr<Descriptor> length{EmptyIntDescriptor<sizeof(short)>()}; |
| 387 |
ASSERT_NE(length, nullptr); |
| 388 |
|
| 389 |
EXPECT_EQ(0, RTNAME(GetCommand)(nullptr, length.get(), nullptr)); |
| 390 |
CheckDescriptorEqInt<short>(length.get(), 51); |
| 391 |
} |
| 392 |
|
| 393 |
class EnvironmentVariables : public CommandFixture { |
| 394 |
protected: |
| 395 |
EnvironmentVariables() : CommandFixture(0, nullptr) { |
| 396 |
SetEnv("NAME", "VALUE"); |
| 397 |
SetEnv("EMPTY", ""); |
| 398 |
} |
| 399 |
|
| 400 |
// If we have access to setenv, we can run some more fine-grained tests. |
| 401 |
template <typename ParamType = char> |
| 402 |
void SetEnv(const ParamType *name, const ParamType *value, |
| 403 |
decltype(setenv(name, value, 1)) *Enabled = nullptr) { |
| 404 |
ASSERT_EQ(0, setenv(name, value, /*overwrite=*/1)); |
| 405 |
canSetEnv = true; |
| 406 |
} |
| 407 |
|
| 408 |
// Fallback method if setenv is not available. |
| 409 |
template <typename Unused = void> void SetEnv(const void *, const void *) {} |
| 410 |
|
| 411 |
bool EnableFineGrainedTests() const { return canSetEnv; } |
| 412 |
|
| 413 |
private: |
| 414 |
bool canSetEnv{false}; |
| 415 |
}; |
| 416 |
|
| 417 |
TEST_F(EnvironmentVariables, Nonexistent) { |
| 418 |
CheckMissingEnvVarValue("DOESNT_EXIST"); |
| 419 |
|
| 420 |
CheckMissingEnvVarValue(" "); |
| 421 |
CheckMissingEnvVarValue(""); |
| 422 |
} |
| 423 |
|
| 424 |
TEST_F(EnvironmentVariables, Basic) { |
| 425 |
// Test a variable that's expected to exist in the environment. |
| 426 |
char *path{std::getenv("PATH")}; |
| 427 |
auto expectedLen{static_cast<int64_t>(std::strlen(path))}; |
| 428 |
EXPECT_EQ(expectedLen, RTNAME(EnvVariableLength)(*CharDescriptor("PATH"))); |
| 429 |
} |
| 430 |
|
| 431 |
TEST_F(EnvironmentVariables, Trim) { |
| 432 |
if (EnableFineGrainedTests()) { |
| 433 |
EXPECT_EQ(5, RTNAME(EnvVariableLength)(*CharDescriptor("NAME "))); |
| 434 |
CheckEnvVarValue("VALUE", "NAME "); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
TEST_F(EnvironmentVariables, NoTrim) { |
| 439 |
if (EnableFineGrainedTests()) { |
| 440 |
CheckMissingEnvVarValue("NAME ", /*trim_name=*/false); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
TEST_F(EnvironmentVariables, Empty) { |
| 445 |
if (EnableFineGrainedTests()) { |
| 446 |
EXPECT_EQ(0, RTNAME(EnvVariableLength)(*CharDescriptor("EMPTY"))); |
| 447 |
CheckEnvVarValue("", "EMPTY"); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
TEST_F(EnvironmentVariables, NoValueOrErrmsg) { |
| 452 |
ASSERT_EQ(std::getenv("DOESNT_EXIST"), nullptr) |
| 453 |
<< "Environment variable DOESNT_EXIST actually exists"; |
| 454 |
EXPECT_EQ(RTNAME(EnvVariableValue)(*CharDescriptor("DOESNT_EXIST")), 1); |
| 455 |
|
| 456 |
if (EnableFineGrainedTests()) { |
| 457 |
EXPECT_EQ(RTNAME(EnvVariableValue)(*CharDescriptor("NAME")), 0); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
TEST_F(EnvironmentVariables, ValueTooShort) { |
| 462 |
if (EnableFineGrainedTests()) { |
| 463 |
OwningPtr<Descriptor> tooShort{CreateEmptyCharDescriptor<2>()}; |
| 464 |
ASSERT_NE(tooShort, nullptr); |
| 465 |
EXPECT_EQ(RTNAME(EnvVariableValue)(*CharDescriptor("NAME"), tooShort.get(), |
| 466 |
/*trim_name=*/true, nullptr), |
| 467 |
-1); |
| 468 |
CheckDescriptorEqStr(tooShort.get(), "VALUE"); |
| 469 |
|
| 470 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor()}; |
| 471 |
ASSERT_NE(errMsg, nullptr); |
| 472 |
|
| 473 |
EXPECT_EQ(RTNAME(EnvVariableValue)(*CharDescriptor("NAME"), tooShort.get(), |
| 474 |
/*trim_name=*/true, errMsg.get()), |
| 475 |
-1); |
| 476 |
|
| 477 |
std::string expectedErrMsg{ |
| 478 |
GetPaddedStr("Value too short", errMsg->ElementBytes())}; |
| 479 |
CheckDescriptorEqStr(errMsg.get(), expectedErrMsg); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
TEST_F(EnvironmentVariables, ErrMsgTooShort) { |
| 484 |
ASSERT_EQ(std::getenv("DOESNT_EXIST"), nullptr) |
| 485 |
<< "Environment variable DOESNT_EXIST actually exists"; |
| 486 |
|
| 487 |
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor<3>()}; |
| 488 |
EXPECT_EQ(RTNAME(EnvVariableValue)(*CharDescriptor("DOESNT_EXIST"), nullptr, |
| 489 |
/*trim_name=*/true, errMsg.get()), |
| 490 |
1); |
| 491 |
CheckDescriptorEqStr(errMsg.get(), "Mis"); |
| 492 |
} |