LCOV - code coverage report
Current view: top level - librpc/tests - test_ndr_dns_nbt.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 115 119 96.6 %
Date: 2024-02-28 12:06:22 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Tests for librpc ndr functions
       3             :  *
       4             :  * Copyright (C) Catalyst.NET Ltd 2020
       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 3 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             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  *
      19             :  */
      20             : 
      21             : #include "replace.h"
      22             : #include <setjmp.h>
      23             : #include <cmocka.h>
      24             : 
      25             : #include "includes.h"
      26             : #include "librpc/ndr/libndr.h"
      27             : #include "librpc/gen_ndr/ndr_dns.h"
      28             : #include "librpc/gen_ndr/ndr_nbt.h"
      29             : #include "lib/util/time.h"
      30             : 
      31             : #define NBT_NAME "EOGFGLGPCACACACACACACACACACACACA" /* "neko" */
      32             : 
      33             : 
      34           4 : static DATA_BLOB generate_obnoxious_dns_name(TALLOC_CTX *mem_ctx,
      35             :                                              size_t n_labels,
      36             :                                              size_t dot_every,
      37             :                                              bool is_nbt)
      38             : {
      39           4 :         size_t i, j;
      40           4 :         char *s;
      41           4 :         DATA_BLOB name = data_blob_talloc(mem_ctx, NULL, 64 * n_labels + 1);
      42           4 :         assert_non_null(name.data);
      43             : 
      44           4 :         s = (char*)name.data;
      45           4 :         if (is_nbt) {
      46           2 :                 size_t len = strlen(NBT_NAME);
      47           2 :                 *s = len;
      48           2 :                 s++;
      49           2 :                 memcpy(s, NBT_NAME, len);
      50           2 :                 s += len;
      51           2 :                 n_labels--;
      52             :         }
      53             : 
      54         510 :         for (i = 0; i < n_labels; i++) {
      55         506 :                 *s = 63;
      56         506 :                 s++;
      57       32384 :                 for (j = 0; j < 63; j++) {
      58       31878 :                         if (j % dot_every == (dot_every - 1)) {
      59       23782 :                                 *s = '.';
      60             :                         } else {
      61        8096 :                                 *s = 'x';
      62             :                         }
      63       31878 :                         s++;
      64             :                 }
      65             :         }
      66           4 :         *s = 0;
      67           4 :         s++;
      68           4 :         name.length = s - (char*)name.data;
      69           4 :         return name;
      70             : }
      71             : 
      72             : 
      73           4 : static char *_test_ndr_pull_dns_string_list(TALLOC_CTX *mem_ctx,
      74             :                                             size_t n_labels,
      75             :                                             size_t dot_every,
      76             :                                             bool is_nbt)
      77             : {
      78           4 :         enum ndr_err_code ndr_err;
      79           4 :         DATA_BLOB blob = generate_obnoxious_dns_name(mem_ctx,
      80             :                                                      n_labels,
      81             :                                                      dot_every,
      82             :                                                      is_nbt);
      83             : 
      84           4 :         char *name;
      85           4 :         ndr_pull_flags_fn_t fn;
      86             : 
      87           4 :         if (is_nbt) {
      88             :                 fn = (ndr_pull_flags_fn_t)ndr_pull_nbt_string;
      89             :         } else {
      90           2 :                 fn = (ndr_pull_flags_fn_t)ndr_pull_dns_string;
      91             :         }
      92             : 
      93           4 :         ndr_err = ndr_pull_struct_blob(&blob,
      94             :                                        mem_ctx,
      95             :                                        &name,
      96             :                                        fn);
      97             :         /* Success here is not expected, but we let it go to measure timing. */
      98           4 :         if (ndr_err == NDR_ERR_SUCCESS) {
      99           4 :                 printf("pull succeed\n");
     100             :         } else {
     101           0 :                 assert_int_equal(ndr_err, NDR_ERR_STRING);
     102             :         }
     103             : 
     104           4 :         TALLOC_FREE(blob.data);
     105           4 :         return name;
     106             : }
     107             : 
     108             : 
     109           4 : static void _test_ndr_push_dns_string_list(TALLOC_CTX *mem_ctx,
     110             :                                            char *name,
     111             :                                            bool is_nbt)
     112             : {
     113           4 :         DATA_BLOB blob;
     114           4 :         enum ndr_err_code ndr_err;
     115           4 :         ndr_push_flags_fn_t fn;
     116             : 
     117           4 :         if (is_nbt) {
     118             :                 fn = (ndr_push_flags_fn_t)ndr_push_nbt_string;
     119             :         } else {
     120           2 :                 fn = (ndr_push_flags_fn_t)ndr_push_dns_string;
     121             :         }
     122             : 
     123           4 :         ndr_err = ndr_push_struct_blob(&blob,
     124             :                                        mem_ctx,
     125             :                                        name,
     126             :                                        fn);
     127             : 
     128             :         /* Success here is not expected, but we let it go to measure timing. */
     129           4 :         if (ndr_err == NDR_ERR_SUCCESS) {
     130           0 :                 printf("push succeed\n");
     131             :         } else {
     132           4 :                 assert_int_equal(ndr_err, NDR_ERR_STRING);
     133             :         }
     134           4 : }
     135             : 
     136             : 
     137           8 : static uint64_t elapsed_time(struct timespec start, const char *print)
     138             : {
     139           8 :         struct timespec end;
     140           8 :         unsigned long long microsecs;
     141           8 :         clock_gettime_mono(&end);
     142           8 :         end.tv_sec -= start.tv_sec;
     143           8 :         if (end.tv_nsec < start.tv_nsec) {
     144             :                 /* we need to borrow */
     145           0 :                 end.tv_nsec += 1000 * 1000 * 1000;
     146           0 :                 end.tv_sec -= 1;
     147             :         }
     148           8 :         end.tv_nsec -= start.tv_nsec;
     149           8 :         microsecs = end.tv_sec * 1000000;
     150           8 :         microsecs += end.tv_nsec / 1000;
     151             : 
     152           8 :         if (print != NULL) {
     153           8 :                 printf(" %s: %llu microseconds\n", print, microsecs);
     154             :         }
     155           8 :         return microsecs;
     156             : }
     157             : 
     158             : 
     159           1 : static void test_ndr_dns_string_half_dots(void **state)
     160             : {
     161           1 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
     162           1 :         char *name;
     163           1 :         struct timespec start;
     164           1 :         uint64_t elapsed;
     165             : 
     166           1 :         clock_gettime_mono(&start);
     167           1 :         name =_test_ndr_pull_dns_string_list(mem_ctx, 127, 2, false);
     168           1 :         elapsed_time(start, "pull");
     169           1 :         _test_ndr_push_dns_string_list(mem_ctx, name, false);
     170           1 :         elapsed = elapsed_time(start, "total");
     171           1 :         assert_in_range(elapsed, 0, 200000);
     172           1 :         talloc_free(mem_ctx);
     173           1 : }
     174             : 
     175           1 : static void test_ndr_nbt_string_half_dots(void **state)
     176             : {
     177           1 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
     178           1 :         char *name;
     179           1 :         struct timespec start;
     180           1 :         uint64_t elapsed;
     181             : 
     182           1 :         clock_gettime_mono(&start);
     183           1 :         name =_test_ndr_pull_dns_string_list(mem_ctx, 127, 2, true);
     184           1 :         elapsed_time(start, "pull");
     185           1 :         _test_ndr_push_dns_string_list(mem_ctx, name, true);
     186           1 :         elapsed = elapsed_time(start, "total");
     187           1 :         assert_in_range(elapsed, 0, 200000);
     188           1 :         talloc_free(mem_ctx);
     189           1 : }
     190             : 
     191           1 : static void test_ndr_dns_string_all_dots(void **state)
     192             : {
     193           1 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
     194           1 :         char *name;
     195           1 :         struct timespec start;
     196           1 :         uint64_t elapsed;
     197             : 
     198           1 :         clock_gettime_mono(&start);
     199           1 :         name =_test_ndr_pull_dns_string_list(mem_ctx, 127, 1, false);
     200           1 :         elapsed_time(start, "pull");
     201           1 :         _test_ndr_push_dns_string_list(mem_ctx, name, false);
     202           1 :         elapsed = elapsed_time(start, "total");
     203           1 :         assert_in_range(elapsed, 0, 200000);
     204           1 :         talloc_free(mem_ctx);
     205           1 : }
     206             : 
     207           1 : static void test_ndr_nbt_string_all_dots(void **state)
     208             : {
     209           1 :         TALLOC_CTX *mem_ctx = talloc_new(NULL);
     210           1 :         char *name;
     211           1 :         struct timespec start;
     212           1 :         uint64_t elapsed;
     213             : 
     214           1 :         clock_gettime_mono(&start);
     215           1 :         name =_test_ndr_pull_dns_string_list(mem_ctx, 127, 1, true);
     216           1 :         elapsed_time(start, "pull");
     217           1 :         _test_ndr_push_dns_string_list(mem_ctx, name, true);
     218           1 :         elapsed = elapsed_time(start, "total");
     219           1 :         assert_in_range(elapsed, 0, 200000);
     220           1 :         talloc_free(mem_ctx);
     221           1 : }
     222             : 
     223             : 
     224             : 
     225           1 : int main(int argc, const char **argv)
     226             : {
     227           1 :         const struct CMUnitTest tests[] = {
     228             :                 cmocka_unit_test(test_ndr_nbt_string_half_dots),
     229             :                 cmocka_unit_test(test_ndr_dns_string_half_dots),
     230             :                 cmocka_unit_test(test_ndr_nbt_string_all_dots),
     231             :                 cmocka_unit_test(test_ndr_dns_string_all_dots),
     232             :         };
     233             : 
     234           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     235           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     236             : }

Generated by: LCOV version 1.14