Skip to content
Permalink
Browse files

Add testUser() and testGroup().

Retrieve the name of the current user/group used for testing.
  • Loading branch information...
dwsteele committed May 13, 2019
1 parent 31d0fe5 commit 431532574b1e2e59979bd6cf3c333979d0817ad2
Showing with 54 additions and 12 deletions.
  1. +4 −0 doc/xml/release.xml
  2. +40 −0 test/src/common/harnessTest.c
  3. +3 −0 test/src/common/harnessTest.h
  4. +7 −12 test/src/module/storage/posixTest.c
@@ -183,6 +183,10 @@
<p>Update containers with <postgres/> minor releases and <id>liblz4</id>.</p>
</release-item>

<release-item>
<p>Add <code>testUser()</code> and <code>testGroup()</code>.</p>
</release-item>

<release-item>
<p>Add <id>build-max</id> option to set max build processes.</p>
</release-item>
@@ -1,10 +1,13 @@
/***********************************************************************************************************************************
C Test Harness
***********************************************************************************************************************************/
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

#include "common/harnessDebug.h"
#include "common/harnessTest.h"
@@ -30,6 +33,9 @@ static const char *testPathData = NULL;
static const char *testRepoPathData = NULL;
static const char *testExpectPathData = NULL;

static char testUserData[64];
static char testGroupData[64];

/***********************************************************************************************************************************
Extern functions
***********************************************************************************************************************************/
@@ -134,6 +140,21 @@ testExpectPathSet(const char *testExpectPath)
FUNCTION_HARNESS_RESULT_VOID();
}

/***********************************************************************************************************************************
Get test user/group
***********************************************************************************************************************************/
const char *
testUser(void)
{
return testUserData;
}

const char *
testGroup(void)
{
return testGroupData;
}

/***********************************************************************************************************************************
Get the time in milliseconds
***********************************************************************************************************************************/
@@ -200,6 +221,25 @@ testBegin(const char *name)

if (testList[testRun - 1].selected)
{
if (testFirst)
{
// Set test user
const char *testUserTemp = getpwuid(getuid())->pw_name;

if (strlen(testUserTemp) > sizeof(testUserData) - 1)
THROW_FMT(AssertError, "test user name must be less than %zu characters", sizeof(testUserData) - 1);

strcpy(testUserData, testUserTemp);

// Set test group
const char *testGroupTemp = getgrgid(getgid())->gr_name;

if (strlen(testGroupTemp) > sizeof(testGroupData) - 1)
THROW_FMT(AssertError, "test group name must be less than %zu characters", sizeof(testGroupData) - 1);

strcpy(testGroupData, testGroupTemp);
}

#ifndef NO_LOG
if (!testFirst)
{
@@ -34,6 +34,9 @@ void testExpectPathSet(const char *testExpectPath);
const char *testRepoPath(void);
void testRepoPathSet(const char *testRepoPath);

const char *testUser(void);
const char *testGroup(void);

/***********************************************************************************************************************************
Maximum size of a formatted result in the TEST_RESULT macro. Strings don't count as they are output directly, so this only applies
to the formatting of bools, ints, floats, etc. This should be plenty of room for any of those types.
@@ -234,8 +234,8 @@ testRun(void)
TEST_RESULT_INT(info.mode, 0770, " check mode");
TEST_RESULT_UINT(info.timeModified, 1555160000, " check mod time");
TEST_RESULT_PTR(info.linkDestination, NULL, " no link destination");
TEST_RESULT_STR(strPtr(info.user), getpwuid(getuid())->pw_name, " check user");
TEST_RESULT_STR(strPtr(info.group), getgrgid(getgid())->gr_name, " check group");
TEST_RESULT_STR(strPtr(info.user), testUser(), " check user");
TEST_RESULT_STR(strPtr(info.group), testGroup(), " check group");

// -------------------------------------------------------------------------------------------------------------------------
const Buffer *buffer = BUFSTRDEF("TESTFILE");
@@ -270,8 +270,8 @@ testRun(void)
TEST_RESULT_INT(info.size, 0, " check size");
TEST_RESULT_INT(info.mode, 0777, " check mode");
TEST_RESULT_STR(strPtr(info.linkDestination), "/tmp", " check link destination");
TEST_RESULT_STR(strPtr(info.user), getpwuid(getuid())->pw_name, " check user");
TEST_RESULT_STR(strPtr(info.group), getgrgid(getgid())->gr_name, " check group");
TEST_RESULT_STR(strPtr(info.user), testUser(), " check user");
TEST_RESULT_STR(strPtr(info.group), testGroup(), " check group");

TEST_ASSIGN(info, storageInfoP(storageTest, linkName, .followLink = true), "get info from path pointed to by link");
TEST_RESULT_PTR(info.name, NULL, " name is not set");
@@ -679,9 +679,7 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(
file,
storageNewWriteP(
storageTest, fileName, .user = strNew(getpwuid(getuid())->pw_name), .group = strNew(getgrgid(getgid())->gr_name),
.timeModified = 1),
storageNewWriteP(storageTest, fileName, .user = strNew(testUser()), .group = strNew(testGroup()), .timeModified = 1),
"new write file (defaults)");
TEST_RESULT_VOID(ioWriteOpen(storageWriteIo(file)), " open file");
TEST_RESULT_INT(
@@ -697,8 +695,7 @@ testRun(void)
String *fileNameTmp = strNewFmt("%s." STORAGE_FILE_TEMP_EXT, strPtr(fileName));

TEST_ASSIGN(
file, storageNewWriteP(storageTest, fileName, .user = strNew(getpwuid(getuid())->pw_name)),
"new write file (defaults)");
file, storageNewWriteP(storageTest, fileName, .user = strNew(testUser())), "new write file (defaults)");
TEST_RESULT_VOID(ioWriteOpen(storageWriteIo(file)), " open file");
TEST_RESULT_VOID(ioWrite(storageWriteIo(file), BUFSTRDEF("TESTDATA")), "write data");
TEST_RESULT_VOID(ioWriteFlush(storageWriteIo(file)), "flush data");
@@ -712,9 +709,7 @@ testRun(void)
fileName = strNewFmt("%s/sub2/testfile", testPath());

TEST_ASSIGN(
file,
storageNewWriteP(
storageTest, fileName, .modePath = 0700, .modeFile = 0600, .group = strNew(getgrgid(getgid())->gr_name)),
file, storageNewWriteP(storageTest, fileName, .modePath = 0700, .modeFile = 0600, .group = strNew(testGroup())),
"new write file (set mode)");
TEST_RESULT_VOID(ioWriteOpen(storageWriteIo(file)), " open file");
TEST_RESULT_VOID(ioWriteClose(storageWriteIo(file)), " close file");

0 comments on commit 4315325

Please sign in to comment.
You can’t perform that action at this time.