Sample code for SIM Authentication

#define NEW_PIN_TEST "5678"
#define FAIL_PIN_TEST "4321"
#define FAIL_PUK_TEST "87654321"
//--------------------------------------------------------------------------------------------------
/**
* Print function.
*
*/
//--------------------------------------------------------------------------------------------------
void Print
(
char* string
)
{
bool sandboxed = (getuid() != 0);
 
if(sandboxed)
{
LE_INFO("%s", string);
}
else
{
fprintf(stderr, "%s\n", string);
}
}
//--------------------------------------------------------------------------------------------------
/**
* Test: Authentication (pin/puk).
*
*/
//--------------------------------------------------------------------------------------------------
void simTest_Authentication
(
le_sim_Id_t simId,
const char* pinPtr,
const char* pukPtr
)
{
bool ready = false;
int32_t remainingPinTries = 0;
int32_t tries = 0;
uint32_t remainingPukTries = 0;
uint32_t pukTries = 0;
char string[100];
 
memset(string, 0, 100);
 
// Get the remaining PIN entries
remainingPinTries = le_sim_GetRemainingPINTries(simId);
 
// Enter PIN code
res = le_sim_EnterPIN(simId, FAIL_PIN_TEST);
 
// Get the remaining PIN entries
LE_ASSERT((remainingPinTries-tries) == 1);
 
// Check that the SIM is not ready
ready = le_sim_IsReady(simId);
LE_ASSERT(!ready);
 
// Enter PIN code
res = le_sim_EnterPIN(simId, pinPtr);
LE_ASSERT(res == LE_OK);
 
// Check that the SIM is ready
ready = le_sim_IsReady(simId);
LE_ASSERT(ready);
 
// Change PIN
res = le_sim_ChangePIN(simId, FAIL_PIN_TEST, NEW_PIN_TEST);
 
// Change the PIN code
res = le_sim_ChangePIN(simId, pinPtr, NEW_PIN_TEST);
LE_ASSERT(res == LE_OK);
 
// block the SIM:
// while remaining PIN entries not null, enter a wrong PIN code
while((remainingPinTries = le_sim_GetRemainingPINTries(simId)) > 0)
{
// Enter PIN code
res = le_sim_EnterPIN(simId, FAIL_PIN_TEST);
}
 
if(remainingPinTries < 0)
{
sprintf(string, "\nle_sim_GetRemainingPINTries error, res.%d (should be >=0)\n",
remainingPinTries);
Print(string);
}
 
// Get the remaining PUK entries
LE_ASSERT_OK(le_sim_GetRemainingPUKTries(simId, &remainingPukTries));
 
// Unblock the SIM using a wrong PUK code (error expected)
res = le_sim_Unblock(simId, FAIL_PUK_TEST, NEW_PIN_TEST);
 
// Get the remaining PUK entries
LE_ASSERT(1 == (remainingPukTries-pukTries));
 
// Unblock the SIM using the correct PUK code
res = le_sim_Unblock(simId, pukPtr, NEW_PIN_TEST);
LE_ASSERT(LE_OK == res);
 
// Get the remaining PUK entries
LE_ASSERT((0 == remainingPukTries-pukTries));
 
Print("End simTest_Authentication");
}