|
| 1 | +/* driver_methods_utest.c - NUT driver code test tool |
| 2 | + * |
| 3 | + * Copyright (C) |
| 4 | + * 2025 Jim Klimov <[email protected]> |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +#include "config.h" |
| 24 | +#include "main.h" |
| 25 | +#include "dstate.h" |
| 26 | +#include "attribute.h" |
| 27 | +#include "nut_stdint.h" |
| 28 | + |
| 29 | +/* driver version */ |
| 30 | +#define DRIVER_NAME "Mock driver for unit tests" |
| 31 | +#define DRIVER_VERSION "0.01" |
| 32 | + |
| 33 | +/* driver description structure */ |
| 34 | +upsdrv_info_t upsdrv_info = { |
| 35 | + DRIVER_NAME, |
| 36 | + DRIVER_VERSION, |
| 37 | + |
| 38 | + DRV_EXPERIMENTAL, |
| 39 | + { NULL } |
| 40 | +}; |
| 41 | + |
| 42 | +static int cases_passed = 0; |
| 43 | +static int cases_failed = 0; |
| 44 | + |
| 45 | +static char * pass_fail[2] = {"pass", "fail"}; |
| 46 | + |
| 47 | +void upsdrv_cleanup(void) {} |
| 48 | +void upsdrv_shutdown(void) {} |
| 49 | + |
| 50 | +static void report_pass(void) { |
| 51 | + printf("%s", pass_fail[0]); |
| 52 | + cases_passed++; |
| 53 | +} |
| 54 | + |
| 55 | +static void report_fail(void) { |
| 56 | + printf("%s", pass_fail[1]); |
| 57 | + cases_failed++; |
| 58 | +} |
| 59 | + |
| 60 | +static int report_0_means_pass(int i) { |
| 61 | + if (i == 0) { |
| 62 | + report_pass(); |
| 63 | + } else { |
| 64 | + report_fail(); |
| 65 | + } |
| 66 | + return i; |
| 67 | +} |
| 68 | +int main(int argc, char **argv) { |
| 69 | + const char *valueStr = NULL; |
| 70 | + |
| 71 | + NUT_UNUSED_VARIABLE(argc); |
| 72 | + NUT_UNUSED_VARIABLE(argv); |
| 73 | + |
| 74 | + cases_passed = 0; |
| 75 | + cases_failed = 0; |
| 76 | + |
| 77 | + /* test case #1 */ |
| 78 | + status_init(); |
| 79 | + nut_debug_level = 6; |
| 80 | + status_set(" OL "); |
| 81 | + status_set("OL BOOST"); |
| 82 | + status_set("OB "); |
| 83 | + status_set(" BOOST"); |
| 84 | + status_commit(); |
| 85 | + valueStr = dstate_getinfo("ups.status"); |
| 86 | + nut_debug_level = 0; |
| 87 | + report_0_means_pass(strcmp(valueStr, "OL BOOST OB")); |
| 88 | + printf(" test for ups.status: '%s'; any duplicates?\n", NUT_STRARG(valueStr)); |
| 89 | + |
| 90 | + /* test case #2, build on top of #1 */ |
| 91 | + alarm_init(); |
| 92 | + alarm_set("Test alarm 1"); |
| 93 | + alarm_set("Test alarm 2"); |
| 94 | + alarm_set("Test alarm 1"); |
| 95 | + alarm_commit(); |
| 96 | + /* Note: normally we re-init and re-set the values */ |
| 97 | + status_commit(); |
| 98 | + valueStr = dstate_getinfo("ups.status"); |
| 99 | + report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB")); |
| 100 | + printf(" test for ups.status: '%s'; got alarm?\n", NUT_STRARG(valueStr)); |
| 101 | + |
| 102 | + /* test case #3, build on top of #2 */ |
| 103 | + valueStr = dstate_getinfo("ups.alarm"); |
| 104 | + /* NOTE: no dedup here! */ |
| 105 | + report_0_means_pass(strcmp(valueStr, "Test alarm 1 Test alarm 2 Test alarm 1")); |
| 106 | + printf(" test for ups.alarm: '%s'; got 3 alarms?\n", NUT_STRARG(valueStr)); |
| 107 | + |
| 108 | + /* test case #4, build on top of #1 and #2 */ |
| 109 | + /* Note: normally we re-init and re-set the values */ |
| 110 | + status_set("BOO"); |
| 111 | + status_set("BOO"); |
| 112 | + status_set("OST"); |
| 113 | + status_set("OST"); |
| 114 | + status_set("OOS"); |
| 115 | + status_set("OOS"); |
| 116 | + status_commit(); |
| 117 | + valueStr = dstate_getinfo("ups.status"); |
| 118 | + nut_debug_level = 0; |
| 119 | + report_0_means_pass(strcmp(valueStr, "ALARM OL BOOST OB BOO OST OOS")); |
| 120 | + printf(" test for ups.status: '%s'; any duplicates?\n", NUT_STRARG(valueStr)); |
| 121 | + |
| 122 | + /* finish */ |
| 123 | + printf("test_rules completed. Total cases %d, passed %d, failed %d\n", |
| 124 | + cases_passed+cases_failed, cases_passed, cases_failed); |
| 125 | + |
| 126 | + dstate_free(); |
| 127 | + upsdrv_cleanup(); |
| 128 | + |
| 129 | + /* Return 0 (exit-code OK, boolean false) if no tests failed and some ran */ |
| 130 | + if ( (cases_failed == 0) && (cases_passed > 0) ) |
| 131 | + return 0; |
| 132 | + |
| 133 | + return 1; |
| 134 | +} |
0 commit comments