IPMI password Max length test

Resolves openbmc/openbmc-test-automation#1937

Signed-off-by: Anusha Dathatri <adathatr@in.ibm.com>
Change-Id: I715a60121953bb10e4eda2b9d6e4b06111e4324f
diff --git a/ipmi/test_ipmi_user.robot b/ipmi/test_ipmi_user.robot
index 25d6bcc..aebd3b6 100644
--- a/ipmi/test_ipmi_user.robot
+++ b/ipmi/test_ipmi_user.robot
@@ -4,6 +4,7 @@
 Resource            ../lib/ipmi_client.robot
 Resource            ../lib/openbmc_ffdc.robot
 Library             ../lib/ipmi_utils.py
+Test Setup          Printn
 
 Test Teardown       Test Teardown Execution
 Suite Teardown      Delete All Non Root IPMI User
@@ -21,6 +22,8 @@
 ${valid_password}       0penBmc1
 ${max_password_length}  20
 ${ipmi_setaccess_cmd}   channel setaccess
+&{password_values}      16=0penBmc10penBmc2  17=0penBmc10penBmc2B
+              ...       20=0penBmc10penBmc2Bmc3  21=0penBmc10penBmc2Bmc34
 
 
 *** Test Cases ***
@@ -33,10 +36,7 @@
     # Delete all non-root IPMI (i.e. except userid 1)
     Delete All Non Root IPMI User
 
-    # Create a valid user and enable it.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
     Run IPMI Standard Command  user enable ${random_userid}
 
     # Verify maximum user count IPMI local user can have. Also verify
@@ -53,9 +53,7 @@
     [Documentation]  Create user via IPMI and verify.
     [Tags]  Test_IPMI_User_Creation_With_Valid_Name_And_ID
 
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
 
 Verify IPMI User Creation With Invalid Name
@@ -90,10 +88,7 @@
     ...  password.
     [Tags]  Verify_Setting_IPMI_User_With_Invalid_Password
 
-    # Create IPMI user.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
     # Set invalid password for newly created user.
     ${msg}=  Run Keyword And Expect Error  *  Run IPMI Standard Command
@@ -117,10 +112,7 @@
     [Documentation]  Verify IPMI user password using test command.
     [Tags]  Verify_IPMI_User_Password_Via_Test_Command
 
-    # Create IPMI user.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
     # Set valid password for newly created user.
     Run IPMI Standard Command
@@ -137,16 +129,12 @@
     [Documentation]  Set valid password for IPMI user and verify.
     [Tags]  Verify_Setting_Valid_Password_For_IPMI_User
 
-    # Create IPMI user.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
     # Set valid password for newly created user.
     Run IPMI Standard Command
     ...  user set password ${random_userid} ${valid_password}
 
-    # Enable IPMI user
     Run IPMI Standard Command  user enable ${random_userid}
 
     # Delay added for IPMI user to get enable
@@ -176,10 +164,7 @@
     ...  password.
     [Tags]  Verify_Setting_IPMI_User_With_Null_Password
 
-    # Create IPMI user.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
     # Set null password for newly created user.
     ${msg}=  Run Keyword And Expect Error  *  Run IPMI Standard Command
@@ -192,9 +177,7 @@
     [Documentation]  Delete user via IPMI and verify.
     [Tags]  Verify_IPMI_User_Deletion
 
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
 
     # Delete IPMI User and verify
     Run IPMI Standard Command  user set name ${random_userid} ""
@@ -244,23 +227,17 @@
     ...  to run IPMI command.
     [Tags]  Enable_IPMI_User_And_Verify
 
-    # Create IPMI user and set valid password.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
     Run IPMI Standard Command
     ...  user set password ${random_userid} ${valid_password}
 
     # Set admin privilege and enable IPMI messaging for newly created user.
     Set Channel Access  ${random_userid}  ipmi=on privilege=${admin_level_priv}
 
-    # Delay added for user privilge to get set.
+    # Delay added for user privilege to get set.
     Sleep  5s
 
-    # Enable IPMI user and verify.
-    Run IPMI Standard Command  user enable ${random_userid}
-    ${user_info}=  Get User Info  ${random_userid}
-    Should Be Equal  ${user_info['enable_status']}  enabled
+    Enable IPMI User And Verify  ${random_userid}
 
     # Verify that enabled IPMI  user is able to run IPMI command.
     Verify IPMI Username And Password  ${random_username}  ${valid_password}
