]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
regress: Add function to test json string in bperl
authorEric Bollengier <eric@baculasystems.com>
Wed, 31 Mar 2021 15:28:43 +0000 (17:28 +0200)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:03:00 +0000 (09:03 +0100)
regress/scripts/functions.pm

index 291277b6dcb979e00ae1013e550bf129f85bc935..790b6a6fd4a43a173205cbb95f4ad9a6c200e566 100644 (file)
@@ -52,7 +52,7 @@ our @EXPORT = qw(update_some_files create_many_files check_multiple_copies
                   check_openfile check_cloud_hash check_bscan add_log_message compare_backup_content
                   check_tls_traces println add_virtual_changer check_events check_events_json
                   create_many_hardlinks check_dot_status parse_fuse_trace generate_random_seek
-                  check_storage_selection
+                  check_storage_selection check_json
 );
 
 
@@ -2551,4 +2551,28 @@ sub check_storage_selection
     exit $err;
 }
 
+# We validate a json string
+sub check_json
+{
+    my ($file) = @_;
+    open(FP, $file) or die "ERROR: Unable to open $file";
+    my $content = join("", <FP>);    
+    close(FP);
+
+    my $test_json;
+    eval 'use JSON qw/decode_json/; $test_json = JSON->new;';
+    if ($@) {
+        print "INFO: JSON validation is disabled ERR=$@\n";
+        exit 1;
+    }
+
+    eval {
+        my $var = $test_json->decode($content);
+    };
+    if ($@) {
+        print "ERROR: Unable to decode json data. ERR=$@\n";
+        exit 1;
+    }
+    exit 0;
+}
 1;