Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

91 lines
3.2 KiB

  1. #include "rangecheck.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. int main() {
  5. char buf[1000];
  6. /* does range_ptrinbuf check all the incoming pointer cases right? */
  7. assert(range_ptrinbuf(buf,sizeof(buf),0)==0);
  8. assert(range_ptrinbuf(buf,sizeof(buf),buf-1)==0);
  9. assert(range_ptrinbuf(buf,sizeof(buf),buf)==1);
  10. assert(range_ptrinbuf(buf,sizeof(buf),buf+50)==1);
  11. assert(range_ptrinbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  12. /* what if we give it an invalid buffer? */
  13. assert(range_ptrinbuf(0,sizeof(buf),0)==0);
  14. assert(range_ptrinbuf(buf,(unsigned long)-1,buf+1)==0);
  15. /* see if range_validbuf works */
  16. assert(range_validbuf(buf,sizeof(buf))==1);
  17. assert(range_validbuf(0,sizeof(buf))==0);
  18. assert(range_validbuf(buf,(unsigned long)-1)==0);
  19. /* range_bufinbuf */
  20. assert(range_bufinbuf(buf,sizeof(buf),buf,sizeof(buf))==1);
  21. assert(range_bufinbuf(buf,sizeof(buf),buf,sizeof(buf)+1)==0);
  22. assert(range_bufinbuf(buf,sizeof(buf),buf-1,sizeof(buf))==0);
  23. assert(range_bufinbuf(buf-1,sizeof(buf)+1,buf,sizeof(buf))==1);
  24. assert(range_bufinbuf(buf-1,sizeof(buf)+2,buf,sizeof(buf))==1);
  25. assert(range_bufinbuf(0,sizeof(buf),(void*)1,1)==0);
  26. assert(range_bufinbuf(buf,(unsigned long)-1,buf,1)==0);
  27. /* the quintessential integer overflow exploit scenario */
  28. assert(range_bufinbuf(buf,sizeof(buf),buf+10,(unsigned long)-5)==0);
  29. assert(range_bufinbuf(buf,sizeof(buf),buf+10,sizeof(buf))==0);
  30. assert(range_bufinbuf(buf,sizeof(buf),buf+10,sizeof(buf)-10)==1);
  31. /* range_arrayinbuf */
  32. assert(range_arrayinbuf(buf,sizeof(buf),buf,1,10)==1);
  33. assert(range_arrayinbuf(buf,sizeof(buf),buf+sizeof(buf)-10,1,10)==1);
  34. assert(range_arrayinbuf(buf,sizeof(buf),buf-1,1,10)==0);
  35. assert(range_arrayinbuf(buf,sizeof(buf),buf+1,1,1000)==0);
  36. assert(range_arrayinbuf(buf,sizeof(buf),buf-1,1,1002)==0);
  37. /* now overflow the array parameters */
  38. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0x10000,0x10000)==0);
  39. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0x80000000,2)==0);
  40. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0xffffffff,1)==0);
  41. /* range_strinbuf */
  42. assert(range_strinbuf(buf,sizeof(buf),0)==0);
  43. assert(range_strinbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  44. {
  45. char* x="fnord";
  46. assert(range_strinbuf(x,5,x)==0);
  47. assert(range_strinbuf(x,6,x)==1);
  48. assert(range_strinbuf(x,6,x+5)==1);
  49. assert(range_strinbuf(x,6,x+6)==0);
  50. }
  51. /* range_str2inbuf */
  52. assert(range_str2inbuf(buf,sizeof(buf),0)==0);
  53. assert(range_str2inbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  54. {
  55. uint16_t y[6];
  56. int i;
  57. for (i=0; i<7; ++i) y[i]="fnord"[i];
  58. assert(range_str2inbuf(y,5*2,y)==0);
  59. assert(range_str2inbuf(y,5*2+1,y)==0);
  60. assert(range_str2inbuf(y,sizeof(y),y)==1);
  61. assert(range_str2inbuf(y,sizeof(y),y+5)==1);
  62. assert(range_str2inbuf(y,sizeof(y),y+6)==0);
  63. }
  64. /* range_str4inbuf */
  65. assert(range_str4inbuf(buf,sizeof(buf),0)==0);
  66. assert(range_str4inbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  67. {
  68. uint32_t y[6];
  69. int i;
  70. for (i=0; i<7; ++i) y[i]="fnord"[i];
  71. assert(range_str4inbuf(y,5*4,y)==0);
  72. assert(range_str4inbuf(y,5*4+3,y)==0);
  73. assert(range_str4inbuf(y,sizeof(y),y)==1);
  74. assert(range_str4inbuf(y,sizeof(y),y+5)==1);
  75. assert(range_str4inbuf(y,sizeof(y),y+6)==0);
  76. }
  77. puts("all tests ok");
  78. return 0;
  79. }