Beispiel #1
0
/**
 * Read command line parameters and add test cases (-add option).
 */
int
AddTcase(int argc, char * argv[])
{
	int i, n, timeout = 20;
	char *c;
	char parameters[PARAMSIZE] = "";

	parameters[0] = '\0';
	for (i = 0; i < (argc - 2); i++) {
		if (strcmp(argv[i], "-timeout") == 0) {
			timeout = atoi(argv[i+1]);
			argv[i] = argv[i+1] = "";
		}
	}

	if (carrega_arquivo_tcase(&testSet, workingDir, testSessionName) == ERRO) {
	        return ERRO;
	}

	if (isInteractive) {
		if (tcase_ex_inter(workingDir, testSessionName, executableDir, executableFilename, parameters, SHELL) == ERRO) {
			return ERRO;;
		}
	} else {
		if (tcase_ex_bat(workingDir, testSessionName, executableDir, executableFilename, parameters) == ERRO) {
			return ERRO;
		}
	}

 	if (exec_from_ascii(executableDir, executableFilename, instrumentedExecutableFilename, parameters, workingDir, testSessionName, &(TREG(&testSet)), timeout,  enableTrace, SHELL, isInteractive) == ERRO ) {
		return ERRO;
	}
	TREG(&testSet).interativo = isInteractive;


	inic_to_buf(TREG(&testSet).param, sizeof(TREG(&testSet).param));
	inic_from_buf(parameters);
	for (c = from_buf(); c != NULL; c = from_buf()) {
		if (to_buf(c) == ERRO) {
			break;
		}
	}
	strcpy(TREG(&testSet).label, Label);
	if (insere_tcase(&testSet,  enableTrace) == ERRO) {
                return ERRO;
	}

	descarrega_arquivo_tcase(&testSet);
}
Beispiel #2
0
/*
 * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
 * this tries to read larger chunks (8192 bytes) into a buffer.
 */
static size_t dsi_buffered_stream_read(DSI *dsi, uint8_t *data, const size_t length)
{
  size_t len;
  size_t buflen;

  LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
  
  len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
  dsi->read_count += len;
  if (len == length) {          /* got enough bytes from there ? */
      return len;               /* yes */
  }

  /* fill the buffer with 8192 bytes or until buffer is full */
  buflen = MIN(8192, dsi->end - dsi->eof);
  if (buflen > 0) {
      ssize_t ret;
      ret = recv(dsi->socket, dsi->eof, buflen, 0);
      if (ret > 0)
          dsi->eof += ret;
  }

  /* now get the remaining data */
  if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
      return 0;
  len += buflen;

  return len;
}
Beispiel #3
0
/*
 * Get bytes from buffer dsi->buffer or read from socket
 *
 * 1. Check if there are bytes in the the dsi->buffer buffer.
 * 2. Return bytes from (1) if yes.
 *    Note: this may return fewer bytes then requested in count !!
 * 3. If the buffer was empty, read from the socket.
 */
