1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Test Description:
6f08c3bdfSopenharmony_ci *  Verify that the system call stime() successfully sets the system's idea
7f08c3bdfSopenharmony_ci *  of date and time if invoked by "root" user.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Expected Result:
10f08c3bdfSopenharmony_ci *  stime() should succeed to set the system data/time to the specified time.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * History
13f08c3bdfSopenharmony_ci *	07/2001 John George
14f08c3bdfSopenharmony_ci *		-Ported
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <time.h>
18f08c3bdfSopenharmony_ci#include <sys/time.h>
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci#include "stime_var.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic struct timeval real_time_tv;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void run(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	time_t new_time;
28f08c3bdfSopenharmony_ci	struct timeval pres_time_tv;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (gettimeofday(&real_time_tv, NULL) < 0)
31f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "gettimeofday() failed");
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	new_time = real_time_tv.tv_sec + 30;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	if (do_stime(&new_time) < 0) {
36f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "stime(%ld) failed", new_time);
37f08c3bdfSopenharmony_ci		return;
38f08c3bdfSopenharmony_ci	}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (gettimeofday(&pres_time_tv, NULL) < 0)
41f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "gettimeofday() failed");
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	switch (pres_time_tv.tv_sec - new_time) {
44f08c3bdfSopenharmony_ci	case 0:
45f08c3bdfSopenharmony_ci	case 1:
46f08c3bdfSopenharmony_ci		tst_res(TINFO, "pt.tv_sec: %ld", pres_time_tv.tv_sec);
47f08c3bdfSopenharmony_ci		tst_res(TPASS, "system time was set to %ld", new_time);
48f08c3bdfSopenharmony_ci	break;
49f08c3bdfSopenharmony_ci	default:
50f08c3bdfSopenharmony_ci		tst_res(TFAIL, "system time not set to %ld (got: %ld)",
51f08c3bdfSopenharmony_ci			new_time, pres_time_tv.tv_sec);
52f08c3bdfSopenharmony_ci	}
53f08c3bdfSopenharmony_ci}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic void setup(void)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	stime_info();
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic struct tst_test test = {
61f08c3bdfSopenharmony_ci	.test_all = run,
62f08c3bdfSopenharmony_ci	.needs_root = 1,
63f08c3bdfSopenharmony_ci	.restore_wallclock = 1,
64f08c3bdfSopenharmony_ci	.setup = setup,
65f08c3bdfSopenharmony_ci	.test_variants = TEST_VARIANTS,
66f08c3bdfSopenharmony_ci};
67