public void replyAcceptMsgData_HaveExistingData() { fileStorage.Setup(x => x.setDeviceName("dev123")).Verifiable(); string sentTxt = null; ctx.SendFunc = (txt) => sentTxt = txt; ProtocolContext evtCtx = null; ctx.OnConnectAccepted += (sr, ev) => { evtCtx = ev.ctx; }; ctx.device_name = "dev"; var util = new Mock <IConnectMsgHandlerUtil>(); util.Setup(x => x.GetClientInfo("id1")).Returns(new Device { device_id = "id1", device_name = "dev", folder_name = "dev123" }); util.Setup(x => x.GetServerId()).Returns("server_id1"); util.Setup(x => x.GetPhotoFolder()).Returns(@"c:\folder1\"); util.Setup(x => x.GetFreeSpace(@"c:\folder1\")).Returns(102410241024L); util.Setup(x => x.GetDeviceSummary("id1")).Returns( new DeviceSummary { photo_count = 100, video_count = 200, audio_count = 300, backup_range = new TimeRange( new DateTime(2012, 1, 2, 0, 0, 0, DateTimeKind.Utc).ToLocalTime(), new DateTime(2012, 1, 3, 0, 0, 0, DateTimeKind.Utc)) }).Verifiable(); var hdl = new ConnectMsgHandler(); hdl.Util = util.Object; hdl.HandleConnectMsg(new TextCommand { action = "connect", device_name = "dev", device_id = "id1", transfer_count = 111 }, ctx); JObject o = JObject.Parse(sentTxt); Assert.AreEqual("accept", o["action"]); Assert.AreEqual("server_id1", o["server_id"]); Assert.AreEqual(@"c:\folder1\", o["backup_folder"]); Assert.AreEqual(102410241024L, o["backup_folder_free_space"]); Assert.AreEqual(100, o["photo_count"]); Assert.AreEqual(200, o["video_count"]); Assert.AreEqual(300, o["audio_count"]); Assert.AreEqual(new DateTime(2012, 1, 2, 0, 0, 0, DateTimeKind.Utc), o["backup_startdate"].Value <DateTime>()); Assert.AreEqual(new DateTime(2012, 1, 3, 0, 0, 0, DateTimeKind.Utc), o["backup_enddate"]); Assert.AreEqual(ctx, evtCtx); Assert.IsTrue(ctx.GetState() is TransmitInitState); fileStorage.VerifyAll(); }
public void recv_file_happy_case() { var saved = new SavedPath { device_folder = "fff", relative_file_path = "rrr" }; temp.Setup(x => x.EndWrite()).Verifiable(); temp.Setup(x => x.Path).Returns("path1").Verifiable(); storage.Setup(x => x.MoveToStorage("path1", ctx.fileCtx)).Returns(saved).Verifiable(); util.Setup(x => x.GetNextSeq()).Returns(112345).Verifiable(); util.Setup(x => x.SaveFileRecord(It.Is <FileAsset>( f => f.device_id == ctx.device_id && f.event_time == ctx.fileCtx.datetime && !f.file_id.Equals(Guid.Empty) && f.file_name == ctx.fileCtx.file_name && f.file_path == Path.Combine(ctx.fileCtx.folder, ctx.fileCtx.file_name) && f.file_size == ctx.fileCtx.file_size && f.type == (int)ctx.fileCtx.type && f.saved_path == saved.relative_file_path && f.parent_folder == Path.GetDirectoryName(saved.relative_file_path) && f.seq == 112345) )).Verifiable(); var state = new TransmitStartedState() { Util = util.Object }; ctx.SetState(state); string sentData = ""; ctx.SendFunc = (x) => { sentData = x; }; bool called = false; ctx.OnFileReceived += (s, e) => { called = true; }; ctx.handleFileEndCmd(new TextCommand { action = "file-end", file_name = "f.jpg" }); Assert.AreEqual(1, ctx.recved_files); Assert.IsTrue(ctx.GetState() is TransmitInitState); util.VerifyAll(); temp.VerifyAll(); storage.VerifyAll(); Assert.IsTrue(called); var o = JsonConvert.DeserializeObject <TextCommand>(sentData); Assert.AreEqual("file-exist", o.action); Assert.AreEqual(ctx.fileCtx.file_name, o.file_name); }
public void testFileStart_has_dup() { fac.Setup(x => x.CreateTempFile()).Returns(tempFile.Object).Verifiable(); var fileUtil = new Mock <IFileUtility>(); fileUtil.Setup(x => x.HasDuplicateFile(It.IsAny <FileContext>(), It.IsAny <string>())).Returns(true); var state = new TransmitInitState(fileUtil.Object); var ctx = new ProtocolContext(fac.Object, storage.Object, state); string sentData = ""; ctx.SendFunc = (x) => { sentData = x; }; var cmd = new TextCommand { action = "file-start", file_name = "name", file_size = 1234, type = "audio", folder = "/sto/pp", datetime = DateTime.Now, total_count = 1000, backuped_count = 333, }; ctx.handleFileStartCmd(cmd); Assert.IsTrue(ctx.GetState() is TransmitInitState); Assert.IsNull(ctx.fileCtx); var o = JsonConvert.DeserializeObject <TextCommand>(sentData); Assert.AreEqual("file-exist", o.action); Assert.AreEqual("name", o.file_name); }
public void testFileStart_no_dup() { fac.Setup(x => x.CreateTempFile()).Returns(tempFile.Object).Verifiable(); var fileUtil = new Mock <IFileUtility>(); var state = new TransmitInitState(fileUtil.Object); var ctx = new ProtocolContext(fac.Object, storage.Object, state); string sentData = ""; ctx.SendFunc = (x) => { sentData = x; }; var cmd = new TextCommand { action = "file-start", file_name = "name", file_size = 1234, type = "audio", folder = "/sto/pp", datetime = DateTime.Now, total_count = 1000, backuped_count = 333, }; ctx.handleFileStartCmd(cmd); Assert.AreEqual(cmd.file_name, ctx.fileCtx.file_name); Assert.AreEqual(cmd.file_size, ctx.fileCtx.file_size); Assert.AreEqual(FileAssetType.audio, ctx.fileCtx.type); Assert.AreEqual(cmd.folder, ctx.fileCtx.folder); Assert.AreEqual(cmd.datetime, ctx.fileCtx.datetime); Assert.AreEqual(cmd.total_count, ctx.total_count); Assert.AreEqual(cmd.backuped_count, ctx.backup_count); Assert.IsTrue(ctx.GetState() is TransmitStartedState); }
public void unconnected_connect_conntected() { var connectMsg = new TextCommand { action = "connect", device_name = "dev", device_id = "guid1" }; var tempFactory = new Mock <ITempFileFactory>(); var fileStorage = new Mock <IFileStorage>(); var connectHandler = new Mock <IConnectMsgHandler>(); var retState = new Mock <AbstractProtocolState>(); var initState = new UnconnectedState(); initState.handler = connectHandler.Object; var ctx = new ProtocolContext(tempFactory.Object, storage.Object, initState); connectHandler.Setup(x => x.HandleConnectMsg(connectMsg, It.IsAny <ProtocolContext>())).Callback(() => { ctx.SetState(retState.Object); }).Verifiable(); ctx.handleConnectCmd(connectMsg); connectHandler.VerifyAll(); Assert.AreEqual(retState.Object, ctx.GetState()); Assert.AreEqual("dev", ctx.device_name); Assert.AreEqual("guid1", ctx.device_id); }
public void testDisapprove() { var state = new WaitForApproveState(); var ctx = new ProtocolContext(null, null, state) { device_id = "dd", device_name = "na" }; string sentData = null; ctx.SendFunc = (t) => { sentData = t; }; CloseStatusCode code = CloseStatusCode.NORMAL; string reason = null; ctx.StopFunc = (code1, reason1) => { code = code1; reason = reason1; }; state.handleDisapprove(ctx); var o = JObject.Parse(sentData); Assert.AreEqual("denied", o["action"]); Assert.AreEqual("user rejected", o["reason"]); Assert.IsTrue(ctx.GetState() is UnconnectedState); Assert.AreEqual(CloseStatusCode.POLICY_VIOLATION, code); Assert.AreEqual("User rejected", reason); }
public void testApprove() { var util = new Mock <IConnectMsgHandlerUtil>(); util.Setup(x => x.GetServerId()).Returns("server_id1").Verifiable(); util.Setup(x => x.GetPhotoFolder()).Returns(@"c:\folder1\").Verifiable(); util.Setup(x => x.GetFreeSpace(It.IsAny <string>())).Returns(123456).Verifiable(); util.Setup(x => x.GetUniqueDeviceFolder("na")).Returns("ggyyUnique_dev_name").Verifiable(); util.Setup(x => x.Save(It.Is <Device>( (d) => d.device_id == "dd" && d.device_name == "na" && d.folder_name == "ggyyUnique_dev_name" ))).Callback <Device>((dev) => { }).Verifiable(); var storage = new Mock <IFileStorage>(); storage.Setup(x => x.setDeviceName("ggyyUnique_dev_name")).Verifiable(); var state = new WaitForApproveState(); var ctx = new ProtocolContext(null, storage.Object, state) { device_id = "dd", device_name = "na" }; string sentData = null; ctx.SendFunc = (t) => { sentData = t; }; state.Util = util.Object; state.handleApprove(ctx); var o = JObject.Parse(sentData); Assert.AreEqual("accept", o["action"]); Assert.AreEqual("server_id1", o["server_id"]); Assert.AreEqual(@"c:\folder1\", o["backup_folder"]); Assert.AreEqual(123456L, o["backup_folder_free_space"]); Assert.IsTrue(ctx.GetState() is TransmitInitState); Assert.AreEqual("ggyyUnique_dev_name", ctx.device_folder_name); storage.VerifyAll(); util.VerifyAll(); }
public void ctx_counts_are_updated_by_update_count_msg() // because update-count is deprecated { var state = new TransmitInitState(); var ctx = new ProtocolContext(fac.Object, storage.Object, state); ctx.total_count = 1000; var cmd = new TextCommand { action = "update-count", transfer_count = 3322, backuped_count = 1000, }; ctx.handleUpdateCountCmd(cmd); Assert.AreEqual(cmd.transfer_count, ctx.total_count); Assert.AreEqual(cmd.backuped_count, ctx.backup_count); Assert.AreEqual(state, ctx.GetState()); }