summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am12
-rw-r--r--tests/test-css-file.c130
-rw-r--r--tests/test-script-file.c160
3 files changed, 302 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..ae244f2
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,12 @@
+
+check_PROGRAMS = test-css-file test-script-file
+
+TESTS = test-css-file test-script-file
+
+test_css_file_SOURCES = test-css-file.c
+test_css_file_CPPFLAGS = -I$(top_srcdir)/src
+test_css_file_LDADD = $(top_builddir)/src/libsvgc.la
+
+test_script_file_SOURCES = test-script-file.c
+test_script_file_CPPFLAGS = -I$(top_srcdir)/src
+test_script_file_LDADD = $(top_builddir)/src/libsvgc.la
diff --git a/tests/test-css-file.c b/tests/test-css-file.c
new file mode 100644
index 0000000..fa4c159
--- /dev/null
+++ b/tests/test-css-file.c
@@ -0,0 +1,130 @@
+
+/***************************************************************
+ SVGC CSS TEST
+
+ Exercise stylesheet inclusion and preservation of input text.
+ ***************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <svgc.h>
+
+
+static int
+write_file( const char *path, const void *data, size_t length )
+{
+ FILE *fp;
+ int status;
+
+ fp = fopen( path, "wb" );
+ if( !fp ) return( 0 );
+ status = (fwrite( data, 1, length, fp ) == length);
+ if( fclose( fp ) != 0 ) status = 0;
+ return( status );
+}
+
+
+static char *
+read_file( const char *path )
+{
+ FILE *fp;
+ char *data;
+ long length;
+
+ fp = fopen( path, "rb" );
+ if( !fp ) return( NULL );
+ if( fseek( fp, 0L, SEEK_END ) != 0 ) goto failed;
+ length = ftell( fp );
+ if( length < 0 ) goto failed;
+ if( fseek( fp, 0L, SEEK_SET ) != 0 ) goto failed;
+ if( (unsigned long)length >= (size_t)-1 ) goto failed;
+
+ data = (char *)malloc( (size_t)length + 1 );
+ if( !data ) goto failed;
+ if( fread( data, 1, (size_t)length, fp ) != (size_t)length )
+ {
+ free( data );
+ goto failed;
+ }
+ data[length] = '\0';
+ fclose( fp );
+ return( data );
+
+failed:
+ fclose( fp );
+ return( NULL );
+}
+
+
+int
+main( void )
+{
+ static const char css_text[] =
+ "@font-face { font-family: 'Roboto';\n"
+ " src: url('./fonts/Roboto/Roboto-Regular.woff2') format('woff2'); }\n"
+ ".a::after { content: '\xD0\xAF ]]> test'; }\n";
+ static const char invalid_utf8[] = { (char)0xC0, (char)0x80 };
+ char large_css[10000];
+ static const char invalid_xml[] = { '.', 'a', '{', (char)0x01, '}' };
+ struct __svg_sheet *sheet;
+ struct __svg_scene *scene = NULL;
+ char *svg = NULL;
+ char css_path[] = "svgc-test-css-XXXXXX";
+ char svg_path[] = "svgc-test-svg-XXXXXX";
+ int fd;
+ int status = 1;
+
+ fd = mkstemp( css_path );
+ if( fd < 0 ) return( 1 );
+ close( fd );
+ fd = mkstemp( svg_path );
+ if( fd < 0 )
+ {
+ unlink( css_path );
+ return( 1 );
+ }
+ close( fd );
+
+ sheet = (struct __svg_sheet *)__svg_new_sheet( 300, 200, 8, 8, 8, 8 );
+ if( !sheet ) goto done;
+ scene = __svg_new_scene( sheet );
+ if( !scene ) goto done;
+
+ if( __svg_sheet_add_css_file( NULL, css_path ) != -1 ) goto done;
+ if( __svg_sheet_add_css_file( sheet, NULL ) != -1 ) goto done;
+ if( __svg_sheet_add_css_file( sheet, "" ) != -1 ) goto done;
+ if( __svg_sheet_add_css_file( sheet, "svgc-file-which-does-not-exist.css" ) != 0 ) goto done;
+ if( !write_file( css_path, invalid_utf8, sizeof( invalid_utf8 ) ) ) goto done;
+ if( __svg_sheet_add_css_file( sheet, css_path ) != 0 ) goto done;
+ if( !write_file( css_path, invalid_xml, sizeof( invalid_xml ) ) ) goto done;
+ if( __svg_sheet_add_css_file( sheet, css_path ) != 0 ) goto done;
+ if( !write_file( css_path, css_text, sizeof( css_text ) - 1 ) ) goto done;
+ if( __svg_sheet_add_css_file( sheet, css_path ) != 1 ) goto done;
+ memset( large_css, 'a', sizeof( large_css ) );
+ memcpy( large_css, "/*", 2 );
+ memcpy( large_css + sizeof( large_css ) - 2, "*/", 2 );
+ if( !write_file( css_path, large_css, sizeof( large_css ) ) ) goto done;
+ if( __svg_sheet_add_css_file( sheet, css_path ) != 1 ) goto done;
+ if( !write_file( css_path, "replaced", 8 ) ) goto done;
+ __svg_paint_scene( scene, 0, svg_path );
+ svg = read_file( svg_path );
+ if( !svg ) goto done;
+ if( !strstr( svg, "<style type=\"text/css\">\n <![CDATA[\n" ) ) goto done;
+ if( !strstr( svg, "src: url('./fonts/Roboto/Roboto-Regular.woff2')" ) ) goto done;
+ if( !strstr( svg, "\xD0\xAF ]]]]><![CDATA[> test" ) ) goto done;
+ if( strstr( svg, "replaced" ) ) goto done;
+ if( !strstr( svg, " ]]>\n </style>\n" ) ) goto done;
+ if( !strstr( svg, "*/\n ]]>\n </style>" ) ) goto done;
+ if( !strstr( svg, "/*aaaaaaaa" ) ) goto done;
+
+ status = 0;
+done:
+ if( status ) fprintf( stderr, "SVGC CSS regression failed\n" );
+ if( scene ) __svg_free_scene( scene );
+ free( svg );
+ unlink( css_path );
+ unlink( svg_path );
+ return( status );
+}
diff --git a/tests/test-script-file.c b/tests/test-script-file.c
new file mode 100644
index 0000000..d0c5688
--- /dev/null
+++ b/tests/test-script-file.c
@@ -0,0 +1,160 @@
+
+/***************************************************************
+ SVGC ECMASCRIPT FILE TEST
+
+ Exercise sheet-local scripts and CSS, raw text preservation,
+ CDATA splitting, invalid input, and copied source ownership.
+ ***************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <svgc.h>
+
+
+static int
+write_file( const char *path, const void *data, size_t length )
+{
+ FILE *fp;
+ int status;
+
+ fp = fopen( path, "wb" );
+ if( !fp ) return( 0 );
+ status = (fwrite( data, 1, length, fp ) == length);
+ if( fclose( fp ) != 0 ) status = 0;
+ return( status );
+}
+
+
+static char *
+read_file( const char *path )
+{
+ FILE *fp;
+ char *data;
+ long length;
+
+ fp = fopen( path, "rb" );
+ if( !fp ) return( NULL );
+ if( fseek( fp, 0L, SEEK_END ) != 0 ) goto failed;
+ length = ftell( fp );
+ if( length < 0 ) goto failed;
+ if( fseek( fp, 0L, SEEK_SET ) != 0 ) goto failed;
+ if( (unsigned long)length >= (size_t)-1 ) goto failed;
+
+ data = (char *)malloc( (size_t)length + 1 );
+ if( !data ) goto failed;
+ if( fread( data, 1, (size_t)length, fp ) != (size_t)length )
+ {
+ free( data );
+ goto failed;
+ }
+ data[length] = '\0';
+ fclose( fp );
+ return( data );
+
+failed:
+ fclose( fp );
+ return( NULL );
+}
+
+
+int
+main( void )
+{
+ static const char script_text[] =
+ "function changeColor(event) {\n"
+ " if (2 < 3 && event.target) {\n"
+ " event.target.style.fill = 'blue';\n"
+ " }\n"
+ "}\n"
+ "// CDATA test: ]]> must be preserved.\n"
+ "// UTF-8 text: \xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82\n";
+ static const char css_text[] = "rect { stroke: black; }\n";
+ static const char bad_utf8[] = { (char)0xC0, (char)0x80 };
+ static const char bad_xml[] = { '/', '/', (char)0x01, '\n' };
+ char big_script[9500];
+ char script_path[] = "svgc-script-test-XXXXXX";
+ char svg_path[] = "svgc-script-svg-XXXXXX";
+ char css_path[] = "svgc-script-css-XXXXXX";
+ struct __svg_sheet *sheet = NULL;
+ struct __svg_scene *scene = NULL;
+ struct __svg_node *rectangle = NULL;
+ char *svg = NULL;
+ char *script, *css, *defs_end;
+ int fd;
+ int status = 1;
+
+ fd = mkstemp( script_path );
+ if( fd < 0 ) return( 1 );
+ close( fd );
+ fd = mkstemp( svg_path );
+ if( fd < 0 ) goto done;
+ close( fd );
+ fd = mkstemp( css_path );
+ if( fd < 0 ) goto done;
+ close( fd );
+
+ sheet = (struct __svg_sheet *)__svg_new_sheet( 400, 200, 8, 8, 8, 8 );
+ if( !sheet ) goto done;
+ scene = __svg_new_scene( sheet );
+ if( !scene ) goto done;
+
+ if( __svg_sheet_add_script_file( NULL, script_path ) != -1 ) goto done;
+ if( __svg_sheet_add_script_file( sheet, NULL ) != -1 ) goto done;
+ if( __svg_sheet_add_script_file( sheet, "" ) != -1 ) goto done;
+ if( __svg_sheet_add_script_file( sheet, "file-which-does-not-exist.js" ) != 0 ) goto done;
+
+ rectangle = __svg_new_rect( 10, 20, 200, 100, 0, 0 );
+ if( !rectangle ) goto done;
+ if( __svg_sheet_add_script_file( (struct __svg_sheet *)rectangle, script_path ) != -1 ) goto done;
+ __svg_free_node( rectangle );
+ rectangle = NULL;
+
+ if( !write_file( script_path, bad_utf8, sizeof( bad_utf8 ) ) ) goto done;
+ if( __svg_sheet_add_script_file( sheet, script_path ) != 0 ) goto done;
+ if( !write_file( script_path, bad_xml, sizeof( bad_xml ) ) ) goto done;
+ if( __svg_sheet_add_script_file( sheet, script_path ) != 0 ) goto done;
+
+ if( !write_file( css_path, css_text, sizeof( css_text ) - 1 ) ) goto done;
+ if( __svg_sheet_add_css_file( sheet, css_path ) != 1 ) goto done;
+ if( !write_file( script_path, script_text, sizeof( script_text ) - 1 ) ) goto done;
+ if( __svg_sheet_add_script_file( sheet, script_path ) != 1 ) goto done;
+
+ memset( big_script, 'a', sizeof( big_script ) );
+ memcpy( big_script, "/*", 2 );
+ memcpy( big_script + sizeof( big_script ) - 2, "*/", 2 );
+ if( !write_file( script_path, big_script, sizeof( big_script ) ) ) goto done;
+ if( __svg_sheet_add_script_file( sheet, script_path ) != 1 ) goto done;
+ if( !write_file( script_path, "changed", 7 ) ) goto done;
+
+ __svg_paint_scene( scene, 0, svg_path );
+ svg = read_file( svg_path );
+ if( !svg ) goto done;
+ css = strstr( svg, "<style type=\"text/css\">\n <![CDATA[\n" );
+ script = strstr( svg, "<script type=\"text/javascript\">\n <![CDATA[\n" );
+ defs_end = strstr( svg, "</defs>" );
+ if( !css || !script || !defs_end || !(css < script && script < defs_end) ) goto done;
+ if( !strstr( svg, "rect { stroke: black; }" ) ) goto done;
+ if( !strstr( svg, "if (2 < 3 && event.target)" ) ) goto done;
+ if( !strstr( svg, "CDATA test: ]]]]><![CDATA[> must be preserved" ) ) goto done;
+ if( !strstr( svg, "// UTF-8 text: \xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82" ) ) goto done;
+ if( !strstr( svg, "/*aaaaaaaa" ) ) goto done;
+ if( strstr( svg, "changed" ) ) goto done;
+ if( !strstr( svg, " ]]>\n </script>\n" ) ) goto done;
+ if( !strstr( svg, "*/\n ]]>\n </script>" ) ) goto done;
+ if( strstr( svg, "<script type=\"text/javascript\"><![CDATA[<script" ) ) goto done;
+
+ status = 0;
+
+done:
+ if( status ) fprintf( stderr, "SVGC ECMAScript regression failed\n" );
+ if( rectangle ) __svg_free_node( rectangle );
+ if( scene ) __svg_free_scene( scene );
+ else if( sheet ) __svg_free_node( sheet );
+ free( svg );
+ unlink( script_path );
+ unlink( svg_path );
+ unlink( css_path );
+ return( status );
+}