1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1995-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
// Test the CE scanner and parser.
//
// jhrg 9/12/95
#include "config.h"
//#define DODS_DEBUG
static char rcsid[] not_used =
{ "$Id: expr-test.cc 19917 2008-11-25 23:47:56Z jimg $" };
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include <iostream>
#include <string>
#include "GetOpt.h"
#include "BaseType.h"
#include "DDS.h"
#include "DataDDS.h"
#include "ConstraintEvaluator.h"
#if 0
#include "XDRFileMarshaller.h"
#endif
#include "XDRFileUnMarshaller.h"
#include "XDRStreamMarshaller.h"
#include "Error.h"
#include "TestSequence.h"
#include "TestCommon.h"
#include "TestTypeFactory.h"
#include "parser.h"
#include "expr.h"
#include "ce_expr.tab.hh"
#include "util.h"
#include "GNU/outbuf.h"
#include "debug.h"
using namespace std;
int test_variable_sleep_interval = 0; // Used in Test* classes for testing
// timeouts.
#define CRLF "\r\n" // Change this here and in cgi_util.cc
#define DODS_DDS_PRX "dods_dds"
#define YY_BUFFER_STATE (void *)
void test_scanner(const string & str);
void test_scanner(bool show_prompt);
void test_parser(ConstraintEvaluator & eval, DDS & table,
const string & dds_name, const string & constraint);
bool read_table(DDS & table, const string & name, bool print);
void evaluate_dds(DDS & table, bool print_constrained);
#if 0
bool loopback_pipe(FILE ** pout, FILE ** pin);
#endif
void constrained_trans(const string & dds_name, const bool constraint_expr,
const string & ce, const bool series_values);
void intern_data_test(const string & dds_name, const bool constraint_expr,
const string & ce, const bool series_values);
int ce_exprlex(); // exprlex() uses the global ce_exprlval
int ce_exprparse(void *arg);
void ce_exprrestart(FILE * in);
// Glue routines declared in expr.lex
void ce_expr_switch_to_buffer(void *new_buffer);
void ce_expr_delete_buffer(void *buffer);
void *ce_expr_string(const char *yy_str);
extern int ce_exprdebug;
static int keep_temps = 0; // MT-safe; test code.
const string version = "version 1.12";
const string prompt = "expr-test: ";
const string options = "sS:bdecvp:w:W:f:k:v";
const string usage = "\
\nexpr-test [-s [-S string] -d -c -v [-p dds-file]\
\n[-e expr] [-w|-W dds-file] [-f data-file] [-k expr]]\
\nTest the expression evaluation software.\
\nOptions:\
\n -s: Feed the input stream directly into the expression scanner, does\
\n not parse.\
\n -S: <string> Scan the string as if it was standard input.\
\n -d: Turn on expression parser debugging.\
\n -c: Print the constrained DDS (the one that will be returned\
\n prepended to a data transmission. Must also supply -p and -e \
\n -v: Verbose output\
\n -V: Print the version of expr-test\
\n -p: DDS-file: Read the DDS from DDS-file and create a DDS object,\
\n then prompt for an expression and parse that expression, given\
\n the DDS object.\
\n -e: Evaluate the constraint expression. Must be used with -p.\
\n -w: Do the whole enchilada. You don't need to supply -p, -e, ...\
\n This prompts for the constraint expression and the optional\
\n data file name. NOTE: The CE parser Error objects do not print\
\n with this option.\
\n -W: Similar to -w but uses the new (11/2007) intern_data() methods\
\n in place of the serialize()/deserialize() combination.\
\n -b: Use periodic/cyclic/changing values. For testing Sequence CEs.\
\n -f: A file to use for data. Currently only used by -w for sequences.\
\n -k: A constraint expression to use with the data. Works with -p,\
\n -e, -t and -w";
int main(int argc, char *argv[])
{
GetOpt getopt(argc, argv, options.c_str());
int option_char;
bool scanner_test = false, parser_test = false, evaluate_test = false;
bool print_constrained = false;
bool whole_enchalada = false, constraint_expr = false;
bool whole_intern_enchalada = false;
bool scan_string = false;
bool verbose = false;
bool series_values = false;
string dds_file_name;
string dataset = "";
string constraint = "";
TestTypeFactory ttf;
DDS table(&ttf);
ConstraintEvaluator eval;
// process options
while ((option_char = getopt()) != EOF)
switch (option_char) {
case 'b':
series_values = true;
break;
case 'd':
ce_exprdebug = true;
break;
case 's':
scanner_test = true;
break;
case 'S':
scanner_test = true;
scan_string = true;
constraint = getopt.optarg;
break;
case 'p':
parser_test = true;
dds_file_name = getopt.optarg;
break;
case 'e':
evaluate_test = true;
break;
case 'c':
print_constrained = true;
break;
case 'w':
whole_enchalada = true;
dds_file_name = getopt.optarg;
break;
case 'W':
whole_intern_enchalada = true;
dds_file_name = getopt.optarg;
break;
case 'k':
constraint_expr = true;
constraint = getopt.optarg;
break;
case 'f':
dataset = getopt.optarg;
break;
case 'v':
verbose = true;
break;
case 'V':
cerr << argv[0] << ": " << version << endl;
exit(0);
case '?':
default:
cerr << usage << endl;
exit(1);
break;
}
try {
if (!scanner_test && !parser_test && !evaluate_test
&& !whole_enchalada && !whole_intern_enchalada) {
cerr << usage << endl;
exit(1);
}
// run selected tests
if (scanner_test) {
if (scan_string)
test_scanner(constraint);
else
test_scanner(true);
exit(0);
}
if (parser_test) {
test_parser(eval, table, dds_file_name, constraint);
}
if (evaluate_test) {
evaluate_dds(table, print_constrained);
}
if (whole_enchalada) {
#if 1
// Broken until we can fix the loopback pipe code to work
// with C++ streams
constrained_trans(dds_file_name, constraint_expr, constraint,
series_values);
#endif
}
if (whole_intern_enchalada) {
intern_data_test(dds_file_name, constraint_expr, constraint,
series_values);
}
}
catch(Error & e) {
cerr <<e.get_error_message() << endl;
exit(1);
}
catch(exception & e) {
cerr << "Caught exception: " << e.what() << endl;
exit(1);
}
exit(0);
}
// Instead of reading the tokens from stdin, read them from a string.
void test_scanner(const string & str)
{
ce_exprrestart(0);
void *buffer = ce_expr_string(str.c_str());
ce_expr_switch_to_buffer(buffer);
test_scanner(false);
ce_expr_delete_buffer(buffer);
}
void test_scanner(bool show_prompt)
{
if (show_prompt)
cout << prompt;
int tok;
while ((tok = ce_exprlex())) {
switch (tok) {
case SCAN_WORD:
cout << "WORD: " << ce_exprlval.id << endl;
break;
case SCAN_STR:
cout << "STR: " << *ce_exprlval.val.v.s << endl;
break;
case SCAN_EQUAL:
cout << "EQUAL: " << ce_exprlval.op << endl;
break;
case SCAN_NOT_EQUAL:
cout << "NOT_EQUAL: " << ce_exprlval.op << endl;
break;
case SCAN_GREATER:
cout << "GREATER: " << ce_exprlval.op << endl;
break;
case SCAN_GREATER_EQL:
cout << "GREATER_EQL: " << ce_exprlval.op << endl;
break;
case SCAN_LESS:
cout << "LESS: " << ce_exprlval.op << endl;
break;
case SCAN_LESS_EQL:
cout << "LESS_EQL: " << ce_exprlval.op << endl;
break;
case SCAN_REGEXP:
cout << "REGEXP: " << ce_exprlval.op << endl;
break;
case '*':
cout << "Dereference" << endl;
break;
case '.':
cout << "Field Selector" << endl;
break;
case ',':
cout << "List Element Separator" << endl;
break;
case '[':
cout << "Left Bracket" << endl;
break;
case ']':
cout << "Right Bracket" << endl;
break;
case '(':
cout << "Left Paren" << endl;
break;
case ')':
cout << "Right Paren" << endl;
break;
case '{':
cout << "Left Brace" << endl;
break;
case '}':
cout << "Right Brace" << endl;
break;
case ':':
cout << "Colon" << endl;
break;
case '&':
cout << "Ampersand" << endl;
break;
default:
cout << "Error: Unrecognized input" << endl;
}
cout << prompt << flush; // print prompt after output
}
}
// NB: The DDS is read in via a file because reading from stdin must be
// terminated by EOF. However, the EOF used to terminate the DDS also closes
// stdin and thus the expr scanner exits immediately.
void
test_parser(ConstraintEvaluator & eval, DDS & dds, const string & dds_name,
const string & constraint)
{
try {
read_table(dds, dds_name, true);
if (constraint != "") {
eval.parse_constraint(constraint, dds);
} else {
ce_exprrestart(stdin);
fprintf(stdout, "%s", prompt.c_str());
parser_arg arg(&eval);
ce_exprparse((void *) &arg);
}
fprintf(stdout, "Input parsed\n"); // Parser throws on failure.
}
catch(Error & e) {
cerr << e.get_error_message() << endl;
}
}
// Read a DDS from stdin and build the cooresponding DDS. IF PRINT is true,
// print the text reprsentation of that DDS on the stdout. The DDS TABLE is
// modified as a side effect.
//
// Returns: true iff that DDS pasted the semantic_check() mfunc, otherwise
// false.
bool read_table(DDS & table, const string & name, bool print)
{
table.parse(name);
if (print)
table.print(cout);
if (table.check_semantics(true))
return true;
else {
fprintf(stdout, "Input did not pass semantic checks\n");
return false;
}
}
void evaluate_dds(DDS & table, bool print_constrained)
{
if (print_constrained)
table.print_constrained(cout);
else
for (DDS::Vars_iter p = table.var_begin(); p != table.var_end();
p++)
(*p)->print_decl(cout, "", true, true);
}
// create a pipe for the caller's process which can be used by the DODS
// software to write to and read from itself.
#if 0
bool loopback_pipe(FILE ** pout, FILE ** pin)
{
#ifdef WIN32
int fd[2];
if (_pipe(fd, 1024, _O_BINARY) < 0) {
fprintf(stderr, "Could not open pipe\n");
return false;
}
*pout = fdopen(fd[1], "w+b");
*pin = fdopen(fd[0], "r+b");
#else
int fd[2];
if (pipe(fd) < 0) {
fprintf(stderr, "Could not open pipe\n");
return false;
}
*pout = fdopen(fd[1], "w");
*pin = fdopen(fd[0], "r");
#endif
return true;
}
#include "xstream/fd.h"
bool loopback_pipe(ostream pout, istream pin)
{
int fd[2];
if (pipe(fd) < 0) {
fprintf(stderr, "Could not open pipe\n");
return false;
}
xstream::fd::streambuf out(fd[1],true);
xstream::fd::streambuf in(fd[0],true);
pout = std::ostream(&out);
pin = std::istream(&in);
return true;
}
#endif
#if 1
#ifndef WIN32
#define PIPE(x) pipe((x))
#else
#define PIPE(x) _pipe((x), 1024, _O_BINARY)
#endif
#endif
bool
loopback_pipe(fdostream **pout, FILE **pin)
{
int fd[2];
if (pipe(fd) < 0) {
fprintf(stderr, "Could not open pipe\n");
return false;
}
*pin = fdopen(fd[0], "r");
*pout = new fdostream(fd[1]);
return true;
}
// Gobble up the mime header. At one time the MIME Headers were output from
// the server filter programs (not the core software) so we could call
// DDS::send() from this test code and not have to parse the MIME header. But
// in order to get errors to work more reliably the header generation was
// moved `closer to the send'. That is, we put off determining whether to
// send a DDS or an Error object until later. That trade off is that the
// header generation is not buried in the core software. This code simply
// reads until the end of the header is found. 3/25/98 jhrg
void parse_mime(FILE * data_source)
{
char line[256];
fgets(line, 256, data_source);
while (strncmp(line, CRLF, 2) != 0)
fgets(line, 256, data_source);
}
void set_series_values(DDS & dds, bool state)
{
for (DDS::Vars_iter q = dds.var_begin(); q != dds.var_end(); q++) {
dynamic_cast < TestCommon & >(**q).set_series_values(state);
}
}
// Test the transmission of constrained datasets. Use read_table() to read
// the DDS from a file. Once done, prompt for the variable name and
// constraint expression. In a real client-server system the server would
// read the DDS for the entire dataset and send it to the client. The client
// would then respond to the server by asking for a variable given a
// constraint.
//
// Once the constraint has been entered, it is evaluated in the context of
// the DDS using DDS:eval_constraint() (this would happen on the server-side
// in a real system). Once the evaluation is complete,
// DDS::print_constrained() is used to create a DDS describing only those
// parts of the dataset that are to be sent to the client process and written
// to the output stream. After that, the marker `Data:' is written to the
// output stream, followed by the binary data.
void
constrained_trans(const string & dds_name, const bool constraint_expr,
const string & constraint, const bool series_values)
{
FILE *pin;
fdostream *pout;
/* Add error check ***/
if (!loopback_pipe(&pout, &pin))
throw InternalErr(__FILE__, __LINE__,
"Could not create the loopback pipe.");
// If the CE was not passed in, read it from the command line.
string ce;
if (!constraint_expr) {
cout << "Constraint:";
char c[256];
cin.getline(c, 256);
if (!cin) {
throw InternalErr(__FILE__, __LINE__,
"Could not read the constraint expression\n");
}
ce = c;
}
else {
ce = constraint;
}
TestTypeFactory ttf;
DDS server(&ttf);
ConstraintEvaluator eval;
cout << "The complete DDS:" << endl;
read_table(server, dds_name, true);
// by default this is false (to get the old-style values that are
// constant); set series_values to true for testing Sequence constraints.
// 01/14/05 jhrg And Array constraints, although it's of limited
// versatility 02/05/07 jhrg
set_series_values(server, series_values);
eval.parse_constraint(ce, server); // Throws Error if the ce doesn't parse.
server.tag_nested_sequences(); // Tag Sequences as Parent or Leaf node.
if (eval.functional_expression()) {
BaseType *var = eval.eval_function(server, dds_name);
if (!var)
throw Error(unknown_error, "Error calling the CE function.");
*pout << "Dataset {\n";
var->print_decl(*pout, " ", true, false, true);
*pout << "} function_value;\n";
*pout << "Data:\n";
*pout << flush;
XDRStreamMarshaller m( *pout ) ;
try {
// In the following call to serialize, suppress CE evaluation.
var->serialize(eval, server, m, false);
}
catch(Error & e) {
delete var;
var = 0;
throw;
}
delete var;
var = 0;
}
else {
// send constrained DDS
server.print_constrained(*pout);
*pout << "Data:\n";
*pout << flush;
// Grab a stream that encodes using XDR.
XDRStreamMarshaller m( *pout ) ;
// Send all variables in the current projection (send_p())
for (DDS::Vars_iter i = server.var_begin(); i != server.var_end();
i++)
if ((*i)->send_p()) {
DBG(cerr << "Sending " << (*i)->name() << endl);
(*i)->serialize(eval, server, m, true);
}
*pout << flush;
}
delete pout;
// Now do what Connect::request_data() does:
// First read the DDS into a new object (using a file to store the DDS
// temporarily - the parser/scanner won't stop reading until an EOF is
// found, this fixes that problem).
// I use the default BaseTypeFactory since we're just printing the
// values here.
BaseTypeFactory factory;
DataDDS dds(&factory, "Test_data", "DAP/3.1"); // Must use DataDDS on receiving end
DBG(cerr << "pin: " << pin << endl);
dds.parse(pin);
XDRFileUnMarshaller um( pin ) ;
// Back on the client side; deserialize the data *using the newly
// generated DDS* (the one sent with the data).
cout << "The data:" << endl;
for (DDS::Vars_iter q = dds.var_begin(); q != dds.var_end(); q++) {
(*q)->deserialize(um, &dds);
(*q)->print_val(cout);
}
}
/** This function does what constrained_trans() does but does not use the
serialize()/deserialize() methods. Instead it uses the new (11/2007)
intern_data() methods.
@param dds_name
@param constraint_expr True is one was given, else false
@param constraint The constraint expression if \c constraint_expr is
true.
@param series_values True if TestTypes should generate 'series values'
like the DTS. False selects the old-style values. */
void
intern_data_test(const string & dds_name, const bool constraint_expr,
const string & constraint, const bool series_values)
{
// If the CE was not passed in, read it from the command line.
string ce;
if (!constraint_expr) {
cout << "Constraint: ";
char c[256];
cin.getline(c, 256);
if (!cin) {
throw InternalErr(__FILE__, __LINE__,
"Could not read the constraint expression\n");
}
ce = c;
}
else {
ce = constraint;
}
TestTypeFactory ttf;
DDS server(&ttf);
ConstraintEvaluator eval;
cout << "The complete DDS:\n";
read_table(server, dds_name, true);
// by default this is false (to get the old-style values that are
// constant); set series_values to true for testing Sequence constraints.
// 01/14/05 jhrg And Array constraints, although it's of limited
// versatility 02/05/07 jhrg
set_series_values(server, series_values);
eval.parse_constraint(ce, server); // Throws Error if the ce doesn't parse.
server.tag_nested_sequences(); // Tag Sequences as Parent or Leaf node.
if (eval.functional_expression()) {
BaseType *var = eval.eval_function(server, dds_name);
if (!var)
throw Error(unknown_error, "Error calling the CE function.");
var->intern_data(eval, server);
var->set_send_p(true);
server.add_var(var);
}
else {
for (DDS::Vars_iter i = server.var_begin(); i != server.var_end(); i++)
if ((*i)->send_p())
(*i)->intern_data(eval, server);
}
cout << "The data:\n";
for (DDS::Vars_iter q = server.var_begin(); q != server.var_end(); q++) {
if ((*q)->send_p()) {
(*q)->print_decl(cout, "", false, false, true);
cout << " = ";
dynamic_cast<TestCommon&>(**q).output_values(cout);
cout << ";\n";
}
}
}
|