Exemplo n.º 1
0
        public void testRegisteredInDatabase()
        {
            LINQDatabase mockDatabase = mocks.Stub <LINQDatabase>();

            User zarakavaUse = new User();

            zarakavaUse.setName("Zarakava");
            zarakavaUse.setPass("Testing");

            using (mocks.Record())
            {
                // The mock will return "Whale Rider" when the call is made with 24
                //mockDatabase.registerUser(zarakavaUse);
                //LastCall.Return(true);
                // mockDatabase.registerUser(null);
                // LastCall.Return(false);
            }

            UserRegistration.setDatabase(mockDatabase);
            Assert.IsTrue(UserRegistration.register(zarakavaUse));
            Assert.IsFalse(UserRegistration.register(null));
        }
Exemplo n.º 2
0
        public void onRecieve(string message)
        {
            string[] parsed = splitMessage(message.TrimEnd((char)0x00));
            // Message will contrain two parts
            // Type
            string type = parsed[0];
            // Params
            string param = parsed[1];

            if (("login".Equals(type)))
            {
                Console.WriteLine("Login " + param);
                string[] userPass = param.Split(' ');
                bool     returner;
                try
                {
                    if (userPass.Length > 2)
                    {
                        throw new Exception();
                    }
                    returner = UserRegistration.login(userPass[0], userPass[1]);
                    username = userPass[0];
                    //Console.WriteLine("User Registration thinks : " + returner);
                }
                catch
                {
                    returner = false;
                    sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
                    //sock.close();
                    throw;
                }
                sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
            }
            else if ("register".Equals(type))
            {
                string[] userPass = param.Split(' ');
                bool     returner;
                try
                {
                    if (userPass.Length > 2)
                    {
                        throw new Exception();
                    }
                    string newUsername = userPass[0];
                    string newUserpass = userPass[1];
                    Console.WriteLine("register " + newUsername + ' ' + newUserpass);
                    returner = UserRegistration.register(new User(newUsername, newUserpass));
                    //Console.WriteLine("User Registration thinks : " + returner);
                }
                catch
                {
                    returner = false;
                    sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
                    //sock.close();
                    throw;
                }
                sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
            }
            else if (username != null)
            {
                if ("requestfile".Equals(type))
                {
                    Console.WriteLine("Requestfile " + param);
                    sendFile(param);
                }
                else if ("ownedfiles".Equals(type))
                {
                    Console.WriteLine("Ownedfiles " + param);
                    ownedFiles(param);
                }


                else if ("searchtag".Equals(type))
                {
                    Console.WriteLine("searchtag " + param);
                    searchtag(message);
                }
                else if ("alltags".Equals(type))
                {
                    Console.WriteLine("alltags " + param);
                    alltags();
                }
                else if ("searchownedfiles".Equals(type))
                {
                    Console.WriteLine("searchownedfiles " + param);
                    searchownedfiles(message);
                }
                else if ("quit".Equals(type))
                {
                    Console.WriteLine("Quit " + param);
                    quit();
                }
                else if ("addfile".Equals(type))
                {
                    string[] userPass = param.Split(' ');
                    bool     returner;
                    try
                    {
                        Console.WriteLine("addfile " + userPass[0] + ' ' + userPass[1]);
                        returner = Search.database.AddOwnership(userPass[0], userPass[1]);
                        //Console.WriteLine("User Registration thinks : " + returner);
                    }
                    catch
                    {
                        returner = true;
                        sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
                        //sock.close();
                        throw;
                    }
                    sock.send(iSocket.aSocket.stringToBytes(returner.ToString(), iSocket.constants.MAXPACKETSIZE));
                }
                else if ("message".Equals(type))
                {
                    string ret = "False";
                    try
                    {
                        string[] args        = param.Split(' ');
                        String   tempMessage = param.Substring(param.IndexOf(' '));
                        //Send mail requires them to be users
                        Console.WriteLine("message T:F:M" + ' ' + args[0] + ' ' + username + ' ' + tempMessage);

                        Search.database.SendMail(new User(args[0], null), new User(username, null), tempMessage);
                        ret = "True";
                    }
                    catch
                    {
                        ret = "False";
                    }
                    sock.send(iSocket.aSocket.stringToBytes(ret.ToString(), iSocket.constants.MAXPACKETSIZE));
                }
                else if ("checkmail".Equals(type))
                {
                    Console.WriteLine("checkmail " + username);
                    List <Mail> mail = Search.database.CheckMail(username);

                    // if there is no mail just say that
                    if (mail.Count == 0)
                    {
                        sock.send(iSocket.aSocket.stringToBytes(mail.Count.ToString() + ' ', iSocket.constants.MAXPACKETSIZE));
                        return;
                    }

                    int    transfers   = mail.Count;
                    Mail   m           = mail[0];
                    String sender      = m.From;
                    String mailMessage = m.Message;
                    int    id          = m.ID;

                    sock.send(iSocket.aSocket.stringToBytes(mail.Count.ToString() + ' ' + sender + ' ' + id + ' ' + mailMessage, iSocket.constants.MAXPACKETSIZE));

                    Console.WriteLine(sender + ' ' + id + ' ' + mailMessage);

                    mail.RemoveAt(0);
                    while (mail.Count > 0)
                    {
                        m           = mail[0];
                        sender      = m.From;
                        mailMessage = m.Message;
                        id          = m.ID;


                        sock.send(iSocket.aSocket.stringToBytes(sender + ' ' + id + ' ' + mailMessage + ' ', iSocket.constants.MAXPACKETSIZE));
                        mail.RemoveAt(0);

                        Console.WriteLine(sender + ' ' + id + ' ' + mailMessage);
                    }
                }
            }
        }