⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xdelta3-test.h

📁 Linux下一个可以比较二进制文件的工具xdelta3.0u的源码。
💻 H
📖 第 1 页 / 共 5 页
字号:
      memset (& inst, 0, sizeof (inst));      if (d->type2 == 0)	{	  inst.type = d->type1;	  if ((inst.size = d->size1) == 0)	    {	      inst.size = TESTBUFSIZE;	    }	  XD3_CHOOSE_INSTRUCTION (stream, NULL, & inst);	  if (inst.code2 != 0 || inst.code1 != i)	    {	      stream->msg = "wrong single instruction";	      return XD3_INTERNAL;	    }	}      else	{	  prev.type = d->type1;	  prev.size = d->size1;	  inst.type = d->type2;	  inst.size = d->size2;	  XD3_CHOOSE_INSTRUCTION (stream, & prev, & inst);	  if (prev.code2 != i)	    {	      stream->msg = "wrong double instruction";	      return XD3_INTERNAL;	    }	}    }  return 0;}/*********************************************************************** TEST INSTRUCTION TABLE CODING ***********************************************************************/#if GENERIC_ENCODE_TABLES/* Test that encoding and decoding a code table works */static inttest_encode_code_table (xd3_stream *stream, int ignore){  int ret;  const uint8_t *comp_data;  usize_t comp_size;  if ((ret = xd3_compute_alternate_table_encoding (stream, & comp_data, & comp_size)))    {      return ret;    }  stream->acache.s_near = __alternate_code_table_desc.near_modes;  stream->acache.s_same = __alternate_code_table_desc.same_modes;  if ((ret = xd3_apply_table_encoding (stream, comp_data, comp_size)))    {      return ret;    }  if (memcmp (stream->code_table, xd3_alternate_code_table (), sizeof (xd3_dinst) * 256) != 0)    {      stream->msg = "wrong code table reconstruction";      return XD3_INTERNAL;    }  return 0;}#endif/*********************************************************************** 64BIT STREAMING ***********************************************************************//* This test encodes and decodes a series of 1 megabyte windows, each * containing a long run of zeros along with a single xoff_t size * record to indicate the sequence. */static inttest_streaming (xd3_stream *in_stream, uint8_t *encbuf, uint8_t *decbuf, uint8_t *delbuf, usize_t megs){  xd3_stream estream, dstream;  int ret;  usize_t i, delsize, decsize;  if ((ret = xd3_config_stream (& estream, NULL)) ||      (ret = xd3_config_stream (& dstream, NULL)))    {      goto fail;    }  for (i = 0; i < megs; i += 1)    {      ((usize_t*) encbuf)[0] = i;      if ((i % 200) == 199) { DOT (); }      if ((ret = xd3_process_stream (1, & estream, xd3_encode_input, 0,				     encbuf, 1 << 20,				     delbuf, & delsize, 1 << 10)))	{	  in_stream->msg = estream.msg;	  goto fail;	}      if ((ret = xd3_process_stream (0, & dstream, xd3_decode_input, 0,				     delbuf, delsize,				     decbuf, & decsize, 1 << 20)))	{	  in_stream->msg = dstream.msg;	  goto fail;	}      if (decsize != 1 << 20 ||	  memcmp (encbuf, decbuf, 1 << 20) != 0)	{	  in_stream->msg = "wrong result";	  ret = XD3_INTERNAL;	  goto fail;	}    }  if ((ret = xd3_close_stream (& estream)) ||      (ret = xd3_close_stream (& dstream)))    {      goto fail;    } fail:  xd3_free_stream (& estream);  xd3_free_stream (& dstream);  return ret;}/* Run tests of data streaming of over and around 4GB of data. */static inttest_compressed_stream_overflow (xd3_stream *stream, int ignore){  int ret;  uint8_t *buf;  if ((buf = (uint8_t*) malloc (TWO_MEGS_AND_DELTA)) == NULL) { return ENOMEM; }  memset (buf, 0, TWO_MEGS_AND_DELTA);  /* Test overflow of a 32-bit file offset. */  if (SIZEOF_XOFF_T == 4)    {      ret = test_streaming (stream, buf, buf + (1 << 20), buf + (2 << 20), (1 << 12) + 1);      if (ret == XD3_INVALID_INPUT && MSG_IS ("decoder file offset overflow"))	{	  ret = 0;	}      else	{          XPR(NT XD3_LIB_ERRMSG (stream, ret));	  stream->msg = "expected overflow condition";	  ret = XD3_INTERNAL;	  goto fail;	}    }  /* Test transfer of exactly 32bits worth of data. */  if ((ret = test_streaming (stream, buf, buf + (1 << 20), buf + (2 << 20), 1 << 12))) { goto fail; } fail:  free (buf);  return ret;}/*********************************************************************** COMMAND LINE ***********************************************************************//* For each pair of command templates in the array below, test that * encoding and decoding commands work.  Also check for the expected * size delta, which should be approximately TEST_ADD_RATIO times the * file size created by test_make_inputs.  Due to differences in the * application header, it is suppressed (-A) so that all delta files * are the same. */static inttest_command_line_arguments (xd3_stream *stream, int ignore){  int i, ret;  static const char* cmdpairs[] =  {    /* standard input, output */    "%s %s -A < %s > %s", "%s -d < %s > %s",    "%s %s -A -e < %s > %s", "%s -d < %s > %s",    "%s %s -A= encode < %s > %s", "%s decode < %s > %s",    "%s %s -A -q encode < %s > %s", "%s -qdq < %s > %s",    /* file input, standard output */    "%s %s -A= %s > %s", "%s -d %s > %s",    "%s %s -A -e %s > %s", "%s -d %s > %s",    "%s %s encode -A= %s > %s", "%s decode %s > %s",    /* file input, output */    "%s %s -A= %s %s", "%s -d %s %s",    "%s %s -A -e %s %s", "%s -d %s %s",    "%s %s -A= encode %s %s", "%s decode %s %s",    /* option placement */    "%s %s -A -f %s %s", "%s -f -d %s %s",    "%s %s -e -A= %s %s", "%s -d -f %s %s",    "%s %s -f encode -A= %s %s", "%s -f decode -f %s %s",  };  char ecmd[TESTBUFSIZE], dcmd[TESTBUFSIZE];  int pairs = SIZEOF_ARRAY (cmdpairs) / 2;  xoff_t tsize;  xoff_t dsize;  double ratio;  mt_init (& static_mtrand, 0x9f73f7fc);  for (i = 0; i < pairs; i += 1)    {      test_setup ();      if ((ret = test_make_inputs (stream, NULL, & tsize))) { return ret; }      sprintf (ecmd, cmdpairs[2*i], program_name,	       test_softcfg_str, TEST_TARGET_FILE, TEST_DELTA_FILE);      sprintf (dcmd, cmdpairs[2*i+1], program_name,	       TEST_DELTA_FILE, TEST_RECON_FILE);      /* Encode and decode. */      if ((ret = system (ecmd)) != 0)	{	  DP(RINT "xdelta3: encode command: %s\n", ecmd);	  stream->msg = "encode cmd failed";	  return XD3_INTERNAL;	}      if ((ret = system (dcmd)) != 0)	{	  DP(RINT "xdelta3: decode command: %s\n", dcmd);	  stream->msg = "decode cmd failed";	  return XD3_INTERNAL;	}      /* Compare the target file. */      if ((ret = compare_files (stream, TEST_TARGET_FILE, TEST_RECON_FILE)))	{	  return ret;	}      if ((ret = test_file_size (TEST_DELTA_FILE, & dsize)))	{	  return ret;	}      ratio = (double) dsize / (double) tsize;      /* Check that it is not too small, not too large. */      if (ratio >= TEST_ADD_RATIO + TEST_EPSILON)	{	  DP(RINT "xdelta3: test encode with size ratio %.4f, "	     "expected < %.4f (%"Q"u, %"Q"u)\n",	    ratio, TEST_ADD_RATIO + TEST_EPSILON, dsize, tsize);	  stream->msg = "strange encoding";	  return XD3_INTERNAL;	}      if (ratio <= TEST_ADD_RATIO * (1.0 - 2 * TEST_EPSILON))	{	  DP(RINT "xdelta3: test encode with size ratio %.4f, "	     "expected > %.4f\n",	    ratio, TEST_ADD_RATIO - TEST_EPSILON);	  stream->msg = "strange encoding";	  return XD3_INTERNAL;	}      /* Also check that compare_files works.  The delta and original should       * not be identical. */      if ((ret = compare_files (stream, TEST_DELTA_FILE,				TEST_TARGET_FILE)) == 0)	{	  stream->msg = "broken compare_files";	  return XD3_INTERNAL;	}      test_cleanup ();      DOT ();    }  return 0;}static intcheck_vcdiff_header (xd3_stream *stream,		     const char *input,		     const char *line_start,		     const char *matches,		     int yes_or_no){  int ret;  char vcmd[TESTBUFSIZE], gcmd[TESTBUFSIZE];  sprintf (vcmd, "%s printhdr -f %s %s",	   program_name, input, TEST_RECON2_FILE);  if ((ret = system (vcmd)) != 0)    {      DP(RINT "xdelta3: printhdr command: %s\n", vcmd);      stream->msg = "printhdr cmd failed";      return XD3_INTERNAL;    }  sprintf (gcmd, "grep \"%s.*%s.*\" %s > /dev/null",	   line_start, matches, TEST_RECON2_FILE);  if (yes_or_no)    {      if ((ret = do_cmd (stream, gcmd)))	{	  DP(RINT "xdelta3: %s\n", gcmd);	  return ret;	}    }  else    {      if ((ret = do_fail (stream, gcmd)))	{	  DP(RINT "xdelta3: %s\n", gcmd);	  return ret;	}    }  return 0;}static inttest_recode_command2 (xd3_stream *stream, int has_source,		      int variant, int change){  int has_adler32 = (variant & 0x1) != 0;  int has_apphead = (variant & 0x2) != 0;  int has_secondary = (variant & 0x4) != 0;  int change_adler32 = (change & 0x1) != 0;  int change_apphead = (change & 0x2) != 0;  int change_secondary = (change & 0x4) != 0;  int recoded_adler32 = change_adler32 ? !has_adler32 : has_adler32;  int recoded_apphead = change_apphead ? !has_apphead : has_apphead;  int recoded_secondary = change_secondary ? !has_secondary : has_secondary;  char ecmd[TESTBUFSIZE], recmd[TESTBUFSIZE], dcmd[TESTBUFSIZE];  xoff_t tsize, ssize;  int ret;  test_setup ();  if ((ret = test_make_inputs (stream, has_source ? & ssize : NULL, & tsize)))    {      return ret;    }  /* First encode */  sprintf (ecmd, "%s %s -f ", program_name, test_softcfg_str);  strcat (ecmd, has_adler32 ? "" : "-n ");  strcat (ecmd, has_apphead ? "-A=encode_apphead " : "-A= ");  strcat (ecmd, has_secondary ? "-S djw " : "-S none ");  if (has_source)    {      strcat (ecmd, "-s ");      strcat (ecmd, TEST_SOURCE_FILE);      strcat (ecmd, " ");    }  strcat (ecmd, TEST_TARGET_FILE);  strcat (ecmd, " ");  strcat (ecmd, TEST_DELTA_FILE);  if ((ret = system (ecmd)) != 0)    {      DP(RINT "xdelta3: encode command: %s\n", ecmd);      stream->msg = "encode cmd failed";      return XD3_INTERNAL;    }  /* Now recode */  sprintf (recmd, "%s recode %s -f ", program_name, test_softcfg_str);  strcat (recmd, recoded_adler32 ? "" : "-n ");  strcat (recmd, !change_apphead ? "" :	  (recoded_apphead ? "-A=recode_apphead " : "-A= "));  strcat (recmd, recoded_secondary ? "-S djw " : "-S none ");  strcat (recmd, TEST_DELTA_FILE);  strcat (recmd, " ");  strcat (recmd, TEST_COPY_FILE);  if ((ret = system (recmd)) != 0)    {      DP(RINT "xdelta3: recode command: %s\n", recmd);      stream->msg = "recode cmd failed";      return XD3_INTERNAL;    }  /* Check recode changes. */  if ((ret = check_vcdiff_header (stream,				  TEST_COPY_FILE,				  "VCDIFF window indicator",				  "VCD_SOURCE",				  has_source))) { return ret; }  if ((ret = check_vcdiff_header (stream,				  TEST_COPY_FILE,				  "VCDIFF header indicator",				  "VCD_SECONDARY",				  recoded_secondary))) { return ret; }  if ((ret = check_vcdiff_header (stream,				  TEST_COPY_FILE,				  "VCDIFF window indicator",				  "VCD_ADLER32",				  /* Recode can't generate an adler32				   * checksum, it can only preserve it or				   * remove it. */				  has_adler32 && recoded_adler32)))    {      return ret;    }  if (!change_apphead)    {      if ((ret = check_vcdiff_header (stream,				      TEST_COPY_FILE,				      "VCDIFF header indicator",				      "VCD_APPHEADER",				      has_apphead)))	{	  return ret;	}      if ((ret = check_vcdiff_header (stream,				      TEST_COPY_FILE,				      "VCDIFF application header",				      "encode_apphead",				      has_apphead)))	{	  return ret;	}    }  else    {      if ((ret = check_vcdiff_header (stream,				      TEST_COPY_FILE,				      "VCDIFF header indicator",				      "VCD_APPHEADER",				      recoded_apphead)))	{	  return ret;	}      if (recoded_apphead &&	  (ret = check_vcdiff_header (stream,				      TEST_COPY_FILE,				      "VCDIFF application header",				      "recode_apphead",				      1)))	{	  return ret;	}    }  /* Now decode */  sprintf (dcmd, "%s -fd ", program_name);    if (has_source)    {      strcat (dcmd, "-s ");      strcat (dcmd, TEST_SOURCE_FILE);      strcat (dcmd, " ");    }  strcat (dcmd, TEST_COPY_FILE);  strcat (dcmd, " ");  strcat (dcmd, TEST_RECON_FILE);  if ((ret = system (dcmd)) != 0)    {      DP(RINT "xdelta3: decode command: %s\n", dcmd);      stream->msg = "decode cmd failed";      return XD3_INTERNAL;    }  /* Now compare. */  if ((ret = compare_files (stream, TEST_TARGET_FILE, TEST_RECON_FILE)))    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -