examples/Base/IPC/secureserver/secureclient.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:
00028 This client executable has all required capabilities to modify a file.
00029 Demonstrates all functionalities provided by the client side interface of the server.  
00030 */
00031 
00032 
00033 
00034 
00038 #include "secureclientandserver.h"
00039 #include "secureclient.h"
00040 
00041 LOCAL_D CConsoleBase* console;
00042 
00043 void DoExampleL()
00044         {
00045         console->Printf(KTxtTestingCountServer);
00046         
00047         // Create a session.
00048         RSecureSession countserv;
00049         User::LeaveIfError(countserv.Connect());
00050         
00051         // Create a sub session.
00052         RSecureSubSession counterA;
00053         counterA.Open(countserv);
00054 
00055         // The user can choose to initialise the counter from the counter.dat file.
00056         console->Printf(KTxtInitCounterAWith);
00057         console->Printf(KTxtOption);
00058         TUint8 inpOption = console->Getch();
00059         if(inpOption == '1')
00060                 {
00061                 // This call will fail if the client executable does not have required file read capability.
00062                 counterA.SetCounterFromFileL();
00063                 console->Printf(KTxtInitFromFile,counterA.CounterValueL());
00064                 }
00065         else
00066                 {
00067                 // This is an invalid initialisation string. Hence, the counter will be initialised to zero.
00068                 _LIT(KTxtValueString,"a2");
00069                 console->Printf(KTxtValueString);
00070                 // Set counter from the KTxtValueString string
00071                 TInt err = counterA.SetFromString(KTxtValueString);
00072                 if (err == ENonNumericString)
00073                         {
00074                         // Print an error message as the initialisation string, KTxtValueString is invalid (non numeric format).
00075                         console->Printf(KTxtInitCounterFailed);
00076                         }
00077                 else
00078                         {
00079                         // Initialisation from string succeeded.
00080                         console->Printf(KTxtInitCounterSucceeded);
00081                         }
00082                 }
00083         RSecureSubSession counterB;
00084 
00085         // Create a sub session.
00086         counterB.Open(countserv);
00087 
00088         // The user can choose to initialise the counter from the counter.dat file.
00089         console->Printf(KTxtInitCounterBWith);
00090         console->Printf(KTxtOption);
00091         inpOption = console->Getch();
00092         if(inpOption == '1')
00093                 {
00094                 // This call will fail if the client executable does not have required file read capability.
00095                 counterB.SetCounterFromFileL();
00096                 console->Printf(KTxtInitFromFile,counterB.CounterValueL());
00097                 }
00098         else
00099                 {
00100                 // A valid numeric string.
00101                 _LIT(KTxtLegalString,"100");
00102                 console->Printf(KTxtLegalString);
00103                 // Set counter from the KTxtLegalString string.
00104                 TInt err = counterB.SetFromString(KTxtLegalString);
00105                 if (err == ENonNumericString)
00106                         {
00107                         // Print an error message as the initialisation string, KTxtValueString is invalid (non numeric format).
00108                         console->Printf(KTxtInitCounterFailed);
00109                         }
00110                 else
00111                         {
00112                         // Initialisation from string succeeded.
00113                         // Counter is initialised to 100.
00114                         console->Printf(KTxtInitCounterSucceeded);
00115                         }
00116                 }
00117         
00118         // Wait for a key press.
00119         console->Printf(KMsgPressAnyKey);
00120         console->Getch();
00121         console->ClearScreen();
00122 
00123         // Print values of both counters.
00124         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00125         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00126 
00127         // Wait for a key press.
00128         console->Printf(KMsgPressAnyKey);
00129         console->Getch();
00130         console->ClearScreen();
00131 
00132         // Increase the counter value by 1 and print it.
00133         _LIT(KTxt1,"\nIncrease counterA by default value (i.e. 1)..\n");
00134         console->Printf(KTxt1);
00135         counterA.Increase();
00136         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00137 
00138         // Increase the counter by a value greater than 1 and print it.
00139         _LIT(KTxt2,"\nIncrease counterA by 2..\n");
00140         console->Printf(KTxt2);
00141         // IncreaseByL operation undergoes a custom security check.
00142         // If the value is greater than 10 and the SID is greater than 0x70fffff5, it fails.
00143         counterA.IncreaseByL(2);
00144         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00145 
00146         // Increase the counter value by 1 and print it.
00147         _LIT(KTxt3,"\nIncrease counterB by default value (i.e. 1)..\n");
00148         console->Printf(KTxt3);
00149         counterB.Increase();
00150         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00151 
00152         // Increase the counter by a value greater than 1 and print it.
00153         _LIT(KTxt4,"\nIncrease counterA by 17..\n");
00154         console->Printf(KTxt4);
00155         // Fails if the SID is greater than 0x70fffff5
00156         counterA.IncreaseByL(17);
00157         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00158 
00159         // Wait for a key press.
00160         console->Printf(KMsgPressAnyKey);
00161         console->Getch();
00162         console->ClearScreen();
00163 
00164         // Increase the counter by a value greater than 1 and print it.
00165         _LIT(KTxt5,"\nIncrease counterB by 5..\n");
00166         console->Printf(KTxt5);
00167         counterB.IncreaseByL(5);
00168         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00169 
00170         // Decrease the counter value by 1 and print it.
00171         _LIT(KTxt6,"\nDecrease counterA..\n");
00172         console->Printf(KTxt6);
00173         counterA.Decrease();
00174         console->Printf(KTxtGetCounterAValue,counterA.CounterValueL());
00175 
00176         // Decrease the counter by a value greater than 1 and print it.
00177         _LIT(KTxt7,"\nDecrease counterB by 3..\n");
00178         console->Printf(KTxt7);
00179         console->Printf(KTxtGetCounterBValue,counterB.CounterValueL());
00180         // DecreaseByL operation undergoes a custom security check.
00181         // If the value is greater than 10 and the SID is greater than 0x70fffff5, it fails.
00182         counterB.DecreaseByL(3);
00183 
00184         // Print the resource count of the server.
00185         _LIT(KTxt8,"\nResource count is.. %d \n");
00186         console->Printf(KTxt8,countserv.ResourceCountL());
00187 
00188         // The user can save the counter value to a file.
00189         console->Printf(KTxtSaveToFile);
00190         TUint8 sel = console->Getch();
00191         if(sel == 'a')
00192                 {
00193                 // Save the value of the counter A.
00194                 console->Printf(KTxtSelectCounter,sel);
00195                 console->Printf(KTxtSaveCounter);
00196                 // This call will fail if the client executable does not have required file write capability.
00197                 counterA.SaveCounterL();
00198                 }
00199         else if(sel == 'b')
00200                 {
00201                 // Save the value of the counter B.
00202                 console->Printf(KTxtSelectCounter,sel);
00203                 console->Printf(KTxtSaveCounter);
00204                 // This call will fail if the client executable does not have required file write capability.
00205                 counterB.SaveCounterL();
00206                 }
00207 
00208         // Close both sub sessions and the main session.
00209         _LIT(KTxt9,"\nClosing counterA and then CounterB..\n");
00210         console->Printf(KTxt9);
00211         counterA.Close();
00212         counterB.Close();
00213         countserv.Close();
00214         }
00215 
00216 GLDEF_C TInt E32Main()
00217         {
00218         __UHEAP_MARK;
00219         CTrapCleanup* cleanup = CTrapCleanup::New();
00220 
00221         TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
00222         if (createError)
00223                 return createError;
00224 
00225         TRAPD(mainError, DoExampleL());
00226         if (mainError)
00227                 console->Printf(KTextFailed, mainError);
00228         console->Printf(KTextPressAnyKey);
00229         console->Getch();
00230 
00231         delete console;
00232         delete cleanup;
00233         __UHEAP_MARKEND;
00234         return KErrNone;
00235         }

Generated by  doxygen 1.6.2