Skip to content

Package: SynchronizeLogic

SynchronizeLogic

nameinstructionbranchcomplexitylinemethod
SynchronizeLogic(SynchronizeCommunication)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
areWeInAdminMode()
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
authenticate(String)
M: 85 C: 0
0%
M: 14 C: 0
0%
M: 9 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
switchToAdminMode(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package update.remote.imond;
2:
3: import java.util.StringTokenizer;
4:
5: /**
6: * This class is for the logic while the connection to the router.
7: *
8: * @author Patrick Kriegel
9: *
10: */
11: public class SynchronizeLogic {
12:
13:         /**
14:          * This attribute represents the SynchronizeCommunication for the communication with imond.
15:          */
16:         private final SynchronizeCommunication synchronizeCommunication;
17:
18:         /**
19:          * This is the constructor for SynchronizeLogic.
20:          *
21:          * @param synchronizecommunicationMain
22:          * this is the SynchronizeCommnication of the main class.
23:          */
24:         public SynchronizeLogic(final SynchronizeCommunication synchronizecommunicationMain) {
25:                 this.synchronizeCommunication = synchronizecommunicationMain;
26:         }
27:
28:         /**
29:          * The method authenticates the user on fli4l and lets check the password. It reacts on the
30:          * answers of the imond in different cases. case 1: user password is needed; case 2: there is
31:          * not an user password, but an admin password. So switch into admin mode; case 3: the password
32:          * is incorrect; case 4: it exists no password, so no authentication necessary.
33:          *
34:          * @param password
35:          * password for authentication on fli4l
36:          * @return nothing
37:          * @throws CommunicationException
38:          * if there are problems with the connection
39:          */
40:         public String authenticate(final String password) throws CommunicationException {
41:                 final String checkPassNeeded = this.synchronizeCommunication.pass();
42:                 final StringTokenizer st = new StringTokenizer(checkPassNeeded, ImondConstants.SPACE);
43:                 st.nextToken();
44:                 final String result = st.nextToken().trim();
45:                 final int check = Integer.parseInt(result);
46:
47:•                switch (check) {
48:                 case 1:
49:                 case 2:
50:•                        if (!password.equals(ImondConstants.EMPTYSTRING)) {
51:                                 switchToAdminMode(password);
52:•                                if (!areWeInAdminMode()) {
53:                                         throw new CommunicationException(ImondConstants.EME_ADMINPASSWORDWRONG);
54:                                 }
55:                         } else {
56:                                 throw new CommunicationException(ImondConstants.EME_PASSWORDISEMPTY);
57:                         }
58:                         break;
59:                 case 2 + 1:
60:•                        if (password.equals(ImondConstants.EMPTYSTRING)) {
61:                                 throw new CommunicationException(ImondConstants.EME_PASSWORDISEMPTY);
62:                         }
63:                         final String answer = this.synchronizeCommunication.pass(password);
64:•                        if (!answer.startsWith(ImondConstants.OK)) {
65:                                 throw new CommunicationException(ImondConstants.EME_PASSWORDISWRONG);
66:                         }
67:                         break;
68:                 case 2 + 2:
69:•                        if (!password.equals(ImondConstants.EMPTYSTRING)) {
70:                                 return ImondConstants.NOPASSWORDNEEDED;
71:                         }
72:                         break;
73:                 default:
74:                         System.out.println(ImondConstants.EME_AUTHENTICATIONPROBLEMPARTONE + checkPassNeeded // NOPMD
75:                                                                                                                                                                                                         // console
76:                                                                                                                                                                                                         // output
77:                                         + ImondConstants.EME_AUTHENTICATIONPROBLEMPARTTWO);
78:                         break;
79:                 }
80:
81:                 return ImondConstants.EMPTYSTRING;
82:         }
83:
84:         /**
85:          * This method switches to admin mode, if the password is the admin password.
86:          *
87:          * @param adminPwd
88:          * admin password
89:          * @throws CommunicationException
90:          * if there are problems with the connection
91:          */
92:         private void switchToAdminMode(final String adminPwd) throws CommunicationException {
93:                 this.synchronizeCommunication.pass(adminPwd);
94:         }
95:
96:         /**
97:          * This method checks if we are in the admin mode.
98:          *
99:          * @return true or false
100:          * @throws CommunicationException
101:          * if there are problems with the connection
102:          */
103:         private boolean areWeInAdminMode() throws CommunicationException {
104:                 final String answer = this.synchronizeCommunication.pass();
105:                 final StringTokenizer st = new StringTokenizer(answer, ImondConstants.SPACE);
106:                 st.nextToken();
107:•                return Integer.parseInt(st.nextToken().trim()) >= ImondConstants.FOUR;
108:         }
109: }