clock_adjtime.c 3.4 KB
Newer Older
G
ganlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

G
ganlan 已提交
16 17
#include <errno.h>
#include <signal.h>
G
ganlan 已提交
18 19 20 21 22
#include <stdlib.h>
#include <sys/timex.h>
#include <time.h>
#include "functionalext.h"

G
ganlan 已提交
23 24 25 26
void exception_handler(int sig)
{
    exit(t_status);
}
G
ganlan 已提交
27

28 29
extern int __clock_adjtime64(clockid_t, struct timex *);

G
ganlan 已提交
30 31 32 33 34 35 36
/**
 * @tc.name      : clock_adjtime_0100
 * @tc.desc      : Verify that the kernel time can be adjusted (all parameters are valid, clock_id is CLOCK_REALTIME)
 * @tc.level     : Level 0
 */
void clock_adjtime_0100(void)
{
G
ganlan 已提交
37 38 39 40 41
    struct timex tx;
    memset(&tx, 0, sizeof(tx));

    int result = clock_adjtime(CLOCK_REALTIME, &tx);
    EXPECT_NE("clock_adjtime_0100", result, -1);
G
ganlan 已提交
42 43 44 45 46 47 48 49 50 51 52
}

/**
 * @tc.name      : clock_adjtime_0200
 * @tc.desc      : Verify that the kernel time can not be adjusted (each parameter is valid, clock_id is
 *                 CLOCK_MONOTONIC)
 * @tc.level     : Level 2
 */
void clock_adjtime_0200(void)
{
    struct timex tx = {ADJ_OFFSET_SS_READ};
G
ganlan 已提交
53 54 55

    int result = clock_adjtime(CLOCK_MONOTONIC, &tx);
    EXPECT_EQ("clock_adjtime_0200", result, -1);
G
ganlan 已提交
56 57 58 59 60 61 62 63 64 65
}

/**
 * @tc.name      : clock_adjtime_0300
 * @tc.desc      : Verify that the kernel time can not be adjusted (all parameters are valid, clock_id is
 *                 CLOCK_PROCESS_CPUTIME_ID)
 * @tc.level     : Level 2
 */
void clock_adjtime_0300(void)
{
G
ganlan 已提交
66 67 68 69 70
    struct timex tx;
    memset(&tx, 0, sizeof(tx));

    int result = clock_adjtime(CLOCK_PROCESS_CPUTIME_ID, &tx);
    EXPECT_EQ("clock_adjtime_0300", result, -1);
G
ganlan 已提交
71 72 73 74 75 76 77 78 79 80 81
}

/**
 * @tc.name      : clock_adjtime_0400
 * @tc.desc      : Verify that the kernel time can not be adjusted (all parameters are valid, clock_id is
 *                 CLOCK_THREAD_CPUTIME_ID)
 * @tc.level     : Level 2
 */
void clock_adjtime_0400(void)
{
    struct timex tx = {0};
G
ganlan 已提交
82 83 84 85
    memset(&tx, 0, sizeof(tx));

    int result = clock_adjtime(CLOCK_THREAD_CPUTIME_ID, &tx);
    EXPECT_EQ("clock_adjtime_0400", result, -1);
G
ganlan 已提交
86 87 88 89 90 91 92 93 94
}

/**
 * @tc.name      : clock_adjtime_0500
 * @tc.desc      : Verify that kernel time cannot be adjusted (tx parameter invalid)
 * @tc.level     : Level 2
 */
void clock_adjtime_0500(void)
{
G
ganlan 已提交
95 96 97 98 99 100
    signal(SIGSEGV, exception_handler);

    errno = 0;
    int result = clock_adjtime(CLOCK_REALTIME, NULL);
    EXPECT_EQ("clock_adjtime_0500", result, -1);
    EXPECT_EQ("clock_adjtime_0500", errno, EFAULT);
G
ganlan 已提交
101 102
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116
/**
 * @tc.name      : clock_adjtime64_0100
 * @tc.desc      : Verify that the kernel time can be adjusted (all parameters are valid, clock_id is CLOCK_REALTIME)
 * @tc.level     : Level 0
 */
void clock_adjtime64_0100(void)
{
    struct timex tx;
    memset(&tx, 0, sizeof(tx));

    int result = __clock_adjtime64(CLOCK_REALTIME, &tx);
    EXPECT_NE("clock_adjtime64_0100", result, -1);
}

G
ganlan 已提交
117
int main(int argc, char *argv[])
G
ganlan 已提交
118 119 120 121 122 123
{
    clock_adjtime_0100();
    clock_adjtime_0200();
    clock_adjtime_0300();
    clock_adjtime_0400();
    clock_adjtime_0500();
124
    clock_adjtime64_0100();
G
ganlan 已提交
125 126
    return t_status;
}