]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add test for valid word list
authorBastian Dehn <hhaalo@arcor.de>
Sat, 25 Oct 2025 17:36:36 +0000 (19:36 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 25 Oct 2025 17:36:36 +0000 (19:36 +0200)
src/kds_s2000w_handler_opts.c
tests/kds_s2000w_net_set_opt_tests.c

index a6ffc7b702768b4c8e518ea7fdf956c857835a52..475d31876f0bfb9e115e49853bf091add1966545 100644 (file)
@@ -137,6 +137,14 @@ json_object* _kds_s2000w_handler_opts_load_config(handler_t* h)
 
 bool _kds_s2000w_opts_validate(SANE_Option_Descriptor* descriptor, const void* value)
 {
+       if (descriptor->constraint_type == SANE_CONSTRAINT_WORD_LIST) {
+               const int32_t* int_value = (const int32_t*) value;
+               for (int32_t i = 1; i < descriptor->constraint.word_list[0]; i++) {
+                       if (*int_value == descriptor->constraint.word_list[i])
+                               return true;
+               }
+       }
+
        if (descriptor->constraint_type == SANE_CONSTRAINT_RANGE) {
                const int32_t* int_value = (const int32_t*) value;
                bool valid = *int_value >= descriptor->constraint.range->min;
index a8e97fef28575dc9bf0e61911ecd74a4f53551b5..02367f2e6933b676abbb9a5ed5f26410c4788de8 100644 (file)
@@ -166,6 +166,45 @@ void kds_s2000w_net_set_option_int(void** state)
        set_value = NULL;
 }
 
+void kds_s2000w_net_set_option_int_word_list(void** state)
+{
+       int32_t* set_value = malloc(sizeof(int32_t));
+       if (set_value == NULL)
+               return;
+
+       *set_value = 300;
+       response_t* resp = (response_t*) *state;
+       handler_t* h = kds_s2000w_handler_init();
+       response_t* set_opt_resp = kds_s2000w_client_response_init();
+       set_opt_resp->code = 200;
+
+       will_return(mock_response, resp);
+       will_return(__wrap_kds_s2000w_client_get_option, 0);
+       will_return(mock_response, set_opt_resp);
+       will_return(__wrap_kds_s2000w_client_set_option, 0);
+       will_return(mock_response, set_opt_resp);
+       will_return(__wrap_kds_s2000w_client_set_option, 0);
+       expect_function_call(__wrap_kds_s2000w_client_get_option);
+       expect_function_call(__wrap_kds_s2000w_client_set_option);
+       expect_function_call(__wrap_kds_s2000w_client_set_option);
+
+       SANE_Status status = sane_kds_s2000w_net_control_option(h, 4, SANE_ACTION_SET_VALUE, set_value, NULL);
+
+       json_object* config = json_object_object_get(h->scanner_config, "Configuration");
+       json_object* value_object = json_object_object_get(config, DPI);
+       int32_t value = json_object_get_int(value_object);
+
+       assert_int_equal(status, SANE_STATUS_GOOD);
+       assert_int_equal(value, 300);
+
+       kds_s2000w_client_response_free(set_opt_resp);
+       set_opt_resp = NULL;
+       kds_s2000w_handler_free(h);
+       h = NULL;
+       free(set_value);
+       set_value = NULL;
+}
+
 void kds_s2000w_net_set_invalid_option_string(void** state)
 {
        int32_t* info = malloc(sizeof(int32_t));
@@ -202,6 +241,7 @@ int main()
        const struct CMUnitTest net_tests[] = {
                cmocka_unit_test_setup_teardown(kds_s2000w_net_set_option_string, setup_default_option, teardown_default_option),
                cmocka_unit_test_setup_teardown(kds_s2000w_net_set_option_int, setup_default_option, teardown_default_option),
+               cmocka_unit_test_setup_teardown(kds_s2000w_net_set_option_int_word_list, setup_default_option, teardown_default_option),
                cmocka_unit_test_setup_teardown(kds_s2000w_net_set_invalid_option_string, setup_default_option, teardown_default_option)
        };