diff -r 000000000000 -r 2c201484c85f securityanddataprivacytools/securitytools/certapp/test/tcertapp/tcertapp.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/securityanddataprivacytools/securitytools/certapp/test/tcertapp/tcertapp.cpp Wed Jul 08 11:25:26 2009 +0100 @@ -0,0 +1,1692 @@ +/* +* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of the License "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + + +#include +#include "certapp-api.h" +#include "badconfigwriter.h" +#include "tcertapp_good.h" +#include "tcertapp_bad.h" +#include "goodconfigwriter.h" +#include +#include +#include +#include +#include "logger.h" +#include "utils.h" + +using namespace std; + +//Funtions to generate Good/Valid config files for tcertapp +void GoodFileCaCertTests(const stringstream &aFilename, int aArray_value); +void GoodFileCertClientsTests(const stringstream &aFilename); +void GoodSwiCertTests(const stringstream &aFilename, int aArray_value); + +//Functions to generate Good/Valid script files for emulator tests +void GenerateGoodCaCertsTestScripts(const stringstream &aFilename); +void GenerateGoodCertClientTestScripts(const stringstream &aFilename); +void GenerateGoodSwiCertTestScripts(const stringstream &aFilename); + +//Funtions to generate Bad/illegal config files for tcertapp +void BadFileCaCertTests(const stringstream &aFilename, int aArray_value); +void BadFileCertClientsTests(const stringstream &aFilename, int aIndex); +void BadSwiCertTests(const stringstream &aFilename, int aArray_value); + +//Function call for RunCertApp api +void RunCertAppTool(int argc, char **argv, stringstream &aStringval); +void RunCertAppToolForStoreDump(int aArgc, char **aArgv, stringstream &aStringval); +void RunCertAppToolForBadParams(int aArgc, char **aArgv, stringstream &aStringval); + +void TestsWithEmptyConfigFile(); +void CreateFileToAugmentStore(); +void TestsWithEncodedFileFormats(); +void TestToDecodeAndEncodeStore(); +void TestToReviewingAggregateStore(); +void TestBadConfigFiles(const stringstream &aFilename, bool aBool); +void CertClientBadParamsFuncCall(stringstream &aFileName); +void decodeEncodeCertAppCall(const char *aArray[]); +int CompareFiles(stringstream &aFileName1, stringstream &aFileName2); +int Compare(istream &aIstream1, istream &aIstream2); +void TestWithUnicodFileName(); + +ofstream file; //global file handle to write to output file +const char *progress = "progress.txt"; // logs the progress report +const char *errors = "error.txt"; // logs the error report + +//some constants used +const int KFileArrayIndex = 8; +const int KSwicertIndex = 10; +const int KSwiBadcertIndex = 11; +const int KFileBadcertIndex = 9; +const int KAppUidIndex = 4; +const int KIndex = 4; + +// to get the count of passed and failed test cases +static int failCount = 0, passCount = 0; + +//pointers required for creation of good filecerts config files +const char **filecert_array[KFileArrayIndex]= +{ + goodCert_array, + goodDeletables, + goodFormats, + goodCertOwnerTypes, + goodSubjectKeyIds, + goodIssuerKeyIds, + goodApplications, + goodTrusteds +}; + +//pointers required for creation of bad filecerts config files +const char **badfilecert_array[KFileBadcertIndex]= +{ + badLabels, + badDeletables, + badFormats, + badCertOwnerTypes, + badSubjectKeyIds, + badIssuerKeyIds, + badApplications, + badTrusteds, + badDatafilePath +}; + + +//pointers required for creation of good swicerts config files +const char **swicert_array[KSwicertIndex]= +{ + goodLabels, + goodFormats, + goodCertOwnerTypes, + goodSubjectKeyIds, + goodIssuerKeyIds, + goodAppNames, + goodTrusteds, + goodCapabilitySets, + goodMandatory, + goodSystemUpgrade +}; + +//pointers required for creation of bad swicerts config files +const char **badswicert_array[KSwiBadcertIndex]= +{ + badLabels, + badFormats, + badCertOwnerTypes, + badSubjectKeyIds, + badIssuerKeyIds, + badApplications, + badTrusteds, + badDatafilePath, + badCapabilitySets, + badMandatory, + badSystemUpgrade +}; + + +//encoded file names for input to the tool +const char *fileEncodeType[4]= +{ +"--hfilecertstore=utf8.txt", +"--hfilecertstore=utf8_without_bom.txt", +"--hfilecertstore=ucs2-bigendian.txt", +"--hfilecertstore=ucs2-littleendian.txt" +}; + + +//RunCertApp call to create store from text input +void RunCertAppTool( int argc, char **argv, stringstream &stringval) +{ + for(int i =0; i(argv_certclient[n1]); + argv = argument_ptr; + } + + RunCertAppTool(argCount3, argv, certclientfileName); // creating dat file + + //Augment the Store with Extra entries + augFileName<<"SDP-Security_Tools-CertApp-CCAUG_0000.dat"; + argv_certclient[0] = "--hcertclients=tCertClient_Augment.txt"; + argv_certclient[1] = argv_certclient[2]; + argv_certclient[2] = "--out"; + argv_certclient[3] = "--bcertclients=SDP-Security_Tools-CertApp-CCAUG_0000.dat"; + for(int x = 0; x(argv_certclient[x]); + argv1 = argument_ptr; + } + RunCertAppTool(argCount4, argv1, augFileName); + + //Dump the dat file to human readable text format + argv_certclient[0] = argv_certclient[3]; + argv_certclient[1] = "--out"; + argv_certclient[2] = "--hcertclients=SDP-Security_Tools-CertApp-CCDUMP_0000.txt"; + + for(int k = 0; k(argv_certclient[k]); + argv1 = argument_ptr; + } + RunCertAppToolForStoreDump(argCount3, argv1, certclientfileName); + //End of t_certclients + + //Creates good config files to augment store and dump store to text format for file certstore - cacerts.dat + //Test for all positive cases + for(num_tests = 0; num_tests<=KFileArrayIndex; num_tests++) + { + stringstream caCertFileName; + caCertFileName << "SDP-Security_Tools-CertApp-FILEDAT_000"<(argv_certclient[n]); + argv = argument_ptr; + } + RunCertAppTool(argCount4, argv, caCertFileName); + + //AUGMENT THE CACERT STORE WITH EXTRA INFORMATION + //ex:certapp --hfilecertstore=tAugment_cacerts_x.txt --bfilecertstore=cacerts.dat --out --bfilecertstore=new_extra_filecertstore.dat + stringstream augFileName, textDumpFileName; + augFileName<<"SDP-Security_Tools-CertApp-FILEAUG_000" <(argv_certclient[x]); + argv1 = argument_ptr; + } + RunCertAppTool(argCount4, argv1, augFileName); + + //DUMP THE STORE TO HUMAN READABLE FORMAT + //ex:certapp --bfilecertstore=test.dat --out --hfilecertstore=t_cacerts.txt + textDumpFileName <<"SDP-Security_Tools-CertApp-FILEDUMP_000"<< num_tests << ".txt"; + argv_certclient[0] = argv_certclient[3]; //--bfilecertstore=aug_cacerts_x.dat + argv_certclient[1] = "--out"; + strArg5<< "--hfilecertstore=SDP-Security_Tools-CertApp-FILEDUMP_000" <(argv_certclient[k]); + argv2 = argument_ptr; + } + //Dumps the augmented store + RunCertAppToolForStoreDump(argCount3, argv2, textDumpFileName); + }//End of cacerts + + + //create config files with legal values for swi certstore + for(num_tests = 0; num_tests(argv_certclient[n]); + argv = argument_ptr; + } + RunCertAppTool(argCount4, argv,swiCertStoreFileName); + + //AUGMENT THE CACERT STORE WITH EXTRA INFORMATION + //certapp --hswicertstore=tAugment_swicertstore_x.txt --bswicertstore=swicertstore_x.dat --out --bswicertstore=aug_swicertstore_x.dat + stringstream augFileName, textDumpFileName; + augFileName << "SDP-Security_Tools-CertApp-SWIAUG_000" << num_tests <<".dat"; + strArg4<<"--bswicertstore=SDP-Security_Tools-CertApp-SWIAUG_000" << num_tests << ".dat"; + argument4 = strArg4.str(); + argv_certclient[0] = "--hswicertstore=tswi_Augment.txt"; //--hswicertstore=tAugment_swicertstore_x.txt + argv_certclient[1] = argv_certclient[3]; //--bswicertstore=swicertstore_x.dat + argv_certclient[2] = "--out"; + argv_certclient[3] = argument4.c_str(); //--bswicertstore=aug_swicertstore_x.dat + char **argv1; + for(int x = 0; x(argv_certclient[x]); + argv1 = argument_ptr; + } + RunCertAppTool(argCount4, argv1, augFileName); + + //DUMP THE SWI CERTSTORE TO HUMAN READABLE FORMAT + textDumpFileName << "SDP-Security_Tools-CertApp-SWIDUMP_000" <(argv_certclient[k]); + argv2 = argument_ptr; + } + + RunCertAppToolForStoreDump(argCount3, argv2, textDumpFileName); + } + + //Test with unicode filename + TestWithUnicodFileName(); + + //create empty config file + TestsWithEmptyConfigFile(); + + //Test case to test for different file formats + TestsWithEncodedFileFormats(); + + //Test case for REQ10313 to check decode/encode of stores + TestToDecodeAndEncodeStore(); + + //Test case for REQ10314 - reviewing aggregate certificate database + TestToReviewingAggregateStore(); + + //Test cases for BAD Params + //create certclient config files + for(num_tests =0; num_tests<3; num_tests++) + { + stringstream badCertClientsFileName; + badCertClientsFileName << "SDP-Security_Tools-CertApp-CCBadParams-000"< " << testCaseCount << endl; + file << failCount << " tests failed out of " << testCaseCount <(argv_certclient[n]); + argv = argument_ptr; + } + + for(int i =0; i(argv_certclient[n]); + argv = argument_ptr; + } + RunCertAppToolForBadParams(argCount4, argv, configFileName); +} + +/** +Generate bad config files for cacerts +*/ +void BadFileCaCertTests(const stringstream &aFilename ,int aValue) +{ + const char **caCertPtr; + int count = 0; + switch(aValue) + { + case 0: + { + // Tests for BadLabels + for(caCertPtr = badfilecert_array[aValue]; *caCertPtr ; ++caCertPtr ) + { + stringstream file1, file2; + file1 << aFilename.str() <<"_"<(argv_certclient[n]); + argv = argument_ptr; + } + RunCertAppToolForBadParams(argCount3, argv, aFileName); +} + + +/** +Generate config file for swicert +*/ +void BadSwiCertTests(const stringstream &aFileName, int aIndex) +{ + const char **swicertPtr1; + int count = 0 ; + switch(aIndex) + { + case 0: + { + // Tests for BadLabels + for(swicertPtr1= badswicert_array[aIndex]; *swicertPtr1; ++swicertPtr1) + { + stringstream file1, file2; + file1 << aFileName.str() <<"_"<(argv_certclient[n]); + argv = argument_ptr; + } + for(int z=0; z(argv_certclient[n]); + argv = argument_ptr; + } + + for(int z=0; z(argv_certclient[n]); + argv = argument_ptr; + } + + int ret = RunCertApp(progress, errors, KIndex, argv); + if(!ret) + { + passCount++; + int compareValue = CompareFiles(file1,file2); // compare with the reference files in perforce + if (compareValue) + { + file << "Failed to successfully create Aggregate Database for " << file1.str() << " is Fail" <(aArray[n]); + argv = argument_ptr; + } + RunCertApp(progress, errors ,3, argv); +} + + +int CompareFiles(stringstream &aFile1, stringstream &aFile2) +{ + ifstream inFile1, inFile2; + string name1 = aFile1.str(); + string name2 = aFile2.str(); + inFile1.open(name1.c_str(), ifstream::in); + if (inFile1.fail()) + { + cout << "Failed to open the file! May be not existing" << endl; + return -1; + } + inFile2.open(name1.c_str(), ifstream::in); + if (inFile2.fail()) + { + cout << "Failed to open the file! May be not existing" << endl; + return -1; + } + return Compare(inFile1, inFile2); +} + + +int Compare(istream &aIstream1, istream &aIstream2) +{ + char ch1, ch2; + while (aIstream1.get(ch1)) + { + if (!aIstream2.get(ch2) || (ch1 != ch2)) + { + cout << "files are not equal"; + return -1; + } + } + if (!aIstream2.get(ch2)) // no more chars to read fom file + { + cout << "files are equal\n"; + return 0; + } + else + { + cout << "files are not equal"; + return -1; + } +} + + +/** +Generate script files for the filecertstore emulator tests +*/ +void GenerateGoodCertClientTestScripts(const stringstream &aFilename) +{ + CertClientsStoreScriptGeneration cert_clients(aFilename); + + cert_clients.WriteInitialiseCertClient(); + cert_clients.WriteGetCount(KAppUidIndex); + cert_clients.WriteGetApplicationsList(); + + for(int i = 0; i