Ejemplo n.º 1
0
        public void UserCollectionCrud()
        {
            IUser user = new User();
            user.Username = "******";
            user.Role = "User";
            user.PublicKeyId = Guid.NewGuid();
            user.ContainerIds = new List<Guid>();

            for (int i = 0; i < 10; i++)
            {
                user.ContainerIds.Add(Guid.NewGuid());
            }

            var newUser = _provider.UserCreate(user);

            Assert.IsNotNull(newUser, "User should not be null");

            Assert.AreEqual(user.Username, newUser.Username, "Username mismatch");
            Assert.AreEqual(user.Role, newUser.Role, "Role mismatch");
            Assert.AreEqual(user.PublicKeyId, newUser.PublicKeyId, "PublicKeyId mismatch");
            Assert.AreEqual(user.ContainerIds.Count, newUser.ContainerIds.Count, "ContainerIds.Count mismatch");

            for (int i = 0; i < user.ContainerIds.Count; i++)
            {
                Assert.IsTrue(newUser.ContainerIds.Contains(user.ContainerIds[i]), "Missing ContainerId");
            }

            newUser.Username = "******";
            newUser.PublicKeyId = Guid.NewGuid();
            newUser.ContainerIds = new List<Guid>();

            for (int i = 0; i < 20; i++)
            {
                newUser.ContainerIds.Add(Guid.NewGuid());
            }

            Assert.IsTrue(_provider.UserUpdate(newUser), "User could not be updated");

            user = newUser;

            newUser = _provider.UserRetrieve(user.Id);

            Assert.IsNotNull(newUser, "User should not be null");

            Assert.AreEqual(user.Username, newUser.Username, "Username mismatch");
            Assert.AreEqual(user.PublicKeyId, newUser.PublicKeyId, "PublicKeyId mismatch");
            Assert.AreEqual(user.ContainerIds.Count, newUser.ContainerIds.Count, "ContainerIds.Count mismatch");

            for (int i = 0; i < user.ContainerIds.Count; i++)
            {
                Assert.IsTrue(newUser.ContainerIds.Contains(user.ContainerIds[i]), "Missing ContainerId");
            }

            Assert.IsTrue(_provider.UserDelete(newUser), "User could not be deleted");

            var deletedUser = _provider.UserRetrieve(newUser.Id);

            Assert.IsNull(deletedUser);
        }
Ejemplo n.º 2
0
        public void UniqueUser()
        {
            IUser user = new User();
            user.Username = DateTime.Now.Ticks.ToString();
            user.PublicKeyId = Guid.NewGuid();
            user.ContainerIds = new List<Guid>();

            for (int i = 0; i < 10; i++)
            {
                user.ContainerIds.Add(Guid.NewGuid());
            }

            var newUser = _provider.UserCreate(user);

            Assert.IsNotNull(newUser);

            var newUser2 = _provider.UserCreate(user);

            var users = _provider.UserRetrieveByList(u => u.Username == user.Username);

            Assert.IsTrue(users.Count() == 1);
        }
Ejemplo n.º 3
0
        public void UserExistsTrigger()
        {
            IUser user = new User();
            user.Username = DateTime.Now.Ticks.ToString();
            user.PublicKeyId = Guid.NewGuid();
            user.ContainerIds = new List<Guid>();

            for (int i = 0; i < 10; i++)
            {
                user.ContainerIds.Add(Guid.NewGuid());
            }

            var newUser = _provider.UserCreate(user);

            Assert.IsNotNull(newUser);

            var newUser2 = _provider.UserCreate(user);

            Assert.Fail("Should have seen exception");
        }
