javacommons/security/legacysupport/tsrc/src/testuserpreferences.cpp
branchRCL_3
changeset 19 04becd199f91
child 47 f40128debb5d
equal deleted inserted replaced
16:f5050f1da672 19: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 <stdio.h>
       
    19 #include <string>
       
    20 
       
    21 #include "TestHarness.h"
       
    22 
       
    23 #include "teststoragehandler.h"
       
    24 #include "javastoragenames.h"
       
    25 #include "javauid.h"
       
    26 #include "javasymbianoslayer.h"
       
    27 
       
    28 #include <muserpreferences.h>
       
    29 #include <mmidletsuiteuserpreferencesiterator.h>
       
    30 #include <mmidletsuiteuserpreferences.h>
       
    31 #include <mmidletsuitecustomattributes.h>
       
    32 #include <msecuritypolicyv2.h>
       
    33 
       
    34 using namespace MIDP;
       
    35 using namespace java::storage;
       
    36 using namespace java::util;
       
    37 
       
    38 _LIT(KMidletSuiteName, "MIDletSuite.Name");
       
    39 _LIT(KMidletSuiteVendor, "MIDletSuite.Vendor");
       
    40 _LIT(KMidletSuiteVersion, "MIDletSuite.Version");
       
    41 _LIT(KAppName1, "appName1");
       
    42 _LIT(KAppVendor1, "appVendor1");
       
    43 _LIT(KAppVersion1, "1.1.0");
       
    44 _LIT(KAppName2, "appName2");
       
    45 _LIT(KAppVendor2, "appVendor2");
       
    46 _LIT(KAppVersion2, "2.0.1");
       
    47 
       
    48 IMPORT_C MSecurityPolicyV2* GetSecurityPolicyL(TUint32 aFlags, const TDesC& aPolicyName);
       
    49 IMPORT_C MUserPreferences* GetUserPreferencesL(TUint32 aFlags, const MSecurityPolicyV2& aSecurityPolicy);
       
    50 
       
    51 
       
    52 TEST_GROUP(TestUserPreferences)
       
    53 {
       
    54     MSecurityPolicyV2* iSecurityPolicy;
       
    55     MUserPreferences* iUserPreferences;
       
    56     MMIDletSuiteUserPreferencesIterator* iIterator;
       
    57     TestStorageHandler* iStorageHandler;
       
    58     Uid iAppUid1;
       
    59     Uid iAppUid2;
       
    60     TUint32 iAppId1;
       
    61     TUint32 iAppId2;
       
    62     MMIDletSuiteUserPreferences* iSuitePref1;
       
    63     MMIDletSuiteUserPreferences* iSuitePref2;
       
    64     MMIDletSuiteCustomAttributes* iCustomAttr;
       
    65 
       
    66     TEST_SETUP()
       
    67     {
       
    68         iAppUid1 = Uid(L"12345678");
       
    69         iAppUid2 = Uid(L"87654321");
       
    70         TUid uid1;
       
    71         uidToTUid(iAppUid1, uid1);
       
    72         iAppId1 = uid1.iUid;
       
    73         TUid uid2;
       
    74         uidToTUid(iAppUid2, uid2);
       
    75         iAppId2 = uid2.iUid;
       
    76         TUidToUid(uid1, iAppUid1);
       
    77         TUidToUid(uid2, iAppUid2);
       
    78         iSecurityPolicy = GetSecurityPolicyL(0 /* security policy flags */, KNullDesC());
       
    79         iUserPreferences = GetUserPreferencesL(0 /* user preferences flags */, *iSecurityPolicy);
       
    80         iStorageHandler = new TestStorageHandler();
       
    81     }
       
    82 
       
    83     TEST_TEARDOWN()
       
    84     {
       
    85         if (iIterator)
       
    86         {
       
    87             delete iIterator;
       
    88             iIterator = NULL;
       
    89         }
       
    90         delete iSecurityPolicy;
       
    91         iSecurityPolicy = NULL;
       
    92         if (iUserPreferences)
       
    93         {
       
    94             delete iUserPreferences;
       
    95             iUserPreferences = NULL;
       
    96         }
       
    97         if (iStorageHandler)
       
    98         {
       
    99             iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid1);
       
   100             iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   101             iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid2);
       
   102             iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid2);
       
   103             delete iStorageHandler;
       
   104             iStorageHandler = NULL;
       
   105         }
       
   106         if (iSuitePref1)
       
   107         {
       
   108             delete iSuitePref1;
       
   109             iSuitePref1 = NULL;
       
   110         }
       
   111         if (iSuitePref2)
       
   112         {
       
   113             delete iSuitePref2;
       
   114             iSuitePref2 = NULL;
       
   115         }
       
   116         if (iCustomAttr)
       
   117         {
       
   118             delete iCustomAttr;
       
   119             iCustomAttr = NULL;
       
   120         }
       
   121     }
       
   122 };
       
   123 
       
   124 TEST(TestUserPreferences, TestUserPreferencesL)
       
   125 {
       
   126     // no "known" suites
       
   127     CHECK(iUserPreferences != NULL);
       
   128     iIterator = iUserPreferences->IteratorL();
       
   129     CHECK(iIterator != NULL);
       
   130     while (iIterator->HasNext())
       
   131     {
       
   132         MMIDletSuiteUserPreferences* suitePref = iIterator->NextL();
       
   133         CHECK(suitePref != NULL);
       
   134         if (suitePref->Id() == iAppId1 || suitePref->Id() == iAppId2)
       
   135         {
       
   136             delete suitePref;
       
   137             suitePref = NULL;
       
   138             CHECK(false);
       
   139         }
       
   140         delete suitePref;
       
   141         suitePref = NULL;
       
   142     }
       
   143     // call NextL when the Iterator has reached the end
       
   144     int error;
       
   145     TRAP(error, iIterator->NextL());
       
   146     CHECK(error == KErrNotFound);
       
   147     // two "known" midlet suites
       
   148     delete iIterator;
       
   149     iIterator = NULL;
       
   150     iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid1);
       
   151     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   152     iStorageHandler->populateApplicationPackageTable(iAppUid1, L"appName1", L"appVendor1", L"1.1.0");
       
   153     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD");
       
   154     iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid2);
       
   155     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid2);
       
   156     iStorageHandler->populateApplicationPackageTable(iAppUid2, L"appName2", L"appVendor2", L"2.0.1");
       
   157     iStorageHandler->populateMIDPPackageTable(iAppUid2, L"OPD");
       
   158     iIterator = iUserPreferences->IteratorL();
       
   159     CHECK(iIterator != NULL);
       
   160     bool app1Found = false;
       
   161     bool app2Found = false;
       
   162     while (iIterator->HasNext())
       
   163     {
       
   164         MMIDletSuiteUserPreferences* suitePref = iIterator->NextL();
       
   165         CHECK(suitePref != NULL);
       
   166         TUint32 id = suitePref->Id();
       
   167         if (id == iAppId1)
       
   168         {
       
   169             app1Found = true;
       
   170             iSuitePref1 = suitePref;
       
   171             continue;
       
   172         }
       
   173         else if (id == iAppId2)
       
   174         {
       
   175             app2Found = true;
       
   176             iSuitePref2 = suitePref;
       
   177             continue;
       
   178         }
       
   179         delete suitePref;
       
   180         suitePref = NULL;
       
   181     }
       
   182     CHECK((app1Found == true) && (app2Found == true));
       
   183     int error1, error2;
       
   184     // check the unsupported methods
       
   185     TRAP(error1, iSuitePref1->SecurityPreferencesL());
       
   186     TRAP(error2, iSuitePref2->SecurityPreferencesL());
       
   187     CHECK((error1 == KErrNotSupported) && (error2 == KErrNotSupported));
       
   188     // null/unknown property
       
   189     _LIT(KDummy, "dummy");
       
   190     TPtrC value;
       
   191     iSuitePref1->PropertyL(KNullDesC(), value);
       
   192     CHECK(value.Length() == 0);
       
   193     iSuitePref1->PropertyL(KDummy, value);
       
   194     CHECK(value.Length() == 0);
       
   195     // check the known properties
       
   196     iSuitePref1->PropertyL(KMidletSuiteName, value);
       
   197     CHECK(value.Compare(KAppName1) == 0);
       
   198     iSuitePref1->PropertyL(KMidletSuiteVendor, value);
       
   199     CHECK(value.Compare(KAppVendor1) == 0);
       
   200     iSuitePref1->PropertyL(KMidletSuiteVersion, value);
       
   201     CHECK(value.Compare(KAppVersion1) == 0);
       
   202     iSuitePref2->PropertyL(KMidletSuiteName, value);
       
   203     CHECK(value.Compare(KAppName2) == 0);
       
   204     iSuitePref2->PropertyL(KMidletSuiteVendor, value);
       
   205     CHECK(value.Compare(KAppVendor2) == 0);
       
   206     iSuitePref2->PropertyL(KMidletSuiteVersion, value);
       
   207     CHECK(value.Compare(KAppVersion2) == 0);
       
   208 }
       
   209 
       
   210 TEST(TestUserPreferences, TestCustomAttributesL)
       
   211 {
       
   212     CHECK(iUserPreferences != NULL);
       
   213     // two "known" suites
       
   214     iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid1);
       
   215     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   216     iStorageHandler->populateApplicationPackageTable(iAppUid1, L"appName1", L"appVendor1", L"1.1.0");
       
   217     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD");
       
   218     iStorageHandler->cleanStorage(APPLICATION_PACKAGE_TABLE, iAppUid2);
       
   219     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid2);
       
   220     iStorageHandler->populateApplicationPackageTable(iAppUid2, L"appName2", L"appVendor2", L"2.0.1");
       
   221     iStorageHandler->populateMIDPPackageTable(iAppUid2, L"OPD");
       
   222     iCustomAttr = iUserPreferences->CustomAttributesL(iAppId1);
       
   223     CHECK(iCustomAttr != NULL);
       
   224     // unsupported methods
       
   225     int error;
       
   226     TRAP(error, iCustomAttr->AddL(KNullDesC(), KNullDesC(), 0));
       
   227     CHECK(error == KErrNotSupported);
       
   228     TRAP(error, iCustomAttr->RemoveAllL());
       
   229     CHECK(error == KErrNotSupported);
       
   230     // unknown attributes
       
   231     _LIT(KDummy, "dummy");
       
   232     TUint32 flags;
       
   233     TRAP(error, iCustomAttr->GetL(KDummy(), flags));
       
   234     CHECK(error == KErrNotSupported);
       
   235     TRAP(error, iCustomAttr->UpdateL(KDummy(), KDummy(), flags));
       
   236     CHECK(error == KErrNotSupported);
       
   237     // retrieval of values for OnScreenKeyboardAttribute (unknown, "no", "gameactions", "navigationkeys")
       
   238     bool success;
       
   239     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   240     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD", L"unknown");
       
   241     TRAP(error, iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags));
       
   242     CHECK(error == KErrNotFound);
       
   243     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   244     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD", L"3");
       
   245     TRAP(error, iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags));
       
   246     CHECK(error == KErrNotFound);
       
   247     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   248     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD", L"0");
       
   249     HBufC* attrValue;
       
   250     attrValue = iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags);
       
   251     success = (attrValue->Compare(KValueNo()) == 0);
       
   252     delete attrValue;
       
   253     attrValue = NULL;
       
   254     CHECK(success);
       
   255     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   256     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD", L"1");
       
   257     attrValue = iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags);
       
   258     success = (attrValue->Compare(KValueGameactions()) == 0);
       
   259     delete attrValue;
       
   260     attrValue = NULL;
       
   261     CHECK(success);
       
   262     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   263     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD", L"2");
       
   264     attrValue = iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags);
       
   265     success = (attrValue->Compare(KValueNavigationkeys()) == 0);
       
   266     delete attrValue;
       
   267     attrValue = NULL;
       
   268     CHECK(success);
       
   269     // updating with different values (unknown, "no", "gameactions", "navigationkeys")
       
   270     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   271     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD");
       
   272     iCustomAttr->UpdateL(KOnScreenKeyboardAttribute, KDummy, flags);
       
   273     std::wstring value = L"";
       
   274     iStorageHandler->readAttribute(MIDP_PACKAGE_TABLE, iAppUid1, ON_SCREEN_KEYPAD, value);
       
   275     CHECK(value.size() == 0);
       
   276     iCustomAttr->UpdateL(KOnScreenKeyboardAttribute, KValueNo, flags);
       
   277     iStorageHandler->readAttribute(MIDP_PACKAGE_TABLE, iAppUid1, ON_SCREEN_KEYPAD, value);
       
   278     CHECK(value.compare(L"0") == 0);
       
   279     iCustomAttr->UpdateL(KOnScreenKeyboardAttribute, KValueGameactions, flags);
       
   280     iStorageHandler->readAttribute(MIDP_PACKAGE_TABLE, iAppUid1, ON_SCREEN_KEYPAD, value);
       
   281     CHECK(value.compare(L"1") == 0);
       
   282     iCustomAttr->UpdateL(KOnScreenKeyboardAttribute, KValueNavigationkeys, flags);
       
   283     iStorageHandler->readAttribute(MIDP_PACKAGE_TABLE, iAppUid1, ON_SCREEN_KEYPAD, value);
       
   284     CHECK(value.compare(L"2") == 0);
       
   285     // OnScreenKeyboardAttribute not found
       
   286     iStorageHandler->cleanStorage(MIDP_PACKAGE_TABLE, iAppUid1);
       
   287     iStorageHandler->populateMIDPPackageTable(iAppUid1, L"UTPD");
       
   288     TRAP(error, iCustomAttr->GetL(KOnScreenKeyboardAttribute, flags));
       
   289     CHECK(error == KErrNotFound);
       
   290 }