Пример #1
0
static void compare_config(t3_config_t *a, t3_config_t *b) {
	if (t3_config_get_type(a) != t3_config_get_type(b))
		fatal("Different type at lines %d/%d: %d != %d\n",
			t3_config_get_line(a), t3_config_get_line(a),
			t3_config_get_type(a), t3_config_get_type(b));
	if ((t3_config_get_name(a) == NULL && t3_config_get_name(b) != NULL) ||
			(t3_config_get_name(a) != NULL && t3_config_get_name(b) == NULL) ||
			(t3_config_get_name(a) != NULL && strcmp(t3_config_get_name(a), t3_config_get_name(b)) != 0))
		fatal("Differently named keys at lines %d/%d: %s != %s\n",
			t3_config_get_line(a), t3_config_get_line(a),
			t3_config_get_name(a), t3_config_get_name(b));

	switch (t3_config_get_type(a)) {
		case T3_CONFIG_INT:
			if (t3_config_get_int(a) != t3_config_get_int(b))
				fatal("Different int value at lines %d/%d %d != %d\n",
					t3_config_get_line(a), t3_config_get_line(a),
					t3_config_get_int(a), t3_config_get_int(b));
			break;
		case T3_CONFIG_NUMBER:
			if (t3_config_get_number(a) != t3_config_get_number(b))
				fatal("Different number value at lines %d/%d: %.18f != %.18f\n",
					t3_config_get_line(a), t3_config_get_line(a),
					t3_config_get_number(a), t3_config_get_number(b));
			break;
		case T3_CONFIG_BOOL:
			if (t3_config_get_bool(a) != t3_config_get_bool(b))
				fatal("Different bool value at lines %d/%d: %d != %d\n",
					t3_config_get_line(a), t3_config_get_line(a),
					t3_config_get_bool(a), t3_config_get_bool(b));
			break;
		case T3_CONFIG_STRING:
			if (strcmp(t3_config_get_string(a), t3_config_get_string(b)) != 0)
				fatal("Different string value at lines %d/%d: %s != %s\n",
					t3_config_get_line(a), t3_config_get_line(a),
					t3_config_get_string(a), t3_config_get_string(b));
			break;
		case T3_CONFIG_LIST:
		case T3_CONFIG_PLIST:
		case T3_CONFIG_SECTION: {
			t3_config_t *a_sub, *b_sub;
			for (a_sub = t3_config_get(a, NULL), b_sub = t3_config_get(b, NULL);
					a_sub != NULL && b_sub != NULL; a_sub = t3_config_get_next(a_sub), b_sub = t3_config_get_next(b_sub))
				compare_config(a_sub, b_sub);
			if (a_sub != NULL || b_sub != NULL)
				fatal("Different (p)list/section lengths for (p)list/sections starting at %d/%d\n",
					t3_config_get_line(a), t3_config_get_line(a));
			break;
		}
		default:
			fatal("Unhandled config type %d\n", t3_config_get_type(a));
	}
}
Пример #2
0
int main(int argc, char *argv[]) {
	t3_config_error_t error;
	FILE *file = stdin;
	t3_config_t *config, *reread;

	setlocale(LC_ALL, "nl_NL.UTF-8");

	if (argc == 2) {
		if ((file = fopen(argv[optind], "r")) == NULL)
			fatal("Could not open input '%s': %m\n");
	} else if (argc > 2) {
		fatal("Usage: test [<test file>]\n");
	}

	path[0] = dirname(dirname(strdup(argv[optind])));
	path[1] = NULL;

	/* Read file. */
	if ((config = t3_config_read_file(file, &error, &opts)) == NULL)
		fatal("Error loading input: %s %s @ %d\n", t3_config_strerror(error.error),
			error.extra == NULL ? "" : error.extra, error.line_number);
	fclose(file);

	/* Write new file. */
	if ((file = fopen("out", "w+")) == NULL)
		fatal("Could not open output: %m\n");
	t3_config_write_file(config, file);
	fclose(file);

	if ((file = fopen("out", "r")) == NULL)
		fatal("Could not re-open output: %m\n");
	if ((reread = t3_config_read_file(file, &error, NULL)) == NULL)
		fatal("Error re-loading output: %s @ %d\n", t3_config_strerror(error.error), error.line_number);
	fclose(file);

	compare_config(config, reread);
	t3_config_delete(config);
	t3_config_delete(reread);

	return EXIT_SUCCESS;
}
Пример #3
0
void load_server(std::istream& is,
    server_base& server, const std::string& id) {
  init_versions();

  char header_buf[48];
  is.read(header_buf, 48);
  if (std::memcmp(header_buf, magic_number, 8) != 0) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error("invalid file format"));
  }
  uint64_t format_version_read = read_big_endian<uint64_t>(&header_buf[8]);
  if (format_version_read != format_version) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "invalid format version: " +
          lexical_cast<string>(format_version_read) +
          ", expected " +
          lexical_cast<string>(format_version)));
  }
  uint32_t jubatus_major_read = read_big_endian<uint32_t>(&header_buf[16]);
  uint32_t jubatus_minor_read = read_big_endian<uint32_t>(&header_buf[20]);
  uint32_t jubatus_maintenance_read =
      read_big_endian<uint32_t>(&header_buf[24]);
  if (jubatus_major_read != jubatus_version_major ||
      jubatus_minor_read != jubatus_version_minor ||
      jubatus_maintenance_read != jubatus_version_maintenance) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "jubatus version mismatched: " +
          lexical_cast<std::string>(jubatus_major_read) + "." +
          lexical_cast<std::string>(jubatus_minor_read) + "." +
          lexical_cast<std::string>(jubatus_maintenance_read) +
          ", expected (current) version: " JUBATUS_VERSION));
  }
  uint32_t crc32_expected = read_big_endian<uint32_t>(&header_buf[28]);
  uint64_t system_data_size = read_big_endian<uint64_t>(&header_buf[32]);
  uint64_t user_data_size = read_big_endian<uint64_t>(&header_buf[40]);

  std::vector<char> system_data_buf(system_data_size);
  is.read(&system_data_buf[0], system_data_size);

  std::vector<char> user_data_buf(user_data_size);
  is.read(&user_data_buf[0], user_data_size);

  uint32_t crc32_actual = calc_crc32(header_buf,
      &system_data_buf[0], system_data_size,
      &user_data_buf[0], user_data_size);
  if (crc32_actual != crc32_expected) {
    std::ostringstream ss;
    ss << "invalid crc32 checksum: " << std::hex << crc32_actual;
    ss << ", expected " << crc32_expected;
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(ss.str()));
  }

  system_data_container system_data_actual;
  try {
    msgpack::unpacked unpacked;
    msgpack::unpack(&unpacked, &system_data_buf[0], system_data_size);
    unpacked.get().convert(&system_data_actual);
  } catch (const msgpack::type_error&) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "system data is broken"));
  }
  system_data_container system_data_expected(server, id);
  if (system_data_actual.version != system_data_expected.version) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "invalid system data version: saved version: " +
          lexical_cast<string>(system_data_actual.version) +
          ", expected " +
          lexical_cast<string>(system_data_expected.version)));
  }
  if (system_data_actual.type != system_data_expected.type) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "server type mismatched: " + system_data_actual.type +
          ", expected " + system_data_expected.type));
  }
  if (!compare_config(
      system_data_actual.config, system_data_expected.config)) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "server config mismatched: " + system_data_actual.config +
          ", expected " + system_data_expected.config));
  }

  try {
    msgpack::unpacked unpacked;
    msgpack::unpack(&unpacked, &user_data_buf[0], user_data_size);

    std::vector<msgpack::object> objs;
    unpacked.get().convert(&objs);
    if (objs.size() != 2) {
      throw JUBATUS_EXCEPTION(
          core::common::exception::runtime_error("invalid user container"));
    }

    uint64_t user_data_version_expected = server.user_data_version();
    uint64_t user_data_version_actual;
    objs[0].convert(&user_data_version_actual);
    if (user_data_version_actual != user_data_version_expected) {
      throw JUBATUS_EXCEPTION(
          core::common::exception::runtime_error(
            "user data version mismatched: " +
            lexical_cast<string>(user_data_version_actual) +
            ", expected " +
            lexical_cast<string>(user_data_version_expected)));
    }

    server.get_driver()->unpack(objs[1]);
  } catch (const msgpack::type_error&) {
    throw JUBATUS_EXCEPTION(
        core::common::exception::runtime_error(
          "user data is broken"));
  }
}