Beispiel #1
0
        public Node EnableRoomEncryption(EnableRoomEncryptionRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));
            request.DataRoomRescueKeyPassword.MustNotNullOrEmptyOrWhitespace(nameof(request.DataRoomRescueKeyPassword), true);
            request.Id.MustPositive(nameof(request.Id));

            #endregion

            ApiUserKeyPair apiDataRoomRescueKey = null;
            if (request.DataRoomRescueKeyPassword != null)
            {
                try {
                    UserKeyPair cryptoPair = Crypto.Sdk.Crypto.GenerateUserKeyPair(request.DataRoomRescueKeyPassword);
                    apiDataRoomRescueKey = UserMapper.ToApiUserKeyPair(cryptoPair);
                } catch (CryptoException ce) {
                    DracoonClient.Log.Debug(Logtag, $"Generation of user key pair failed with '{ce.Message}'!");
                    throw new DracoonCryptoException(CryptoErrorMapper.ParseCause(ce), ce);
                }
            }

            ApiEnableRoomEncryptionRequest apiEnableRoomEncryptionRequest =
                RoomMapper.ToApiEnableRoomEncryptionRequest(request, apiDataRoomRescueKey);
            IRestRequest restRequest = _client.Builder.PutEnableRoomEncryption(request.Id, apiEnableRoomEncryptionRequest);
            ApiNode      result      = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PutEnableRoomEncryption);
            return(NodeMapper.FromApiNode(result));
        }
Beispiel #2
0
        public void RunSync_UnknownFileSize_Success()
        {
            // ARRANGE
            byte[] fileMock = new byte[1888];
            new Random().NextBytes(fileMock);
            Stream s = new MemoryStream(fileMock);
            IInternalDracoonClient c        = FactoryClients.InternalDracoonClientMock(true);
            FileUploadCallbackMock callback = new FileUploadCallbackMock();
            FileUpload             f        = new FileUpload(c, "id1", FactoryFile.UploadFileRequest, s, -1);

            f.AddFileUploadCallback(callback);
            ApiCreateFileUpload acfu = FactoryFile.ApiCreateFileUpload;

            acfu.Classification = null;
            Mock.Arrange(() => c.Builder.PostCreateFileUpload(Arg.IsAny <ApiCreateFileUpload>())).Returns(FactoryRestSharp.PostCreateFileUploadMock())
            .Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUploadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostUploadToken, 0))
            .Returns(FactoryFile.ApiUploadToken).Occurs(1);
            Mock.Arrange(() => c.Builder.PutCompleteFileUpload(Arg.AnyString, Arg.IsAny <ApiCompleteFileUpload>()))
            .Returns(FactoryRestSharp.PutCompleteFileUploadMock("path")).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiNode>(Arg.IsAny <IRestRequest>(), RequestType.PutCompleteUpload, 0))
            .Returns(FactoryNode.ApiNode).Occurs(1);
            Mock.Arrange(() => c.Builder.GetGeneralSettings())
            .Returns(FactoryRestSharp.RestRequestWithAuth(ApiConfig.ApiGetGeneralConfig, Method.GET)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiGeneralSettings>(Arg.IsAny <IRestRequest>(), RequestType.GetGeneralSettings, 0))
            .Returns(FactoryServerSettings.ApiGeneralSettings).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(acfu).Occurs(1);
            Mock.Arrange(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>())).Returns(FactoryNode.Node).Occurs(1);
            Mock.Arrange(() => FileHash.CompareFileHashes(Arg.AnyString, Arg.IsAny <byte[]>(), Arg.AnyInt)).Returns(true).OccursAtLeast(1);
            DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>();

            Mock.Arrange(() => Mock.Create <UploadProgressChangedEventArgs>().BytesSent).IgnoreInstance();
            Mock.Arrange(() => c.Executor.ExecuteWebClientChunkUpload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), Arg.IsAny <byte[]>(),
                                                                      RequestType.PostUploadChunk, Arg.IsAny <Thread>(), Arg.AnyInt)).DoInstead(() => Thread.Sleep(250))
            .Raises(() => wc.UploadProgressChanged += null, null, Mock.Create <UploadProgressChangedEventArgs>()).Returns(new byte[13]);
            Mock.Arrange(() => c.Builder.ProvideChunkUploadWebClient(Arg.AnyInt, Arg.AnyLong, Arg.AnyString, Arg.AnyString)).Returns(wc);
            Mock.Arrange(() => JsonConvert.DeserializeObject <ApiUploadChunkResult>(Arg.AnyString)).Returns(FactoryFile.ApiUploadChunkResult);

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFinished(Arg.AnyString, Arg.IsAny <Node>())).Occurs(1);

            // ACT
            Node actual = f.RunSync();

            s.Close();

            // ASSERT
            Assert.Equal(FactoryNode.Node, actual, new NodeComparer());
            Mock.Assert(callback);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
        }