static ssize_t buf_read(DSI *dsi, uint8_t *buf, size_t count)
{
    ssize_t len;

    LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);

    if (!count)
        return 0;

    len = from_buf(dsi, buf, count); /* 1. */
    if (len)
        return len;             /* 2. */
  
    len = readt(dsi->socket, buf, count, 0, 0); /* 3. */

    LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);

    return len;
}
Beispiel #4
0
		/* sign arbitrary data */
		static int rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
		{
			ExternalPKIImpl* self = (ExternalPKIImpl*)rsa->meth->app_data;

			try {
				if (padding != RSA_PKCS1_PADDING)
				{
					RSAerr (RSA_F_RSA_EAY_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
					throw openssl_external_pki("bad padding size");
				}

				/* convert 'from' to base64 */
				ConstBuffer from_buf(from, flen, true);
				const std::string from_b64 = base64->encode(from_buf);

				/* get signature */
				std::string sig_b64;
				const bool status = self->external_pki->sign("RSA_RAW", from_b64, sig_b64);
				if (!status)
					throw openssl_external_pki("could not obtain signature");

				/* decode base64 signature to binary */
				const int len = RSA_size(rsa);
				Buffer sig(to, len, false);
				base64->decode(sig, sig_b64);

				/* verify length */
				if (sig.size() != len)
					throw openssl_external_pki("incorrect signature length");

				/* return length of signature */
				return len;
			}
			catch (const std::exception& e)
			{
				OPENVPN_LOG("OpenSSLContext::ExternalPKIImpl::rsa_priv_enc: " << e.what());
				++self->n_errors;
				return -1;
			}
		}
Beispiel #5
0
// Converts string
std::string iconvpp::Converter::convert(const std::string& str)
{
	std::size_t from_size = str.size() + 1;

	// Max length of a character is 6 bytes in UTF-8.
	std::size_t to_size = 6 * from_size + 1;

	std::unique_ptr<char[]> from_buf(new char[from_size]);
	std::unique_ptr<char[]> to_buf(new char[to_size]);

	std::strcpy(from_buf.get(), str.c_str());

	// convert
	std::size_t from_size_temp = from_size - 1;
	std::size_t to_size_temp = to_size - 1;
	char* from_ptr = from_buf.get();
	char* to_ptr = to_buf.get();
	if (-1 == ::iconv(iconv_, &from_ptr, &from_size_temp, &to_ptr, &to_size_temp))
		throw iconvpp::Exception("Conversion failed.");
	
	*to_ptr = '\0';

	return std::string(to_buf.get());
}
Beispiel #6
0
/**
 * Import test cases from plain text files.
 */
int
ImportAscii(int argc, char *argv[])
{
	FILE *testCaseFile, *parametersFile;
	int i, j, k, cont;
	char testCaseFilename[NOME_LENGTH+10];
	char parametersFilename[NOME_LENGTH+10];
	long t;
	char *c;
	char parameters[PARAMSIZE] = "";
	char XLabel[NOME_LENGTH+1];

	if (carrega_arquivo_tcase(&testSet, workingDir, testSessionName) == ERRO) {
		msg("Could not load test set");
		return ERRO;
	}

	if (strlen(testCaseInputDataImportFilename) > 0 || strlen(testCaseParametersImportFilename)) {
		msg("Invalid import filename");
	}

	for (i = firstTestCase, cont = 0; i <= lastTestCase; i++) {
		sprintf(parametersFilename, "%s%d", testCaseInputDataImportFilename, i);
		fprintf(stdout, "\nReading command line parameters from file %s", parametersFilename);
		strcpy(parameters, "");
		if (tem_arq(testCaseImportDir, parametersFilename, "")) {
		 	parametersFile = abrearq(testCaseImportDir, parametersFilename, "", 0);
			if (parametersFile == NULL) {
				fprintf(stdout, "\nError reading command line parameters");
			} else {
				posifim(parametersFile);
				t = ftell(parametersFile);
				posiciona(parametersFile, 0);
				if (t > sizeof(parameters)) {
					msg("Parameter too big");
				   	fclose(parametersFile);
				} else {
					fgets(parameters, (int) t, parametersFile);
					fclose(parametersFile);
					k = strlen(parameters);
					if (k > 0 && parameters[k-1] == '\n') {
						parameters[k-1] = '\0';
					}
				}
			}
		}

		sprintf(testCaseFilename, "%s%d", testCaseParametersImportFilename, i);
		printf("\nReading input data from file %s", testCaseFilename);
		if ( tem_arq(testCaseImportDir, testCaseFilename, "")) {
			testCaseFile = abrearq(testCaseImportDir, testCaseFilename, "", 0);
			if (testCaseFile == NULL) {
				fprintf(stdout, "\nError reading input data");
			}
			fecharq(testCaseFile);
			if (exec_from_ascii(executableDir, executableFilename, instrumentedExecutableFilename, parameters, workingDir, testSessionName, &(TREG(&testSet)), 120,  enableTrace, SHELL, isInteractive) == ERRO) {
				fprintf(stdout, "\nCould not run executable against input data and command line parameters");
			} else {
				TREG(&testSet).interativo = isInteractive;
				inic_to_buf(TREG(&testSet).param, sizeof(TREG(&testSet).param));
				inic_from_buf(parameters);
		   		for (c = from_buf(); c != NULL; c = from_buf())	{
					if (to_buf(c) == ERRO) {
						break;
					}
				}
		        	if (! isempty(Label)) {
					sprintf(XLabel, "%s%d", Label, i);
				} else {
        				XLabel[0] = '\0';
				}

			        if (strlen(XLabel) <= LABELSIZE) {
        				strcpy(TREG(&testSet).label, XLabel);
				} else {
     					msg("Invalid label");
					TREG(&testSet).label[0] = '\0';
				}

				if (insere_tcase(&testSet,  enableTrace) == ERRO) {
					fprintf(stderr, "\nCould not import the test case %d", i);
				} else {
					cont++;
			        	if (verbosityChar != '\0' && ! isspace(verbosityChar)) {       
						fprintf(stdout, "%c", verbosityChar);
				       		fflush(stdout);
					}
				}
			}
		}
	}

	fprintf(stdout, "\n%d test cases imported from ASCII files\n", cont);
	descarrega_arquivo_tcase(&testSet);

	return OK;
}
Beispiel #7
0
/**
 * Import test cases from POKE-TOOL.
 */
ImportPoke(int argc, char *argv[])
{
	FILE	*testCaseFile, *parametersFile;
	int	i, j, k, cont, TemPar;
	char 	*c;
	char XLabel[NOME_LENGTH+1];
	char parameters[PARAMSIZE] = "";

	if (carrega_arquivo_tcase(&testSet, workingDir, testSessionName) == ERRO) {
		return ERRO;
	}

	for (i = firstTestCase, cont = 0; i <= lastTestCase; i++) {
		// Try to import command line parameters for test case number i
		printf(testCaseInputDataImportFilename, "input%d.tes", i);
		strcpy(parameters, "");
		TemPar = FALSE;
		if (tem_arq(testCaseImportDir, testCaseInputDataImportFilename, "")) {
			parametersFile = abrearq(testCaseImportDir, testCaseInputDataImportFilename, "", 0);
			if (parametersFile != NULL) {
				posifim(parametersFile);
				k = ftell(parametersFile);
				if (k >= sizeof(parameters)) {
					msg("Invalid POKE-TOOL parameter file");
					fecharq(parametersFile);
				} else {
					posiciona(parametersFile, 0);
					fgets(parameters, (int) k, parametersFile);
				        k = strlen(parameters);
				        if (k > 0 && parameters[k-1] == '\n') {
						parameters[k-1] = '\0';
					}
					fecharq(parametersFile);
					TemPar = TRUE;
				}
			}
		}

		// Try to import test case number i
		sprintf(testCaseInputDataImportFilename, "tec%d.tes", i);
		if (tem_arq(testCaseImportDir, testCaseInputDataImportFilename, "")) {
			testCaseFile = abrearq(testCaseImportDir, testCaseInputDataImportFilename, "", 0);
			if (testCaseFile == NULL) {
				msg("Invalid POKE-TOOL test case");
				break;
			}
			fecharq(testCaseFile);
		}

		if (exec_from_ascii(executableDir, executableFilename, instrumentedExecutableFilename, parameters, workingDir, testSessionName, &(TREG(&testSet)), 120,  enableTrace, SHELL, isInteractive) == ERRO) {
			break;
		}
		TREG(&testSet).interativo = isInteractive;
		inic_to_buf(TREG(&testSet).param, sizeof(TREG(&testSet).param));
		inic_from_buf(parameters);
	   	for (c = from_buf(); c != NULL; c = from_buf()) {
			if (to_buf(c) == ERRO) {
				break;
			}
		}
		if (! isempty(Label)) {
			sprintf(XLabel, "%s%d", Label, i);
		} else {
			testCaseInputDataImportFilename[0] = '\0';
		}

		if (strlen(XLabel) <= LABELSIZE) {
			strcpy(TREG(&testSet).label, XLabel);
		} else {
			msg("Invalid label");
			TREG(&testSet).label[0] = '\0';
		}

		if (insere_tcase(&testSet,  enableTrace) == ERRO) {
                	break;
		}

		cont++;
		if (verbosityChar != '\0' && ! isspace(verbosityChar)) {
			fprintf(stdout, "%c", verbosityChar);
			fflush(stdout);
		}
	}

	printf("\n%d test cases imported from POKE-TOOL files\n", cont);

	descarrega_arquivo_tcase(&testSet);
	return OK;
}
Beispiel #8
0
/**
 * Import test cases from Proteum.
 */
int
ImportProteum(int argc, char *argv[])
{
	int i, k, cont = 0;
	int recordNumber;
	FILE *testCaseFile;
	char *c;
	HAND_TCASE importedTestSet;
	char XLabel[NOME_LENGTH+1];

	if (carrega_arquivo_tcase(&testSet, workingDir, testSessionName) == ERRO) {
		return ERRO;
	}

	if (carrega_arquivo_tcase(&importedTestSet, testCaseImportDir, testCaseInputDataImportFilename) == ERRO) {
		descarrega_arquivo_tcase(&testSet);
		return ERRO;
	}

	lastTestCase = MINI(lastTestCase, NTCASE(&importedTestSet));
	for (i = firstTestCase, cont = 0; i <= lastTestCase; i++) {
		recordNumber = ltofis_tcase(&importedTestSet, i);
		if (recordNumber == ERRO) {
			fprintf(stderr, "Error finding test case %d in the test set", i);
			continue;
		}

		if (le_tcase_reg(&importedTestSet, recordNumber) == ERRO) {
			fprintf(stderr, "Error importing test case %d (physical record %d)", i, recordNumber);
			continue;
		}

		testCaseFile = criarq(workingDir, testSessionName, SUFIXO_INPUT);
		if (testCaseFile == NULL) {
			fprintf(stderr, "Error creating temporary test case for imported test case %d", i);
			continue;
		}
		
		if (copy_file(TFILEIO(&importedTestSet), TREG(&testSet).apont_ent, TREG(&testSet).tamanho_ent, testCaseFile) == ERRO) {
			fprintf(stderr, "Error copying imported test case %i to temporary test case", i);
			continue;
		}
		fecharq(testCaseFile);

		if (exec_from_ascii(executableDir, executableFilename, instrumentedExecutableFilename, TREG(&importedTestSet).param, workingDir, testSessionName, &(TREG(&testSet)), 120,  enableTrace, NULL, TREG(&testSet).interativo) == ERRO) {
			continue;
		}
		TREG(&testSet).interativo = TREG(&importedTestSet).interativo;
		inic_to_buf(TREG(&testSet).param, sizeof(TREG(&testSet).param));
		inic_from_buf(TREG(&importedTestSet).param);
		for (c = from_buf(); c != NULL; c = from_buf()) {
			if (to_buf(c) == ERRO) {
				break;
			}
		}
   		
 		if (Label != NULL) {
			sprintf(XLabel, "%s%d", Label, i);
		} else {
	   		strcpy(XLabel, (char*) TREG(&importedTestSet).label);
		}

		if (strlen(XLabel) <= LABELSIZE) {
			strcpy(TREG(&testSet).label, XLabel);
		} else {
           		fprintf(stderr, "Invalid label for test case %d", i);
			TREG(&testSet).label[0] = '\0';
		}

		if (insere_tcase(&testSet, enableTrace) == ERRO) {
			return ERRO;
		} 

		if (verbosityChar != '\0') {	
			fprintf(stdout, "%c", verbosityChar);
			fflush(stdout);
		}
		cont++;
	}
	fprintf(stdout, "\n%d test cases imported from Proteum file", cont);

	descarrega_arquivo_tcase(&importedTestSet);
	descarrega_arquivo_tcase(&testSet);

	return OK;
}