18ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian/*
28ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * Copyright (C) 2009 The Android Open Source Project
38ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian *
48ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
58ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * you may not use this file except in compliance with the License.
68ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * You may obtain a copy of the License at
78ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian *
88ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
98ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian *
108ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * Unless required by applicable law or agreed to in writing, software
118ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
128ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * See the License for the specific language governing permissions and
148ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian * limitations under the License.
158ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian */
168ae2335a3c93d0c00e998fdec18f64dfe43b94cbMathias Agopian
17b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/BackupHelpers.h>
183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <stdio.h>
203ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <string.h>
213ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
224535e40544aeb957d44fad75fbe5676effe03689Joe Onoratousing namespace android;
234535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
24c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato#if TEST_BACKUP_HELPERS
253ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
263ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato// ============================================================
273ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato// ============================================================
283ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratotypedef int (*test_func)();
293ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
303ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostruct Test {
313ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    const char* name;
323ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    test_func func;
333ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    int result;
343ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    bool run;
353ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato};
363ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
373ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoTest TESTS[] = {
383ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    { "backup_helper_test_empty", backup_helper_test_empty, 0, false },
393ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    { "backup_helper_test_four", backup_helper_test_four, 0, false },
403ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    { "backup_helper_test_files", backup_helper_test_files, 0, false },
4123ecae3bbb60c5af940f3a22170d75eb6ac05b69Joe Onorato    { "backup_helper_test_null_base", backup_helper_test_null_base, 0, false },
42ce88cb15b52998e16c3ba548a4ec49117a835e21Joe Onorato    { "backup_helper_test_missing_file", backup_helper_test_missing_file, 0, false },
434535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    { "backup_helper_test_data_writer", backup_helper_test_data_writer, 0, false },
442e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    { "backup_helper_test_data_reader", backup_helper_test_data_reader, 0, false },
453ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    { 0, NULL, 0, false}
463ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato};
473ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
483ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoint
493ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratomain(int argc, const char** argv)
503ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato{
513ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    Test* t;
523ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
533ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    if (argc == 1) {
543ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        t = TESTS;
553ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        while (t->name) {
563ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            t->run = true;
573ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            t++;
583ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        }
593ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    } else {
603ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        t = TESTS;
613ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        while (t->name) {
623ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            for (int i=1; i<argc; i++) {
633ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                if (0 == strcmp(t->name, argv[i])) {
643ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                    t->run = true;
653ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                }
663ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            }
673ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            t++;
683ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        }
693ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    }
703ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
713ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    int testCount = 0;
723ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    t = TESTS;
733ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    while (t->name) {
743ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        if (t->run) {
753ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            testCount++;
763ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        }
773ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        t++;
783ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    }
793ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
803ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
813ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    int failed = 0;
823ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    int i = 1;
833ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    t = TESTS;
843ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    while (t->name) {
853ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        if (t->run) {
863ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            printf("===== Running %s (%d of %d) ==============================\n",
873ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                    t->name, i, testCount);
883ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            fflush(stdout);
893ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            fflush(stderr);
903ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            t->result = t->func();
913ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            if (t->result != 0) {
923ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                failed++;
933ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                printf("failed\n");
943ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            } else {
953ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                printf("passed\n");
963ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            }
973ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            i++;
983ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        }
993ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        t++;
1003ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    }
1013ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1023ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    printf("=================================================================\n");
1033ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    if (failed == 0) {
1043ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        printf("All %d test(s) passed\n", testCount);
1053ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    } else {
1063ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        printf("Tests failed: (%d of %d)\n", failed, testCount);
1073ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        t = TESTS;
1083ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        while (t->name) {
1093ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            if (t->run) {
1103ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                if (t->result != 0) {
1113ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                    printf("  %s\n", t->name);
1123ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato                }
1133ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            }
1143ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            t++;
1153ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        }
1163ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    }
1173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
118c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato
119c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato#else
120c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onoratoint
121c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onoratomain(int argc, char** argv)
122c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato{
123c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato    printf ("test_backup_helper built without the tests\n");
124c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato    return 0;
125c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato}
126c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22Joe Onorato#endif
127