Ejemplo n.º 4
0
        public void FullUsageTest()
        {
            IUser user1 = new User();
            user1.Username = "******";

            var createdUser = _provider.UserCreate(user1);

            Assert.IsNotNull(createdUser);

            IUser user2 = new User();
            user2.Username = "******";

            var secondUser = _provider.UserCreate(user2);

            Assert.IsNotNull(secondUser);

            IContainer container = new Container();
            container.Name = "TestContainer";
            container.OwnerUserId = createdUser.Id;

            var createdContainer = _provider.ContainerCreate(container);

            Assert.IsNotNull(createdContainer);

            IBlob blob = new Blob();
            blob.OwnerUserId = createdUser.Id;

            var createdBlob = _provider.BlobCreate(blob);

            Assert.IsNotNull(createdBlob);

            //MemoryStream stream = new MemoryStream();

            //using (StreamWriter sw = new StreamWriter(stream))
            //{
            //    sw.Write("This is a test blob");
            //    sw.Flush();

            //    //var blobUpdateSuccess = _provider.BlobUpdateStream(createdBlob, stream);

            //    //Assert.IsTrue(blobUpdateSuccess);
            //}

            createdContainer.BlobIds = new List<Guid>();
            createdContainer.BlobIds.Add(createdBlob.Id);

            var updatedContainer = _provider.ContainerUpdate(createdContainer);

            Assert.IsTrue(updatedContainer);

            IBlob keyBlob = new Blob();
            keyBlob.OwnerUserId = secondUser.Id;

            var createdKeyBlob = _provider.BlobCreate(keyBlob);

            Assert.IsNotNull(createdKeyBlob);

            //MemoryStream keyStream = new MemoryStream();

            //using (StreamWriter sw = new StreamWriter(keyStream))
            //{
            //    sw.Write("Is this the key?!!!");
            //    sw.Flush();

            //    var blobUpdateSuccess = _provider.BlobUpdateStream(createdKeyBlob, keyStream);

            //    Assert.IsTrue(blobUpdateSuccess);
            //}

            ITransfer transfer = new Transfer();

            transfer.TargetBlobId = createdBlob.Id;
            transfer.KeyId = createdKeyBlob.Id;
            transfer.TargetUserId = secondUser.Id;

            var createdTransfer = _provider.TransferCreate(transfer);

            Assert.IsNotNull(createdTransfer);

            IContainer secondUserContainer = new Container();
            secondUserContainer.Name = "SecondUserContainer";
            secondUserContainer.OwnerUserId = secondUser.Id;

            var secondUserCreatedContainer = _provider.ContainerCreate(secondUserContainer);

            Assert.IsNotNull(secondUserCreatedContainer);

            if (secondUserCreatedContainer.BlobIds == null)
                secondUserCreatedContainer.BlobIds = new List<Guid>();

            var transfers = _provider.TransferRetrieveByList(t => t.TargetUserId == secondUser.Id && !t.Complete);

            Assert.IsNotNull(transfers);

            Assert.IsTrue(transfers.Any());

            foreach (ITransfer trans in transfers)
            {
                if (!trans.KeyId.Equals(Guid.Empty))
                {
                    IKey transKey = _provider.KeyRetrieve(trans.KeyId);

                    Assert.IsNotNull(transKey);

                    //Assert.IsNotNull(transKey.KeyData);
                    //Add to keyring
                }

                secondUserCreatedContainer.BlobIds.Add(trans.TargetBlobId);

                trans.Complete = true;

                var updatedTrans = _provider.TransferUpdate(trans);

                Assert.IsTrue(updatedTrans);
            }

            var updatedSecondUserContainer = _provider.ContainerUpdate(secondUserCreatedContainer);

            Assert.IsTrue(updatedSecondUserContainer);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Registers the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="publicPrivateKey">The public private key.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException">
        /// Could not create user.
        /// or
        /// Could not create public/private keys.
        /// or
        /// Could not create public key.
        /// or
        /// Could not save user.
        /// </exception>
        public ISdkUser Register(string username, string password, out Stream publicPrivateKey)
        {
            LogVerbose("OjibweClient.Register entering.");
            ISdkUser sdkUser = null;
            ISdkKey sdkPublicKey = null;

            try
            {
                bool success = true;
                IUser user = new User();
                user.Username = username;
                user.PasswordHash = password;
                user.Role = "User";

                sdkUser = _api.Execute<SdkUser>(user, HttpMethod.Post, "users");

                success = sdkUser != null;

                if (!success)
                    throw new ApplicationException("Could not create user.");

                sdkUser = Login(username, password);

                Stream publicKeyStream;
                if (!CryptoUtility.TryGenerateNewAsymetricKeys(out publicKeyStream, out publicPrivateKey))
                    throw new ApplicationException("Could not create public/private keys.");

                IKey publicKey = new Key();
                publicKey.OwnerUserId = sdkUser.Id;
                publicKey.Secure = false;
                publicKey.KeyData = StreamUtility.GetBytes(publicKeyStream);

                sdkPublicKey = _api.Execute<SdkKey>(publicKey, HttpMethod.Post, "keys");

                success = sdkPublicKey != null;

                if (!success)
                    throw new ApplicationException("Could not create public key.");

                sdkUser.PublicKey = sdkPublicKey;
                success = sdkUser.Save();

                sdkUser.PrivateKey = publicPrivateKey;

                if (!success)
                    throw new ApplicationException("Could not save user.");

                LogVerbose("OjibweClient.Register leaving.");
                return sdkUser;
            }
            catch (Exception ex)
            {
                LogError("Failed to Register user.", ex);

                //Cleanup
                publicPrivateKey = Stream.Null;
                if (sdkPublicKey != null)
                    _api.Execute<bool>(null, HttpMethod.Delete, "keys/{id}", "id", sdkPublicKey.Id.ToString());
                if (sdkUser != null)
                    _api.Execute<bool>(null, HttpMethod.Delete, "users/{id}", "id", sdkUser.Id.ToString());
                throw;
            }
        }