2005-11-02 03:32:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* test-file-stuff.c
|
|
|
|
|
*
|
|
|
|
|
* Thu Sep 29 22:50:50 2005
|
|
|
|
|
* Copyright 2005 GnuCash team
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-11-17 05:35:02 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301, USA.
|
2005-11-02 03:32:36 +00:00
|
|
|
*/
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <glib.h>
|
2007-01-21 16:38:42 +00:00
|
|
|
#include <glib/gstdio.h>
|
2001-08-18 20:33:51 +00:00
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
#include "gnc-engine.h"
|
2001-08-18 20:33:51 +00:00
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
|
#include "test-file-stuff.h"
|
|
|
|
|
#include "test-stuff.h"
|
|
|
|
|
#include "io-gncxml-gen.h"
|
|
|
|
|
#include "sixtp-utils.h"
|
|
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
files_return(int ret, const char* msg)
|
|
|
|
|
{
|
2008-07-08 21:30:26 +00:00
|
|
|
printf("%s", msg);
|
2001-08-18 20:33:51 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
files_compare(const gchar* f1, const gchar* f2)
|
|
|
|
|
{
|
|
|
|
|
gchar buf1[512];
|
|
|
|
|
gchar buf2[512];
|
|
|
|
|
int fd1, fd2;
|
|
|
|
|
int amount_read1, amount_read2;
|
|
|
|
|
|
2007-01-21 19:28:31 +00:00
|
|
|
fd1 = g_open(f1, O_RDONLY, 0);
|
|
|
|
|
fd2 = g_open(f2, O_RDONLY, 0);
|
2001-08-18 20:33:51 +00:00
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
int memcmp_ret;
|
|
|
|
|
|
|
|
|
|
amount_read1 = read(fd1, buf1, 512);
|
|
|
|
|
amount_read2 = read(fd2, buf2, 512);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (amount_read1 > amount_read2)
|
2001-08-18 20:33:51 +00:00
|
|
|
return files_return(1, "read1 > read2");
|
2009-12-29 20:12:48 +00:00
|
|
|
if (amount_read1 < amount_read2)
|
2001-08-18 20:33:51 +00:00
|
|
|
return files_return(-1, "read1 < read2");
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if ((memcmp_ret = memcmp(buf1, buf2, amount_read1)) != 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return files_return(memcmp_ret, "memcmp return");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
}
|
|
|
|
|
while (amount_read1 == 512);
|
2001-08-18 20:33:51 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
write_dom_node_to_file(xmlNodePtr node, int fd)
|
|
|
|
|
{
|
|
|
|
|
FILE *out;
|
|
|
|
|
|
|
|
|
|
out = fdopen(fd, "w");
|
|
|
|
|
|
|
|
|
|
xmlElemDump(out, NULL, node);
|
|
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
print_dom_tree(gpointer data_for_children, GSList* data_from_children,
|
|
|
|
|
GSList* sibling_data, gpointer parent_data,
|
|
|
|
|
gpointer global_data, gpointer *result, const gchar *tag)
|
|
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
if (parent_data == NULL)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
xmlElemDump((FILE*)global_data, NULL, (xmlNodePtr)data_for_children);
|
|
|
|
|
xmlFreeNode(data_for_children);
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
check_dom_tree_version(xmlNodePtr node, gchar *verstr)
|
|
|
|
|
{
|
|
|
|
|
char *verteststr;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
g_return_val_if_fail(verstr, FALSE);
|
|
|
|
|
g_return_val_if_fail(node->properties, FALSE);
|
|
|
|
|
g_return_val_if_fail(node->properties->xmlAttrPropertyValue, FALSE);
|
|
|
|
|
g_return_val_if_fail(node->properties->xmlAttrPropertyValue->content,
|
|
|
|
|
FALSE);
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
verteststr = (char*) node->properties->xmlAttrPropertyValue->content;
|
2012-08-07 17:24:55 +00:00
|
|
|
if (g_strcmp0(verstr, verteststr) == 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
equals_node_val_vs_string(xmlNodePtr node, const gchar* str)
|
|
|
|
|
{
|
|
|
|
|
gchar *cmp1;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
g_return_val_if_fail(str, FALSE);
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
cmp1 = dom_tree_to_text(node);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!cmp1)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2012-08-07 17:24:55 +00:00
|
|
|
else if (g_strcmp0(cmp1, str) == 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
g_free(cmp1);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
else
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
printf("Differing types: node:`%s' vs string:`%s'\n", cmp1, str);
|
|
|
|
|
g_free(cmp1);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
equals_node_val_vs_int(xmlNodePtr node, gint64 val)
|
|
|
|
|
{
|
|
|
|
|
gchar *text;
|
|
|
|
|
gint64 test_val;
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
|
|
|
|
|
text = dom_tree_to_text(node);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!string_to_gint64(text, &test_val))
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
g_free(text);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
|
|
return val == test_val;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-16 22:17:23 +00:00
|
|
|
gboolean
|
|
|
|
|
equals_node_val_vs_boolean(xmlNodePtr node, gboolean val)
|
|
|
|
|
{
|
|
|
|
|
return equals_node_val_vs_string(node, val ? "TRUE" : "FALSE");
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
gboolean
|
2010-03-27 21:01:21 +00:00
|
|
|
equals_node_val_vs_guid(xmlNodePtr node, const GncGUID *id)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
2010-03-27 21:01:21 +00:00
|
|
|
GncGUID *cmpid;
|
2001-08-18 20:33:51 +00:00
|
|
|
|
|
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
g_return_val_if_fail(id, FALSE);
|
|
|
|
|
|
|
|
|
|
cmpid = dom_tree_to_guid(node);
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(cmpid, FALSE);
|
2009-12-29 20:12:48 +00:00
|
|
|
|
|
|
|
|
if (guid_compare(cmpid, id) == 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
g_free(cmpid);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_free(cmpid);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
2009-12-29 20:12:48 +00:00
|
|
|
equals_node_val_vs_commodity(xmlNodePtr node, const gnc_commodity *com,
|
2006-08-09 03:31:00 +00:00
|
|
|
QofBook *book)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
gnc_commodity *cmpcom;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
g_return_val_if_fail(com, FALSE);
|
|
|
|
|
|
2002-01-02 04:28:32 +00:00
|
|
|
cmpcom = dom_tree_to_commodity_ref_no_engine(node, book);
|
2001-08-18 20:33:51 +00:00
|
|
|
|
|
|
|
|
g_return_val_if_fail(cmpcom, FALSE);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (gnc_commodity_equiv(com, cmpcom))
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
gnc_commodity_destroy(cmpcom);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gnc_commodity_destroy(cmpcom);
|
2006-08-09 01:08:56 +00:00
|
|
|
return FALSE;
|
2001-08-18 20:33:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
equals_node_val_vs_kvp_frame(xmlNodePtr node, const kvp_frame *frm)
|
|
|
|
|
{
|
|
|
|
|
kvp_frame *cmpfrm;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(node, FALSE);
|
|
|
|
|
g_return_val_if_fail(frm, FALSE);
|
|
|
|
|
|
|
|
|
|
cmpfrm = dom_tree_to_kvp_frame(node);
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail(cmpfrm, FALSE);
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (kvp_frame_compare(frm, cmpfrm) == 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
kvp_frame_delete(cmpfrm);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gchar *frm1str;
|
|
|
|
|
gchar *frm2str;
|
|
|
|
|
|
|
|
|
|
frm1str = kvp_frame_to_string(frm);
|
|
|
|
|
frm2str = kvp_frame_to_string(cmpfrm);
|
|
|
|
|
|
|
|
|
|
printf("%s vs %s\n", frm1str, frm2str);
|
|
|
|
|
|
|
|
|
|
g_free(frm1str);
|
|
|
|
|
g_free(frm2str);
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
kvp_frame_delete(cmpfrm);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
equals_node_val_vs_date(xmlNodePtr node, const Timespec tm)
|
|
|
|
|
{
|
2003-09-01 15:08:26 +00:00
|
|
|
Timespec tm_test = dom_tree_to_timespec(node);
|
2001-08-18 20:33:51 +00:00
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (tm_test.tv_sec == tm.tv_sec && tm_test.tv_nsec == tm.tv_nsec)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-09-01 15:08:26 +00:00
|
|
|
return FALSE;
|
2001-08-18 20:33:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
just_dom_tree_end_handler(gpointer data_for_children,
|
|
|
|
|
GSList* data_from_children, GSList* sibling_data,
|
|
|
|
|
gpointer parent_data, gpointer global_data,
|
|
|
|
|
gpointer *result, const gchar *tag)
|
|
|
|
|
{
|
|
|
|
|
xmlNodePtr tree = (xmlNodePtr)data_for_children;
|
|
|
|
|
xmlNodePtr *globaldata = (xmlNodePtr*)global_data;
|
|
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
if (parent_data)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* OK. For some messed up reason this is getting called again with a
|
|
|
|
|
NULL tag. So we ignore those cases */
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!tag)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
g_return_val_if_fail(tree, FALSE);
|
|
|
|
|
|
|
|
|
|
*globaldata = tree;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static xmlNodePtr
|
|
|
|
|
grab_file_doc(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
sixtp *parser;
|
|
|
|
|
xmlNodePtr ret;
|
|
|
|
|
gpointer parse_result = NULL;
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
parser = sixtp_dom_parser_new(just_dom_tree_end_handler, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
sixtp_parse_file(parser, filename, NULL, &ret, &parse_result);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
static void
|
2009-12-29 20:12:48 +00:00
|
|
|
test_load_file(const char *filename, gxpf_callback cb, sixtp *top_parser,
|
2003-10-17 05:21:37 +00:00
|
|
|
QofBook *book)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
xmlNodePtr node;
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
node = grab_file_doc(filename);
|
|
|
|
|
|
2006-01-12 21:22:03 +00:00
|
|
|
if (!node)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
failure_args("failure of libxml to parse file", __FILE__, __LINE__,
|
|
|
|
|
"%s", filename);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-12 21:22:03 +00:00
|
|
|
if (!gnc_xml_parse_file(top_parser, filename, cb, node->children, book))
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
failure_args("failure to parse file", __FILE__, __LINE__,
|
|
|
|
|
"%s", filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmlFreeNode(node);
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
void
|
|
|
|
|
test_files_in_dir(int argc, char **argv, gxpf_callback cb,
|
2001-10-03 10:07:45 +00:00
|
|
|
sixtp *parser, const char *parser_tag,
|
2003-10-17 05:21:37 +00:00
|
|
|
QofBook *book)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
int count;
|
2006-01-12 21:22:03 +00:00
|
|
|
sixtp *main_parser;
|
|
|
|
|
sixtp *top_parser;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
top_parser = sixtp_new();
|
|
|
|
|
main_parser = sixtp_new();
|
|
|
|
|
|
|
|
|
|
if (!sixtp_add_some_sub_parsers(top_parser, TRUE, "gnc-v2", main_parser,
|
|
|
|
|
NULL, NULL))
|
|
|
|
|
return;
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2006-01-12 21:22:03 +00:00
|
|
|
if (!sixtp_add_some_sub_parsers(main_parser, TRUE, parser_tag, parser,
|
|
|
|
|
NULL, NULL))
|
|
|
|
|
return;
|
|
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
|
2009-12-29 20:12:48 +00:00
|
|
|
for (count = 1; count < argc; count++)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
|
|
|
|
struct stat file_info;
|
|
|
|
|
const char *to_open = argv[count];
|
2009-12-29 20:12:48 +00:00
|
|
|
if (g_stat(to_open, &file_info) != 0)
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
2006-01-12 21:22:03 +00:00
|
|
|
printf("cannot stat %s.\n", to_open);
|
2001-08-18 20:33:51 +00:00
|
|
|
failure("unable to stat file");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-12-29 20:12:48 +00:00
|
|
|
if (!S_ISDIR(file_info.st_mode))
|
2001-08-18 20:33:51 +00:00
|
|
|
{
|
2002-10-15 02:18:36 +00:00
|
|
|
#if 0
|
2002-10-06 22:31:05 +00:00
|
|
|
printf( "testing load of file \"%s\":\n", argv[count] );
|
2002-10-15 02:18:36 +00:00
|
|
|
#endif
|
2006-01-12 21:22:03 +00:00
|
|
|
test_load_file(to_open, cb, top_parser, book);
|
2001-08-18 20:33:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-29 20:12:48 +00:00
|
|
|
|
2006-01-12 21:22:03 +00:00
|
|
|
sixtp_destroy(top_parser);
|
2001-08-18 20:33:51 +00:00
|
|
|
}
|