@@ -271,10 +248,7 @@
     ...  is unable to run IPMI command.
     [Tags]  Disable_IPMI_User_And_Verify
 
-    # Create IPMI user and set valid password.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
     Run IPMI Standard Command
     ...  user set password ${random_userid} ${valid_password}
 
@@ -312,10 +286,7 @@
     [Documentation]  Set administrator and no access privilege for different channels and verify.
     [Tags]  Verify_Administrator_And_No_Access_Privilege_For_Different_Channels
 
-    # Create IPMI user and set valid password.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
     Run IPMI Standard Command
     ...  user set password ${random_userid} ${valid_password}
 
@@ -325,10 +296,7 @@
     # Set no access privilege for newly created user with channel 2.
     Set Channel Access  ${random_userid}  ipmi=on privilege=${no_access_priv}  2
 
-    # Enable IPMI user and verify.
-    Run IPMI Standard Command  user enable ${random_userid}
-    ${user_info}=  Get User Info  ${random_userid}
-    Should Be Equal  ${user_info['enable_status']}  enabled
+    Enable IPMI User And Verify  ${random_userid}
 
     # Verify that user is able to run administrator level IPMI command with channel 1.
     Verify IPMI Command  ${random_username}  ${valid_password}  Administrator  1
@@ -341,10 +309,7 @@
     [Documentation]  Set operator and user privilege for different channels and verify.
     [Tags]  Verify_Operator_And_User_Privilege_For_Different_Channels
 
-    # Create IPMI user and set valid password.
-    ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
-    IPMI Create User  ${random_userid}  ${random_username}
+    ${random_userid}  ${random_username}=  Create Random IPMI User
     Run IPMI Standard Command
     ...  user set password ${random_userid} ${valid_password}
 
@@ -354,10 +319,7 @@
     # Set user privilege for newly created user with channel 2.
     Set Channel Access  ${random_userid}  ipmi=on privilege=${user_priv}  2
 
-    # Enable IPMI user and verify.
-    Run IPMI Standard Command  user enable ${random_userid}
-    ${user_info}=  Get User Info  ${random_userid}
-    Should Be Equal  ${user_info['enable_status']}  enabled
+    Enable IPMI User And Verify  ${random_userid}
 
     # Verify that user is able to run operator level IPMI command with channel 1.
     Verify IPMI Command  ${random_username}  ${valid_password}  Operator  1
@@ -366,8 +328,71 @@
     Verify IPMI Command  ${random_username}  ${valid_password}  User  2
 
 
+Verify Setting IPMI User With Max Password Length
+    [Documentation]  Verify IPMI user creation with password length of 20 characters.
+    [Tags]  Verify_Setting_IPMI_User_With_Max_Password_Length
+    [Template]  Set User Password And Verify
+
+    # password_length  password_option  expected_status
+    20                 20               ${True}
+
+
+Verify Setting IPMI User With Invalid Password Length
+    [Documentation]  Verify that IPMI user cannot be set with 21 character password using 16 char
+    ...  or 20 char password option.
+    [Tags]  Verify_Setting_IPMI_User_With_Invalid_Password_Length
+    [Template]  Set User Password And Verify
+
+    # password_length  password_option  expected_status
+    21                 16               ${False}
+    21                 20               ${False}
+
+
+Verify Setting IPMI User With 16 Character Password
+    [Documentation]  Verify that IPMI user can create a 16 character password using 16 char or 20
+    ...  char password option.
+    [Tags]  Verify_Setting_IPMI_User_With_16_Character_Password
+    [Template]  Set User Password And Verify
+
+    # password_length  password_option  expected_status
+    16                 16               ${True}
+    16                 20               ${True}
+
+
+Verify Default Selection Of 16 Character Password For IPMI User
+    [Documentation]  Verify that ipmitool by default opts for the 16 character option when given a
+    ...  password whose length is in between 17 and 20.
+    [Tags]  Verify_Default_Selection_Of_16_Character_Password_For_IPMI_User
+    [Template]  Set User Password And Verify
+
+    # password_length  password_option  expected_status
+    17                 16               ${True}
+    20                 16               ${True}
+
+
 *** Keywords ***
 
