Home | History | Annotate | Line # | Download | only in test
      1 /* test-api.c
      2  *
      3  * Copyright (c) 2023-2024 Apple Inc. All rights reserved.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     https://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  * srp host API test harness
     18  */
     19 
     20 #include <sys/types.h>
     21 #include <sys/socket.h>
     22 #include <ifaddrs.h>
     23 
     24 typedef struct test_state test_state_t;
     25 typedef struct io_context io_context_t;
     26 typedef struct dns_service_event dns_service_event_t;
     27 typedef struct test_packet_state test_packet_state_t;
     28 
     29 extern ready_callback_t NULLABLE srp_test_dnssd_tls_listener_ready;
     30 extern void *NULLABLE srp_test_tls_listener_context;
     31 extern void (*NULLABLE srp_test_dso_message_finished)(void *NULLABLE context, message_t *NONNULL message,
     32                                                       dso_state_t *NONNULL dso);
     33 typedef bool (*dns_service_query_record_callback_intercept_t)(DNSServiceRef NONNULL sdRef, DNSServiceFlags flags,
     34                                                               uint32_t interfaceIndex, DNSServiceErrorType errorCode,
     35                                                               const char *NULLABLE fullname, uint16_t rrtype,
     36                                                               uint16_t rrclass, uint16_t rdlen,
     37                                                               const void *NULLABLE rdata, uint32_t ttl,
     38                                                               void *NULLABLE context);
     39 
     40 struct test_state {
     41     test_state_t *NULLABLE next, *NULLABLE finished_tests;
     42     srp_server_t *NULLABLE primary;
     43     comm_t *NULLABLE srp_listener;
     44     io_context_t *NULLABLE current_io_context;
     45     test_packet_state_t *NULLABLE test_packet_state;
     46     dns_service_event_t *NULLABLE dns_service_events;
     47     void *NULLABLE context;
     48     int counter;
     49     const char *NONNULL title;
     50     const char *NULLABLE variant_title;
     51     const char *NONNULL explanation;
     52     const char *NULLABLE variant_info;
     53     void (*NULLABLE continue_testing)(test_state_t *NONNULL next_state);
     54     bool (*NULLABLE dnssd_proxy_configurer)(void);
     55     int (*NULLABLE getifaddrs)(srp_server_t *NULLABLE server_state, struct ifaddrs *NULLABLE *NONNULL ifaddrs,
     56                                void *NULLABLE context);
     57     void (*NULLABLE freeifaddrs)(srp_server_t *NULLABLE server_state, struct ifaddrs *NONNULL ifaddrs,
     58                                  void *NULLABLE context);
     59     DNSServiceErrorType (*NULLABLE query_record_intercept)(test_state_t *NONNULL state,
     60                                                            DNSServiceRef NONNULL *NULLABLE sdRef, DNSServiceFlags flags,
     61                                                            uint32_t interfaceIndex, const char *NONNULL fullname,
     62                                                            uint16_t rrtype, uint16_t rrclass,
     63                                                            DNSServiceAttribute const *NULLABLE attr,
     64                                                            DNSServiceQueryRecordReply NONNULL callBack,
     65                                                            void *NULLABLE context);
     66     dns_service_query_record_callback_intercept_t NULLABLE dns_service_query_callback_intercept;
     67     int variant;
     68     bool test_complete;
     69 };
     70 
     71 #define TEST_FAIL(test_state, message)                          \
     72     do {                                                        \
     73         srp_test_state_explain(test_state);                     \
     74         fprintf(stderr, "test failed: " message "\n\n");        \
     75         exit(1);                                                \
     76     } while (0)
     77 
     78 #define TEST_FAIL_CHECK(test_state, success_condition, message) \
     79     do {                                                        \
     80         if (!(success_condition)) {                             \
     81             TEST_FAIL(test_state, message);                     \
     82         }                                                       \
     83     } while (0)
     84 
     85 #define TEST_FAIL_STATUS(test_state, message, status)               \
     86     do {                                                            \
     87            srp_test_state_explain(test_state);                      \
     88            fprintf(stderr, "test failed: " message "\n\n", status); \
     89            exit(1);                                                 \
     90     } while (0)
     91 
     92 #define TEST_FAIL_CHECK_STATUS(test_state, success_condition, message, status) \
     93     do {                                                                       \
     94        if (!(success_condition)) {                                             \
     95            TEST_FAIL_STATUS(test_state, message, status);                      \
     96        }                                                                       \
     97     } while (0)
     98 
     99 #define TEST_PASSED(test_state)         \
    100     srp_test_state_explain(test_state); \
    101     INFO("test passed\n");              \
    102     srp_test_state_next(test_state);
    103 
    104 void srp_test_set_local_example_address(test_state_t *NONNULL state);
    105 void srp_test_network_localhost_start(test_state_t *NONNULL state);
    106 test_state_t *NULLABLE test_state_create(srp_server_t *NONNULL primary,
    107                                          const char *NONNULL title, const char *NULLABLE variant_title,
    108                                          const char *NONNULL explanation, const char *NULLABLE variant_name);
    109 void test_state_add_srp_server(test_state_t *NONNULL state, srp_server_t *NONNULL server);
    110 void srp_test_state_explain(test_state_t *NULLABLE state);
    111 void srp_test_state_next(test_state_t *NONNULL state);
    112 void srp_test_state_add_timeout(test_state_t *NONNULL state, int timeout);
    113 struct ifaddrs;
    114 int srp_test_getifaddrs(srp_server_t *NULLABLE server_state, struct ifaddrs *NULLABLE *NONNULL ifaddrs, void *NULLABLE context);
    115 void srp_test_freeifaddrs(srp_server_t *NULLABLE server_state, struct ifaddrs *NONNULL ifaddrs, void *NULLABLE context);
    116 void srp_test_enable_stub_router(test_state_t *NONNULL state, srp_server_t *NONNULL server_state);
    117 
    118 // Local Variables:
    119 // mode: C
    120 // tab-width: 4
    121 // c-file-style: "bsd"
    122 // c-basic-offset: 4
    123 // fill-column: 108
    124 // indent-tabs-mode: nil
    125 // End:
    126