javatools/usersettingsconfigurator/tsrc/src/testusersettingsconfigurator.cpp
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     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 #include <vector>
       
    21 #include <e32std.h>
       
    22 
       
    23 #include "TestHarness.h"
       
    24 #include "teststoragehandler.h"
       
    25 #include "j2me/midp2/security/MFunctionGroupBinding.h"
       
    26 #include "j2me/midp2/security/MExtendedProtectionDomain.h"
       
    27 #include "j2me/midp2/security/MFunctionGroup.h"
       
    28 #include "j2me/midp2/security/MPermission.h"
       
    29 #include "j2me/midp2/security/MSecurityPolicyV2.h"
       
    30 
       
    31 using namespace MIDP;
       
    32 
       
    33 const TInt KSecurityPolicyFlags = 0x0200;
       
    34 _LIT(KSecurityPolicy, "midp2_rp");
       
    35 extern MSecurityPolicyV2* GetSecurityPolicyL(TUint32 aFlags, const TDesC& aPolicyName);
       
    36 const TInt BLANKET        = 1;
       
    37 const TInt SESSION        = 2;
       
    38 const TInt ONESHOT        = 3;
       
    39 const TInt NO             = 4;
       
    40 const TInt UNKNOWN        = 5;
       
    41 const TInt INVALID        = 6;
       
    42 const TInt EMPTY          = 7;
       
    43 const TInt GAMEACTIONS    = 8;
       
    44 const TInt NAVIGATIONKEYS = 9;
       
    45 
       
    46 TEST_GROUP(TestUserSettingsConfigurator)
       
    47 {
       
    48     MSecurityPolicyV2* iSecurityPolicy;
       
    49     TEST_SETUP()
       
    50     {
       
    51         iSecurityPolicy = GetSecurityPolicyL(KSecurityPolicyFlags, KSecurityPolicy);
       
    52     }
       
    53 
       
    54     TEST_TEARDOWN()
       
    55     {
       
    56         delete iSecurityPolicy;
       
    57     }
       
    58 };
       
    59 
       
    60 HBufC8* GetCustomAttributes(const TDesC& aValue)
       
    61 {
       
    62     _LIT(KOnScreenKeyboardAttribute, "Nokia-MIDlet-On-Screen-Keypad");
       
    63     _LIT8(KSpace, " ");
       
    64 
       
    65     TInt countLength = 1;
       
    66     TInt spaceLength = KSpace().Length();
       
    67     TInt keyLength       = KOnScreenKeyboardAttribute().Length();
       
    68     TInt keyByteLength   = keyLength + keyLength;
       
    69     TInt valueLength     = 2 + aValue.Length();
       
    70     TInt valueByteLength = valueLength + valueLength;
       
    71     HBufC8* customAttributesValue = HBufC8::NewL(
       
    72                                         4 /* count */
       
    73                                         + 4 /* key length */
       
    74                                         + keyByteLength /* key */
       
    75                                         + 3 /* max padding */
       
    76                                         + 4 /* value length */
       
    77                                         + 4 /* flag */
       
    78                                         + valueByteLength /* value */
       
    79                                         + 3 /* max padding */);
       
    80     TPtr8 ptr = customAttributesValue->Des();
       
    81     ptr.Append(TPtrC8((const TUint8*)&countLength, 4));
       
    82     ptr.Append(TPtrC8((const TUint8*)&keyLength, 4));
       
    83     ptr.Append(TPtrC8((const TUint8*)KOnScreenKeyboardAttribute().Ptr(), keyByteLength));
       
    84     switch (keyByteLength % 4)
       
    85     {
       
    86     case 1:
       
    87         ptr.Append(KSpace);
       
    88     case 2:
       
    89         ptr.Append(KSpace);
       
    90     case 3:
       
    91         ptr.Append(KSpace);
       
    92         break;
       
    93     }
       
    94     // flag
       
    95     ptr.Append(TPtrC8((const TUint8*)&valueLength, 4));
       
    96     ptr.Append(KSpace);
       
    97     ptr.Append(KSpace);
       
    98     ptr.Append(KSpace);
       
    99     ptr.Append(KSpace);
       
   100     ptr.Append(TPtrC8((const TUint8*)aValue.Ptr(), (2 * aValue.Length())));
       
   101     switch (valueByteLength % 4)
       
   102     {
       
   103     case 1:
       
   104         ptr.Append(KSpace);
       
   105     case 2:
       
   106         ptr.Append(KSpace);
       
   107     case 3:
       
   108         ptr.Append(KSpace);
       
   109         break;
       
   110     }
       
   111     return customAttributesValue;
       
   112 }
       
   113 
       
   114 HBufC8* GetSecurityPreferences(const MSecurityPolicyV2& aSecurityPolicy, std::wstring aSecuritySettingsName, int aInteractionMode)
       
   115 {
       
   116     int BITS_PER_BYTE = 8;
       
   117     int BITS_PER_GROUP = 2;
       
   118     int GROUPS_PER_BYTE = (BITS_PER_BYTE/BITS_PER_GROUP);
       
   119     RPointerArray<const MFunctionGroupBinding>  bindings;
       
   120     aSecurityPolicy.UntrustedProtectionDomainL().FunctionGroupBindingsL(bindings);
       
   121     TInt count = bindings.Count();
       
   122     std::string preferences = "";
       
   123     std::string interactionMode;
       
   124     HBufC8* prefs = HBufC8::NewMaxL((count + 3)/4);
       
   125     TPtr8 ptr = prefs->Des();
       
   126     TInt length = ptr.Length();
       
   127     for (TInt i = 0; i < length; i++)
       
   128     {
       
   129         ptr[i] = 0;
       
   130     }
       
   131     for (TInt i = 0; i < count; i++)
       
   132     {
       
   133         const MFunctionGroupBinding& binding = *(bindings[i]);
       
   134         HBufC* name = binding.FunctionGroup().Name().AllocLC();
       
   135         std::wstring securitySettingsName = std::wstring((wchar_t*) name->Ptr(), name->Length());
       
   136         CleanupStack::PopAndDestroy(name);
       
   137         TInt    byteIndex = ((i) * BITS_PER_GROUP) / BITS_PER_BYTE;
       
   138         TInt    bitIndex  = ((i) - (byteIndex * GROUPS_PER_BYTE)) * BITS_PER_GROUP;
       
   139         TUint8  byte      = ptr[byteIndex];
       
   140         if (securitySettingsName == aSecuritySettingsName)
       
   141         {
       
   142             switch (aInteractionMode)
       
   143             {
       
   144             case ONESHOT:
       
   145                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (2 << bitIndex));
       
   146                 break;
       
   147             case SESSION:
       
   148                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (3 << bitIndex));
       
   149                 break;
       
   150             case BLANKET:
       
   151                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (1 << bitIndex));
       
   152                 break;
       
   153             case NO:
       
   154                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (0 << bitIndex));
       
   155                 break;
       
   156             case UNKNOWN:
       
   157                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (4 << bitIndex));
       
   158                 break;
       
   159             }
       
   160         }
       
   161         else
       
   162         {
       
   163             // get the default one from the legacy policy
       
   164             switch (binding.DefaultInteractionMode())
       
   165             {
       
   166             case MPermission::EDenied:
       
   167                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (0 << bitIndex));
       
   168                 break;
       
   169             case MPermission::EBlanket:
       
   170                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (1 << bitIndex));
       
   171                 break;
       
   172             case MPermission::EOneshot:
       
   173                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (2 << bitIndex));
       
   174                 break;
       
   175             case MPermission::ESession:
       
   176                 ptr[byteIndex] = (TText8)((byte & ~(3 << bitIndex)) | (3 << bitIndex));
       
   177                 break;
       
   178             }
       
   179         }
       
   180     }
       
   181     bindings.Close();
       
   182     return prefs;
       
   183 }
       
   184 
       
   185 
       
   186 std::wstring GetDefaultInteractionModeFromPolicy(const MSecurityPolicyV2& aSecurityPolicy, std::wstring aSecuritySettingsName)
       
   187 {
       
   188     RPointerArray<const MFunctionGroupBinding>  bindings;
       
   189     aSecurityPolicy.UntrustedProtectionDomainL().FunctionGroupBindingsL(bindings);
       
   190     TInt count = bindings.Count();
       
   191     std::wstring interactionMode = L"";
       
   192     for (TInt i = 0; i < count; i++)
       
   193     {
       
   194         const MFunctionGroupBinding& binding = *(bindings[i]);
       
   195         HBufC* name = binding.FunctionGroup().Name().AllocLC();
       
   196         std::wstring securitySettingsName = std::wstring((wchar_t*) name->Ptr(), name->Length());
       
   197         CleanupStack::PopAndDestroy(name);
       
   198         if (securitySettingsName == aSecuritySettingsName)
       
   199         {
       
   200             // get the default one from the legacy policy
       
   201             switch (binding.DefaultInteractionMode())
       
   202             {
       
   203             case MPermission::EDenied:
       
   204                 interactionMode = L"4";
       
   205                 break;
       
   206             case MPermission::EBlanket:
       
   207                 interactionMode = L"3";
       
   208                 break;
       
   209             case MPermission::EOneshot:
       
   210                 interactionMode = L"1";
       
   211                 break;
       
   212             case MPermission::ESession:
       
   213                 interactionMode = L"2";
       
   214                 break;
       
   215             }
       
   216             break;
       
   217         }
       
   218     }
       
   219     bindings.Close();
       
   220     return interactionMode;
       
   221 }
       
   222 
       
   223 void ExecuteConfigurator()
       
   224 {
       
   225     _LIT(KJavaUsersettingsConfiguratorExe, "javausersettingsconfigurator.exe");
       
   226     RProcess proc;
       
   227     int rc = proc.Create(KJavaUsersettingsConfiguratorExe, KNullDesC());
       
   228     if (rc == KErrNone)
       
   229     {
       
   230         proc.Resume();
       
   231         TRequestStatus status;
       
   232         proc.Logon(status);
       
   233         User::WaitForRequest(status);
       
   234     }
       
   235     proc.Close();
       
   236 }
       
   237 
       
   238 TEST(TestUserSettingsConfigurator, TestUserSecuritySettingsL)
       
   239 {
       
   240     // init
       
   241     TInt err;
       
   242     TestStorageHandler* storage = TestStorageHandler::NewL();
       
   243     CleanupStack::PushL(storage);
       
   244     std::vector<SecuritySettings*> securitySettings;
       
   245     bool success = false;
       
   246     // unknown unsigned suite
       
   247     storage->cleanup(TUint32(0), L"appUid");
       
   248     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), _L8("prefs"));
       
   249     storage->populateOmjStorageL(L"appUid", L"name1", L"vendor", L"version", L"settingsName", L"1");
       
   250     ExecuteConfigurator();
       
   251     securitySettings.clear();
       
   252     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   253     storage->cleanup(TUint32(0), L"appUid");
       
   254     success = (securitySettings.size() == 0);
       
   255     for (int i=0; i<securitySettings.size(); i++)
       
   256     {
       
   257         delete securitySettings[i];
       
   258     }
       
   259     CHECK(success);
       
   260     // OMJ "Net Access" settings: No for current interaction mode, Oneshot&Session for allowed interaction modes
       
   261     // Legacy settings: unknown value for "Net Access"
       
   262     // -> new OMJ settings: the default interaction mode taken from the legacy policy for "Net Access"
       
   263     storage->cleanup(TUint32(0), L"appUid");
       
   264     HBufC8* prefs = GetSecurityPreferences(*iSecurityPolicy, L"Local Connectivity", UNKNOWN);
       
   265     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), *prefs);
       
   266     delete prefs;
       
   267     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Net Access", L"4" , L"12");
       
   268     ExecuteConfigurator();
       
   269     securitySettings.clear();
       
   270     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   271     storage->cleanup(TUint32(0), L"appUid");
       
   272     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == GetDefaultInteractionModeFromPolicy(*iSecurityPolicy, L"Net Access") && securitySettings[0]->iSettingsName == L"Net Access");
       
   273     for (int i=0; i<securitySettings.size(); i++)
       
   274     {
       
   275         delete securitySettings[i];
       
   276     }
       
   277     CHECK(success);
       
   278     // OMJ "Read User Data Access" settings: No for current interaction mode, Session for allowed interaction modes
       
   279     // Legacy settings: unknown value for "Read User Data Access"
       
   280     // -> new OMJ settings: unchanged settings (the default interaction mode taken from the legacy policy for Net Access is not found among the allowed interaction modes)
       
   281     storage->cleanup(TUint32(0), L"appUid");
       
   282     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), _L8("prefs"));
       
   283     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Read User Data Access", L"4" , L"4");
       
   284     ExecuteConfigurator();
       
   285     securitySettings.clear();
       
   286     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   287     storage->cleanup(TUint32(0), L"appUid");
       
   288     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == L"4" && securitySettings[0]->iSettingsName == L"Read User Data Access");
       
   289     for (int i=0; i<securitySettings.size(); i++)
       
   290     {
       
   291         delete securitySettings[i];
       
   292     }
       
   293     CHECK(success);
       
   294     // OMJ "Write User Data Access" settings: No for current interaction mode, Oneshot&Session for allowed interaction modes
       
   295     // Legacy settings: oneshot for "Write User Data Access"
       
   296     // -> new OMJ settings: oneshot for "Write User Data Access"
       
   297     storage->cleanup(TUint32(0), L"appUid");
       
   298     prefs = GetSecurityPreferences(*iSecurityPolicy, L"Write User Data Access", ONESHOT);
       
   299     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), *prefs);
       
   300     delete prefs;
       
   301     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Write User Data Access", L"4" , L"12");
       
   302     ExecuteConfigurator();
       
   303     securitySettings.clear();
       
   304     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   305     storage->cleanup(TUint32(0), L"appUid");
       
   306     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == L"1" && securitySettings[0]->iSettingsName == L"Write User Data Access");
       
   307     for (int i=0; i<securitySettings.size(); i++)
       
   308     {
       
   309         delete securitySettings[i];
       
   310     }
       
   311     CHECK(success);
       
   312     // OMJ "Local Connectivity" settings: Oneshot for current interaction mode, Oneshot&Session for allowed interaction modes
       
   313     // Legacy settings: session for "Local Connectivity"
       
   314     // -> new OMJ settings: session for "Local Connectivity"
       
   315     storage->cleanup(TUint32(0), L"appUid");
       
   316     prefs = GetSecurityPreferences(*iSecurityPolicy, L"Local Connectivity", SESSION);
       
   317     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), *prefs);
       
   318     delete prefs;
       
   319     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Local Connectivity", L"1" , L"12");
       
   320     ExecuteConfigurator();
       
   321     securitySettings.clear();
       
   322     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   323     storage->cleanup(TUint32(0), L"appUid");
       
   324     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == L"2" && securitySettings[0]->iSettingsName == L"Local Connectivity");
       
   325     for (int i=0; i<securitySettings.size(); i++)
       
   326     {
       
   327         delete securitySettings[i];
       
   328     }
       
   329     CHECK(success);
       
   330     // OMJ "Messaging" settings: Oneshot for current interaction mode, Oneshot&Session&Blanket for allowed interaction modes
       
   331     // Legacy settings: Blanket for "Messaging"
       
   332     // -> new OMJ settings: Blanket for "Messaging"
       
   333     storage->cleanup(TUint32(0), L"appUid");
       
   334     prefs = GetSecurityPreferences(*iSecurityPolicy, L"Messaging", BLANKET);
       
   335     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), *prefs);
       
   336     delete prefs;
       
   337     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Messaging", L"1" , L"14");
       
   338     ExecuteConfigurator();
       
   339     securitySettings.clear();
       
   340     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   341     storage->cleanup(TUint32(0), L"appUid");
       
   342     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == L"3" && securitySettings[0]->iSettingsName == L"Messaging");
       
   343     for (int i=0; i<securitySettings.size(); i++)
       
   344     {
       
   345         delete securitySettings[i];
       
   346     }
       
   347     CHECK(success);
       
   348     // OMJ "ApplicationAutoInvocation" settings: Oneshot for current interaction mode, Oneshot&No for allowed interaction modes
       
   349     // Legacy settings: No for "ApplicationAutoInvocation"
       
   350     // -> new OMJ settings: No for "ApplicationAutoInvocation"
       
   351     storage->cleanup(TUint32(0), L"appUid");
       
   352     prefs = GetSecurityPreferences(*iSecurityPolicy, L"Application Auto Invocation", NO);
       
   353     storage->populateLegacyStorageL(TUint32(0), _L("name"), _L("vendor"), _L("version"), *prefs);
       
   354     delete prefs;
       
   355     storage->populateOmjStorageL(L"appUid", L"name", L"vendor", L"version", L"Application Auto Invocation", L"1" , L"9");
       
   356     ExecuteConfigurator();
       
   357     securitySettings.clear();
       
   358     storage->retrieveSecuritySettings(L"name", L"vendor", L"version", securitySettings);
       
   359     storage->cleanup(TUint32(0), L"appUid");
       
   360     success = (securitySettings.size() == 1 && securitySettings[0]->iCurrentInteractionMode == L"4" && securitySettings[0]->iSettingsName == L"Application Auto Invocation");
       
   361     for (int i=0; i<securitySettings.size(); i++)
       
   362     {
       
   363         delete securitySettings[i];
       
   364     }
       
   365     CHECK(success);
       
   366     // cleanup
       
   367     CleanupStack::PopAndDestroy(storage);
       
   368 }
       
   369 
       
   370 TEST(TestUserSettingsConfigurator, TestUserCustomAttributesSettingsL)
       
   371 {
       
   372     // init
       
   373     TInt err;
       
   374     TestStorageHandler* storage = TestStorageHandler::NewL();
       
   375     CleanupStack::PushL(storage);
       
   376     bool success = false;
       
   377     // "gameactions" value
       
   378     HBufC8* customAttributes = GetCustomAttributes(_L("gameactions"));
       
   379     storage->populateLegacyStorageL(TUint32(12345), *customAttributes);
       
   380     delete customAttributes;
       
   381     storage->populateOmjStorageL(L"[00003039]", L"0");
       
   382     ExecuteConfigurator();
       
   383     std::wstring onScreenKeypad;
       
   384     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   385     storage->cleanup(TUint32(0), L"[00003039]");
       
   386     success = (onScreenKeypad == L"1");
       
   387     CHECK(success);
       
   388     // "navigationkeys" value
       
   389     customAttributes = GetCustomAttributes(_L("navigationkeys"));
       
   390     storage->populateLegacyStorageL(TUint32(12345), *customAttributes);
       
   391     delete customAttributes;
       
   392     storage->populateOmjStorageL(L"[00003039]", L"0");
       
   393     ExecuteConfigurator();
       
   394     onScreenKeypad = L"";
       
   395     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   396     storage->cleanup(TUint32(0), L"[00003039]");
       
   397     success = (onScreenKeypad == L"2");
       
   398     CHECK(success);
       
   399     // "no" value
       
   400     customAttributes = GetCustomAttributes(_L("no"));
       
   401     storage->populateLegacyStorageL(TUint32(12345), *customAttributes);
       
   402     delete customAttributes;
       
   403     storage->populateOmjStorageL(L"[00003039]", L"2");
       
   404     ExecuteConfigurator();
       
   405     onScreenKeypad = L"";
       
   406     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   407     storage->cleanup(TUint32(0), L"[00003039]");
       
   408     success = (onScreenKeypad == L"0");
       
   409     CHECK(success);
       
   410     // unknown value
       
   411     customAttributes = GetCustomAttributes(_L("unknown"));
       
   412     storage->populateLegacyStorageL(TUint32(12345), *customAttributes);
       
   413     delete customAttributes;
       
   414     storage->populateOmjStorageL(L"[00003039]", L"2");
       
   415     ExecuteConfigurator();
       
   416     onScreenKeypad = L"";
       
   417     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   418     storage->cleanup(TUint32(0), L"[00003039]");
       
   419     success = (onScreenKeypad == L"2");
       
   420     CHECK(success);
       
   421     // no value at all
       
   422     storage->populateOmjStorageL(L"[00003039]", L"1");
       
   423     ExecuteConfigurator();
       
   424     onScreenKeypad = L"";
       
   425     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   426     storage->cleanup(TUint32(0), L"[00003039]");
       
   427     success = (onScreenKeypad == L"1");
       
   428     CHECK(success);
       
   429     // empty value
       
   430     customAttributes = GetCustomAttributes(_L(""));
       
   431     storage->populateLegacyStorageL(TUint32(12345), *customAttributes);
       
   432     delete customAttributes;
       
   433     storage->populateOmjStorageL(L"[00003039]", L"1");
       
   434     ExecuteConfigurator();
       
   435     onScreenKeypad = L"";
       
   436     storage->retrieveOnScreenKeypad(L"[00003039]", onScreenKeypad);
       
   437     storage->cleanup(TUint32(0), L"[00003039]");
       
   438     success = (onScreenKeypad == L"1");
       
   439     CHECK(success);
       
   440     // cleanup
       
   441     CleanupStack::PopAndDestroy(storage);
       
   442 }