1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Linaro Limited. All rights reserved. 4f08c3bdfSopenharmony_ci * Author: Rafael David Tinoco <rafael.tinoco@linaro.org> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci * Basic test for clock_settime(2) on REALTIME clock: 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * 1) advance DELTA_SEC seconds 11f08c3bdfSopenharmony_ci * 2) go backwards DELTA_SEC seconds 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * Restore wall clock at the end of test. 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "config.h" 17f08c3bdfSopenharmony_ci#include "time64_variants.h" 18f08c3bdfSopenharmony_ci#include "tst_timer.h" 19f08c3bdfSopenharmony_ci#include "tst_safe_clocks.h" 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#define DELTA_SEC 10 22f08c3bdfSopenharmony_ci#define DELTA_US (long long) (DELTA_SEC * 1000000) 23f08c3bdfSopenharmony_ci#define DELTA_EPS (long long) (DELTA_US * 0.1) 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic struct tst_ts *begin, *change, *end; 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic struct time64_variants variants[] = { 28f08c3bdfSopenharmony_ci { .clock_gettime = libc_clock_gettime, .clock_settime = libc_clock_settime, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"}, 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci#if (__NR_clock_settime != __LTP__NR_INVALID_SYSCALL) 31f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime, .clock_settime = sys_clock_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"}, 32f08c3bdfSopenharmony_ci#endif 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci#if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL) 35f08c3bdfSopenharmony_ci { .clock_gettime = sys_clock_gettime64, .clock_settime = sys_clock_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"}, 36f08c3bdfSopenharmony_ci#endif 37f08c3bdfSopenharmony_ci}; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistatic void setup(void) 40f08c3bdfSopenharmony_ci{ 41f08c3bdfSopenharmony_ci begin->type = change->type = end->type = variants[tst_variant].ts_type; 42f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc); 43f08c3bdfSopenharmony_ci} 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void do_clock_gettime(struct time64_variants *tv, struct tst_ts *ts) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci int ret; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci ret = tv->clock_gettime(CLOCK_REALTIME, tst_ts_get(ts)); 50f08c3bdfSopenharmony_ci if (ret == -1) 51f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_REALTIME) failed"); 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void verify_clock_settime(void) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci struct time64_variants *tv = &variants[tst_variant]; 57f08c3bdfSopenharmony_ci long long elapsed; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci /* test 01: move forward */ 60f08c3bdfSopenharmony_ci do_clock_gettime(tv, begin); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci *change = tst_ts_add_us(*begin, DELTA_US); 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci TEST(tv->clock_settime(CLOCK_REALTIME, tst_ts_get(change))); 65f08c3bdfSopenharmony_ci if (TST_RET == -1) { 66f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s", 67f08c3bdfSopenharmony_ci tst_clock_name(CLOCK_REALTIME)); 68f08c3bdfSopenharmony_ci return; 69f08c3bdfSopenharmony_ci } 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci do_clock_gettime(tv, end); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci elapsed = tst_ts_diff_us(*end, *begin); 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci if (elapsed >= DELTA_US && elapsed < (DELTA_US + DELTA_EPS)) 76f08c3bdfSopenharmony_ci tst_res(TPASS, "clock_settime(2): was able to advance time"); 77f08c3bdfSopenharmony_ci else 78f08c3bdfSopenharmony_ci tst_res(TFAIL, "clock_settime(2): could not advance time"); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci /* test 02: move backward */ 81f08c3bdfSopenharmony_ci do_clock_gettime(tv, begin); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci *change = tst_ts_sub_us(*begin, DELTA_US); 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci TEST(tv->clock_settime(CLOCK_REALTIME, tst_ts_get(change))); 86f08c3bdfSopenharmony_ci if (TST_RET == -1) { 87f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s", 88f08c3bdfSopenharmony_ci tst_clock_name(CLOCK_REALTIME)); 89f08c3bdfSopenharmony_ci return; 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci do_clock_gettime(tv, end); 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci elapsed = tst_ts_diff_us(*end, *begin); 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci if (~(elapsed) <= DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS)) 97f08c3bdfSopenharmony_ci tst_res(TPASS, "clock_settime(2): was able to recede time"); 98f08c3bdfSopenharmony_ci else 99f08c3bdfSopenharmony_ci tst_res(TFAIL, "clock_settime(2): could not recede time"); 100f08c3bdfSopenharmony_ci} 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_cistatic struct tst_test test = { 103f08c3bdfSopenharmony_ci .test_all = verify_clock_settime, 104f08c3bdfSopenharmony_ci .test_variants = ARRAY_SIZE(variants), 105f08c3bdfSopenharmony_ci .setup = setup, 106f08c3bdfSopenharmony_ci .needs_root = 1, 107f08c3bdfSopenharmony_ci .restore_wallclock = 1, 108f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 109f08c3bdfSopenharmony_ci {&begin, .size = sizeof(*begin)}, 110f08c3bdfSopenharmony_ci {&change, .size = sizeof(*change)}, 111f08c3bdfSopenharmony_ci {&end, .size = sizeof(*end)}, 112f08c3bdfSopenharmony_ci {}, 113f08c3bdfSopenharmony_ci } 114f08c3bdfSopenharmony_ci}; 115