Beispiel #3
0
        public void FromApiNode_Null()
        {
            // ARRANGE
            Node    expected = null;
            ApiNode param    = null;

            // ACT
            Node actual = NodeMapper.FromApiNode(param);

            // ASSERT
            Assert.Equal(expected, actual, new NodeComparer());
        }
Beispiel #4
0
        public void RunAsync_IOException()
        {
            // ARRANGE
            byte[] fileMock = new byte[1888];
            new Random().NextBytes(fileMock);
            Stream s = new MemoryStream(fileMock);
            IInternalDracoonClient c        = FactoryClients.InternalDracoonClientMock(true);
            FileUploadCallbackMock callback = new FileUploadCallbackMock();
            FileUpload             f        = new FileUpload(c, "id1", FactoryFile.UploadFileRequest, s, fileMock.Length);

            f.AddFileUploadCallback(callback);
            Mock.Arrange(() => c.Builder.PostCreateFileUpload(Arg.IsAny <ApiCreateFileUpload>())).Returns(FactoryRestSharp.PostCreateFileUploadMock())
            .OnAllThreads();
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUploadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostUploadToken, 0))
            .Returns(FactoryFile.ApiUploadToken).OnAllThreads();
            Mock.Arrange(() => c.Builder.PutCompleteFileUpload(Arg.AnyString, Arg.IsAny <ApiCompleteFileUpload>()))
            .Returns(FactoryRestSharp.PutCompleteFileUploadMock("path")).OnAllThreads();
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiNode>(Arg.IsAny <IRestRequest>(), RequestType.PutCompleteUpload, 0))
            .Returns(FactoryNode.ApiNode).OnAllThreads();
            Mock.Arrange(() => c.Builder.GetGeneralSettings())
            .Returns(FactoryRestSharp.RestRequestWithAuth(ApiConfig.ApiGetGeneralConfig, Method.GET)).OnAllThreads();
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiGeneralSettings>(Arg.IsAny <IRestRequest>(), RequestType.GetGeneralSettings, 0))
            .Returns(FactoryServerSettings.ApiGeneralSettings).OnAllThreads();
            Mock.Arrange(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(FactoryFile.ApiCreateFileUpload)
            .OnAllThreads();
            Mock.Arrange(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>())).Returns(FactoryNode.Node).OnAllThreads();
            Mock.Arrange(() => FileHash.CompareFileHashes(Arg.AnyString, Arg.IsAny <byte[]>(), Arg.AnyInt)).Returns(true).OnAllThreads();
            DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>();

            Mock.Arrange(() => Mock.Create <UploadProgressChangedEventArgs>().BytesSent).IgnoreInstance().OnAllThreads();
            Mock.Arrange(() => c.Executor.ExecuteWebClientChunkUpload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), Arg.IsAny <byte[]>(),
                                                                      RequestType.PostUploadChunk, Arg.IsAny <Thread>(), Arg.AnyInt))
            .Raises(() => wc.UploadProgressChanged += null, null, Mock.Create <UploadProgressChangedEventArgs>()).Returns(new byte[13])
            .OnAllThreads();
            Mock.Arrange(() => c.Builder.ProvideChunkUploadWebClient(Arg.AnyInt, Arg.AnyLong, Arg.AnyString, Arg.AnyString)).Returns(wc)
            .OnAllThreads();
            Mock.Arrange(() => JsonConvert.DeserializeObject <ApiUploadChunkResult>(Arg.AnyString)).Returns(FactoryFile.ApiUploadChunkResult)
            .OnAllThreads();
            Mock.Arrange(() => s.Read(Arg.IsAny <byte[]>(), Arg.AnyInt, Arg.AnyInt)).Throws(new IOException()).OnAllThreads();
            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnFailed(Arg.AnyString, Arg.IsAny <DracoonException>())).Occurs(1);

            // ACT
            f.RunAsync();
            while (f.RunningThread.IsAlive)
            {
            }

            s.Close();

            // ASSERT
            Mock.Assert(callback);
        }
Beispiel #5
0
        public Node GetNode(long nodeId)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            nodeId.MustPositive(nameof(nodeId));

            #endregion

            IRestRequest restRequest = _client.Builder.GetNode(nodeId);
            ApiNode      result      = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.GetNode);
            return(NodeMapper.FromApiNode(result));
        }
Beispiel #6
0
        public Node UpdateFile(UpdateFileRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));
            request.Id.MustPositive(nameof(request.Id));
            request.Name.MustNotNullOrEmptyOrWhitespace(nameof(request.Name), true);

            #endregion

            ApiUpdateFileRequest apiUpdateFileRequest = FileMapper.ToApiUpdateFileRequest(request);
            IRestRequest         restRequest          = _client.Builder.PutFile(request.Id, apiUpdateFileRequest);
            ApiNode result = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PutFile);
            return(NodeMapper.FromApiNode(result));
        }
