Alexey Kodanev | e23db98 | 2013-11-28 16:17:14 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | * |
| 18 | * Author: Alexey Kodanev <alexey.kodanev@oracle.com> |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include "test.h" |
Alexey Kodanev | e23db98 | 2013-11-28 16:17:14 +0400 | [diff] [blame] | 25 | #include "tst_module.h" |
| 26 | #include "safe_macros.h" |
| 27 | #include "safe_stdio.h" |
| 28 | |
| 29 | #include "ltp_acpi.h" |
| 30 | |
| 31 | char *TCID = "ltp_acpi"; |
| 32 | int TST_TOTAL = ACPI_TC_NUM; |
| 33 | |
| 34 | static const char dev_result[] = "/sys/devices/" ACPI_TEST_NAME "/result"; |
| 35 | static const char dev_path[] = "/sys/devices/" ACPI_TEST_NAME "/path"; |
| 36 | static const char dev_str[] = "/sys/devices/" ACPI_TEST_NAME "/str"; |
| 37 | static const char dev_tcase[] = "/sys/devices/" ACPI_TEST_NAME "/tcase"; |
| 38 | static const char module_name[] = "ltp_acpi_cmds.ko"; |
| 39 | static int module_loaded; |
| 40 | |
| 41 | static void cleanup(void) |
| 42 | { |
| 43 | if (module_loaded) |
| 44 | tst_module_unload(NULL, module_name); |
Alexey Kodanev | e23db98 | 2013-11-28 16:17:14 +0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int read_sysfs_file(const char *name, char *buf, int size) |
| 48 | { |
| 49 | FILE *f = SAFE_FOPEN(cleanup, name, "r"); |
| 50 | char *res = fgets(buf, size, f); |
| 51 | SAFE_FCLOSE(cleanup, f); |
| 52 | return (res) ? 0 : 1; |
| 53 | } |
| 54 | |
| 55 | static int tc_acpi_str(void) |
| 56 | { |
| 57 | int res, ret = 0; |
| 58 | char descr[4096], sysfs_path[4096]; |
| 59 | |
| 60 | int not_kver_3_7 = tst_kvercmp(3, 7, 0) < 0; |
| 61 | |
| 62 | while (1) { |
| 63 | |
| 64 | SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", ACPI_TRAVERSE); |
| 65 | SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res); |
| 66 | if (res) |
| 67 | return TFAIL; |
| 68 | /* |
| 69 | * if device has _STR object, we should get |
| 70 | * a valid string from 'str' sysfs file and then can |
| 71 | * find it in sysfs. |
| 72 | */ |
| 73 | if (read_sysfs_file(dev_str, descr, 4096)) { |
| 74 | /* None of left devices has _STR */ |
| 75 | break; |
| 76 | } |
| 77 | tst_resm(TINFO, "read description %s", descr); |
| 78 | |
| 79 | /* device's sysfs path */ |
| 80 | strcpy(sysfs_path, "/sys"); |
| 81 | if (read_sysfs_file(dev_path, sysfs_path + 4, 4092)) { |
| 82 | /* |
| 83 | * Device doesn't have sysfs entry |
| 84 | * continue, because others might have it |
| 85 | */ |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Find device description in sysfs. |
| 91 | * |
| 92 | * New sysfs interface to export device description |
| 93 | * implemented since Linux 3.7 |
| 94 | */ |
| 95 | if (not_kver_3_7) { |
| 96 | tst_resm(TINFO, "sysfs _STR check required Linux 3.7+"); |
| 97 | ret = TCONF; |
| 98 | /* continue, we can still traverse ACPI devices */ |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | strcat(sysfs_path, "/description"); |
| 103 | if (access(sysfs_path, R_OK)) { |
| 104 | tst_resm(TINFO, "can't find description file '%s'", |
| 105 | sysfs_path); |
| 106 | return TFAIL; |
| 107 | } |
| 108 | tst_resm(TINFO, "found description file '%s'", sysfs_path); |
| 109 | |
| 110 | char sysfs_descr[4096]; |
| 111 | if (read_sysfs_file(sysfs_path, sysfs_descr, 4096)) |
| 112 | return TFAIL; |
| 113 | |
| 114 | /* |
| 115 | * Compare sysfs file and description from test driver |
| 116 | */ |
| 117 | int res = strncmp(descr, sysfs_descr, strlen(descr)); |
| 118 | |
| 119 | ret |= res ? TFAIL : TPASS; |
| 120 | } |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static void test_run(void) |
| 126 | { |
| 127 | int i, res; |
| 128 | |
| 129 | for (i = 0; i < TST_TOTAL; ++i) { |
| 130 | |
| 131 | if (i == ACPI_TRAVERSE) { |
| 132 | res = tc_acpi_str(); |
| 133 | } else { |
| 134 | SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i); |
| 135 | SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res); |
| 136 | res = res ? TFAIL : TPASS; |
| 137 | } |
| 138 | |
| 139 | tst_resm(res, "Test-case '%d'", i); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char *argv[]) |
| 144 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 145 | const char *msg; |
Alexey Kodanev | e23db98 | 2013-11-28 16:17:14 +0400 | [diff] [blame] | 146 | msg = parse_opts(argc, argv, NULL, NULL); |
| 147 | if (msg != NULL) |
| 148 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 149 | |
| 150 | tst_require_root(NULL); |
| 151 | |
| 152 | if (tst_kvercmp(2, 6, 0) < 0) { |
| 153 | tst_brkm(TCONF, NULL, |
| 154 | "Test must be run with kernel 2.6 or newer"); |
| 155 | } |
| 156 | |
| 157 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 158 | |
| 159 | tst_module_load(NULL, module_name, NULL); |
| 160 | module_loaded = 1; |
| 161 | |
| 162 | test_run(); |
| 163 | |
| 164 | cleanup(); |
| 165 | |
| 166 | tst_exit(); |
| 167 | } |