static void instrument_file(const char * source_file, const char * destination_file, const char * id, int instrumenting) {
  if (g_verbose) {
    printf("Instrumenting file %s\n", id);
  }

  /* check if they are the same */
  char * canonical_source_file = make_canonical_path(source_file);
  char * canonical_destination_file = make_canonical_path(destination_file);
  check_same_file(canonical_source_file, canonical_destination_file);
  free(canonical_source_file);
  free(canonical_destination_file);

  if (instrumenting) {
    enum FileType file_type = get_file_type(source_file);
    switch (file_type) {
    case FILE_TYPE_OTHER:
    case FILE_TYPE_HTML:
      copy_file(source_file, destination_file);
      break;
    case FILE_TYPE_JS:
      {
        FILE * input = xfopen(source_file, "rb");
        FILE * output = xfopen(destination_file, "wb");

        Stream * input_stream = Stream_new(0);
        Stream * output_stream = Stream_new(0);

        Stream_write_file_contents(input_stream, input);

        size_t num_characters = input_stream->length;
        uint16_t * characters = NULL;
        int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters);
        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
          fatal("encoding %s not supported", jscoverage_encoding);
        }
        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
          fatal("error decoding %s in file %s", jscoverage_encoding, id);
        }
        jscoverage_instrument_js(id, characters, num_characters, output_stream);
        free(characters);

        if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) {
          fatal("cannot write to file: %s", destination_file);
        }

        Stream_delete(input_stream);
        Stream_delete(output_stream);

        fclose(input);
        fclose(output);
      }
      break;
    }
  }
  else {
    copy_file(source_file, destination_file);
  }
}
Example #2
0
int main(void) {
  jscoverage_init();

  Stream * stream = Stream_new(0);

  FILE * f = xfopen("store.json", "r");
  Stream_write_file_contents(stream, f);
  fclose(f);

  Coverage * coverage = Coverage_new();
  int result = jscoverage_parse_json(coverage, stream->data, stream->length);
  assert(result == 0);

  Coverage_delete(coverage);
  Stream_delete(stream);
  jscoverage_cleanup();

  exit(EXIT_SUCCESS);
}
Example #3
0
static void instrument_file(const char * source_file, const char * destination_file, const char * id, int instrumenting) {
  /* check if they are the same */
  char * canonical_source_file = make_canonical_path(source_file);
  char * canonical_destination_file = make_canonical_path(destination_file);
  check_same_file(canonical_source_file, canonical_destination_file);
  free(canonical_source_file);
  free(canonical_destination_file);

  if (instrumenting) {
    enum FileType file_type = get_file_type(source_file);
    switch (file_type) {
    case FILE_TYPE_OTHER:
    case FILE_TYPE_HTML:
      if (g_verbose) {
        printf("Copying file %s\n", id);
      }
      copy_file(source_file, destination_file);
      break;
    case FILE_TYPE_JS:
      {
        if (g_verbose) {
          printf("Instrumenting file %s\n", id);
        }

        FILE * input = xfopen(source_file, "rb");
        FILE * output = xfopen(destination_file, "wb");

        Stream * input_stream = Stream_new(0);
        Stream * output_stream = Stream_new(0);

        Stream_write_file_contents(input_stream, input);

        /*
        Check if the source file looks like an instrumented JavaScript file.
        */
        if (input_stream->length >= JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH &&
            memcmp(input_stream->data, JSCOVERAGE_INSTRUMENTED_HEADER, JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH) == 0) {
          fatal_command_line("file %s in the source directory appears to be already instrumented", id);
        }

        size_t num_characters = input_stream->length;
        uint16_t * characters = NULL;
        int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters);
        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
          fatal("encoding %s not supported", jscoverage_encoding);
        }
        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
          fatal("error decoding %s in file %s", jscoverage_encoding, id);
        }
        jscoverage_instrument_js(id, characters, num_characters, output_stream);
        free(characters);

        if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) {
          fatal("cannot write to file: %s", destination_file);
        }

        Stream_delete(input_stream);
        Stream_delete(output_stream);

        fclose(input);
        fclose(output);
      }
      break;
    }
  }
  else {
    if (g_verbose) {
      printf("Copying file %s (on --no-instrument list)\n", id);
    }

    copy_file(source_file, destination_file);
  }
}
Example #4
0
int main(void) {
  Stream * stream;

  stream = Stream_new(0);
  Stream_write_string(stream, "a");
  Stream_write_string(stream, "bc");
  assert(stream->length == 3);
  assert(memcmp(stream->data, "abc", 3) == 0);
  Stream_delete(stream);

  /* test writing chars to stream */
  stream = Stream_new(2);
  Stream_write_char(stream, 'a');
  Stream_write_char(stream, 'b');
  Stream_write_char(stream, 'c');
  assert(stream->length == 3);
  assert(memcmp(stream->data, "abc", 3) == 0);
  Stream_reset(stream);
  Stream_write_char(stream, 'x');
  Stream_write_char(stream, 'y');
  assert(stream->length == 2);
  assert(memcmp(stream->data, "xy", 2) == 0);
  Stream_delete(stream);

  /* test writing file to stream */
  stream = Stream_new(0);
  FILE * f = xfopen("Makefile", "r");
  fseek(f, 0, SEEK_END);
  long file_length = ftell(f);
  fseek(f, 0, SEEK_SET);
  uint8_t * file_contents = xmalloc(file_length);
  fread(file_contents, 1, file_length, f);
  fseek(f, 0, SEEK_SET);
  Stream_write_file_contents(stream, f);
  fclose(f);
  assert(stream->length == (size_t) file_length);
  assert(memcmp(stream->data, file_contents, file_length) == 0);
  free(file_contents);
  Stream_delete(stream);

  stream = Stream_new(0);
  Stream_printf(stream, "%s %d\n", "abc", 123);
  assert(stream->length == 8);
  assert(memcmp(stream->data, "abc 123\n", 8) == 0);
  Stream_delete(stream);

  stream = Stream_new(10);
  size_t length = 0;
  for (int i = 0; i < 100; i++) {
    Stream_printf(stream, "%s %d\n", "abc", i);
    if (i < 10) {
      length += 6;
    }
    else {
      length += 7;
    }
  }
  assert(stream->length == length);
  length = 0;
  for (int i = 0; i < 100; i++) {
    char buffer[8];
    int result = sprintf(buffer, "%s %d\n", "abc", i);
    assert(memcmp(stream->data + length, buffer, result) == 0);
    length += result;
  }
  assert(stream->length == length);
  Stream_delete(stream);

  stream = Stream_new(10);
  char buffer[100];
  for (int i = 0; i < 100; i++) {
    buffer[i] = 'x';
  }
  Stream_write(stream, buffer, 100);
  assert(stream->length == 100);
  for (int i = 0; i < 100; i++) {
    assert(stream->data[i] == 'x');
  }
  Stream_delete(stream);

  exit(EXIT_SUCCESS);
}