Beispiel #7
0
        public Node CreateFolder(CreateFolderRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));
            request.ParentId.MustPositive(nameof(request.ParentId));
            request.Name.MustNotNullOrEmptyOrWhitespace(nameof(request.Name));

            #endregion

            ApiCreateFolderRequest apiCreateFolderRequest = FolderMapper.ToApiCreateFolderRequest(request);
            IRestRequest           restRequest            = _client.Builder.PostFolder(apiCreateFolderRequest);
            ApiNode result = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PostFolder);
            return(NodeMapper.FromApiNode(result));
        }
Beispiel #8
0
        public Node MoveNodes(MoveNodesRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));
            request.TargetNodeId.MustPositive(nameof(request.TargetNodeId));
            request.NodesToBeMoved.EnumerableMustNotNullOrEmpty(nameof(request.NodesToBeMoved));
            request.NodesToBeMoved.ForEach(current => current.NodeId.MustPositive(nameof(current.NodeId)));

            #endregion

            ApiMoveNodesRequest apiMoveNodesRequest = NodeMapper.ToApiMoveNodesRequest(request);
            IRestRequest        restRequest         = _client.Builder.PostMoveNodes(request.TargetNodeId, apiMoveNodesRequest);
            ApiNode             result = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PostMoveNodes);
            return(NodeMapper.FromApiNode(result));
        }
        protected override Node StartUpload()
        {
            NotifyStarted(ActionId);
            ApiCreateFileUpload apiFileUploadRequest = FileMapper.ToApiCreateFileUpload(FileUploadRequest);

            try {
                apiFileUploadRequest.UseS3 = CheckUseS3();
            } catch (DracoonApiException apiException) {
                DracoonClient.Log.Warn(LogTag, "S3 direct upload is not possible.", apiException);
            }

            IRestRequest uploadTokenRequest = Client.Builder.PostCreateFileUpload(apiFileUploadRequest);

            UploadToken = Client.Executor.DoSyncApiCall <ApiUploadToken>(uploadTokenRequest, RequestType.PostUploadToken);

            Node                  publicResultNode;
            PlainFileKey          plainFileKey          = CreateFileKey();
            ApiCompleteFileUpload apiCompleteFileUpload = FileMapper.ToApiCompleteFileUpload(FileUploadRequest);

            if (apiFileUploadRequest.UseS3.HasValue && apiFileUploadRequest.UseS3.Value)
            {
                List <ApiS3FileUploadPart> s3Parts          = EncryptedS3Upload(ref plainFileKey);
                EncryptedFileKey           encryptedFileKey = EncryptFileKey(plainFileKey);
                apiCompleteFileUpload.FileKey = FileMapper.ToApiFileKey(encryptedFileKey);
                apiCompleteFileUpload.Parts   = s3Parts;
                IRestRequest completeFileUploadRequest = Client.Builder.PutCompleteS3FileUpload(UploadToken.UploadId, apiCompleteFileUpload);
                Client.Executor.DoSyncApiCall <VoidResponse>(completeFileUploadRequest, RequestType.PutCompleteS3Upload);
                publicResultNode = NodeMapper.FromApiNode(S3Finished());
            }
            else
            {
                EncryptedUpload(ref plainFileKey);
                EncryptedFileKey encryptedFileKey = EncryptFileKey(plainFileKey);
                apiCompleteFileUpload.FileKey = FileMapper.ToApiFileKey(encryptedFileKey);
                IRestRequest completeFileUploadRequest =
                    Client.Builder.PutCompleteFileUpload(new Uri(UploadToken.UploadUrl).PathAndQuery, apiCompleteFileUpload);
                ApiNode resultNode = Client.Executor.DoSyncApiCall <ApiNode>(completeFileUploadRequest, RequestType.PutCompleteUpload);
                publicResultNode = NodeMapper.FromApiNode(resultNode);
            }

            NotifyFinished(ActionId, publicResultNode);
            return(publicResultNode);
        }
