|
10 | 10 | #include "gmock/gmock.h"
|
11 | 11 | #include "gtest/gtest.h"
|
12 | 12 | #include "flang/Runtime/descriptor.h"
|
| 13 | +#include "flang/Runtime/extensions.h" |
13 | 14 | #include "flang/Runtime/main.h"
|
| 15 | +#include <cstddef> |
14 | 16 | #include <cstdlib>
|
15 | 17 |
|
| 18 | +#if _REENTRANT || _POSIX_C_SOURCE >= 199506L |
| 19 | +#include <limits.h> // LOGIN_NAME_MAX used in getlog test |
| 20 | +#endif |
| 21 | + |
16 | 22 | using namespace Fortran::runtime;
|
17 | 23 |
|
18 | 24 | template <std::size_t n = 64>
|
@@ -59,6 +65,13 @@ class CommandFixture : public ::testing::Test {
|
59 | 65 | return res;
|
60 | 66 | }
|
61 | 67 |
|
| 68 | + void CheckCharEqStr(const char *value, const std::string &expected) const { |
| 69 | + ASSERT_NE(value, nullptr); |
| 70 | + EXPECT_EQ(std::strncmp(value, expected.c_str(), expected.size()), 0) |
| 71 | + << "expected: " << expected << "\n" |
| 72 | + << "value: " << value; |
| 73 | + } |
| 74 | + |
62 | 75 | void CheckDescriptorEqStr(
|
63 | 76 | const Descriptor *value, const std::string &expected) const {
|
64 | 77 | ASSERT_NE(value, nullptr);
|
@@ -397,6 +410,11 @@ class EnvironmentVariables : public CommandFixture {
|
397 | 410 | protected:
|
398 | 411 | EnvironmentVariables() : CommandFixture(0, nullptr) {
|
399 | 412 | SetEnv("NAME", "VALUE");
|
| 413 | +#ifdef _WIN32 |
| 414 | + SetEnv("USERNAME", "loginName"); |
| 415 | +#else |
| 416 | + SetEnv("LOGNAME", "loginName"); |
| 417 | +#endif |
400 | 418 | SetEnv("EMPTY", "");
|
401 | 419 | }
|
402 | 420 |
|
@@ -494,3 +512,68 @@ TEST_F(EnvironmentVariables, ErrMsgTooShort) {
|
494 | 512 | 1);
|
495 | 513 | CheckDescriptorEqStr(errMsg.get(), "Mis");
|
496 | 514 | }
|
| 515 | + |
| 516 | +// username first char must not be null |
| 517 | +TEST_F(EnvironmentVariables, GetlogGetName) { |
| 518 | + const int charLen{3}; |
| 519 | + char input[charLen]{"\0\0"}; |
| 520 | + |
| 521 | + FORTRAN_PROCEDURE_NAME(getlog) |
| 522 | + (reinterpret_cast<std::byte *>(input), charLen); |
| 523 | + |
| 524 | + EXPECT_NE(input[0], '\0'); |
| 525 | +} |
| 526 | + |
| 527 | +#if _REENTRANT || _POSIX_C_SOURCE >= 199506L |
| 528 | +TEST_F(EnvironmentVariables, GetlogPadSpace) { |
| 529 | + // guarantee 1 char longer than max, last char should be pad space |
| 530 | + const int charLen{LOGIN_NAME_MAX + 2}; |
| 531 | + char input[charLen]; |
| 532 | + |
| 533 | + FORTRAN_PROCEDURE_NAME(getlog) |
| 534 | + (reinterpret_cast<std::byte *>(input), charLen); |
| 535 | + |
| 536 | + EXPECT_EQ(input[charLen - 1], ' '); |
| 537 | +} |
| 538 | +#endif |
| 539 | + |
| 540 | +#ifdef _WIN32 // Test ability to get name from environment variable |
| 541 | +TEST_F(EnvironmentVariables, GetlogEnvGetName) { |
| 542 | + if (EnableFineGrainedTests()) { |
| 543 | + ASSERT_NE(std::getenv("USERNAME"), nullptr) |
| 544 | + << "Environment variable USERNAME does not exist"; |
| 545 | + |
| 546 | + char input[]{"XXXXXXXXX"}; |
| 547 | + FORTRAN_PROCEDURE_NAME(getlog) |
| 548 | + (reinterpret_cast<std::byte *>(input), sizeof(input)); |
| 549 | + |
| 550 | + CheckCharEqStr(input, "loginName"); |
| 551 | + } |
| 552 | +} |
| 553 | + |
| 554 | +TEST_F(EnvironmentVariables, GetlogEnvBufferShort) { |
| 555 | + if (EnableFineGrainedTests()) { |
| 556 | + ASSERT_NE(std::getenv("USERNAME"), nullptr) |
| 557 | + << "Environment variable USERNAME does not exist"; |
| 558 | + |
| 559 | + char input[]{"XXXXXX"}; |
| 560 | + FORTRAN_PROCEDURE_NAME(getlog) |
| 561 | + (reinterpret_cast<std::byte *>(input), sizeof(input)); |
| 562 | + |
| 563 | + CheckCharEqStr(input, "loginN"); |
| 564 | + } |
| 565 | +} |
| 566 | + |
| 567 | +TEST_F(EnvironmentVariables, GetlogEnvPadSpace) { |
| 568 | + if (EnableFineGrainedTests()) { |
| 569 | + ASSERT_NE(std::getenv("USERNAME"), nullptr) |
| 570 | + << "Environment variable USERNAME does not exist"; |
| 571 | + |
| 572 | + char input[]{"XXXXXXXXXX"}; |
| 573 | + FORTRAN_PROCEDURE_NAME(getlog) |
| 574 | + (reinterpret_cast<std::byte *>(input), sizeof(input)); |
| 575 | + |
| 576 | + CheckCharEqStr(input, "loginName "); |
| 577 | + } |
| 578 | +} |
| 579 | +#endif |
0 commit comments