<commit_before>//! @file test-getusername.cpp
//! @author <NAME> <<EMAIL>>
//! @brief Unit tests for GetUserName

#include <string>
#include <vector>
#include <unistd.h>
#include <gtest/gtest.h>
#include <unicode/utypes.h>
#include <unicode/ucnv.h>
#include <unicode/ustring.h>
#include <unicode/uchar.h>
#include "getusername.h"

//! Test fixture for GetUserNameW
class GetUserNameTest : public ::testing::Test
{
protected:
    DWORD lpnSize;
    std::vector<WCHAR_T> lpBuffer;
    BOOL result;
    std::string expectedUsername;
    DWORD expectedSize;

    GetUserNameTest(): expectedUsername(std::string(getlogin())),
                       expectedSize(expectedUsername.length()+1)
    {
    }

    //! Invokes GetUserNameW with lpnSize and lpBuffer, saves result.
    //!
    //! @param size Assigns to lpnSize and allocates lpBuffer with
    //! size number of null characters.
    void TestWithSize(DWORD size)
    {
        lpnSize = size;
        // allocate a WCHAR_T buffer to receive username
        lpBuffer.assign(lpnSize, 0);
        result = GetUserNameW(&lpBuffer[0], &lpnSize);
    }

    //! Checks the effects of GetUserNameW for success.
    void TestSuccess()
    {
        SCOPED_TRACE("");

        //! Returns TRUE on success.
        EXPECT_EQ(TRUE, result);

        //! Sets lpnSize to number of WCHARs including null.
        ASSERT_EQ(expectedSize, lpnSize);

        // setup for conversion from UTF-16LE
        const char* begin = reinterpret_cast<char*>(&lpBuffer[0]);
        // multiply to get number of bytes
        icu::UnicodeString username16(begin, lpnSize*sizeof(char16_t), "UTF-16LE");
        // username16 length includes null and is number of characters
        ASSERT_EQ(expectedSize, username16.length());

        // convert (minus null) to UTF-8 for comparison
        std::string username(lpnSize-1, 0);
        ASSERT_EQ(expectedUsername.length(), username.length());
        username16.extract(0, username.length(),
                           reinterpret_cast<char*>(&username[0]), "UTF-8");

        //! Returned username (after conversion) is what was expected.
        EXPECT_EQ(expectedUsername, username);
    }

    //! Checks the effects of GetUserNameW on failure with invalid parameters.
    void TestInvalidParameter()
    {
        SCOPED_TRACE("");

        //! Returns FALSE on failure.
        EXPECT_EQ(FALSE, result);

        //! Sets errno to ERROR_INVALID_PARAMETER when lpBuffer is null
        //! (which is the case for an empty vector).
        EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
    }<commit_msg>Use libicu properly in tests<commit_after>//! @file test-getusername.cpp
//! @author <NAME> <<EMAIL>>
//! @brief Unit tests for GetUserName

#include <string>
#include <vector>
#include <unistd.h>
#include <gtest/gtest.h>
#include <unicode/unistr.h>
#include "getusername.h"

//! Test fixture for GetUserNameW
class GetUserNameTest : public ::testing::Test
{
protected:
    DWORD lpnSize;
    std::vector<WCHAR_T> lpBuffer;
    BOOL result;
    std::string expectedUsername;
    DWORD expectedSize;

    GetUserNameTest(): expectedUsername(std::string(getlogin())),
                       expectedSize(expectedUsername.length()+1)
    {
    }

    //! Invokes GetUserNameW with lpnSize and lpBuffer, saves result.
    //!
    //! @param size Assigns to lpnSize and allocates lpBuffer with
    //! size number of null characters.
    void TestWithSize(DWORD size)
    {
        lpnSize = size;
        // allocate a WCHAR_T buffer to receive username
        lpBuffer.assign(lpnSize, 0);
        result = GetUserNameW(&lpBuffer[0], &lpnSize);
    }

    //! Checks the effects of GetUserNameW for success.
    void TestSuccess()
    {
        SCOPED_TRACE("");

        //! Returns TRUE on success.
        EXPECT_EQ(TRUE, result);

        //! Sets lpnSize to number of WCHARs including null.
        ASSERT_EQ(expectedSize, lpnSize);

        // Read lpBuffer into UnicodeString (without null)
        const char* begin = reinterpret_cast<char*>(&lpBuffer[0]);
        icu::UnicodeString username16(begin, (lpnSize-1)*sizeof(UChar), "UTF-16LE");
        ASSERT_EQ(expectedUsername.length(), username16.length());
        // Convert to UTF-8 for comparison
        std::string username;
        username16.toUTF8String(username);
        ASSERT_EQ(expectedUsername.length(), username.length());

        //! Returned username (after conversion) is what was expected.
        EXPECT_EQ(expectedUsername, username);
    }

    //! Checks the effects of GetUserNameW on failure with invalid parameters.
    void TestInvalidParameter()
    {
        SCOPED_TRACE("");

        //! Returns FALSE on failure.
        EXPECT_EQ(FALSE, result);

        //! Sets errno to ERROR_INVALID_PARAMETER when lpBuffer is null
        //! (which is the case for an empty vector).
        EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
    }
{'fraction_non_alphanumeric': 0.11321909424724602, 'fraction_numerical': 0.00795593635250918, 'mean_word_length': 2.774441878367975, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7430904', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1413, 'n_words': 396}