LCOV - code coverage report
Current view: top level - test/fuzz - fuzz_diff.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 37 91.9 %
Date: 2021-11-24 03:28:48 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2016-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information */
       3             : 
       4             : #define CONSDIFF_PRIVATE
       5             : 
       6             : #include "orconfig.h"
       7             : #include "core/or/or.h"
       8             : #include "feature/dircommon/consdiff.h"
       9             : 
      10             : #include "test/fuzz/fuzzing.h"
      11             : 
      12             : static int
      13        2744 : mock_consensus_compute_digest_(const char *c, size_t len,
      14             :                                consensus_digest_t *d)
      15             : {
      16        2744 :   (void)c;
      17        2744 :   (void)len;
      18        2744 :   memset(d->sha3_256, 3, sizeof(d->sha3_256));
      19        2744 :   return 0;
      20             : }
      21             : 
      22             : int
      23         987 : fuzz_init(void)
      24             : {
      25         987 :   MOCK(consensus_compute_digest, mock_consensus_compute_digest_);
      26         987 :   MOCK(consensus_compute_digest_as_signed, mock_consensus_compute_digest_);
      27         987 :   return 0;
      28             : }
      29             : 
      30             : int
      31         987 : fuzz_cleanup(void)
      32             : {
      33         987 :   UNMOCK(consensus_compute_digest);
      34         987 :   UNMOCK(consensus_compute_digest_as_signed);
      35         987 :   return 0;
      36             : }
      37             : 
      38             : int
      39         987 : fuzz_main(const uint8_t *stdin_buf, size_t data_size)
      40             : {
      41             : #define SEP "=====\n"
      42             : #define SEPLEN strlen(SEP)
      43         987 :   const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
      44         987 :   if (! separator)
      45             :     return 0;
      46         936 :   size_t c1_len = separator - stdin_buf;
      47         936 :   const char *c1 = (const char *)stdin_buf;
      48         936 :   size_t c2_len = data_size - c1_len - SEPLEN;
      49         936 :   const char *c2 = (const char *)separator + SEPLEN;
      50             : 
      51         936 :   const char *cp = memchr(c1, 0, c1_len);
      52         936 :   if (cp)
      53          67 :     c1_len = cp - c1;
      54             : 
      55         936 :   cp = memchr(c2, 0, c2_len);
      56         936 :   if (cp)
      57         308 :     c2_len = cp - c2;
      58             : 
      59         936 :   char *c3 = consensus_diff_generate(c1, c1_len, c2, c2_len);
      60             : 
      61         936 :   if (c3) {
      62         436 :     char *c4 = consensus_diff_apply(c1, c1_len, c3, strlen(c3));
      63         436 :     tor_assert(c4);
      64         436 :     int equal = (c2_len == strlen(c4)) && fast_memeq(c2, c4, c2_len);
      65           0 :     if (! equal) {
      66             :       //printf("%s\n", escaped(c1));
      67             :       //printf("%s\n", escaped(c2));
      68           0 :       printf("%s\n", escaped(c3));
      69           0 :       printf("%s\n", escaped(c4));
      70             :     }
      71         436 :     tor_assert(equal);
      72         436 :     tor_free(c3);
      73         436 :     tor_free(c4);
      74             :   }
      75             : 
      76             :   return 0;
      77             : }

Generated by: LCOV version 1.14