Skip to content

Commit

Permalink
time: add utimes() and futimes() test
Browse files Browse the repository at this point in the history
JIRA: DTR-276
  • Loading branch information
ziemleszcz committed Feb 21, 2022
1 parent 9d14664 commit e2bfa64
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 0 deletions.
5 changes: 5 additions & 0 deletions time/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NAME := test_utimes
LOCAL_SRCS := test_utimes.c
DEP_LIBS := unity

include $(binary.mk)
239 changes: 239 additions & 0 deletions time/test_utimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* Phoenix-RTOS
*
* phoenix-rtos-tests
*
* utimes tests
*
* Copyright 2022 Phoenix Systems
* Author: Ziemowit Leszczynski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>

#include "unity_fixture.h"


#define FILENAME "/var/tmp/utimes"


TEST_GROUP(test_utimes);


TEST_SETUP(test_utimes)
{
int fd;

if ((fd = creat(FILENAME, 0644)) < 0)
FAIL("creat");
close(fd);
}


TEST_TEAR_DOWN(test_utimes) { unlink(FILENAME); }


TEST(test_utimes, set_null)
{
struct timeval now;
struct stat statbuf;

sleep(2);

if (gettimeofday(&now, NULL) < 0)
FAIL("gettimeofday");

if (utimes(FILENAME, NULL) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

TEST_ASSERT(now.tv_sec == statbuf.st_mtime);
TEST_ASSERT(now.tv_sec == statbuf.st_atime);
}


TEST(test_utimes, set_now)
{
struct timeval ctv[2];
struct stat statbuf;

sleep(2);

if (gettimeofday(&ctv[0], NULL) < 0)
FAIL("gettimeofday");
ctv[1] = ctv[0];

if (utimes(FILENAME, ctv) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

TEST_ASSERT(ctv[0].tv_sec == statbuf.st_atime);
TEST_ASSERT(ctv[1].tv_sec == statbuf.st_mtime);
}


TEST(test_utimes, set_past)
{
struct timeval ctv[2];
struct stat statbuf;

ctv[0].tv_sec = 0;
ctv[1].tv_sec = 24 * 60 * 60;
ctv[0].tv_usec = 0;
ctv[1].tv_usec = 0;

if (utimes(FILENAME, ctv) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

TEST_ASSERT(ctv[0].tv_sec == statbuf.st_atime);
TEST_ASSERT(ctv[1].tv_sec == statbuf.st_mtime);
}


TEST_GROUP(test_futimes);


TEST_SETUP(test_futimes)
{
int fd;

if ((fd = creat(FILENAME, 0644)) < 0)
FAIL("creat");
close(fd);
}


TEST_TEAR_DOWN(test_futimes) { unlink(FILENAME); }


TEST(test_futimes, set_null)
{
int fd;
struct timeval now;
struct stat statbuf;

sleep(2);

if ((fd = open(FILENAME, O_RDONLY)) < 0)
FAIL("open");

if (gettimeofday(&now, NULL) < 0)
FAIL("gettimeofday");

if (utimes(FILENAME, NULL) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

close(fd);

TEST_ASSERT(now.tv_sec == statbuf.st_mtime);
TEST_ASSERT(now.tv_sec == statbuf.st_atime);
}


TEST(test_futimes, set_now)
{
int fd;
struct timeval ctv[2];
struct stat statbuf;

sleep(2);

if ((fd = open(FILENAME, O_RDONLY)) < 0)
FAIL("open");

if (gettimeofday(&ctv[0], NULL) < 0)
FAIL("gettimeofday");

ctv[1] = ctv[0];

if (utimes(FILENAME, ctv) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

close(fd);

TEST_ASSERT(ctv[0].tv_sec == statbuf.st_atime);
TEST_ASSERT(ctv[1].tv_sec == statbuf.st_mtime);
}


TEST(test_futimes, set_past)
{
int fd;
struct timeval ctv[2];
struct stat statbuf;

ctv[0].tv_sec = 0;
ctv[1].tv_sec = 24 * 60 * 60;
ctv[0].tv_usec = 0;
ctv[1].tv_usec = 0;

if ((fd = open(FILENAME, O_RDONLY)) < 0)
FAIL("open");

if (utimes(FILENAME, ctv) < 0)
FAIL("utimes");

if (stat(FILENAME, &statbuf) < 0)
FAIL("stat");

close(fd);

TEST_ASSERT(ctv[0].tv_sec == statbuf.st_atime);
TEST_ASSERT(ctv[1].tv_sec == statbuf.st_mtime);
}


TEST_GROUP_RUNNER(test_utimes)
{
RUN_TEST_CASE(test_utimes, set_null);
RUN_TEST_CASE(test_utimes, set_now);
RUN_TEST_CASE(test_utimes, set_past);
}


TEST_GROUP_RUNNER(test_futimes)
{
RUN_TEST_CASE(test_futimes, set_null);
RUN_TEST_CASE(test_futimes, set_now);
RUN_TEST_CASE(test_futimes, set_past);
}


void runner(void)
{
RUN_TEST_GROUP(test_utimes);
RUN_TEST_GROUP(test_futimes);
}


int main(int argc, char* argv[])
{
UnityMain(argc, (const char**) argv, runner);
return 0;
}

0 comments on commit e2bfa64

Please sign in to comment.