Beispiel #10
0
        public Node CreateRoom(CreateRoomRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));
            request.ParentId.MustPositive(nameof(request.ParentId));
            request.Name.MustNotNullOrEmptyOrWhitespace(nameof(request.Name));
            request.Quota.NullableMustNotNegative(nameof(request.Quota));
            request.RecycleBinRetentionPeriod.NullableMustNotNegative(nameof(request.RecycleBinRetentionPeriod));
            if (request.AdminUserIds.CheckEnumerableNullOrEmpty() && request.AdminGroupIds.CheckEnumerableNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(request.AdminUserIds) + " | " + nameof(request.AdminGroupIds),
                                                "Room must have an admin user or admin group.");
            }

            if (request.AdminUserIds != null)
            {
                request.AdminUserIds.EnumerableMustNotNullOrEmpty(nameof(request.AdminUserIds));
                request.AdminUserIds.ForEach(id => id.MustPositive(nameof(request.AdminUserIds) + " element"));
            }

            if (request.AdminGroupIds != null)
            {
                request.AdminGroupIds.EnumerableMustNotNullOrEmpty(nameof(request.AdminGroupIds));
                request.AdminGroupIds.ForEach(id => id.MustPositive(nameof(request.AdminGroupIds) + " element"));
            }

            #endregion

            ApiCreateRoomRequest apiCreateRoomRequest = RoomMapper.ToApiCreateRoomRequest(request);
            IRestRequest         restRequest          = _client.Builder.PostRoom(apiCreateRoomRequest);
            ApiNode result = _client.Executor.DoSyncApiCall <ApiNode>(restRequest, RequestType.PostRoom);
            return(NodeMapper.FromApiNode(result));
        }
Beispiel #11
0
        public void FromApiNodeList()
        {
            // ARRANGE
            Classification expectedClassification = Classification.Confidential;
            NodeType       expectedType           = NodeType.File;
            string         expectedTypeValue      = "file";

            NodeList expected = FactoryNode.NodeList;

            ApiNodeList param = new ApiNodeList {
                Range = new ApiRange {
                    Offset = expected.Offset,
                    Limit  = expected.Limit,
                    Total  = expected.Total
                },
                Items = new List <ApiNode>(expected.Items.Count)
            };

            foreach (Node current in expected.Items)
            {
                current.Type           = expectedType;
                current.Classification = expectedClassification;
                ApiNode currentApi = new ApiNode {
                    Id             = current.Id,
                    Type           = expectedTypeValue,
                    ParentId       = current.ParentId,
                    ParentPath     = current.ParentPath,
                    Name           = current.Name,
                    MediaType      = current.MediaType,
                    MediaToken     = current.MediaToken,
                    Size           = current.Size,
                    Quota          = current.Quota,
                    Classification = (int)current.Classification,
                    Notes          = current.Notes,
                    Hash           = current.Hash,
                    ExpireAt       = current.ExpireAt,
                    CreatedAt      = current.CreatedAt,
                    CreatedBy      = new ApiUserInfo {
                        Id          = current.CreatedBy.Id.Value,
                        AvatarUuid  = current.CreatedBy.AvatarUUID,
                        DisplayName = current.CreatedBy.DisplayName
                    },
                    UpdatedAt = current.UpdatedAt,
                    UpdatedBy = new ApiUserInfo {
                        Id          = current.UpdatedBy.Id.Value,
                        AvatarUuid  = current.UpdatedBy.AvatarUUID,
                        DisplayName = current.UpdatedBy.DisplayName
                    },
                    InheritPermissions = current.HasInheritPermissions,
                    Permissions        = new ApiNodePermissions {
                        Manage = current.Permissions.Manage,
                        Read   = current.Permissions.Read,
                        Create = current.Permissions.Create,
                        Change = current.Permissions.Change,
                        Delete = current.Permissions.Delete,
                        ManageDownloadShare = current.Permissions.ManageDownloadShare,
                        ManageUploadShare   = current.Permissions.ManageUploadShare,
                        ReadRecycleBin      = current.Permissions.CanReadRecycleBin,
                        RestoreRecycleBin   = current.Permissions.CanRestoreRecycleBin,
                        DeleteRecycleBin    = current.Permissions.CanDeleteRecycleBin
                    },
                    IsFavorite                = current.IsFavorite,
                    IsEncrypted               = current.IsEncrypted,
                    CountChildren             = current.CountChildren,
                    CountFiles                = current.CountFiles,
                    CountRooms                = current.CountRooms,
                    CountFolders              = current.CountFolders,
                    CountDeletedVersions      = current.CountDeletedVersions,
                    RecycleBinRetentionPeriod = current.RecycleBinRetentionPeriod,
                    CountDownloadShares       = current.CountDownloadShares,
                    CountUploadShares         = current.CountUploadShares,
                    BranchVersion             = current.BranchVersion,
                    FileType = current.Extension
                };
                param.Items.Add(currentApi);
                Mock.Arrange(() => NodeMapper.FromApiNode(currentApi)).Returns(current);
            }

            // ACT
            NodeList actual = NodeMapper.FromApiNodeList(param);

            // ASSERT
            Assert.Equal(expected, actual, new NodeListComparer());
        }
