javacommons/security/javaunicertstoreplugin/tsrc/utils/testdata.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <memory>
       
    20 #include <fstream>
       
    21 #include "testdata.h"
       
    22 #include "javacommonutils.h"
       
    23 
       
    24 _LIT(KStar,"*");
       
    25 
       
    26 using namespace std;
       
    27 using namespace java::util;
       
    28 
       
    29 CTestData::CTestData()
       
    30 {
       
    31 }
       
    32 
       
    33 CTestData::~CTestData()
       
    34 {
       
    35     mRfs.Close();
       
    36 }
       
    37 
       
    38 CTestData* CTestData::NewL()
       
    39 {
       
    40     CTestData* self = new(ELeave) CTestData();
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop(self);
       
    44     return self;
       
    45 }
       
    46 
       
    47 void CTestData::ConstructL()
       
    48 {
       
    49     User::LeaveIfError(mRfs.Connect());
       
    50 }
       
    51 
       
    52 void CTestData::DeleteTestDataL()
       
    53 {
       
    54     std::auto_ptr<CFileMan> fileMan(CFileMan::NewL(mRfs));
       
    55     auto_ptr<HBufC> workDirBuf(HBufC::NewL(KCertMetaDataRootDir().Length()+2));
       
    56     TPtr workDirPtr(workDirBuf->Des());
       
    57     workDirPtr.Append(KCertMetaDataRootDir());
       
    58     workDirPtr.Append(KStar());
       
    59     TInt err = fileMan->Delete(workDirBuf->Des());
       
    60     if ((KErrNone != err) && (KErrNotFound != err) && (KErrPathNotFound != err))
       
    61     {
       
    62         User::Leave(err);
       
    63     }
       
    64     auto_ptr<HBufC> workDirBuf1(HBufC::NewL(KCertDataRootDir().Length()+2));
       
    65     TPtr workDirPtr1(workDirBuf1->Des());
       
    66     workDirPtr1.Append(KCertDataRootDir());
       
    67     workDirPtr1.Append(KStar());
       
    68     err = fileMan->Delete(workDirBuf1->Des());
       
    69     if ((KErrNone != err) && (KErrNotFound != err) && (KErrPathNotFound != err))
       
    70     {
       
    71         User::Leave(err);
       
    72     }
       
    73 }
       
    74 
       
    75 void CTestData::StoreTestData(std::list<CertMetaData>& aTestData)
       
    76 {
       
    77     RFs rfs;
       
    78     rfs.Connect();
       
    79     rfs.MkDirAll(KCertMetaDataRootDir);
       
    80     rfs.MkDirAll(KCertDataRootDir);
       
    81 
       
    82     std::list<CertMetaData>::iterator iter = aTestData.begin();
       
    83     int index = 0;
       
    84     for (; iter != aTestData.end(); ++iter)
       
    85     {
       
    86         std::string fileName(CertDataRootDir);
       
    87         fileName.append("cert");
       
    88         fileName.append(JavaCommonUtils::intToString(index));
       
    89         string certFileNameWithoutSuffix(fileName);
       
    90         //string certMetadata(fileName);
       
    91         fileName.append(".metadata");
       
    92         ofstream outfile(fileName.c_str());
       
    93         outfile << "name=" << iter->mNameOfCert.c_str() << endl;
       
    94         outfile << "category=ITPD" << endl;
       
    95         int removable = 0;
       
    96         if (true == iter->mRemovable)
       
    97             removable = 1;
       
    98         int disablable = 0;
       
    99         if (true == iter->mDisablable)
       
   100             disablable = 1;
       
   101         outfile << "removable=" << removable << endl;
       
   102         outfile << "disablable=" << disablable << endl;
       
   103         outfile << "hash=" << iter->mHash.c_str() << endl;
       
   104         outfile.close();
       
   105 
       
   106         std::string certContent(certFileNameWithoutSuffix);
       
   107         certContent.append(".der");
       
   108         ofstream outfile2(certContent.c_str());
       
   109         outfile2 << iter->mCertContent.c_str() << endl;
       
   110         outfile2.close();
       
   111 
       
   112         //std::string certState(certFileNameWithoutSuffix);
       
   113         fileName = CertMetaDataRootDir;
       
   114         fileName.append("cert");
       
   115         fileName.append(JavaCommonUtils::intToString(index));
       
   116         fileName.append(".state");
       
   117         int len = fileName.size();
       
   118         HBufC* filePath = HBufC::NewL(len);
       
   119         CleanupStack::PushL(filePath);
       
   120         TPtr ptr = filePath->Des();
       
   121         TPtr8 ptr8((unsigned char *)fileName.c_str(),len);
       
   122         ptr8.SetLength(len);
       
   123         ptr.Copy(ptr8);
       
   124         RFile stateFile;
       
   125         int err = stateFile.Create(rfs, ptr, EFileWrite);
       
   126         if (err == KErrNone)
       
   127         {
       
   128             CleanupClosePushL(stateFile);
       
   129             TPckgBuf<TUint32> state(iter->mState);
       
   130             stateFile.Write(0, state);
       
   131             CleanupStack::PopAndDestroy();  // state file
       
   132         }
       
   133         CleanupStack::PopAndDestroy(); // file path
       
   134         index++;
       
   135     }//end for
       
   136     rfs.Close();
       
   137 }
       
   138