add test case where (fake) sms messages never arrive

This commit is contained in:
Eric House 2018-07-14 15:43:42 -07:00
parent 352d87a327
commit 2d8ac995b7
4 changed files with 62 additions and 44 deletions

View file

@ -639,6 +639,7 @@ typedef enum {
,CMD_DROPSENDRELAY ,CMD_DROPSENDRELAY
,CMD_DROPRCVRELAY ,CMD_DROPRCVRELAY
,CMD_DROPSENDSMS ,CMD_DROPSENDSMS
,CMD_SMSFAILPCT
,CMD_DROPRCVSMS ,CMD_DROPRCVSMS
,CMD_FORCECHANNEL ,CMD_FORCECHANNEL
@ -762,6 +763,7 @@ static CmdInfoRec CmdInfoRecs[] = {
,{ CMD_DROPSENDRELAY, false, "drop-send-relay", "start new games with relay send disabled" } ,{ CMD_DROPSENDRELAY, false, "drop-send-relay", "start new games with relay send disabled" }
,{ CMD_DROPRCVRELAY, false, "drop-receive-relay", "start new games with relay receive disabled" } ,{ CMD_DROPRCVRELAY, false, "drop-receive-relay", "start new games with relay receive disabled" }
,{ CMD_DROPSENDSMS, false, "drop-send-sms", "start new games with sms send disabled" } ,{ CMD_DROPSENDSMS, false, "drop-send-sms", "start new games with sms send disabled" }
,{ CMD_SMSFAILPCT, true, "sms-fail-pct", "percent of sms sends, randomly chosen, never arrive" }
,{ CMD_DROPRCVSMS, false, "drop-receive-sms", "start new games with sms receive disabled" } ,{ CMD_DROPRCVSMS, false, "drop-receive-sms", "start new games with sms receive disabled" }
,{ CMD_FORCECHANNEL, true, "force-channel", "force (clients) to use this hostid/channel" } ,{ CMD_FORCECHANNEL, true, "force-channel", "force (clients) to use this hostid/channel" }
@ -2445,6 +2447,10 @@ main( int argc, char** argv )
case CMD_DROPSENDSMS: case CMD_DROPSENDSMS:
mainParams.commsDisableds[COMMS_CONN_SMS][1] = XP_TRUE; mainParams.commsDisableds[COMMS_CONN_SMS][1] = XP_TRUE;
break; break;
case CMD_SMSFAILPCT:
mainParams.smsSendFailPct = atoi(optarg);
XP_ASSERT( mainParams.smsSendFailPct >= 0 && mainParams.smsSendFailPct <= 100 );
break;
case CMD_DROPRCVSMS: case CMD_DROPRCVSMS:
mainParams.commsDisableds[COMMS_CONN_SMS][0] = XP_TRUE; mainParams.commsDisableds[COMMS_CONN_SMS][0] = XP_TRUE;
break; break;

View file

@ -114,64 +114,74 @@ unlock_queue( LinSMSData* storage )
} }
static XP_S16 static XP_S16
send_sms( LinSMSData* storage, XWStreamCtxt* stream, write_fake_sms( LaunchParams* params, XWStreamCtxt* stream,
const XP_UCHAR* phone, XP_U16 port ) const XP_UCHAR* phone, XP_U16 port )
{ {
const XP_U8* buf = stream_getPtr( stream ); XP_S16 nSent;
XP_U16 buflen = stream_getSize( stream ); XP_U16 pct = XP_RANDOM() % 100;
XP_LOGF( "%s(phone=%s, port=%d, len=%d)", __func__, phone, XP_Bool skipWrite = pct < params->smsSendFailPct;
port, buflen );
XP_S16 nSent = -1; if ( skipWrite ) {
XP_ASSERT( !!storage ); nSent = stream_getSize( stream );
char path[256]; XP_LOGF( "%s(): dropping sms msg of len %d to phone %s", __func__,
nSent, phone );
} else {
LinSMSData* storage = getStorage( params );
const XP_U8* buf = stream_getPtr( stream );
XP_U16 buflen = stream_getSize( stream );
XP_LOGF( "%s(phone=%s, port=%d, len=%d)", __func__, phone,
port, buflen );
lock_queue( storage ); XP_ASSERT( !!storage );
char path[256];
lock_queue( storage );
#ifdef DEBUG #ifdef DEBUG
gchar* str64 = g_base64_encode( buf, buflen ); gchar* str64 = g_base64_encode( buf, buflen );
#endif #endif
formatQueuePath( phone, port, path, sizeof(path) ); formatQueuePath( phone, port, path, sizeof(path) );
/* Random-number-based name is fine, as we pick based on age. */ /* Random-number-based name is fine, as we pick based on age. */
int rint = makeRandomInt(); int rint = makeRandomInt();
g_mkdir_with_parents( path, 0777 ); /* just in case */ g_mkdir_with_parents( path, 0777 ); /* just in case */
int len = strlen( path ); int len = strlen( path );
snprintf( &path[len], sizeof(path)-len, "/%u", rint ); snprintf( &path[len], sizeof(path)-len, "/%u", rint );
XP_UCHAR sms[buflen*2]; /* more like (buflen*4/3) */ XP_UCHAR sms[buflen*2]; /* more like (buflen*4/3) */
XP_U16 smslen = sizeof(sms); XP_U16 smslen = sizeof(sms);
binToSms( sms, &smslen, buf, buflen ); binToSms( sms, &smslen, buf, buflen );
XP_ASSERT( smslen == strlen(sms) ); XP_ASSERT( smslen == strlen(sms) );
XP_LOGF( "%s: writing msg to %s", __func__, path ); XP_LOGF( "%s: writing msg to %s", __func__, path );
#ifdef DEBUG #ifdef DEBUG
XP_ASSERT( !strcmp( str64, sms ) ); XP_ASSERT( !strcmp( str64, sms ) );
g_free( str64 ); g_free( str64 );
XP_U8 testout[buflen]; XP_U8 testout[buflen];
XP_U16 lenout = sizeof( testout ); XP_U16 lenout = sizeof( testout );
XP_ASSERT( smsToBin( testout, &lenout, sms, smslen ) ); XP_ASSERT( smsToBin( testout, &lenout, sms, smslen ) );
XP_ASSERT( lenout == buflen ); XP_ASSERT( lenout == buflen );
// valgrind doesn't like this; punting on figuring out // valgrind doesn't like this; punting on figuring out
// XP_ASSERT( XP_MEMCMP( testout, buf, smslen ) ); // XP_ASSERT( XP_MEMCMP( testout, buf, smslen ) );
#endif #endif
FILE* fp = fopen( path, "w" ); FILE* fp = fopen( path, "w" );
XP_ASSERT( !!fp ); XP_ASSERT( !!fp );
(void)fprintf( fp, ADDR_FMT, storage->myPhone, storage->myPort ); (void)fprintf( fp, ADDR_FMT, storage->myPhone, storage->myPort );
(void)fprintf( fp, "%s\n", sms ); (void)fprintf( fp, "%s\n", sms );
fclose( fp ); fclose( fp );
sync(); sync();
unlock_queue( storage ); unlock_queue( storage );
nSent = buflen; nSent = buflen;
LOG_RETURNF( "%d", nSent ); LOG_RETURNF( "%d", nSent );
}
return nSent; return nSent;
} /* linux_sms_send */ } /* write_fake_sms */
static XP_S16 static XP_S16
decodeAndDelete( LinSMSData* storage, const gchar* name, decodeAndDelete( LinSMSData* storage, const gchar* name,
@ -350,8 +360,7 @@ linux_sms_invite( LaunchParams* params, const CurGameInfo* gi,
addrToStream( stream, addr ); addrToStream( stream, addr );
LinSMSData* storage = getStorage( params ); write_fake_sms( params, stream, toPhone, toPort );
send_sms( storage, stream, toPhone, toPort );
stream_destroy( stream ); stream_destroy( stream );
} }
@ -381,8 +390,7 @@ doSend( LaunchParams* params, const XP_U8* buf,
stream_putU32( stream, gameID ); stream_putU32( stream, gameID );
stream_putBytes( stream, buf, buflen ); stream_putBytes( stream, buf, buflen );
LinSMSData* storage = getStorage( params ); (void)write_fake_sms( params, stream, phone, port );
(void)send_sms( storage, stream, phone, port );
stream_destroy( stream ); stream_destroy( stream );
} }