Beispiel #12
0
        public void FromApiNode()
        {
            // ARRANGE
            Classification expectedClassification = Classification.Confidential;
            NodeType       expectedType           = NodeType.File;
            string         expectedTypeValue      = "file";

            Node expected = FactoryNode.Node;

            expected.Type           = expectedType;
            expected.Classification = expectedClassification;

            ApiNode param = new ApiNode {
                Id             = expected.Id,
                Type           = expectedTypeValue,
                ParentId       = expected.ParentId,
                ParentPath     = expected.ParentPath,
                Name           = expected.Name,
                MediaType      = expected.MediaType,
                MediaToken     = expected.MediaToken,
                Size           = expected.Size,
                Quota          = expected.Quota,
                Classification = (int)expected.Classification,
                Notes          = expected.Notes,
                Hash           = expected.Hash,
                ExpireAt       = expected.ExpireAt,
                CreatedAt      = expected.CreatedAt,
                CreatedBy      = new ApiUserInfo {
                    Id          = expected.CreatedBy.Id.Value,
                    AvatarUuid  = expected.CreatedBy.AvatarUUID,
                    DisplayName = expected.CreatedBy.DisplayName
                },
                UpdatedAt = expected.UpdatedAt,
                UpdatedBy = new ApiUserInfo {
                    Id          = expected.UpdatedBy.Id.Value,
                    AvatarUuid  = expected.UpdatedBy.AvatarUUID,
                    DisplayName = expected.UpdatedBy.DisplayName
                },
                InheritPermissions = expected.HasInheritPermissions,
                Permissions        = new ApiNodePermissions {
                    Manage = expected.Permissions.Manage,
                    Read   = expected.Permissions.Read,
                    Create = expected.Permissions.Create,
                    Change = expected.Permissions.Change,
                    Delete = expected.Permissions.Delete,
                    ManageDownloadShare = expected.Permissions.ManageDownloadShare,
                    ManageUploadShare   = expected.Permissions.ManageUploadShare,
                    ReadRecycleBin      = expected.Permissions.CanReadRecycleBin,
                    RestoreRecycleBin   = expected.Permissions.CanRestoreRecycleBin,
                    DeleteRecycleBin    = expected.Permissions.CanDeleteRecycleBin
                },
                IsFavorite                = expected.IsFavorite,
                IsEncrypted               = expected.IsEncrypted,
                CountChildren             = expected.CountChildren,
                CountFiles                = expected.CountFiles,
                CountRooms                = expected.CountRooms,
                CountFolders              = expected.CountFolders,
                CountDeletedVersions      = expected.CountDeletedVersions,
                RecycleBinRetentionPeriod = expected.RecycleBinRetentionPeriod,
                CountDownloadShares       = expected.CountDownloadShares,
                CountUploadShares         = expected.CountUploadShares,
                BranchVersion             = expected.BranchVersion,
                FileType = expected.Extension
            };

            Mock.Arrange(() => EnumConverter.ConvertValueToNodeTypeEnum(expectedTypeValue)).Returns(expectedType);
            Mock.Arrange(() => EnumConverter.ConvertValueToClassificationEnum((int)expectedClassification)).Returns(expectedClassification);
            Mock.Arrange(() => UserMapper.FromApiUserInfo(param.CreatedBy)).Returns(expected.CreatedBy);
            Mock.Arrange(() => UserMapper.FromApiUserInfo(param.UpdatedBy)).Returns(expected.UpdatedBy);

            // ACT
            Node actual = NodeMapper.FromApiNode(param);

            // ASSERT
            Assert.Equal(expected, actual, new NodeComparer());
        }
