ioctl.c 3.9 KB
Newer Older
G
ganlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*
 * 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.
 */

#include <errno.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <resolv.h>
#include <arpa/nameser.h>
#include <poll.h>
#include <pthread.h>
#include "functionalext.h"

typedef void (*TEST_FUN)();
unsigned char TEST_IOCTL_ACCESS_TOKEN_ID = 'A';
unsigned int GET_TOKEN_ID = 1;
unsigned int SET_TOKEN_ID = 2;
const char device_tokenid[] = "/dev/access_token_id";
#define TEST_TOKENID_GET_TOKENID _IOR(TEST_IOCTL_ACCESS_TOKEN_ID, GET_TOKEN_ID, unsigned long long)
#define TEST_TOKENID_SET_TOKENID _IOW(TEST_IOCTL_ACCESS_TOKEN_ID, SET_TOKEN_ID, unsigned long long)
static unsigned long long GetRand64(void)
{
    unsigned long long randValue = 0;
    int randFd = open("/dev/random", O_RDONLY);
    if (randFd > 0) {
        read(randFd, &randValue, sizeof(unsigned long long));
    }
    close(randFd);
    return randValue;
}

/**
 * @tc.name      : ioctl_0100
 * @tc.desc      : Verify ioctl management of character device.
 * @tc.level     : Level 0
 */
void ioctl_0100(void)
{
    unsigned long long tokenGet;
    unsigned long long token = GetRand64();
    int fd = open(device_tokenid, O_RDWR);
    if (fd < 0) {
        printf("open %s failed\n", device_tokenid);
        EXPECT_TRUE(ioctl_0100, false);
        return;
    }
    int ret = ioctl(fd, TEST_TOKENID_GET_TOKENID, &token);
    EXPECT_EQ("ioctl_0100", 0, ret);
    ret = ioctl(fd, TEST_TOKENID_GET_TOKENID, &tokenGet);
    EXPECT_EQ("ioctl_0100", 0, ret);
    EXPECT_EQ("ioctl_0100", 0, ret);
}

/**
 * @tc.name      : ioctl_0200
 * @tc.desc      : Verify ioctl management of network device.
 * @tc.level     : Level 0
 */
void ioctl_0200(void)
{
    int sockFd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockFd < 0) {
        printf("socket failed\n");
        EXPECT_TRUE(ioctl_0200, false);
        close(sockFd);
        return;
    }
    EXPECT_NE("ioctl_0200", -1, sockFd);
    struct ifreq ifr[3];
    struct ifconf ifc = {0};
    ifc.ifc_len = 3 * sizeof(struct ifreq);
    ifc.ifc_buf = (char *)ifr;
    int ret = ioctl(sockFd, SIOCGIFCONF, &ifc);
    EXPECT_EQ("ioctl_0200", 0, ret);
    int ifrCount = ifc.ifc_len / sizeof(struct ifreq);
    for (int i = 0; i < ifrCount; i++) {
        printf("interface name: %s\n", ifr[i].ifr_name);
        printf("address: %s\n", inet_ntoa(((struct sockaddr_in *)&(ifr[i].ifr_addr))->sin_addr));
    }
    if (ifrCount == 0) {
        close(sockFd);
        return;
    }
    char interName[128];
    strcpy(interName, ifr[0].ifr_name);
    char interAddr[128];
    strcpy(interAddr, inet_ntoa(((struct sockaddr_in *)&(ifr[0].ifr_addr))->sin_addr));
    struct ifreq ifrAddr = {0};
    strcpy(ifrAddr.ifr_name, interName);
    ret = ioctl(sockFd, SIOCGIFADDR, &ifrAddr);
    EXPECT_EQ("ioctl_0200", 0, ret);
    struct sockaddr_in *srvAddr = (struct sockaddr_in *)&ifrAddr.ifr_addr;
    EXPECT_STREQ("ioctl_0200", interAddr, inet_ntoa(srvAddr->sin_addr));
    close(sockFd);
}

TEST_FUN G_Fun_Array[] = {
    ioctl_0100,
    ioctl_0200,
};

int main()
{
    int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
    for (int pos = 0; pos < num; ++pos) {
        G_Fun_Array[pos]();
    }

    return t_status;
}