Esempio n. 1
0
 public static ServiceMessage FromBytes(byte[] input)
 {
     var res = new ServiceMessage();
     res.MessageType = (MessageType)input[0];
     res.Data = input.Skip(1).ToArray();
     return res;
 }
Esempio n. 2
0
        public void LogonCredentialsConsistencyTest2()
        {
            var creds = new LogonCredentials { Login = "******", Password = "******" };
            var serializedCreds = creds.ToBytes();
            var serviceMessage = new ServiceMessage {MessageType = MessageType.Logon, Data = serializedCreds};
            var serializedServiceMessage = serviceMessage.ToBytes();

            var deserializedServiceMessage = ServiceMessage.FromBytes(serializedServiceMessage);
            Assert.AreEqual(MessageType.Logon, deserializedServiceMessage.MessageType);
            var deserializedCreds = LogonCredentials.FromBytes(deserializedServiceMessage.Data);

            Assert.AreEqual("peter", deserializedCreds.Login);
            Assert.AreEqual("secret", deserializedCreds.Password);
        }
Esempio n. 3
0
        public int LogOnInvalidMessage1()
        {
            try
            {
                var creds = new LogonCredentials { Login = this.Login, Password = this.password };
                var serializedCreds = creds.ToBytes();
                var serviceMessage = new ServiceMessage { MessageType = MessageType.Logon, Data = serializedCreds };

                var serviceMessageSerialized = serviceMessage.ToBytes();

                // Craft corrupted message
                var data = new byte[4 + 10];
                BitConverter.GetBytes(12).CopyTo(data, 0);
                this.stream.Write(data, 0, data.Length); // Send corrupted message

                var respData = this.cryptoWrapper.Receive();
                var smr = ServiceMessageResponse.FromBytes(respData);
                if (smr.IsSuccess)
                {
                    Logger.Debug(string.Format("Logon succeeded for user '{0}'", this.Login));
                    return 0;
                }
                else
                {
                    Logger.Debug(string.Format("Logon failed for user '{0}'. Reason: '{1}'", this.Login, smr.Message));
                    this.FreeClient();
                    return 1;
                }
            }
            catch (ArgumentNullException ex)
            {
                Logger.Error(ex.ToString);
                return 3;
            }
            catch (SocketException ex)
            {
                Logger.Error(ex.ToString);
                return 4;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString);
                return 5;
            }
        }
Esempio n. 4
0
 public int LogOn()
 {
     try
     {
         var creds = new LogonCredentials { Login = this.Login, Password = this.password };
         var serializedCreds = creds.ToBytes();
         var serviceMessage = new ServiceMessage { MessageType = MessageType.Logon, Data = serializedCreds };
         this.cryptoWrapper.Send(serviceMessage.ToBytes());
         var respData = this.cryptoWrapper.Receive();
         var smr = ServiceMessageResponse.FromBytes(respData);
         if (smr.IsSuccess)
         {
             Logger.Debug(string.Format("Logon succeeded for user '{0}'", this.Login));
             return 0;
         }
         else
         {
             Logger.Debug(string.Format("Logon failed for user '{0}'. Reason: '{1}'", this.Login, smr.Message));
             this.FreeClient();
             return 1;
         }
     }
     catch (ArgumentNullException ex)
     {
         Logger.Error(ex.ToString);
         return 3;
     }
     catch (SocketException ex)
     {
         Logger.Error(ex.ToString);
         return 4;
     }
     catch (Exception ex)
     {
         Logger.Error(ex.ToString);
         return 5;
     }
 }