javacommons/security/tsrc/javasrc/com/nokia/mj/impl/security/midp/storage/SecurityStorageTests.java
changeset 21 2a9601315dfc
child 64 0ea12c182930
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 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 
       
    19 package com.nokia.mj.impl.security.midp.storage;
       
    20 
       
    21 import com.nokia.mj.impl.installer.utils.InstallerMain;
       
    22 import com.nokia.mj.impl.security.midp.storage.SecurityStorage;
       
    23 import com.nokia.mj.impl.security.midp.common.PolicyBasedPermission;
       
    24 import com.nokia.mj.impl.security.midp.common.PolicyBasedPermissionImpl;
       
    25 import com.nokia.mj.impl.security.midp.common.UserSecuritySettings;
       
    26 import com.nokia.mj.impl.security.midp.common.UserSecuritySettingsImpl;
       
    27 import com.nokia.mj.impl.security.midp.common.AuthorizationRestriction;
       
    28 import com.nokia.mj.impl.security.midp.common.AuthorizationRestrictionAttribute;
       
    29 import com.nokia.mj.impl.security.midp.common.AuthenticationAttribute;
       
    30 import com.nokia.mj.impl.security.midp.common.SecurityAttributes;
       
    31 import com.nokia.mj.impl.security.midp.common.GeneralSecuritySettings;
       
    32 import com.nokia.mj.impl.utils.Uid;
       
    33 import com.nokia.mj.impl.storage.StorageFactory;
       
    34 import com.nokia.mj.impl.storage.StorageSession;
       
    35 import com.nokia.mj.impl.storage.StorageException;
       
    36 import com.nokia.mj.impl.storage.StorageNames;
       
    37 import com.nokia.mj.impl.storage.StorageAttribute;
       
    38 import com.nokia.mj.impl.storage.StorageEntry;
       
    39 import java.util.Vector;
       
    40 
       
    41 import j2meunit.framework.Test;
       
    42 import j2meunit.framework.TestCase;
       
    43 import j2meunit.framework.TestMethod;
       
    44 import j2meunit.framework.TestSuite;
       
    45 
       
    46 /**
       
    47  * SecurityDescriptorAttributes unit tests.
       
    48  */
       
    49 public class SecurityStorageTests extends TestCase implements InstallerMain
       
    50 {
       
    51 
       
    52     // data being tested
       
    53     private SecurityStorage storage = null;
       
    54 
       
    55     // Begin j2meunit test framework setup
       
    56     public void installerMain(String[] args)
       
    57     {
       
    58         TestSuite suite = new TestSuite(this.getClass().getName());
       
    59 
       
    60         suite.addTest(new SecurityStorageTests("testOpenAndClose", new TestMethod()
       
    61         {
       
    62             public void run(TestCase tc)
       
    63             {
       
    64                 ((SecurityStorageTests)tc).testOpenAndClose();
       
    65             }
       
    66         }));
       
    67         suite.addTest(new SecurityStorageTests("testGrantedPermissions", new TestMethod()
       
    68         {
       
    69             public void run(TestCase tc)
       
    70             {
       
    71                 ((SecurityStorageTests)tc).testGrantedPermissions();
       
    72             }
       
    73         }));
       
    74         suite.addTest(new SecurityStorageTests("testAuthenticationData", new TestMethod()
       
    75         {
       
    76             public void run(TestCase tc)
       
    77             {
       
    78                 ((SecurityStorageTests)tc).testAuthenticationData();
       
    79             }
       
    80         }));
       
    81         suite.addTest(new SecurityStorageTests("testUserSettingsData", new TestMethod()
       
    82         {
       
    83             public void run(TestCase tc)
       
    84             {
       
    85                 ((SecurityStorageTests)tc).testUserSettingsData();
       
    86             }
       
    87         }));
       
    88         suite.addTest(new SecurityStorageTests("testAppAccessAuthorizationRestrictionsData", new TestMethod()
       
    89         {
       
    90             public void run(TestCase tc)
       
    91             {
       
    92                 ((SecurityStorageTests)tc).testAppAccessAuthorizationRestrictionsData();
       
    93             }
       
    94         }));
       
    95         suite.addTest(new SecurityStorageTests("testMisc", new TestMethod()
       
    96         {
       
    97             public void run(TestCase tc)
       
    98             {
       
    99                 ((SecurityStorageTests)tc).testMisc();
       
   100             }
       
   101         }));
       
   102 
       
   103         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
   104     }
       
   105 
       
   106     public SecurityStorageTests()
       
   107     {
       
   108     }
       
   109 
       
   110     public SecurityStorageTests(String aTestName, TestMethod aTestMethod)
       
   111     {
       
   112         super(aTestName, aTestMethod);
       
   113     }
       
   114 
       
   115     public void assertFalse(String aMsg, boolean aCondition)
       
   116     {
       
   117         assertTrue(aMsg, !aCondition);
       
   118     }
       
   119 
       
   120     // End j2meunit test framework setup
       
   121 
       
   122     protected void setUp()
       
   123     {
       
   124         storage = new SecurityStorage();
       
   125     }
       
   126 
       
   127     protected void tearDown()
       
   128     {
       
   129         storage.close();
       
   130     }
       
   131 
       
   132     protected void testOpenAndClose()
       
   133     {
       
   134         // another session
       
   135         StorageSession session = StorageFactory.createSession();
       
   136         storage = new SecurityStorage(session);
       
   137         assertTrue(storage != null);
       
   138         storage = new SecurityStorage();
       
   139         assertTrue(storage != null);
       
   140     }
       
   141 
       
   142     protected void testGrantedPermissions()
       
   143     {
       
   144         Vector readPermissions = null;
       
   145         Vector grantedPermissions = null;
       
   146         Vector grantedPermissions2 = null;
       
   147         Uid appUID = Uid.createUid("myUID");
       
   148         Uid appUID2 = Uid.createUid("myUID2");
       
   149         // null values
       
   150         storage.readGrantedPermissions(null);
       
   151         storage.writeGrantedPermissions(null, null, null);
       
   152         storage.removeGrantedPermissions(null);
       
   153         assertTrue(true);
       
   154         // full read/write/remove cycle with default values for current interaction mode
       
   155         storage.removeGrantedPermissions(appUID);
       
   156         grantedPermissions = storage.readGrantedPermissions(appUID);
       
   157         assertTrue(grantedPermissions == null);
       
   158         grantedPermissions = new Vector();
       
   159         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   160         storage.removeGrantedPermissions(appUID);
       
   161         assertTrue(true);
       
   162         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   163                                           "permission1",
       
   164                                           "target1",
       
   165                                           "action1",
       
   166                                           PolicyBasedPermission.ALLOWED_TYPE));
       
   167         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   168                                           "permission2",
       
   169                                           "target2",
       
   170                                           "action2",
       
   171                                           PolicyBasedPermission.ASSIGNED_TYPE));
       
   172         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   173                                           "permission3",
       
   174                                           "target3",
       
   175                                           "action3",
       
   176                                           new UserSecuritySettingsImpl("settingsname",
       
   177                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   178                                                                        new int[] { UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   179                                                                                    UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   180                                                                                    UserSecuritySettings.BLANKET_INTERACTION_MODE,
       
   181                                                                                    UserSecuritySettings.NO_INTERACTION_MODE
       
   182                                                                                  })));
       
   183         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   184         readPermissions = storage.readGrantedPermissions(appUID);
       
   185         assertTrue(readPermissions != null
       
   186                    && readPermissions.size() == grantedPermissions.size()
       
   187                    && checkGrantedPermissions(readPermissions, grantedPermissions));
       
   188         storage.removeGrantedPermissions(appUID);
       
   189         readPermissions = storage.readGrantedPermissions(appUID);
       
   190         assertTrue(readPermissions == null);
       
   191         // full read/write/remove cycle for update -> the old settings are retained
       
   192         storage.removeGrantedPermissions(appUID);
       
   193         grantedPermissions = storage.readGrantedPermissions(appUID);
       
   194         assertTrue(grantedPermissions == null);
       
   195         grantedPermissions = new Vector();
       
   196         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   197         storage.removeGrantedPermissions(appUID);
       
   198         assertTrue(true);
       
   199         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   200                                           "permission1",
       
   201                                           "target1",
       
   202                                           "action1",
       
   203                                           PolicyBasedPermission.ALLOWED_TYPE));
       
   204         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   205                                           "permission2",
       
   206                                           "target2",
       
   207                                           "action2",
       
   208                                           PolicyBasedPermission.ASSIGNED_TYPE));
       
   209         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   210                                           "permission3",
       
   211                                           "target3",
       
   212                                           "action3",
       
   213                                           new UserSecuritySettingsImpl("settingsname",
       
   214                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   215                                                                        new int[] { UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   216                                                                                    UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   217                                                                                    UserSecuritySettings.BLANKET_INTERACTION_MODE,
       
   218                                                                                    UserSecuritySettings.NO_INTERACTION_MODE
       
   219                                                                                  })));
       
   220         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   221         readPermissions = storage.readGrantedPermissions(appUID);
       
   222         assertTrue(readPermissions != null
       
   223                    && readPermissions.size() == grantedPermissions.size()
       
   224                    && checkGrantedPermissions(readPermissions, grantedPermissions));
       
   225         grantedPermissions2 = new Vector();
       
   226         grantedPermissions2.addElement(new PolicyBasedPermissionImpl(
       
   227                                            "permission1",
       
   228                                            "target1",
       
   229                                            "action1",
       
   230                                            PolicyBasedPermission.ALLOWED_TYPE));
       
   231         grantedPermissions2.addElement(new PolicyBasedPermissionImpl(
       
   232                                            "permission2",
       
   233                                            "target2",
       
   234                                            "action2",
       
   235                                            PolicyBasedPermission.ASSIGNED_TYPE));
       
   236         grantedPermissions2.addElement(new PolicyBasedPermissionImpl(
       
   237                                            "permission3",
       
   238                                            "target3",
       
   239                                            "action3",
       
   240                                            new UserSecuritySettingsImpl("settingsname",
       
   241                                                                         UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   242                                                                         new int[] { UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   243                                                                                     UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   244                                                                                     UserSecuritySettings.BLANKET_INTERACTION_MODE,
       
   245                                                                                     UserSecuritySettings.NO_INTERACTION_MODE
       
   246                                                                                   })));
       
   247         storage.writeGrantedPermissions(appUID, appUID, grantedPermissions2);
       
   248         readPermissions = storage.readGrantedPermissions(appUID);
       
   249         assertTrue(readPermissions != null
       
   250                    && readPermissions.size() == grantedPermissions.size()
       
   251                    && checkGrantedPermissions(readPermissions, grantedPermissions));
       
   252         storage.removeGrantedPermissions(appUID);
       
   253         readPermissions = storage.readGrantedPermissions(appUID);
       
   254         assertTrue(readPermissions == null);
       
   255         // full read/write/remove cycle with different valid values for permissions and user settings
       
   256         storage.removeGrantedPermissions(appUID);
       
   257         grantedPermissions = storage.readGrantedPermissions(appUID);
       
   258         assertTrue(grantedPermissions == null);
       
   259         grantedPermissions = new Vector();
       
   260         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   261         storage.removeGrantedPermissions(appUID);
       
   262         assertTrue(true);
       
   263         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   264                                           "permission1",
       
   265                                           "target1",
       
   266                                           "",
       
   267                                           PolicyBasedPermission.ALLOWED_TYPE));
       
   268         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   269                                           "permission2",
       
   270                                           "",
       
   271                                           null,
       
   272                                           PolicyBasedPermission.ASSIGNED_TYPE));
       
   273         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   274                                           "permission3",
       
   275                                           "target3",
       
   276                                           null,
       
   277                                           new UserSecuritySettingsImpl("settingsname",
       
   278                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   279                                                                        null)));
       
   280         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   281         readPermissions = storage.readGrantedPermissions(appUID);
       
   282         assertTrue(readPermissions != null
       
   283                    && readPermissions.size() == grantedPermissions.size()
       
   284                    && checkGrantedPermissions(readPermissions, grantedPermissions));
       
   285         storage.removeGrantedPermissions(appUID);
       
   286         readPermissions = storage.readGrantedPermissions(appUID);
       
   287         assertTrue(readPermissions == null);
       
   288         // full read/write/remove cycle with same permission settings
       
   289         storage.removeGrantedPermissions(appUID);
       
   290         grantedPermissions = storage.readGrantedPermissions(appUID);
       
   291         assertTrue(grantedPermissions == null);
       
   292         grantedPermissions = new Vector();
       
   293         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   294         storage.removeGrantedPermissions(appUID);
       
   295         assertTrue(true);
       
   296         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   297                                           "permission1",
       
   298                                           "target1",
       
   299                                           "",
       
   300                                           new UserSecuritySettingsImpl("settingsname",
       
   301                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   302                                                                        null)));
       
   303         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   304                                           "permission3",
       
   305                                           "target3",
       
   306                                           null,
       
   307                                           new UserSecuritySettingsImpl("settingsname",
       
   308                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   309                                                                        null)));
       
   310         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   311         readPermissions = storage.readGrantedPermissions(appUID);
       
   312         assertTrue(readPermissions != null
       
   313                    && readPermissions.size() == grantedPermissions.size()
       
   314                    && checkGrantedPermissions(readPermissions, grantedPermissions));
       
   315         storage.removeGrantedPermissions(appUID);
       
   316         readPermissions = storage.readGrantedPermissions(appUID);
       
   317         assertTrue(readPermissions == null);
       
   318         // full read/write/remove cycle with both valid & invalid values for user settings
       
   319         storage.removeGrantedPermissions(appUID);
       
   320         grantedPermissions = storage.readGrantedPermissions(appUID);
       
   321         assertTrue(grantedPermissions == null);
       
   322         grantedPermissions = new Vector();
       
   323         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   324         storage.removeGrantedPermissions(appUID);
       
   325         assertTrue(true);
       
   326         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   327                                           "permission1",
       
   328                                           "target1",
       
   329                                           "actionList1",
       
   330                                           new UserSecuritySettingsImpl(UserSecuritySettings.PHONE_CALL_SETTINGS,
       
   331                                                                        100,
       
   332                                                                        new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE, 2000,
       
   333                                                                                   UserSecuritySettings.SESSION_INTERACTION_MODE
       
   334                                                                                  })));
       
   335         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   336                                           "permission2",
       
   337                                           "target2",
       
   338                                           "actionList2",
       
   339                                           new UserSecuritySettingsImpl(UserSecuritySettings.PHONE_CALL_SETTINGS,
       
   340                                                                        UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   341                                                                        new int[] {UserSecuritySettings.BLANKET_INTERACTION_MODE})));
       
   342         grantedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   343                                           "permission3",
       
   344                                           "target3",
       
   345                                           "",
       
   346                                           new UserSecuritySettingsImpl(UserSecuritySettings.NET_ACCESS_SETTINGS,
       
   347                                                                        UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   348                                                                        new int[] {2000})));
       
   349         storage.writeGrantedPermissions(appUID, null, grantedPermissions);
       
   350         readPermissions = storage.readGrantedPermissions(appUID);
       
   351         // grantedPermissions with all the unknown values filtered out, NO as available interaction mode and NO as current interaction mode where the interaction mode was unknown
       
   352         Vector expectedPermissions = new Vector();
       
   353         expectedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   354                                            "permission1",
       
   355                                            "target1",
       
   356                                            "actionList1",
       
   357                                            new UserSecuritySettingsImpl(UserSecuritySettings.PHONE_CALL_SETTINGS,
       
   358                                                                         UserSecuritySettings.NO_INTERACTION_MODE,
       
   359                                                                         new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   360                                                                                    UserSecuritySettings.SESSION_INTERACTION_MODE, UserSecuritySettings.NO_INTERACTION_MODE
       
   361                                                                                   })));
       
   362         expectedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   363                                            "permission2",
       
   364                                            "target2",
       
   365                                            "actionList2",
       
   366                                            new UserSecuritySettingsImpl(UserSecuritySettings.PHONE_CALL_SETTINGS,
       
   367                                                                         UserSecuritySettings.NO_INTERACTION_MODE,
       
   368                                                                         new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   369                                                                                    UserSecuritySettings.SESSION_INTERACTION_MODE, UserSecuritySettings.NO_INTERACTION_MODE
       
   370                                                                                   })));
       
   371         expectedPermissions.addElement(new PolicyBasedPermissionImpl(
       
   372                                            "permission3",
       
   373                                            "target3",
       
   374                                            "",
       
   375                                            new UserSecuritySettingsImpl(UserSecuritySettings.NET_ACCESS_SETTINGS,
       
   376                                                                         UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   377                                                                         new int[] {UserSecuritySettings.NO_INTERACTION_MODE})));
       
   378         assertTrue(readPermissions != null
       
   379                    && readPermissions.size() == expectedPermissions.size()
       
   380                    && checkGrantedPermissions(readPermissions, expectedPermissions));
       
   381         storage.removeGrantedPermissions(appUID);
       
   382         readPermissions = storage.readGrantedPermissions(appUID);
       
   383         assertTrue(readPermissions == null);
       
   384     }
       
   385 
       
   386     protected void testAuthenticationData()
       
   387     {
       
   388         AuthenticationStorageData readData = null;
       
   389         AuthenticationStorageData writtenData = null;
       
   390         AuthenticationStorageData expectedData = null;
       
   391         String protectionDomain = null;
       
   392         String protectionDomainCategory = null;
       
   393         Uid appUID = Uid.createUid("myUID");
       
   394         // null values
       
   395         readData = storage.readAuthenticationStorageData(null);
       
   396         storage.writeAuthenticationStorageData(null, null);
       
   397         storage.removeAuthenticationStorageData(null);
       
   398         protectionDomain = storage.readProtectionDomain(null);
       
   399         assertTrue(readData == null && protectionDomain == null);
       
   400         readData = storage.readAuthenticationStorageData(null, null, null, 0);
       
   401         assertTrue(readData == null);
       
   402         readData = storage.readAuthenticationStorageData("name", null, null, 0);
       
   403         assertTrue(readData == null);
       
   404         readData = storage.readAuthenticationStorageData("name", "version", null, 0);
       
   405         assertTrue(readData == null);
       
   406         // full read/write/remove cycle
       
   407         storage.removeAuthenticationStorageData(appUID);
       
   408         readData = storage.readAuthenticationStorageData(appUID);
       
   409         protectionDomain = storage.readProtectionDomain(appUID);
       
   410         assertTrue(readData == null && protectionDomain == null);
       
   411         readData = storage.readAuthenticationStorageData("name", "version", "vendor", 0);
       
   412         assertTrue(readData == null);
       
   413         readData = storage.readAuthenticationStorageData("name", "version", "vendor", SecurityStorage.AUTHENTICATION_DOMAIN_NAME_QUERY);
       
   414         assertTrue(readData == null);
       
   415         readData = storage.readAuthenticationStorageData("name", "version", "vendor", SecurityStorage.AUTHENTICATION_DOMAIN_CATEGORY_QUERY);
       
   416         assertTrue(readData == null);
       
   417         readData = storage.readAuthenticationStorageData("name", "version", "vendor", SecurityStorage.AUTHENTICATION_ROOT_HASH_QUERY);
       
   418         assertTrue(readData == null);
       
   419         readData = storage.readAuthenticationStorageData("name", "version", "vendor", SecurityStorage.AUTHENTICATION_JAR_HASH_QUERY);
       
   420         assertTrue(readData == null);
       
   421         readData = storage.readAuthenticationStorageData("name", "version", "vendor", SecurityStorage.AUTHENTICATION_VALID_CERTS_QUERY);
       
   422         assertTrue(readData == null);
       
   423         readData = storage.readAuthenticationStorageData("name", "version", "vendor",
       
   424                    SecurityStorage.AUTHENTICATION_DOMAIN_NAME_QUERY | SecurityStorage.AUTHENTICATION_DOMAIN_CATEGORY_QUERY
       
   425                    | SecurityStorage.AUTHENTICATION_ROOT_HASH_QUERY | SecurityStorage.AUTHENTICATION_JAR_HASH_QUERY
       
   426                    | SecurityStorage.AUTHENTICATION_VALID_CERTS_QUERY);
       
   427         assertTrue(readData == null);
       
   428         // no values at all -> protection domain is mandatory
       
   429         writtenData = new AuthenticationStorageData(null, null, null, null, null, null);
       
   430         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   431         readData = storage.readAuthenticationStorageData(appUID);
       
   432         protectionDomain = storage.readProtectionDomain(appUID);
       
   433         assertTrue(readData == null && protectionDomain == null);
       
   434         storage.removeAuthenticationStorageData(appUID);
       
   435         assertTrue(true);
       
   436         // all possible combinations of authentication data
       
   437         writtenData = new AuthenticationStorageData("protDomain", "protDomainCategory", null, null, null, null);
       
   438         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   439         readData = storage.readAuthenticationStorageData(appUID);
       
   440         storage.removeAuthenticationStorageData(appUID);
       
   441         assertTrue(checkAuthenticationStorageData(writtenData,readData));
       
   442         Vector validatedChainIndexes = new Vector();
       
   443         writtenData = new AuthenticationStorageData("protDomain", "protDomainCategory", "", "", validatedChainIndexes);
       
   444         expectedData = new AuthenticationStorageData("protDomain", "protDomainCategory", "jarHash", "certHash", null, null);
       
   445         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   446         readData = storage.readAuthenticationStorageData(appUID);
       
   447         protectionDomain = storage.readProtectionDomain(appUID);
       
   448         protectionDomainCategory = storage.readProtectionDomainCategory(appUID);
       
   449         storage.removeAuthenticationStorageData(appUID);
       
   450         assertTrue(checkAuthenticationStorageData(expectedData,readData) && protectionDomain.equals("protDomain") && protectionDomainCategory.equals("protDomainCategory"));
       
   451         validatedChainIndexes = new Vector();
       
   452         validatedChainIndexes.addElement(new Integer(1));
       
   453         validatedChainIndexes.addElement(new Integer(2));
       
   454         validatedChainIndexes.addElement(new Integer(3));
       
   455         validatedChainIndexes.addElement(new Integer(4));
       
   456         validatedChainIndexes.addElement(new Integer(5));
       
   457         writtenData = new AuthenticationStorageData("protDomain", "protDomainCategory","jarHash", "certHash", validatedChainIndexes);
       
   458         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   459         readData = storage.readAuthenticationStorageData(appUID);
       
   460         storage.removeAuthenticationStorageData(appUID);
       
   461         assertTrue(checkAuthenticationStorageData(writtenData,readData));
       
   462         // duplicates inside validated chain indexes are ignored
       
   463         validatedChainIndexes = new Vector();
       
   464         validatedChainIndexes.addElement(new Integer(1));
       
   465         validatedChainIndexes.addElement(new Integer(1));
       
   466         writtenData = new AuthenticationStorageData("protDomain", "category", "jarHash", "certHash", validatedChainIndexes);
       
   467         validatedChainIndexes = new Vector();
       
   468         validatedChainIndexes.addElement(new Integer(1));
       
   469         expectedData = new AuthenticationStorageData("protDomain", "category", "jarHash", "certHash", validatedChainIndexes);
       
   470         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   471         readData = storage.readAuthenticationStorageData(appUID);
       
   472         storage.removeAuthenticationStorageData(appUID);
       
   473         assertTrue(checkAuthenticationStorageData(expectedData,readData));
       
   474         // update authentication data
       
   475         validatedChainIndexes = new Vector();
       
   476         storage.removeAuthenticationStorageData(appUID);
       
   477         validatedChainIndexes.addElement(new Integer(1));
       
   478         writtenData = new AuthenticationStorageData("protDomain", "protDomainCategory","jarHash", "certHash", validatedChainIndexes);
       
   479         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   480         AuthenticationStorageData updatedData = new AuthenticationStorageData("protDomainUpdate", "protDomainCategoryUpdate","jarHashUpdate", "certHashUpdate", validatedChainIndexes);
       
   481         storage.writeAuthenticationStorageData(appUID, updatedData, true);
       
   482         readData = storage.readAuthenticationStorageData(appUID);
       
   483         storage.removeAuthenticationStorageData(appUID);
       
   484         assertTrue(checkAuthenticationStorageData(updatedData,readData));
       
   485         // populate the application package table with name, vendor and version
       
   486         StorageSession session = StorageFactory.createSession();
       
   487         session.open();
       
   488         StorageEntry entry = null;
       
   489         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
   490         entry = new StorageEntry();
       
   491         entry.addAttribute(new StorageAttribute(
       
   492                                StorageNames.ID,
       
   493                                appUID.getStringValue()));
       
   494         entry.addAttribute(new StorageAttribute(
       
   495                                StorageNames.PACKAGE_NAME,
       
   496                                "appName"));
       
   497         entry.addAttribute(new StorageAttribute(
       
   498                                StorageNames.VENDOR,
       
   499                                "appVendor"));
       
   500         entry.addAttribute(new StorageAttribute(
       
   501                                StorageNames.VERSION,
       
   502                                "appVersion"));
       
   503         session.write(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
   504         // auth data is not yet available
       
   505         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor",
       
   506                    SecurityStorage.AUTHENTICATION_DOMAIN_NAME_QUERY | SecurityStorage.AUTHENTICATION_DOMAIN_CATEGORY_QUERY
       
   507                    | SecurityStorage.AUTHENTICATION_ROOT_HASH_QUERY | SecurityStorage.AUTHENTICATION_JAR_HASH_QUERY
       
   508                    | SecurityStorage.AUTHENTICATION_VALID_CERTS_QUERY);
       
   509         assertTrue(readData == null);
       
   510         // populate storage with authentication data
       
   511         validatedChainIndexes = new Vector();
       
   512         validatedChainIndexes.addElement(new Integer(1));
       
   513         validatedChainIndexes.addElement(new Integer(2));
       
   514         validatedChainIndexes.addElement(new Integer(3));
       
   515         validatedChainIndexes.addElement(new Integer(4));
       
   516         validatedChainIndexes.addElement(new Integer(5));
       
   517         writtenData = new AuthenticationStorageData("protDomainName", "protDomainCategory","jarHash", "certHash", validatedChainIndexes);
       
   518         storage.writeAuthenticationStorageData(appUID, writtenData);
       
   519         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", 0);
       
   520         assertTrue(readData == null);
       
   521         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", SecurityStorage.AUTHENTICATION_DOMAIN_NAME_QUERY);
       
   522         assertTrue(readData != null
       
   523                    && readData.getProtectionDomain() != null && readData.getProtectionDomain().equals("protDomainName")
       
   524                    && readData.getProtectionDomainCategory() == null
       
   525                    && readData.getJarHashValue() == null
       
   526                    && readData.getRootHashValue() == null
       
   527                    && readData.getValidatedChainIndexes() == null);
       
   528         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", SecurityStorage.AUTHENTICATION_DOMAIN_CATEGORY_QUERY);
       
   529         assertTrue(readData != null
       
   530                    && readData.getProtectionDomain() == null
       
   531                    && readData.getProtectionDomainCategory() != null && readData.getProtectionDomainCategory().equals("protDomainCategory")
       
   532                    && readData.getJarHashValue() == null
       
   533                    && readData.getRootHashValue() == null
       
   534                    && readData.getValidatedChainIndexes() == null);
       
   535         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", SecurityStorage.AUTHENTICATION_JAR_HASH_QUERY);
       
   536         assertTrue(readData != null
       
   537                    && readData.getProtectionDomain() == null
       
   538                    && readData.getProtectionDomainCategory() == null
       
   539                    && readData.getJarHashValue() != null && readData.getJarHashValue().equals("jarHash")
       
   540                    && readData.getRootHashValue() == null
       
   541                    && readData.getValidatedChainIndexes() == null);
       
   542         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", SecurityStorage.AUTHENTICATION_ROOT_HASH_QUERY);
       
   543         assertTrue(readData != null
       
   544                    && readData.getProtectionDomain() == null
       
   545                    && readData.getProtectionDomainCategory() == null
       
   546                    && readData.getJarHashValue() == null
       
   547                    && readData.getRootHashValue() != null && readData.getRootHashValue().equals("certHash")
       
   548                    && readData.getValidatedChainIndexes() == null);
       
   549         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor", SecurityStorage.AUTHENTICATION_VALID_CERTS_QUERY);
       
   550         assertTrue(readData != null
       
   551                    && readData.getProtectionDomain() == null
       
   552                    && readData.getProtectionDomainCategory() == null
       
   553                    && readData.getJarHashValue() == null
       
   554                    && readData.getRootHashValue() == null
       
   555                    && readData.getValidatedChainIndexes() != null && readData.getValidatedChainIndexes().length == 5);
       
   556         readData = storage.readAuthenticationStorageData("appName", "appVersion", "appVendor",
       
   557                    SecurityStorage.AUTHENTICATION_DOMAIN_NAME_QUERY | SecurityStorage.AUTHENTICATION_DOMAIN_CATEGORY_QUERY
       
   558                    | SecurityStorage.AUTHENTICATION_ROOT_HASH_QUERY | SecurityStorage.AUTHENTICATION_JAR_HASH_QUERY
       
   559                    | SecurityStorage.AUTHENTICATION_VALID_CERTS_QUERY);
       
   560         assertTrue(readData != null
       
   561                    && readData.getProtectionDomain() != null && readData.getProtectionDomain().equals("protDomainName")
       
   562                    && readData.getProtectionDomainCategory() != null && readData.getProtectionDomainCategory().equals("protDomainCategory")
       
   563                    && readData.getJarHashValue() != null && readData.getJarHashValue().equals("jarHash")
       
   564                    && readData.getRootHashValue() != null && readData.getRootHashValue().equals("certHash")
       
   565                    && readData.getValidatedChainIndexes() != null && readData.getValidatedChainIndexes().length == 5);
       
   566         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
   567     }
       
   568 
       
   569     protected void testUserSettingsData()
       
   570     {
       
   571         Uid appUID = Uid.createUid("myUID");
       
   572         Uid appUID2 = Uid.createUid("myUID2");
       
   573         UserSecuritySettings readSettings = null;
       
   574         UserSecuritySettings[] multipleSettings = null;
       
   575         boolean blanketPromptShown = false;
       
   576         // update settings which are not present into the storage
       
   577         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.SESSION_INTERACTION_MODE);
       
   578         readSettings = storage.readUserSecuritySettings(appUID, "settingsname");
       
   579         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   580         blanketPromptShown = storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname");
       
   581         assertTrue(readSettings == null && !blanketPromptShown && multipleSettings == null);
       
   582         // populate storage with some data
       
   583         Vector perms = new Vector();
       
   584         perms.addElement(new PolicyBasedPermissionImpl(
       
   585                              "permission",
       
   586                              "target",
       
   587                              "actionList",
       
   588                              new UserSecuritySettingsImpl("settingsname",
       
   589                                                           UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   590                                                           new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   591                                                                      UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   592                                                                      UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   593                                                                     })));
       
   594         storage.writeGrantedPermissions(appUID, null, perms);
       
   595         // change the current interaction mode to different values and check the update succeeds
       
   596         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.SESSION_INTERACTION_MODE);
       
   597         readSettings = storage.readUserSecuritySettings(appUID, "settingsname");
       
   598         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   599         assertTrue(readSettings != null
       
   600                    && readSettings.getCurrentInteractionMode() == UserSecuritySettings.SESSION_INTERACTION_MODE
       
   601                    && multipleSettings != null
       
   602                    && multipleSettings.length == 1
       
   603                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.SESSION_INTERACTION_MODE);
       
   604         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.ONESHOT_INTERACTION_MODE);
       
   605         readSettings = storage.readUserSecuritySettings(appUID, "settingsname");
       
   606         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   607         assertTrue(readSettings != null
       
   608                    && readSettings.getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   609                    && multipleSettings != null
       
   610                    && multipleSettings.length == 1
       
   611                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE);
       
   612         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.NO_INTERACTION_MODE);
       
   613         readSettings = storage.readUserSecuritySettings(appUID, "settingsname");
       
   614         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   615         assertTrue(readSettings != null
       
   616                    && readSettings.getCurrentInteractionMode() == UserSecuritySettings.NO_INTERACTION_MODE
       
   617                    && multipleSettings != null
       
   618                    && multipleSettings.length == 1
       
   619                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.NO_INTERACTION_MODE);
       
   620         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.BLANKET_INTERACTION_MODE);
       
   621         readSettings = storage.readUserSecuritySettings(appUID, "settingsname");
       
   622         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   623         assertTrue(readSettings != null
       
   624                    && readSettings.getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   625                    && multipleSettings != null
       
   626                    && multipleSettings.length == 1
       
   627                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE);
       
   628         // change the blanket current interaction mode to different values and check the update succeeds
       
   629         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.SESSION_INTERACTION_MODE, true);
       
   630         blanketPromptShown = storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname");
       
   631         assertTrue(blanketPromptShown);
       
   632         storage.writeUserSecuritySettings(appUID, "settingsname",UserSecuritySettings.SESSION_INTERACTION_MODE, false);
       
   633         blanketPromptShown = storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname");
       
   634         assertTrue(!blanketPromptShown);
       
   635         storage.writeUserSecuritySettingsPromptFlag(appUID, "settingsname",true);
       
   636         blanketPromptShown = storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname");
       
   637         assertTrue(blanketPromptShown);
       
   638         storage.removeGrantedPermissions(appUID);
       
   639         // change ALL the settings to different values and check that the updates are done accordingly
       
   640         perms = new Vector();
       
   641         perms.addElement(new PolicyBasedPermissionImpl(
       
   642                              "permission",
       
   643                              "target",
       
   644                              "actionList",
       
   645                              new UserSecuritySettingsImpl("settingsname1",
       
   646                                                           UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   647                                                           new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   648                                                                      UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   649                                                                      UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   650                                                                     })));
       
   651         perms.addElement(new PolicyBasedPermissionImpl(
       
   652                              "permission",
       
   653                              "target",
       
   654                              "actionList",
       
   655                              new UserSecuritySettingsImpl("settingsname2",
       
   656                                                           UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   657                                                           new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   658                                                                      UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   659                                                                      UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   660                                                                     })));
       
   661         perms.addElement(new PolicyBasedPermissionImpl(
       
   662                              "permission",
       
   663                              "target",
       
   664                              "actionList",
       
   665                              new UserSecuritySettingsImpl("settingsname3",
       
   666                                                           UserSecuritySettings.BLANKET_INTERACTION_MODE,
       
   667                                                           new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   668                                                                      UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   669                                                                      UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   670                                                                     })));
       
   671         perms.addElement(new PolicyBasedPermissionImpl(
       
   672                              "permission",
       
   673                              "target",
       
   674                              "actionList",
       
   675                              new UserSecuritySettingsImpl("settingsname4",
       
   676                                                           UserSecuritySettings.NO_INTERACTION_MODE,
       
   677                                                           new int[] {UserSecuritySettings.ONESHOT_INTERACTION_MODE,
       
   678                                                                      UserSecuritySettings.SESSION_INTERACTION_MODE,
       
   679                                                                      UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   680                                                                     })));
       
   681         storage.writeGrantedPermissions(appUID, null, perms);
       
   682         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   683         assertTrue(multipleSettings != null
       
   684                    && multipleSettings.length == 4
       
   685                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   686                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname1") == false)
       
   687                    && multipleSettings[1].getCurrentInteractionMode() == UserSecuritySettings.SESSION_INTERACTION_MODE
       
   688                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname2") == false)
       
   689                    && multipleSettings[2].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   690                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname3") == false)
       
   691                    && multipleSettings[3].getCurrentInteractionMode() == UserSecuritySettings.NO_INTERACTION_MODE
       
   692                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname4") == false));
       
   693         storage.writeUserSecuritySettings(appUID2, UserSecuritySettings.ONESHOT_INTERACTION_MODE, false);
       
   694         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   695         assertTrue(multipleSettings != null
       
   696                    && multipleSettings.length == 4
       
   697                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   698                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname1") == false)
       
   699                    && multipleSettings[1].getCurrentInteractionMode() == UserSecuritySettings.SESSION_INTERACTION_MODE
       
   700                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname2") == false)
       
   701                    && multipleSettings[2].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   702                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname3") == false)
       
   703                    && multipleSettings[3].getCurrentInteractionMode() == UserSecuritySettings.NO_INTERACTION_MODE
       
   704                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname4") == false));
       
   705         storage.writeUserSecuritySettings(appUID, UserSecuritySettings.ONESHOT_INTERACTION_MODE, false);
       
   706         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   707         assertTrue(multipleSettings != null
       
   708                    && multipleSettings.length == 4
       
   709                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   710                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname1") == false)
       
   711                    && multipleSettings[1].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   712                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname2") == false)
       
   713                    && multipleSettings[2].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   714                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname3") == false)
       
   715                    && multipleSettings[3].getCurrentInteractionMode() == UserSecuritySettings.ONESHOT_INTERACTION_MODE
       
   716                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname4") == false));
       
   717         storage.writeUserSecuritySettings(appUID, UserSecuritySettings.BLANKET_INTERACTION_MODE, true);
       
   718         multipleSettings = storage.readUserSecuritySettings(appUID);
       
   719         assertTrue(multipleSettings != null
       
   720                    && multipleSettings.length == 4
       
   721                    && multipleSettings[0].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   722                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname1") == true)
       
   723                    && multipleSettings[1].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   724                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname2") == true)
       
   725                    && multipleSettings[2].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   726                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname3") == true)
       
   727                    && multipleSettings[3].getCurrentInteractionMode() == UserSecuritySettings.BLANKET_INTERACTION_MODE
       
   728                    && (storage.readUserSecuritySettingsPromptFlag(appUID, "settingsname4") == true));
       
   729         storage.removeGrantedPermissions(appUID);
       
   730     }
       
   731 
       
   732     protected void testAppAccessAuthorizationRestrictionsData()
       
   733     {
       
   734         Uid appUID = Uid.createUid("myUID");
       
   735         AuthorizationRestriction[] readRestrictions = null;
       
   736         StorageSession session = StorageFactory.createSession();
       
   737         session.open();
       
   738         StorageEntry entry = null;
       
   739         AppAccessAuthorizationStorageData appAccessAuthData = null;
       
   740         // null values
       
   741         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(null, null, null, 0);
       
   742         assertTrue(appAccessAuthData == null);
       
   743         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", null, null, 0);
       
   744         assertTrue(appAccessAuthData == null);
       
   745         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version", null, 0);
       
   746         assertTrue(appAccessAuthData == null);
       
   747         // no restrictions present
       
   748         readRestrictions = storage.readAuthorizationRestrictions(null);
       
   749         assertTrue(readRestrictions == null);
       
   750         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
   751         readRestrictions = storage.readAuthorizationRestrictions(appUID);
       
   752         assertTrue(readRestrictions == null);
       
   753         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version","vendor", 0);
       
   754         assertTrue(appAccessAuthData == null);
       
   755         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version","vendor", SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY);
       
   756         assertTrue(appAccessAuthData == null);
       
   757         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version","vendor", SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY);
       
   758         assertTrue(appAccessAuthData == null);
       
   759         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version","vendor", SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   760         assertTrue(appAccessAuthData == null);
       
   761         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("name", "version","vendor",
       
   762                             SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY
       
   763                             | SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY
       
   764                             | SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   765         assertTrue(appAccessAuthData == null);
       
   766         // populate the APPLICATION_PACKAGE_ATTRIBUTES_TABLE with only restriction without certificate
       
   767         entry = new StorageEntry();
       
   768         entry.addAttribute(new StorageAttribute(
       
   769                                StorageNames.ID,
       
   770                                appUID.getStringValue()));
       
   771         entry.addAttribute(new StorageAttribute(
       
   772                                StorageNames.NAME,
       
   773                                AuthorizationRestrictionAttribute.MAIN_ATTRIBUTE_PREFIX + "1"));
       
   774         entry.addAttribute(new StorageAttribute(
       
   775                                StorageNames.VALUE,
       
   776                                "domain=SELF;vendor=ANY;signer=ANY"));
       
   777         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   778         readRestrictions = storage.readAuthorizationRestrictions(appUID);
       
   779         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   780         assertTrue(readRestrictions != null
       
   781                    && readRestrictions.length == 1
       
   782                    && readRestrictions[0].getDomain().equals("SELF")
       
   783                    && readRestrictions[0].getVendor().equals("ANY")
       
   784                    && readRestrictions[0].getSigner().equals("ANY"));
       
   785         // populate the APPLICATION_PACKAGE_ATTRIBUTES_TABLE with only certificate but not a corresponding restriction
       
   786         entry = new StorageEntry();
       
   787         entry.addAttribute(new StorageAttribute(
       
   788                                StorageNames.ID,
       
   789                                appUID.getStringValue()));
       
   790         entry.addAttribute(new StorageAttribute(
       
   791                                StorageNames.NAME,
       
   792                                AuthorizationRestrictionAttribute.SECOND_ATTRIBUTE_PREFIX + "1"));
       
   793         entry.addAttribute(new StorageAttribute(
       
   794                                StorageNames.VALUE,
       
   795                                "cert"));
       
   796         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   797         readRestrictions = storage.readAuthorizationRestrictions(appUID);
       
   798         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   799         assertTrue(readRestrictions == null);
       
   800         // populate the APPLICATION_PACKAGE_ATTRIBUTES_TABLE with 2 restrictions and only 1 certificate -> 2 pairs retrieved
       
   801         entry = new StorageEntry();
       
   802         entry.addAttribute(new StorageAttribute(
       
   803                                StorageNames.ID,
       
   804                                appUID.getStringValue()));
       
   805         entry.addAttribute(new StorageAttribute(
       
   806                                StorageNames.NAME,
       
   807                                AuthorizationRestrictionAttribute.MAIN_ATTRIBUTE_PREFIX + "1"));
       
   808         entry.addAttribute(new StorageAttribute(
       
   809                                StorageNames.VALUE,
       
   810                                "domain=SELF;vendor=ANY;signer=alias"));
       
   811         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   812         entry = new StorageEntry();
       
   813         entry.addAttribute(new StorageAttribute(
       
   814                                StorageNames.ID,
       
   815                                appUID.getStringValue()));
       
   816         entry.addAttribute(new StorageAttribute(
       
   817                                StorageNames.NAME,
       
   818                                AuthorizationRestrictionAttribute.MAIN_ATTRIBUTE_PREFIX + "2"));
       
   819         entry.addAttribute(new StorageAttribute(
       
   820                                StorageNames.VALUE,
       
   821                                "domain=ANY;vendor=vendor;signer=ANY"));
       
   822         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   823         entry = new StorageEntry();
       
   824         entry.addAttribute(new StorageAttribute(
       
   825                                StorageNames.ID,
       
   826                                appUID.getStringValue()));
       
   827         entry.addAttribute(new StorageAttribute(
       
   828                                StorageNames.NAME,
       
   829                                AuthorizationRestrictionAttribute.SECOND_ATTRIBUTE_PREFIX + "1"));
       
   830         entry.addAttribute(new StorageAttribute(
       
   831                                StorageNames.VALUE,
       
   832                                "alias cert"));
       
   833         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   834         readRestrictions = storage.readAuthorizationRestrictions(appUID);
       
   835         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   836         assertTrue(readRestrictions != null
       
   837                    && readRestrictions.length == 2
       
   838                    && readRestrictions[0].getDomain().equals("SELF")
       
   839                    && readRestrictions[0].getVendor().equals("ANY")
       
   840                    && readRestrictions[0].getSigner().equals("cert")
       
   841                    && readRestrictions[1].getDomain().equals("ANY")
       
   842                    && readRestrictions[1].getVendor().equals("vendor")
       
   843                    && readRestrictions[1].getSigner().equals("ANY"));
       
   844         // populate the APPLICATION_PACKAGE_ATTRIBUTES_TABLE with 2 restrictions and only 2 certificate -> 2 pairs retrieved
       
   845         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
   846         entry = new StorageEntry();
       
   847         entry.addAttribute(new StorageAttribute(
       
   848                                StorageNames.ID,
       
   849                                appUID.getStringValue()));
       
   850         entry.addAttribute(new StorageAttribute(
       
   851                                StorageNames.NAME,
       
   852                                AuthorizationRestrictionAttribute.MAIN_ATTRIBUTE_PREFIX + "1"));
       
   853         entry.addAttribute(new StorageAttribute(
       
   854                                StorageNames.VALUE,
       
   855                                "domain=SELF;vendor=vendor1;signer=alias1"));
       
   856         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   857         entry = new StorageEntry();
       
   858         entry.addAttribute(new StorageAttribute(
       
   859                                StorageNames.ID,
       
   860                                appUID.getStringValue()));
       
   861         entry.addAttribute(new StorageAttribute(
       
   862                                StorageNames.NAME,
       
   863                                AuthorizationRestrictionAttribute.MAIN_ATTRIBUTE_PREFIX + "2"));
       
   864         entry.addAttribute(new StorageAttribute(
       
   865                                StorageNames.VALUE,
       
   866                                "domain=ANY;vendor=vendor2;signer=alias2"));
       
   867         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   868         entry = new StorageEntry();
       
   869         entry.addAttribute(new StorageAttribute(
       
   870                                StorageNames.ID,
       
   871                                appUID.getStringValue()));
       
   872         entry.addAttribute(new StorageAttribute(
       
   873                                StorageNames.NAME,
       
   874                                AuthorizationRestrictionAttribute.SECOND_ATTRIBUTE_PREFIX + "1"));
       
   875         entry.addAttribute(new StorageAttribute(
       
   876                                StorageNames.VALUE,
       
   877                                "alias1 cert1"));
       
   878         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   879         entry = new StorageEntry();
       
   880         entry.addAttribute(new StorageAttribute(
       
   881                                StorageNames.ID,
       
   882                                appUID.getStringValue()));
       
   883         entry.addAttribute(new StorageAttribute(
       
   884                                StorageNames.NAME,
       
   885                                AuthorizationRestrictionAttribute.SECOND_ATTRIBUTE_PREFIX + "2"));
       
   886         entry.addAttribute(new StorageAttribute(
       
   887                                StorageNames.VALUE,
       
   888                                "alias2 cert2"));
       
   889         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   890         readRestrictions = storage.readAuthorizationRestrictions(appUID);
       
   891         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
   892         assertTrue(readRestrictions != null
       
   893                    && readRestrictions.length == 2
       
   894                    && readRestrictions[0].getDomain().equals("SELF")
       
   895                    && readRestrictions[0].getVendor().equals("vendor1")
       
   896                    && readRestrictions[0].getSigner().equals("cert1")
       
   897                    && readRestrictions[1].getDomain().equals("ANY")
       
   898                    && readRestrictions[1].getVendor().equals("vendor2")
       
   899                    && readRestrictions[1].getSigner().equals("cert2"));
       
   900         // no app access authorization data at all
       
   901         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(null);
       
   902         assertTrue(appAccessAuthData == null);
       
   903         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
   904         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
   905         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
   906         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID);
       
   907         assertTrue(appAccessAuthData == null);
       
   908         // populate APPLICATION_PACKAGE_TABLE with VENDOR
       
   909         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
   910         entry = new StorageEntry();
       
   911         entry.addAttribute(new StorageAttribute(
       
   912                                StorageNames.ID,
       
   913                                appUID.getStringValue()));
       
   914         entry.addAttribute(new StorageAttribute(
       
   915                                StorageNames.VENDOR,
       
   916                                "vendor"));
       
   917         session.write(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
   918         // populate MIDP_PACKAGE_TABLE with domain and valid certs
       
   919         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
   920         entry = new StorageEntry();
       
   921         entry.addAttribute(new StorageAttribute(
       
   922                                StorageNames.ID,
       
   923                                appUID.getStringValue()));
       
   924         entry.addAttribute(new StorageAttribute(
       
   925                                StorageNames.SECURITY_DOMAIN,
       
   926                                "domain"));
       
   927         entry.addAttribute(new StorageAttribute(
       
   928                                StorageNames.VALID_CERTS,
       
   929                                "1,2,3"));
       
   930         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
   931         // populate APPLICATION_PACKAGE_ATTRIBUTES_TABLE with restrictions
       
   932         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
   933         entry = new StorageEntry();
       
   934         entry.addAttribute(new StorageAttribute(
       
   935                                StorageNames.ID,
       
   936                                appUID.getStringValue()));
       
   937         entry.addAttribute(new StorageAttribute(
       
   938                                StorageNames.NAME,
       
   939                                AuthenticationAttribute.MAIN_ATTRIBUTE_PREFIX + "1-1"));
       
   940         entry.addAttribute(new StorageAttribute(
       
   941                                StorageNames.VALUE,
       
   942                                "cert1"));
       
   943         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
   944         // request only vendor
       
   945         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY);
       
   946         assertTrue(appAccessAuthData != null &&
       
   947                    appAccessAuthData.getVendorName().equals("vendor") &&
       
   948                    appAccessAuthData.getDomainName()== null &&
       
   949                    appAccessAuthData.getSignersList() == null);
       
   950         // request only domain
       
   951         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY);
       
   952         assertTrue(appAccessAuthData != null &&
       
   953                    appAccessAuthData.getVendorName() == null &&
       
   954                    appAccessAuthData.getDomainName().equals("domain") &&
       
   955                    appAccessAuthData.getSignersList() == null);
       
   956         // request only signers list
       
   957         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   958         assertTrue(appAccessAuthData != null &&
       
   959                    appAccessAuthData.getVendorName() == null &&
       
   960                    appAccessAuthData.getDomainName() == null &&
       
   961                    appAccessAuthData.getSignersList() != null &&
       
   962                    appAccessAuthData.getSignersList().length == 1 &&
       
   963                    appAccessAuthData.getSignersList()[0].equals("cert1"));
       
   964         // request vendor and domain
       
   965         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY | SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY);
       
   966         assertTrue(appAccessAuthData != null &&
       
   967                    appAccessAuthData.getVendorName().equals("vendor") &&
       
   968                    appAccessAuthData.getDomainName().equals("domain") &&
       
   969                    appAccessAuthData.getSignersList() == null);
       
   970         // request vendor and signers
       
   971         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY | SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   972         assertTrue(appAccessAuthData != null &&
       
   973                    appAccessAuthData.getVendorName().equals("vendor") &&
       
   974                    appAccessAuthData.getDomainName() == null &&
       
   975                    appAccessAuthData.getSignersList() != null &&
       
   976                    appAccessAuthData.getSignersList().length == 1 &&
       
   977                    appAccessAuthData.getSignersList()[0].equals("cert1"));
       
   978         // request domain and signers
       
   979         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY | SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   980         assertTrue(appAccessAuthData != null &&
       
   981                    appAccessAuthData.getVendorName() == null &&
       
   982                    appAccessAuthData.getDomainName().equals("domain") &&
       
   983                    appAccessAuthData.getSignersList() != null &&
       
   984                    appAccessAuthData.getSignersList().length == 1 &&
       
   985                    appAccessAuthData.getSignersList()[0].equals("cert1"));
       
   986         // request everything
       
   987         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID);
       
   988         assertTrue(appAccessAuthData != null &&
       
   989                    appAccessAuthData.getVendorName().equals("vendor") &&
       
   990                    appAccessAuthData.getDomainName().equals("domain") &&
       
   991                    appAccessAuthData.getSignersList() != null &&
       
   992                    appAccessAuthData.getSignersList().length == 1 &&
       
   993                    appAccessAuthData.getSignersList()[0].equals("cert1"));
       
   994         // remove data from MIDP_PACKAGE_TABLE and request signers list
       
   995         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
   996         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID, SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
   997         assertTrue(appAccessAuthData == null);
       
   998         // populate APPLICATION_PACKAGE_ATTRIBUTES_TABLE with 3 certs, populate MIDP_PACKAGE_TABLE with valid certs only and request everything
       
   999         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
  1000         entry = new StorageEntry();
       
  1001         entry.addAttribute(new StorageAttribute(
       
  1002                                StorageNames.ID,
       
  1003                                appUID.getStringValue()));
       
  1004         entry.addAttribute(new StorageAttribute(
       
  1005                                StorageNames.NAME,
       
  1006                                AuthenticationAttribute.MAIN_ATTRIBUTE_PREFIX + "1-1"));
       
  1007         entry.addAttribute(new StorageAttribute(
       
  1008                                StorageNames.VALUE,
       
  1009                                "cert1"));
       
  1010         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1011         entry = new StorageEntry();
       
  1012         entry.addAttribute(new StorageAttribute(
       
  1013                                StorageNames.ID,
       
  1014                                appUID.getStringValue()));
       
  1015         entry.addAttribute(new StorageAttribute(
       
  1016                                StorageNames.NAME,
       
  1017                                AuthenticationAttribute.MAIN_ATTRIBUTE_PREFIX + "2-1"));
       
  1018         entry.addAttribute(new StorageAttribute(
       
  1019                                StorageNames.VALUE,
       
  1020                                "cert2"));
       
  1021         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1022         entry = new StorageEntry();
       
  1023         entry.addAttribute(new StorageAttribute(
       
  1024                                StorageNames.ID,
       
  1025                                appUID.getStringValue()));
       
  1026         entry.addAttribute(new StorageAttribute(
       
  1027                                StorageNames.NAME,
       
  1028                                AuthenticationAttribute.MAIN_ATTRIBUTE_PREFIX + "3-1"));
       
  1029         entry.addAttribute(new StorageAttribute(
       
  1030                                StorageNames.VALUE,
       
  1031                                "cert3"));
       
  1032         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1033         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1034         entry = new StorageEntry();
       
  1035         entry.addAttribute(new StorageAttribute(
       
  1036                                StorageNames.ID,
       
  1037                                appUID.getStringValue()));
       
  1038         entry.addAttribute(new StorageAttribute(
       
  1039                                StorageNames.VALID_CERTS,
       
  1040                                "1,3"));
       
  1041         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1042         appAccessAuthData = storage.readAppAccessAuthorizationStorageData(appUID,
       
  1043                             SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY | SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY | SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY);
       
  1044         assertTrue(appAccessAuthData != null &&
       
  1045                    appAccessAuthData.getVendorName().equals("vendor") &&
       
  1046                    (appAccessAuthData.getDomainName() == null || appAccessAuthData.getDomainName().length() == 0) &&
       
  1047                    appAccessAuthData.getSignersList() != null &&
       
  1048                    appAccessAuthData.getSignersList().length == 2 &&
       
  1049                    appAccessAuthData.getSignersList()[0].equals("cert1") &&
       
  1050                    appAccessAuthData.getSignersList()[1].equals("cert3"));
       
  1051         // play with filters and name|vendor|version combinations
       
  1052         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
  1053         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1054         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
  1055         entry = new StorageEntry();
       
  1056         entry.addAttribute(new StorageAttribute(
       
  1057                                StorageNames.ID,
       
  1058                                appUID.getStringValue()));
       
  1059         entry.addAttribute(new StorageAttribute(
       
  1060                                StorageNames.PACKAGE_NAME,
       
  1061                                "appName"));
       
  1062         entry.addAttribute(new StorageAttribute(
       
  1063                                StorageNames.VENDOR,
       
  1064                                "appVendor"));
       
  1065         entry.addAttribute(new StorageAttribute(
       
  1066                                StorageNames.VERSION,
       
  1067                                "appVersion"));
       
  1068         session.write(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
  1069         // auth data is not yet available
       
  1070         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor",
       
  1071                             SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY | SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
  1072         assertTrue(appAccessAuthData == null);
       
  1073         // populate storage with authentication data
       
  1074         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1075         entry = new StorageEntry();
       
  1076         entry.addAttribute(new StorageAttribute(
       
  1077                                StorageNames.ID,
       
  1078                                appUID.getStringValue()));
       
  1079         entry.addAttribute(new StorageAttribute(
       
  1080                                StorageNames.SECURITY_DOMAIN,
       
  1081                                "domain"));
       
  1082         entry.addAttribute(new StorageAttribute(
       
  1083                                StorageNames.VALID_CERTS,
       
  1084                                "1"));
       
  1085         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1086         // populate APPLICATION_PACKAGE_ATTRIBUTES_TABLE with restrictions
       
  1087         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
  1088         entry = new StorageEntry();
       
  1089         entry.addAttribute(new StorageAttribute(
       
  1090                                StorageNames.ID,
       
  1091                                appUID.getStringValue()));
       
  1092         entry.addAttribute(new StorageAttribute(
       
  1093                                StorageNames.NAME,
       
  1094                                AuthenticationAttribute.MAIN_ATTRIBUTE_PREFIX + "1-1"));
       
  1095         entry.addAttribute(new StorageAttribute(
       
  1096                                StorageNames.VALUE,
       
  1097                                "cert1"));
       
  1098         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1099         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor", 0);
       
  1100         assertTrue(appAccessAuthData == null);
       
  1101         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor", SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY);
       
  1102         assertTrue(appAccessAuthData != null
       
  1103                    && appAccessAuthData.getDomainName() != null && appAccessAuthData.getDomainName().equals("domain")
       
  1104                    && appAccessAuthData.getVendorName() == null
       
  1105                    && appAccessAuthData.getSignersList() == null);
       
  1106         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor", SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY);
       
  1107         assertTrue(appAccessAuthData != null
       
  1108                    && appAccessAuthData.getDomainName() == null
       
  1109                    && appAccessAuthData.getVendorName().equals("appVendor")
       
  1110                    && appAccessAuthData.getSignersList() == null);
       
  1111         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor", SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
  1112         assertTrue(appAccessAuthData != null
       
  1113                    && appAccessAuthData.getDomainName() == null
       
  1114                    && appAccessAuthData.getVendorName() == null
       
  1115                    && appAccessAuthData.getSignersList() != null && appAccessAuthData.getSignersList().length == 1);
       
  1116         appAccessAuthData = storage.readAppAccessAuthorizationStorageData("appName", "appVersion", "appVendor",
       
  1117                             SecurityStorage.APP_ACCESS_AUTH_DOMAIN_QUERY
       
  1118                             | SecurityStorage.APP_ACCESS_AUTH_VENDOR_QUERY
       
  1119                             | SecurityStorage.APP_ACCESS_AUTH_SIGNERS_LIST_QUERY);
       
  1120         assertTrue(appAccessAuthData != null
       
  1121                    && appAccessAuthData.getDomainName() != null && appAccessAuthData.getDomainName().equals("domain")
       
  1122                    && appAccessAuthData.getVendorName().equals("appVendor")
       
  1123                    && appAccessAuthData.getSignersList() != null && appAccessAuthData.getSignersList().length == 1);
       
  1124         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, appUID);
       
  1125         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1126         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, appUID);
       
  1127     }
       
  1128 
       
  1129     protected void testMisc()
       
  1130     {
       
  1131         Uid appUID = Uid.createUid("myUID");
       
  1132         StorageSession session = StorageFactory.createSession();
       
  1133         session.open();
       
  1134         StorageEntry entry = new StorageEntry();
       
  1135         entry.addAttribute(new StorageAttribute(
       
  1136                                StorageNames.ID,
       
  1137                                appUID.getStringValue()));
       
  1138         // non existent values
       
  1139         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1140         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
  1141         assertTrue(storage.readSuiteVersion(appUID) == null
       
  1142                    && storage.readSuiteMediaId(appUID) == null);
       
  1143         // populate the tables
       
  1144         entry.addAttribute(new StorageAttribute(
       
  1145                                StorageNames.NAME,
       
  1146                                SecurityAttributes.MIDP_VERSION_ATTRIBUTE_NAME));
       
  1147         entry.addAttribute(new StorageAttribute(
       
  1148                                StorageNames.VALUE,
       
  1149                                "version"));
       
  1150         session.write(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1151         entry = new StorageEntry();
       
  1152         entry.addAttribute(new StorageAttribute(
       
  1153                                StorageNames.ID,
       
  1154                                appUID.getStringValue()));
       
  1155         entry.addAttribute(new StorageAttribute(
       
  1156                                StorageNames.MEDIA_ID,
       
  1157                                "mediaid"));
       
  1158         session.write(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
  1159         assertTrue(storage.readSuiteVersion(appUID).equals("version")
       
  1160                    && storage.readSuiteMediaId(appUID).equals("mediaid"));
       
  1161         // security warnings mode - read value for unknown uid -> default
       
  1162         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1163         assertTrue(storage.readSecurityWarningsMode(Uid.createUid("myUnknownUID")) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1164         // security warnings mode - read GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1165         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1166         entry = new StorageEntry();
       
  1167         entry.addAttribute(new StorageAttribute(
       
  1168                                StorageNames.ID,
       
  1169                                appUID.getStringValue()));
       
  1170         entry.addAttribute(new StorageAttribute(
       
  1171                                StorageNames.SECURITY_WARNINGS,
       
  1172                                "" + GeneralSecuritySettings.DEFAULT_SECURITY_MODE));
       
  1173         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1174         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1175         // security warnings mode - read GeneralSecuritySettings.USER_SECURITY_MODE
       
  1176         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1177         entry = new StorageEntry();
       
  1178         entry.addAttribute(new StorageAttribute(
       
  1179                                StorageNames.ID,
       
  1180                                appUID.getStringValue()));
       
  1181         entry.addAttribute(new StorageAttribute(
       
  1182                                StorageNames.SECURITY_WARNINGS,
       
  1183                                "" + GeneralSecuritySettings.USER_SECURITY_MODE));
       
  1184         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1185         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.USER_SECURITY_MODE);
       
  1186         // security warnings mode - unknown value in storage -> return GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1187         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1188         entry = new StorageEntry();
       
  1189         entry.addAttribute(new StorageAttribute(
       
  1190                                StorageNames.ID,
       
  1191                                appUID.getStringValue()));
       
  1192         entry.addAttribute(new StorageAttribute(
       
  1193                                StorageNames.SECURITY_WARNINGS,
       
  1194                                "5"));
       
  1195         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1196         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1197         // security warnings mode - invalid value in storage -> return GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1198         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1199         entry = new StorageEntry();
       
  1200         entry.addAttribute(new StorageAttribute(
       
  1201                                StorageNames.ID,
       
  1202                                appUID.getStringValue()));
       
  1203         entry.addAttribute(new StorageAttribute(
       
  1204                                StorageNames.SECURITY_WARNINGS,
       
  1205                                "value"));
       
  1206         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1207         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1208         // security warnings mode - try to write invalid -> return GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1209         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1210         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1211         entry = new StorageEntry();
       
  1212         entry.addAttribute(new StorageAttribute(
       
  1213                                StorageNames.ID,
       
  1214                                appUID.getStringValue()));
       
  1215         entry.addAttribute(new StorageAttribute(
       
  1216                                StorageNames.SECURITY_WARNINGS,
       
  1217                                "value"));
       
  1218         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1219         storage.writeSecurityWarningsMode(appUID, 5);
       
  1220         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1221         // security warnings mode - write GeneralSecuritySettings.DEFAULT_SECURITY_MODE -> return GeneralSecuritySettings.DEFAULT_SECURITY_MODE
       
  1222         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1223         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1224         entry = new StorageEntry();
       
  1225         entry.addAttribute(new StorageAttribute(
       
  1226                                StorageNames.ID,
       
  1227                                appUID.getStringValue()));
       
  1228         entry.addAttribute(new StorageAttribute(
       
  1229                                StorageNames.SECURITY_WARNINGS,
       
  1230                                "value"));
       
  1231         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1232         storage.writeSecurityWarningsMode(appUID, GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1233         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.DEFAULT_SECURITY_MODE);
       
  1234         // security warnings mode - write GeneralSecuritySettings.USER_SECURITY_MODE -> return GeneralSecuritySettings.USER_SECURITY_MODE
       
  1235         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1236         entry = new StorageEntry();
       
  1237         entry.addAttribute(new StorageAttribute(
       
  1238                                StorageNames.ID,
       
  1239                                appUID.getStringValue()));
       
  1240         entry.addAttribute(new StorageAttribute(
       
  1241                                StorageNames.SECURITY_WARNINGS,
       
  1242                                "value"));
       
  1243         session.write(StorageNames.MIDP_PACKAGE_TABLE, entry);
       
  1244         storage.writeSecurityWarningsMode(appUID, GeneralSecuritySettings.USER_SECURITY_MODE);
       
  1245         assertTrue(storage.readSecurityWarningsMode(appUID) == GeneralSecuritySettings.USER_SECURITY_MODE);
       
  1246         // cleanup
       
  1247         entry = new StorageEntry();
       
  1248         entry.addAttribute(new StorageAttribute(
       
  1249                                StorageNames.ID,
       
  1250                                appUID.getStringValue()));
       
  1251         session.remove(StorageNames.APPLICATION_PACKAGE_ATTRIBUTES_TABLE, entry);
       
  1252         session.remove(StorageNames.MIDP_PACKAGE_TABLE, appUID);
       
  1253         session.remove(StorageNames.APPLICATION_PACKAGE_TABLE, entry);
       
  1254     }
       
  1255 
       
  1256 
       
  1257     private boolean checkAuthenticationStorageData(AuthenticationStorageData d1, AuthenticationStorageData d2)
       
  1258     {
       
  1259         if ((d1 == null && d2 != null)
       
  1260                 || (d1 != null && d2 == null))
       
  1261         {
       
  1262             return false;
       
  1263         }
       
  1264         if (d1 == null && d2 == null)
       
  1265         {
       
  1266             return true;
       
  1267         }
       
  1268         if ((d1.getProtectionDomain() == null && d2.getProtectionDomain() != null)
       
  1269                 || (d1.getProtectionDomain() != null && d1.equals(d2.getProtectionDomain()))
       
  1270                 || (d1.getJarHashValue() == null && d2.getJarHashValue() != null)
       
  1271                 || (d1.getJarHashValue() != null && d1.equals(d2.getJarHashValue()))
       
  1272                 || (d1.getRootHashValue() == null && d2.getRootHashValue() != null)
       
  1273                 || (d1.getRootHashValue() != null && d1.equals(d2.getRootHashValue()))
       
  1274                 || (d1.getProtectionDomainCategory() == null && d2.getProtectionDomainCategory() != null)
       
  1275                 || (d1.getProtectionDomainCategory() != null && d1.equals(d2.getProtectionDomainCategory()))
       
  1276                 || !arraysEqual(d1.getValidatedChainIndexes(), d2.getValidatedChainIndexes()))
       
  1277         {
       
  1278             return false;
       
  1279         }
       
  1280         return true;
       
  1281     }
       
  1282 
       
  1283     private boolean checkGrantedPermissions(Vector p1, Vector p2)
       
  1284     {
       
  1285         boolean[] permissionsChecked = new boolean[p1.size()];
       
  1286         initBoolArray(permissionsChecked);
       
  1287         for (int i=0; i<p1.size(); i++)
       
  1288         {
       
  1289             for (int j=0; j<p2.size(); j++)
       
  1290             {
       
  1291                 if (p1.elementAt(i).equals(p2.elementAt(j)))
       
  1292                 {
       
  1293                     permissionsChecked[i] = true;
       
  1294                     break;
       
  1295                 }
       
  1296             }
       
  1297         }
       
  1298         return checkBoolArray(permissionsChecked);
       
  1299     }
       
  1300 
       
  1301     private boolean arraysEqual(int[] array1, int[] array2)
       
  1302     {
       
  1303         if (array1 == null && array2 == null)
       
  1304         {
       
  1305             return true;
       
  1306         }
       
  1307         if ((array1 == null && array2 != null) ||
       
  1308                 (array1 != null && array2 == null) ||
       
  1309                 (array1.length != array2.length))
       
  1310         {
       
  1311             return false;
       
  1312         }
       
  1313         for (int i=0; i<array1.length; i++)
       
  1314         {
       
  1315             boolean found = false;
       
  1316             for (int j=0; j<array2.length; j++)
       
  1317             {
       
  1318                 if (array1[i] == array2[j])
       
  1319                 {
       
  1320                     found = true;
       
  1321                     break;
       
  1322                 }
       
  1323             }
       
  1324             if (!found)
       
  1325             {
       
  1326                 return false;
       
  1327             }
       
  1328             return true;
       
  1329         }
       
  1330         return true;
       
  1331     }
       
  1332 
       
  1333 
       
  1334     private void initBoolArray(boolean[] boolArray)
       
  1335     {
       
  1336         for (int i=0; i<boolArray.length; i++)
       
  1337         {
       
  1338             boolArray[i] = false;
       
  1339         }
       
  1340     }
       
  1341 
       
  1342     private boolean checkBoolArray(boolean[] boolArray)
       
  1343     {
       
  1344         for (int i=0; i<boolArray.length; i++)
       
  1345         {
       
  1346             if (boolArray[i] == false)
       
  1347             {
       
  1348                 return false;
       
  1349             }
       
  1350         }
       
  1351         return true;
       
  1352     }
       
  1353 }