Beispiel #13
0
        public void Encrypted_RunSync_S3_KnownFileSize_Success()
        {
            // ARRANGE
            byte[] fileMock = new byte[1564];
            new Random().NextBytes(fileMock);
            Stream s = new MemoryStream(fileMock);
            IInternalDracoonClient c        = FactoryClients.InternalDracoonClientMock(true);
            FileUploadCallbackMock callback = new FileUploadCallbackMock();
            EncFileUpload          f        = new EncFileUpload(c, "id1", FactoryFile.UploadFileRequest, s, FactoryUser.UserPublicKey, fileMock.Length);

            f.AddFileUploadCallback(callback);
            ApiGeneralSettings generalSettings = FactoryServerSettings.ApiGeneralSettings;

            generalSettings.UseS3Storage = true;
            ApiCreateFileUpload acfu = FactoryFile.ApiCreateFileUpload;

            acfu.Classification = null;
            Mock.Arrange(() => c.Builder.PostCreateFileUpload(Arg.IsAny <ApiCreateFileUpload>())).Returns(FactoryRestSharp.PostCreateFileUploadMock())
            .Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUploadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostUploadToken, 0))
            .Returns(FactoryFile.ApiUploadToken).Occurs(1);
            Mock.Arrange(() => c.Builder.PutCompleteS3FileUpload(Arg.AnyString, Arg.IsAny <ApiCompleteFileUpload>()))
            .Returns(FactoryRestSharp.PutCompleteFileUploadMock("path")).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <VoidResponse>(Arg.IsAny <IRestRequest>(), RequestType.PutCompleteS3Upload, 0)).DoNothing()
            .Occurs(1);
            Mock.Arrange(() => c.Builder.GetGeneralSettings())
            .Returns(FactoryRestSharp.RestRequestWithAuth(ApiConfig.ApiGetGeneralConfig, Method.GET)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiGeneralSettings>(Arg.IsAny <IRestRequest>(), RequestType.GetGeneralSettings, 0))
            .Returns(generalSettings).Occurs(1);
            Mock.Arrange(() => c.Builder.PostGetS3Urls(Arg.AnyString, Arg.IsAny <ApiGetS3Urls>())).Returns(FactoryRestSharp.PostGetS3UrlsMock())
            .OccursAtLeast(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiS3Urls>(Arg.IsAny <IRestRequest>(), RequestType.PostGetS3Urls, 0))
            .Returns(FactoryFile.ApiS3Urls).OccursAtLeast(1);
            Mock.Arrange(() => c.Builder.GetS3Status(Arg.AnyString)).Returns(FactoryRestSharp.GetS3StatusMock).OccursAtLeast(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiS3Status>(Arg.IsAny <IRestRequest>(), RequestType.GetS3Status, 0))
            .Returns(FactoryFile.ApiS3Status).OccursAtLeast(1);
            Mock.Arrange(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(acfu).Occurs(1);
            Mock.Arrange(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>())).Returns(FactoryNode.Node).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiCompleteFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(FactoryFile.ApiCompleteFileUpload);
            DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>();

            Mock.Arrange(() => Mock.Create <UploadProgressChangedEventArgs>().BytesSent).IgnoreInstance();
            Mock.Arrange(() => c.Executor.ExecuteWebClientChunkUpload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), Arg.IsAny <byte[]>(),
                                                                      RequestType.PutUploadS3Chunk, Arg.IsAny <Thread>(), Arg.AnyInt)).DoInstead(() => Thread.Sleep(250))
            .Raises(() => wc.UploadProgressChanged += null, null, Mock.Create <UploadProgressChangedEventArgs>()).Returns(new byte[13]);
            Mock.Arrange(() => c.Builder.ProvideS3ChunkUploadWebClient()).Returns(wc);
            Mock.Arrange(() => JsonConvert.DeserializeObject <ApiUploadChunkResult>(Arg.AnyString)).Returns(FactoryFile.ApiUploadChunkResult);

            Mock.Arrange(() => Crypto.Sdk.Crypto.GenerateFileKey(Arg.AnyString)).Returns(FactoryFile.PlainFileKey).Occurs(1);
            FileEncryptionCipher cipher = Mock.Create <FileEncryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileEncryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher).Occurs(1);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <PlainDataContainer>())).Returns(new EncryptedDataContainer(fileMock, fileMock))
            .OccursAtLeast(1);
            Mock.Arrange(() => cipher.DoFinal()).Returns(new EncryptedDataContainer(fileMock, fileMock)).OccursAtLeast(1);
            Mock.Arrange(() => Crypto.Sdk.Crypto.EncryptFileKey(Arg.IsAny <PlainFileKey>(), Arg.IsAny <UserPublicKey>()))
            .Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiFileKey(Arg.IsAny <EncryptedFileKey>())).Returns(FactoryFile.ApiFileKey);

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFinished(Arg.AnyString, Arg.IsAny <Node>())).Occurs(1);

            // ACT
            Node actual = f.RunSync();

            s.Close();

            // ASSERT
            Assert.Equal(FactoryNode.Node, actual, new NodeComparer());
            Mock.Assert(callback);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
        }