View file

@ -59,6 +59,7 @@ typedef struct LaunchParams {
char* dbName; char* dbName;
sqlite3* pDb; /* null unless opened */ sqlite3* pDb; /* null unless opened */
XP_U16 saveFailPct; XP_U16 saveFailPct;
XP_U16 smsSendFailPct;
const XP_UCHAR* playerDictNames[MAX_NUM_PLAYERS]; const XP_UCHAR* playerDictNames[MAX_NUM_PLAYERS];
#ifdef USE_SQLITE #ifdef USE_SQLITE
char* dbFileName; char* dbFileName;

View file

@ -393,6 +393,8 @@ def build_cmds(args):
PARAMS += ['--use-udp'] PARAMS += ['--use-udp']
if args.ADD_SMS: if args.ADD_SMS:
PARAMS += [ '--sms-number', PHONE_BASE + str(DEV - 1) ] PARAMS += [ '--sms-number', PHONE_BASE + str(DEV - 1) ]
if args.SMS_FAIL_PCT > 0:
PARAMS += [ '--sms-fail-pct', args.SMS_FAIL_PCT ]
if DEV > 1: if DEV > 1:
PARAMS += [ '--server-sms-number', PHONE_BASE + '0' ] PARAMS += [ '--server-sms-number', PHONE_BASE + '0' ]
@ -655,6 +657,7 @@ def mkParser():
parser.add_argument('--trade-pct', dest = 'TRADE_PCT', default = 0, type = int) parser.add_argument('--trade-pct', dest = 'TRADE_PCT', default = 0, type = int)
parser.add_argument('--add-sms', dest = 'ADD_SMS', default = False, action = 'store_true') parser.add_argument('--add-sms', dest = 'ADD_SMS', default = False, action = 'store_true')
parser.add_argument('--sms-fail-pct', dest = 'SMS_FAIL_PCT', default = 0, type = int)
parser.add_argument('--remove-relay', dest = 'ADD_RELAY', default = True, action = 'store_false') parser.add_argument('--remove-relay', dest = 'ADD_RELAY', default = True, action = 'store_false')
parser.add_argument('--with-valgrind', dest = 'VALGRIND', default = False, parser.add_argument('--with-valgrind', dest = 'VALGRIND', default = False,