public void Client_Upload_UseFile_CheckOverwrite()

in DUTRemoteTests/ClientTests.cs [307:369]


        public void Client_Upload_UseFile_CheckOverwrite()
        {
            // determine file paths
            var fileToSend = Path.GetTempPath() + "sampleFileToSend";
            var dirToRecv = Path.Combine(Path.GetTempPath(), "sampleRecvDir");
            var fileToRecv = Path.Combine(dirToRecv, "sampleFileToSend");

            Directory.CreateDirectory(dirToRecv);

            // create 100 MB random data, and write it to our file.
            byte[] data = new byte[100 * 1024 * 1024];
            Random rng = new Random();
            rng.NextBytes(data);
            File.WriteAllBytes(fileToSend, data);

            // perform the upload
            client.Upload(fileToSend, dirToRecv, true);

            Thread.Sleep(500); // give time for writes to disk to complete.
            byte[] receivedData = File.ReadAllBytes(fileToRecv);
            Assert.IsTrue(data.SequenceEqual(receivedData), "Data received and data sent do not match.");

            // redo the upload with new data
            rng.NextBytes(data);
            File.WriteAllBytes(fileToSend, data);

            // upload again
            client.Upload(fileToSend, dirToRecv, true);

            Thread.Sleep(500); // give time for writes to disk to complete.
            receivedData = File.ReadAllBytes(fileToRecv);
            Assert.IsTrue(data.SequenceEqual(receivedData), "Data received (after overwrite) and data sent do not match.");

            // check the this fails if we try to upload again with overwrite off
            // and confirm existing file isn't altered
            var originalData = data;
            byte[] newdata = new byte[10 * 1024 * 1024];
            rng.NextBytes(newdata);
            File.WriteAllBytes(fileToSend, data);

            // attempt upload - this should fail
            try
            {
                client.Upload(fileToSend, dirToRecv, false);
                Assert.Fail("Upload succeeded even when target already existed and overwrite was false.");
            }
            catch (Exception ex)
            {
                // confirm we're not catching the assertion failed
                if (ex is AssertFailedException) throw;

                // otherwsie, we're fine. 
            }

            // confirm that the file on disk was not changed
            Assert.IsTrue(originalData.SequenceEqual(File.ReadAllBytes(fileToRecv)), "Existing file changed (even though overwrite was off).");

            // if we get to this point, delete extra files
            File.Delete(fileToSend);
            File.Delete(fileToRecv);

            Directory.Delete(dirToRecv);
        }