|
| 1 | +/**************************************************************** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one * |
| 3 | + * or more contributor license agreements. See the NOTICE file * |
| 4 | + * distributed with this work for additional information * |
| 5 | + * regarding copyright ownership. The ASF licenses this file * |
| 6 | + * to you under the Apache License, Version 2.0 (the * |
| 7 | + * "License"); you may not use this file except in compliance * |
| 8 | + * with the License. You may obtain a copy of the License at * |
| 9 | + * * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 11 | + * * |
| 12 | + * Unless required by applicable law or agreed to in writing, * |
| 13 | + * software distributed under the License is distributed on an * |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
| 15 | + * KIND, either express or implied. See the License for the * |
| 16 | + * specific language governing permissions and limitations * |
| 17 | + * under the License. * |
| 18 | + ****************************************************************/ |
| 19 | + |
| 20 | +package org.apache.james.managesieveserver; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Base64; |
| 25 | + |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Disabled; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +public class AuthenticateTest { |
| 33 | + private ManageSieveClient client; |
| 34 | + private final ManageSieveServerTestSystem testSystem; |
| 35 | + |
| 36 | + public AuthenticateTest() throws Exception { |
| 37 | + this.testSystem = new ManageSieveServerTestSystem(); |
| 38 | + } |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setUp() throws Exception { |
| 42 | + this.testSystem.setUp(); |
| 43 | + this.client = new ManageSieveClient(); |
| 44 | + this.client.connect(this.testSystem.getBindedIP(), this.testSystem.getBindedPort()); |
| 45 | + this.client.readResponse(); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + void tearDown() { |
| 50 | + this.testSystem.manageSieveServer.destroy(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void plainLoginWithCorrectCredentialsShouldSucceed() throws IOException { |
| 55 | + this.authenticatePlain(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void plainLoginWithWrongPasswordShouldNotSucceed() throws IOException { |
| 60 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD + "wrong"); |
| 61 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 62 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 63 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void plainLoginWithNotExistingUserShouldNotSucceed() throws IOException { |
| 68 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "not-existing" + "\0" + "pwd"); |
| 69 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 70 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 71 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void plainLoginWithoutPasswordShouldNotSucceed() throws IOException { |
| 76 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0"); |
| 77 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 78 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 79 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 80 | + } |
| 81 | + |
| 82 | + // The SASL PLAIN standard (https://datatracker.ietf.org/doc/html/rfc4616) defines the following message: |
| 83 | + // message = [authzid] UTF8NUL authcid UTF8NUL passwd |
| 84 | + // The current code is more lenient. |
| 85 | + @Disabled |
| 86 | + @Test |
| 87 | + void plainLoginWithMalformedMessageShouldNotSucceed() throws IOException { |
| 88 | + String initialClientResponse = (ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 89 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 90 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 91 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void plainLoginWithoutMechanismQuotesShouldNotSucceed() throws IOException { |
| 96 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 97 | + this.client.sendCommand("AUTHENTICATE PLAIN \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 98 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 99 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void plainLoginWithoutInitialResponseQuotesShouldNotSucceed() throws IOException { |
| 104 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 105 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" " + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8))); |
| 106 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 107 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void plainLoginWithContinuationShouldSucceed() throws IOException { |
| 112 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\""); |
| 113 | + ManageSieveClient.ServerResponse continuationResponse = this.client.readResponse(); |
| 114 | + Assertions.assertThat(continuationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 115 | + Assertions.assertThat(continuationResponse.responseLines()).containsExactly("\"\""); |
| 116 | + |
| 117 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 118 | + this.client.sendCommand("\"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 119 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 120 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void plainLoginWithContinuationCanBeAborted() throws IOException { |
| 125 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\""); |
| 126 | + ManageSieveClient.ServerResponse continuationResponse = this.client.readResponse(); |
| 127 | + Assertions.assertThat(continuationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 128 | + Assertions.assertThat(continuationResponse.responseLines()).containsExactly("\"\""); |
| 129 | + |
| 130 | + this.client.sendCommand("\"*\""); |
| 131 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 132 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 133 | + Assertions.assertThat(authenticationResponse.explanation()).get().isEqualTo("authentication aborted"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void doubleAuthenticationShouldFail() throws IOException { |
| 138 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 139 | + String command = "AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""; |
| 140 | + |
| 141 | + this.client.sendCommand(command); |
| 142 | + ManageSieveClient.ServerResponse firstAuthenticationResponse = this.client.readResponse(); |
| 143 | + Assertions.assertThat(firstAuthenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 144 | + |
| 145 | + this.client.sendCommand(command); |
| 146 | + ManageSieveClient.ServerResponse secondAuthenticationResponse = this.client.readResponse(); |
| 147 | + Assertions.assertThat(secondAuthenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 148 | + Assertions.assertThat(secondAuthenticationResponse.explanation()).get().isEqualTo("already authenticated"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void unauthenticateInUnauthenticatedStateShouldFail() throws IOException { |
| 153 | + this.client.sendCommand("UNAUTHENTICATE"); |
| 154 | + ManageSieveClient.ServerResponse response = this.client.readResponse(); |
| 155 | + Assertions.assertThat(response.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void unauthenticateInAuthenticatedStateShouldSucceed() throws IOException { |
| 160 | + this.authenticatePlain(); |
| 161 | + |
| 162 | + this.client.sendCommand("UNAUTHENTICATE"); |
| 163 | + ManageSieveClient.ServerResponse response = this.client.readResponse(); |
| 164 | + Assertions.assertThat(response.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void authenticatedStateUnlocksNewCommands() throws IOException { |
| 169 | + this.client.sendCommand("LISTSCRIPTS"); |
| 170 | + ManageSieveClient.ServerResponse unauthenticatedResponse = this.client.readResponse(); |
| 171 | + Assertions.assertThat(unauthenticatedResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 172 | + |
| 173 | + this.authenticatePlain(); |
| 174 | + |
| 175 | + this.client.sendCommand("LISTSCRIPTS"); |
| 176 | + ManageSieveClient.ServerResponse authenticatedResponse = this.client.readResponse(); |
| 177 | + Assertions.assertThat(authenticatedResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 178 | + |
| 179 | + this.client.sendCommand("UNAUTHENTICATE"); |
| 180 | + ManageSieveClient.ServerResponse response = this.client.readResponse(); |
| 181 | + Assertions.assertThat(response.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 182 | + |
| 183 | + this.client.sendCommand("LISTSCRIPTS"); |
| 184 | + ManageSieveClient.ServerResponse loggedOutResponse = this.client.readResponse(); |
| 185 | + Assertions.assertThat(loggedOutResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.NO); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void logoutShouldWorkInUnauthenticatedState() throws IOException, InterruptedException { |
| 190 | + this.client.sendCommand("LOGOUT"); |
| 191 | + ManageSieveClient.ServerResponse response = this.client.readResponse(); |
| 192 | + Assertions.assertThat(response.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 193 | + Assertions.assertThat(this.client.isConnected()).isFalse(); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void logoutShouldWorkInAuthenticatedState() throws IOException, InterruptedException { |
| 198 | + this.authenticatePlain(); |
| 199 | + |
| 200 | + this.client.sendCommand("LOGOUT"); |
| 201 | + ManageSieveClient.ServerResponse response = this.client.readResponse(); |
| 202 | + Assertions.assertThat(response.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 203 | + Assertions.assertThat(this.client.isConnected()).isFalse(); |
| 204 | + } |
| 205 | + |
| 206 | + void authenticatePlain() throws IOException { |
| 207 | + String initialClientResponse = ("\0" + ManageSieveServerTestSystem.USERNAME.asString() + "\0" + ManageSieveServerTestSystem.PASSWORD); |
| 208 | + this.client.sendCommand("AUTHENTICATE \"PLAIN\" \"" + Base64.getEncoder().encodeToString(initialClientResponse.getBytes(StandardCharsets.UTF_8)) + "\""); |
| 209 | + ManageSieveClient.ServerResponse authenticationResponse = this.client.readResponse(); |
| 210 | + Assertions.assertThat(authenticationResponse.responseType()).isEqualTo(ManageSieveClient.ResponseType.OK); |
| 211 | + } |
| 212 | +} |
0 commit comments