Exemplo n.º 1
0
        public void BasicFileSerialisation()
        {
            string filePath = Path.GetPathRoot(Directory.GetCurrentDirectory()) + "scanimage" +
                              Path.DirectorySeparatorChar + "test.jpg";

            if (!File.Exists(filePath))
            {
                byte[] data = new byte[100];
                new Random().NextBytes(data);
                byte[] eom = Encoding.ASCII.GetBytes(Constants.EndOfMessage);

                Array.Copy(eom, 0, data, data.Length - eom.Length, eom.Length);

                File.WriteAllBytes(filePath, data);
            }

            Assert.IsTrue(File.Exists(filePath));

            byte[] bytes = ByteHelpers.FileToBytes(filePath);

            Assert.IsTrue(ByteManipulation.SearchEndOfMessage(bytes, bytes.Length));
            Assert.IsTrue(ByteManipulation.SearchEndOfMessageIndex(bytes, bytes.Length) ==
                          bytes.Length - Constants.EndOfMessage.Length - 1);

            File.Delete(filePath);
        }
Exemplo n.º 2
0
        public void FileSerialisationIncorrectPath()
        {
            string fakePath = Path.GetPathRoot(Directory.GetCurrentDirectory()) + "this" + Path.DirectorySeparatorChar +
                              "path" + Path.DirectorySeparatorChar + "does" + Path.DirectorySeparatorChar + "not" +
                              Path.DirectorySeparatorChar + "exist.txt";

            Assert.IsFalse(File.Exists(fakePath));

            try
            {
                ByteHelpers.FileToBytes(fakePath);
                Assert.Fail("An Exception should have been thrown...");
            }
            catch (IOException)
            {
                Assert.Pass("Exception was thrown, yay");
            }
        }
Exemplo n.º 3
0
        public void ReadFile()
        {
            Random rand = new Random();
            int    size = rand.Next(514875, 1048576);

            byte[] fakeData = new byte[size];
            rand.NextBytes(fakeData);

            string path = "C:\\scanimage" + Path.DirectorySeparatorChar + "test.txt";

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (FileStream file = new FileStream(path, FileMode.CreateNew))
            {
                for (int i = 0; i < fakeData.Length; i++)
                {
                    file.WriteByte(fakeData[i]);
                }
            }

            byte[] methodData = ByteHelpers.FileToBytes(path), fileData = new byte[size];
            string name       = "";

            Assert.True(ByteManipulation.SeperateData(out name, methodData, out methodData, Constants.MessageSeperator));
            Array.Copy(methodData, fileData, methodData.Length - Constants.EndOfMessage.Length);

            Assert.True(name == Path.DirectorySeparatorChar + "test.txt");
            Assert.True(fileData.Length == fakeData.Length);

            for (int i = 0; i < fakeData.Length; i++)
            {
                Assert.True(fakeData[i] == methodData[i]);
            }

            File.Delete(path);
        }
Exemplo n.º 4
0
        private void InternalProcess(CameraRequest request)
        {
            Console.WriteLine("Executing request: " + request);
            byte[] messageData;

            switch (request)
            {
            case CameraRequest.Alive:
                messageData = Encoding.ASCII.GetBytes(Constants.SuccessString + Constants.EndOfMessage);
                break;

            case CameraRequest.SendFullResImage:
                string imageLocation = camera.CaptureImage(imageName);

                messageData = ByteHelpers.FileToBytes(imageLocation);
                SendResponse(client, EndOfMessage(messageData));

                if (File.Exists(imageLocation))
                {
                    File.Delete(imageLocation);
                }
                return;

            case CameraRequest.SendTestImage:
                //For testing, send a static image saved on the device
                messageData = ByteHelpers.FileToBytes(Path.DirectorySeparatorChar + "scanimage" + Path.DirectorySeparatorChar + "test.jpg");
                SendResponse(client, EndOfMessage(messageData));
                return;

            case CameraRequest.SetProporties:
                return;

            default:
                messageData = FailedRequest();
                break;
            }

            SendResponse(client, EndOfMessage(messageData));
        }