Beispiel #14
0
        public void RunSync_S3_PollingError()
        {
            // ARRANGE
            ApiS3Status resultStatus = FactoryFile.ApiS3Status;

            resultStatus.Node      = null;
            resultStatus.Status    = "error";
            resultStatus.ErrorInfo = null;
            byte[] fileMock = new byte[1888];
            new Random().NextBytes(fileMock);
            Stream s = new MemoryStream(fileMock);
            IInternalDracoonClient c        = FactoryClients.InternalDracoonClientMock(true);
            FileUploadCallbackMock callback = new FileUploadCallbackMock();
            FileUpload             f        = new FileUpload(c, "id1", FactoryFile.UploadFileRequest, s, fileMock.Length);

            f.AddFileUploadCallback(callback);
            ApiGeneralSettings generalSettings = FactoryServerSettings.ApiGeneralSettings;

            generalSettings.UseS3Storage = true;
            ApiCreateFileUpload acfu = FactoryFile.ApiCreateFileUpload;

            acfu.Classification = null;
            Mock.Arrange(() => c.Builder.PostCreateFileUpload(Arg.IsAny <ApiCreateFileUpload>())).Returns(FactoryRestSharp.PostCreateFileUploadMock())
            .Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUploadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostUploadToken, 0))
            .Returns(FactoryFile.ApiUploadToken).Occurs(1);
            Mock.Arrange(() => c.Builder.PutCompleteS3FileUpload(Arg.AnyString, Arg.IsAny <ApiCompleteFileUpload>()))
            .Returns(FactoryRestSharp.PutCompleteFileUploadMock("path")).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <VoidResponse>(Arg.IsAny <IRestRequest>(), RequestType.PutCompleteS3Upload, 0)).DoNothing()
            .Occurs(1);
            Mock.Arrange(() => c.Builder.GetGeneralSettings())
            .Returns(FactoryRestSharp.RestRequestWithAuth(ApiConfig.ApiGetGeneralConfig, Method.GET)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiGeneralSettings>(Arg.IsAny <IRestRequest>(), RequestType.GetGeneralSettings, 0))
            .Returns(generalSettings).Occurs(1);
            Mock.Arrange(() => c.Builder.PostGetS3Urls(Arg.AnyString, Arg.IsAny <ApiGetS3Urls>())).Returns(FactoryRestSharp.PostGetS3UrlsMock())
            .OccursAtLeast(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiS3Urls>(Arg.IsAny <IRestRequest>(), RequestType.PostGetS3Urls, 0))
            .Returns(FactoryFile.ApiS3Urls).OccursAtLeast(1);
            Mock.Arrange(() => c.Builder.GetS3Status(Arg.AnyString)).Returns(FactoryRestSharp.GetS3StatusMock).OccursAtLeast(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiS3Status>(Arg.IsAny <IRestRequest>(), RequestType.GetS3Status, 0)).Returns(resultStatus)
            .OccursAtLeast(1);
            Mock.Arrange(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(acfu).Occurs(1);
            Mock.Arrange(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>())).Returns(FactoryNode.Node).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiCompleteFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(FactoryFile.ApiCompleteFileUpload);
            DracoonWebClientExtension wc = Mock.Create <DracoonWebClientExtension>();

            Mock.Arrange(() => Mock.Create <UploadProgressChangedEventArgs>().BytesSent).IgnoreInstance();
            Mock.Arrange(() => c.Executor.ExecuteWebClientChunkUpload(Arg.IsAny <WebClient>(), Arg.IsAny <Uri>(), Arg.IsAny <byte[]>(),
                                                                      RequestType.PutUploadS3Chunk, Arg.IsAny <Thread>(), Arg.AnyInt)).DoInstead(() => Thread.Sleep(250))
            .Raises(() => wc.UploadProgressChanged += null, null, Mock.Create <UploadProgressChangedEventArgs>()).Returns(new byte[13]);
            Mock.Arrange(() => c.Builder.ProvideS3ChunkUploadWebClient()).Returns(wc);
            Mock.Arrange(() => JsonConvert.DeserializeObject <ApiUploadChunkResult>(Arg.AnyString)).Returns(FactoryFile.ApiUploadChunkResult);
            Mock.Arrange(() => DracoonErrorParser.ParseError(Arg.IsAny <ApiErrorResponse>(), RequestType.GetS3Status)).DoNothing();

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFailed(Arg.AnyString, Arg.IsAny <DracoonException>())).Occurs(1);

            // ACT
            Assert.Throws <DracoonApiException>(() => f.RunSync());
            s.Close();

            // ASSERT
            Mock.Assert(callback);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
        }