+Create Random IPMI User
+    [Documentation]  Create IPMI user with random username and userid and return those fields.
+
+    ${random_username}=  Generate Random String  8  [LETTERS]
+    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
+    IPMI Create User  ${random_userid}  ${random_username}
+    [Return]  ${random_userid}  ${random_username}
+
+
+Enable IPMI User And Verify
+    [Documentation]  Enable the userid and verify that it has been enabled.
+    [Arguments]  ${userid}
+
+    # Description of argument(s):
+    # userid   A numeric userid (e.g. "4").
+
+    Run IPMI Standard Command  user enable ${userid}
+    ${user_info}=  Get User Info  ${userid}
+    Valid Value  user_info['enable_status']  ['enabled']
+
+
 Set Default Password For IPMI Root User
     [Documentation]  Set default password for IPMI root user (i.e. 0penBmc).
     # Set default password for root user.
@@ -403,13 +428,10 @@
     # Set privilege and enable IPMI messaging for newly created user.
     Set Channel Access  ${random_userid}  ipmi=on privilege=${privilege_level}
 
-    # Delay added for user privilge to get set.
+    # Delay added for user privilege to get set.
     Sleep  5s
 
-    # Enable IPMI user and verify.
-    Run IPMI Standard Command  user enable ${random_userid}
-    ${user_info}=  Get User Info  ${random_userid}
-    Should Be Equal  ${user_info['enable_status']}  enabled
+    Enable IPMI User And Verify  ${random_userid}
 
     Verify IPMI Command  ${random_username}  ${valid_password}  User
     ...  expected_status=${user_cmd_status}
@@ -426,7 +448,7 @@
     # Description of argument(s):
     # username         The user name (e.g. "root", "robert", etc.).
     # password         The user password (e.g. "0penBmc", "0penBmc1", etc.).
-    # privilege        The session privilge for IPMI command (e.g. "User", "Operator", etc.).
+    # privilege        The session privilege for IPMI command (e.g. "User", "Operator", etc.).
     # channel          The user channel number (e.g. "1" or "2").
     # expected_status  Expected status of IPMI command run with the user
     #                  of above password and privilege (i.e. "Passed" or "Failed").
@@ -437,6 +459,43 @@
     ...  L=${privilege}
 
 
+Set User Password And Verify
+    [Documentation]  Create a user and set its password with given length and option.
+    [Arguments]  ${password_length}  ${password_option}  ${expected_result}
+    # Description of argument(s):
+    # password_length  Length of password to be generated and used (e.g. "16").
+    # password_option  Password length option to be given in IPMI command (e.g. "16", "20").
+    # expected_result  Expected result for setting the user's password (e.g. "True", "False").
+
+    Rprint Vars  password_length  password_option  expected_result
+    ${random_userid}  ${random_username}=  Create Random IPMI User
+    ${password}=  Get From Dictionary  ${password_values}  ${password_length}
+    Rprint Vars  random_userid  password
+
+    # Set password for newly created user.
+    ${status}=  Run Keyword And Return Status  Run IPMI Standard Command
+    ...  user set password ${random_userid} ${password} ${password_option}
+    Rprint Vars  status
+    Valid Value  status  [${expected_result}]
+
+    # Set admin privilege and enable IPMI messaging for newly created user.
+    Set Channel Access  ${random_userid}  ipmi=on privilege=${admin_level_priv}
+
+    # Delay added for user privilege to get set.
+    Sleep  5s
+
+    Enable IPMI User And Verify  ${random_userid}
+
+    # For password_option 16, passwords with length between 17 and 20 will be truncated.
+    # For all other cases, passwords will be retained as it is to verify.
+    ${truncated_password}=  Set Variable  ${password[:${password_option}]}
+    Rprint Vars  truncated_password
+    ${status}=  Run Keyword And Return Status  Verify IPMI Username And Password  ${random_username}
+    ...  ${truncated_password}
+    Rprint Vars  status
+    Valid Value  status  [${expected_result}]
+
+
 Test Teardown Execution
     [Documentation]  Do the test teardown execution.