Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 07 Jan 2013 07:39:15 -0800
changeset 6803fbe83a6d10fa
parent 6801 f92e3d5f71a2
child 6804 a91717ac8ff1
Add a few keyboard tests; fix missing input validation in SDL_GetKeyFromName
src/events/SDL_keyboard.c
test/testautomation_keyboard.c
     1.1 --- a/src/events/SDL_keyboard.c	Sun Jan 06 18:19:30 2013 -0800
     1.2 +++ b/src/events/SDL_keyboard.c	Mon Jan 07 07:39:15 2013 -0800
     1.3 @@ -953,6 +953,9 @@
     1.4  {
     1.5  	SDL_Keycode key;
     1.6  
     1.7 +        /* Check input */
     1.8 +        if (name == NULL) return SDLK_UNKNOWN;
     1.9 +        
    1.10  	/* If it's a single UTF-8 character, then that's the keycode itself */
    1.11  	key = *(const unsigned char *)name;
    1.12  	if (key >= 0xF0) {
     2.1 --- a/test/testautomation_keyboard.c	Sun Jan 06 18:19:30 2013 -0800
     2.2 +++ b/test/testautomation_keyboard.c	Mon Jan 07 07:39:15 2013 -0800
     2.3 @@ -9,16 +9,12 @@
     2.4  
     2.5  /* ================= Test Case Implementation ================== */
     2.6  
     2.7 -/*!
     2.8 - * TODO: Add tests for keyboard here
     2.9 - *
    2.10 - */
    2.11 -
    2.12  /* Test case functions */
    2.13  
    2.14  /**
    2.15 - * @brief Check call to SDL_GetKeyboardState
    2.16 + * @brief Check call to SDL_GetKeyboardState with and without numkeys reference.
    2.17   * 
    2.18 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardState
    2.19   */
    2.20  int
    2.21  keyboard_getKeyboardState(void *arg)
    2.22 @@ -41,15 +37,119 @@
    2.23     return TEST_COMPLETED;
    2.24  }
    2.25  
    2.26 +/**
    2.27 + * @brief Check call to SDL_GetKeyboardFocus
    2.28 + * 
    2.29 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyboardFocus
    2.30 + */
    2.31 +int
    2.32 +keyboard_getKeyboardFocus(void *arg)
    2.33 +{
    2.34 +   SDL_Window* window;
    2.35 +
    2.36 +   /* Call, but ignore return value */
    2.37 +   window = SDL_GetKeyboardFocus();
    2.38 +   SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()");
    2.39 +
    2.40 +   return TEST_COMPLETED;
    2.41 +}
    2.42 +
    2.43 +/**
    2.44 + * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name.
    2.45 + * 
    2.46 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromName
    2.47 + */
    2.48 +int
    2.49 +keyboard_getKeyFromName(void *arg)
    2.50 +{
    2.51 +   SDL_Keycode result;
    2.52 +
    2.53 +   /* Case where Key is known, 1 character input */
    2.54 +   result = SDL_GetKeyFromName("A");
    2.55 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)");
    2.56 +   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
    2.57 +
    2.58 +   /* Case where Key is known, 2 character input */
    2.59 +   result = SDL_GetKeyFromName("F1");
    2.60 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)");
    2.61 +   SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i", SDLK_F1, result);
    2.62 +
    2.63 +   /* Case where Key is known, 3 character input */
    2.64 +   result = SDL_GetKeyFromName("End");
    2.65 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)");
    2.66 +   SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i", SDLK_END, result);
    2.67 +
    2.68 +   /* Case where Key is known, 4 character input */
    2.69 +   result = SDL_GetKeyFromName("Find");
    2.70 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)");
    2.71 +   SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i", SDLK_FIND, result);
    2.72 +
    2.73 +   /* Case where Key is known, multiple character input */
    2.74 +   result = SDL_GetKeyFromName("AudioStop");
    2.75 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
    2.76 +   SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i", SDLK_AUDIOSTOP, result);
    2.77 +
    2.78 +   /* Case where Key is unknown */
    2.79 +   result = SDL_GetKeyFromName("NotThere");
    2.80 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)");
    2.81 +   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    2.82 +
    2.83 +   /* Case where input is NULL/invalid */
    2.84 +   result = SDL_GetKeyFromName(NULL);
    2.85 +   SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)");
    2.86 +   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
    2.87 +
    2.88 +   return TEST_COMPLETED;
    2.89 +}
    2.90 +
    2.91 +/**
    2.92 + * @brief Check call to SDL_GetKeyFromScancode
    2.93 + * 
    2.94 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetKeyFromScancode
    2.95 + */
    2.96 +int
    2.97 +keyboard_getKeyFromScancode(void *arg)
    2.98 +{
    2.99 +   SDL_Keycode result;
   2.100 +
   2.101 +   /* Case where input is valid */
   2.102 +   result = SDL_GetKeyFromScancode(SDL_SCANCODE_A);
   2.103 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
   2.104 +   SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i", SDLK_a, result);
   2.105 +
   2.106 +   /* Case where input is zero */
   2.107 +   result = SDL_GetKeyFromScancode(0);
   2.108 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(zero)");
   2.109 +   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   2.110 +
   2.111 +   /* Case where input is invalid */
   2.112 +   result = SDL_GetKeyFromScancode(-999);
   2.113 +   SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(invalid)");
   2.114 +   SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i", SDLK_UNKNOWN, result);
   2.115 +
   2.116 +   return TEST_COMPLETED;
   2.117 +}
   2.118 +
   2.119 +
   2.120 +
   2.121  /* ================= Test References ================== */
   2.122  
   2.123  /* Keyboard test cases */
   2.124  static const SDLTest_TestCaseReference keyboardTest1 =
   2.125 -		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState", TEST_ENABLED };
   2.126 +		{ (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED };
   2.127 +
   2.128 +static const SDLTest_TestCaseReference keyboardTest2 =
   2.129 +		{ (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED };
   2.130 +
   2.131 +static const SDLTest_TestCaseReference keyboardTest3 =
   2.132 +		{ (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED };
   2.133 +
   2.134 +static const SDLTest_TestCaseReference keyboardTest4 =
   2.135 +		{ (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED };
   2.136  
   2.137  /* Sequence of Keyboard test cases */
   2.138  static const SDLTest_TestCaseReference *keyboardTests[] =  {
   2.139 -	&keyboardTest1, NULL
   2.140 +	&keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, NULL
   2.141  };
   2.142  
   2.143  /* Keyboard test suite (global) */