Subversion Repositories SvarDOS

Rev

Rev 340 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
339 mateuszvis 1
/*
2
 * test suite for the unchunk() function.
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
5
 */
6
 
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <time.h>
11
#include <unistd.h>
12
 
13
#include "unchunk.h"
14
 
15
static size_t randchunkdata(void *file_chunked, const void *file_raw, size_t file_raw_len, int maxchunksz) {
16
  size_t file_raw_read = 0;
17
  size_t file_chunked_len = 0;
18
 
19
  for (;;) {
20
    size_t chunklen = (rand() % maxchunksz) + 1;
21
    if (file_raw_read + chunklen > file_raw_len) chunklen = file_raw_len - file_raw_read;
22
 
23
    file_chunked_len += sprintf((char *)file_chunked + file_chunked_len, "%x\r\n", chunklen);
24
    if (chunklen > 0) memcpy((char *)file_chunked + file_chunked_len, (char *)file_raw + file_raw_read, chunklen);
25
    file_raw_read += chunklen;
26
    file_chunked_len += chunklen;
27
    file_chunked_len += sprintf((char *)file_chunked + file_chunked_len, "\r\n");
28
    if (chunklen == 0) return(file_chunked_len);
29
  }
30
}
31
 
32
 
33
/* writes buffer of fsize bytes to file fname */
34
static void dumpfile(const char *fname, const void *buff, size_t fsize) {
35
  FILE *fd;
36
  fd = fopen(fname, "wb");
37
  fwrite(buff, 1, fsize, fd);
38
  fclose(fd);
39
}
40
 
41
 
42
int main(int argc, char **argv) {
43
  static unsigned char file_raw[30000u];     /* original file */
44
  static unsigned char file_chunked[60000u]; /* chunked version */
45
  static unsigned char file_decoded[60000u]; /* after being un-chunked */
46
  size_t file_raw_len;
47
  size_t file_chunked_len;
48
  FILE *fd;
49
  int trycount;
50
 
51
  if ((argc != 2) || (argv[1][0] == '/')) {
52
    puts("Usage: unchtest <file>");
53
    return(1);
54
  }
55
 
56
  fd = fopen(argv[1], "rb");
57
  if (fd == NULL) {
58
    puts("ERROR: failed to open file");
59
    return(1);
60
  }
61
  file_raw_len = fread(file_raw, 1, sizeof(file_raw), fd);
62
  fclose(fd);
63
 
64
  printf("Loaded '%s' (%zu bytes)\r\n", argv[1], file_raw_len);
65
  srand(time(NULL));
66
 
67
  for (trycount = 0; trycount < 1000; trycount++) {
68
    size_t bytesprocessed = 0;
69
    size_t file_decoded_len = 0;
70
    int maxchunksz;
71
 
72
    /* segment file into chunks of random size */
73
    maxchunksz = (rand() % 1024) + 1;
74
    file_chunked_len = randchunkdata(file_chunked, file_raw, file_raw_len, maxchunksz);
75
 
76
    printf("=== TRY %d (CHUNKS: %d BYTES MAX) ======================\r\n", trycount + 1, maxchunksz);
77
 
78
    for (;;) {
79
      size_t bytes;
80
      int decodedbytes;
81
      unsigned char buffer[4096];
82
 
83
      bytes = min((rand() % 256) + 1, file_chunked_len - bytesprocessed);
84
      printf("processing %4zu bytes of chunked data", bytes);
85
      memcpy(buffer, file_chunked + bytesprocessed, bytes);
86
 
87
      /* decode the chunked version reading random amounts of data and build a decoded version */
88
      decodedbytes = unchunk(buffer, bytes);
89
      printf(" -> decoded into %4d raw bytes\r\n", decodedbytes);
90
      memcpy(file_decoded + file_decoded_len, buffer, decodedbytes);
91
      file_decoded_len += decodedbytes;
92
      bytesprocessed += bytes;
93
      if (bytesprocessed == file_chunked_len) break;
94
    }
95
 
96
    /* compare decoded and original */
97
    if ((file_decoded_len != file_raw_len) || (memcmp(file_decoded, file_raw, file_raw_len) != 0)) {
98
      printf("ERROR: decoded file does not match the original. see tst-orig.dat, tst-chnk.txt and tst-unch.dat\r\n");
99
      dumpfile("tst-orig.dat", file_raw, file_raw_len);
100
      dumpfile("tst-chnk.dat", file_chunked, file_chunked_len);
101
      dumpfile("tst-unch.dat", file_decoded, file_decoded_len);
102
      return(1);
103
    }
104
  }
105
 
106
  printf("OK\r\n");
107
 
108
  return(0);
109
}