Beispiel #15
0
        public void Encrypted_RunSync_KnownFileSize_Success()
        {
            // ARRANGE
            byte[] fileMock = new byte[1888];
            new Random().NextBytes(fileMock);
            Stream s = new MemoryStream(fileMock);
            IInternalDracoonClient c        = FactoryClients.InternalDracoonClientMock(true);
            FileUploadCallbackMock callback = new FileUploadCallbackMock();
            ApiCreateFileUpload    acfu     = FactoryFile.ApiCreateFileUpload;

            acfu.Classification = null;
            EncFileUpload f = new EncFileUpload(c, "id1", FactoryFile.UploadFileRequest, s, FactoryUser.UserPublicKey, fileMock.Length);

            f.AddFileUploadCallback(callback);
            Mock.Arrange(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(acfu).Occurs(1);
            Mock.Arrange(() => c.Builder.PostCreateFileUpload(Arg.IsAny <ApiCreateFileUpload>())).Returns(FactoryRestSharp.PostCreateFileUploadMock())
            .Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiUploadToken>(Arg.IsAny <IRestRequest>(), RequestType.PostUploadToken, 0))
            .Returns(FactoryFile.ApiUploadToken).Occurs(1);
            Mock.Arrange(() => c.Builder.GetGeneralSettings())
            .Returns(FactoryRestSharp.RestRequestWithAuth(ApiConfig.ApiGetGeneralConfig, Method.GET)).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiGeneralSettings>(Arg.IsAny <IRestRequest>(), RequestType.GetGeneralSettings, 0))
            .Returns(FactoryServerSettings.ApiGeneralSettings).Occurs(1);
            Mock.Arrange(() => Crypto.Sdk.Crypto.GenerateFileKey(Arg.AnyString)).Returns(FactoryFile.PlainFileKey).Occurs(1);
            FileEncryptionCipher cipher = Mock.Create <FileEncryptionCipher>();

            Mock.Arrange(() => Crypto.Sdk.Crypto.CreateFileEncryptionCipher(Arg.IsAny <PlainFileKey>())).Returns(cipher).Occurs(1);
            Mock.Arrange(() => cipher.ProcessBytes(Arg.IsAny <PlainDataContainer>())).Returns(new EncryptedDataContainer(fileMock, fileMock))
            .OccursAtLeast(1);
            Mock.Arrange(() => cipher.DoFinal()).Returns(new EncryptedDataContainer(fileMock, fileMock)).OccursAtLeast(1);
            Mock.NonPublic.Arrange <ApiUploadChunkResult>(f, "UploadChunkWebClient", ArgExpr.IsAny <Uri>(), ArgExpr.IsAny <byte[]>(),
                                                          ArgExpr.IsAny <long>(), ArgExpr.IsAny <int>()).Returns(FactoryFile.ApiUploadChunkResult).OccursAtLeast(1);
            Mock.Arrange(() => FileHash.CompareFileHashes(Arg.AnyString, Arg.IsAny <byte[]>(), Arg.AnyInt)).Returns(true).OccursAtLeast(1);
            Mock.Arrange(() => Crypto.Sdk.Crypto.EncryptFileKey(Arg.IsAny <PlainFileKey>(), Arg.IsAny <UserPublicKey>()))
            .Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiCompleteFileUpload(Arg.IsAny <FileUploadRequest>())).Returns(FactoryFile.ApiCompleteFileUpload)
            .Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiFileKey(Arg.IsAny <EncryptedFileKey>())).Returns(FactoryFile.ApiFileKey);
            Mock.Arrange(() => c.Builder.PutCompleteFileUpload(Arg.AnyString, Arg.IsAny <ApiCompleteFileUpload>()))
            .Returns(FactoryRestSharp.PutCompleteFileUploadMock("path")).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiNode>(Arg.IsAny <IRestRequest>(), RequestType.PutCompleteUpload, 0))
            .Returns(FactoryNode.ApiNode).Occurs(1);
            Mock.Arrange(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>())).Returns(FactoryNode.Node).Occurs(1);

            Mock.Arrange(() => callback.OnStarted(Arg.AnyString)).Occurs(1);
            Mock.Arrange(() => callback.OnRunning(Arg.AnyString, Arg.AnyLong, Arg.AnyLong)).OccursAtLeast(1);
            Mock.Arrange(() => callback.OnFinished(Arg.AnyString, Arg.IsAny <Node>())).Occurs(1);

            // ACT
            Node actual = f.RunSync();

            s.Close();

            // ASSERT
            Assert.Equal(FactoryNode.Node, actual, new NodeComparer());
            Mock.Assert(() => FileMapper.ToApiCreateFileUpload(Arg.IsAny <FileUploadRequest>()));
            Mock.Assert(() => Crypto.Sdk.Crypto.GenerateFileKey(Arg.AnyString));
            Mock.Assert(() => Crypto.Sdk.Crypto.CreateFileEncryptionCipher(Arg.IsAny <PlainFileKey>()));
            Mock.Assert(() => FileHash.CompareFileHashes(Arg.AnyString, Arg.IsAny <byte[]>(), Arg.AnyInt));
            Mock.Assert(() => Crypto.Sdk.Crypto.EncryptFileKey(Arg.IsAny <PlainFileKey>(), Arg.IsAny <UserPublicKey>()));
            Mock.Assert(() => FileMapper.ToApiCompleteFileUpload(Arg.IsAny <FileUploadRequest>()));
            Mock.Assert(() => FileMapper.ToApiFileKey(Arg.IsAny <EncryptedFileKey>()));
            Mock.Assert(() => NodeMapper.FromApiNode(Arg.IsAny <ApiNode>()));
            Mock.Assert(callback);
            Mock.Assert(cipher);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
            Mock.Assert(c.NodesImpl);
        }