WSL/test/linux/unit_tests/user.c
WSL Team 697572d664 Initial open source commit for WSL.
Many Microsoft employees have contributed to the Windows Subsystem for Linux, this commit is the result of their work since 2016.

The entire history of the Windows Subsystem for Linux can't be shared here, but here's an overview of WSL's history after it moved to it own repository in 2021:

Number of commits on the main branch: 2930
Number of contributors: 31

Head over https://github.com/microsoft/WSL/releases for a more detailed history of the features added to WSL since 2021.
2025-05-15 12:09:45 -07:00

120 lines
2.4 KiB
C

/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
user.c
Abstract:
This file is the source for the user management.
--*/
#include "lxtcommon.h"
#include "unittests.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#define LXT_NAME "user"
int ValidateUserTest(char* Username, uid_t Uid, uid_t Gid);
int UserTestEntry(int Argc, char* Argv[])
/*++
--*/
{
LXT_ARGS Args;
uid_t Gid;
int Result = LXT_RESULT_FAILURE;
uid_t Uid;
char* Username;
LxtCheckResult(LxtInitialize(Argc, Argv, &Args, LXT_NAME));
if (Argc < 4)
{
LxtLogError("User test requires three arguments: username, uid, gid");
goto ErrorExit;
}
Username = Argv[1];
Uid = atoi(Argv[2]);
Gid = atoi(Argv[3]);
LxtCheckResult(ValidateUserTest(Username, Uid, Gid));
ErrorExit:
LxtUninitialize();
return !LXT_SUCCESS(Result);
}
int ValidateUserTest(char* Username, uid_t Uid, uid_t Gid)
/*++
--*/
{
struct passwd* PasswordEntry;
uid_t RealGid;
uid_t RealUid;
int Result = LXT_RESULT_FAILURE;
RealUid = getuid();
if (Uid != RealUid)
{
LxtLogError("Uid %u does not match RealUid %u", Uid, RealUid);
goto ErrorExit;
}
RealGid = getgid();
if (Gid != RealGid)
{
LxtLogError("Gid %u does not match RealGid %u", Gid, RealGid);
goto ErrorExit;
}
//
// Compare passed-in values to the values stored in the password entry file.
//
PasswordEntry = getpwnam(Username);
if (PasswordEntry == NULL)
{
LxtLogError("getpwnam %s failed", Username);
goto ErrorExit;
}
if (Uid != PasswordEntry->pw_uid)
{
LxtLogError("Uid %u does not match PasswordEntry->pw_uid %u", Uid, PasswordEntry->pw_uid);
goto ErrorExit;
}
if (Gid != PasswordEntry->pw_gid)
{
LxtLogError("Gid %u does not match PasswordEntry->pw_gid %u", Gid, PasswordEntry->pw_gid);
goto ErrorExit;
}
if (strstr(PasswordEntry->pw_dir, Username) == NULL)
{
LxtLogError("Home path %s does not contain Username %s", PasswordEntry->pw_dir, Username);
goto ErrorExit;
}
Result = LXT_RESULT_SUCCESS;
LxtLogPassed("Username %s, Uid %u, Gid %u successfully validated!", Username, Uid, Gid);
ErrorExit:
return Result;
}