public void Validate_WhenStreamIsNull_Throws()
        {
            var file      = new StubHttpPostedFile(contentLength: 1024, fileName: "a.cer", inputStream: null);
            var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

            Assert.Equal("The file stream is invalid.", exception.Message);
        }
        public void Validate_WhenContentLengthIsTooSmall_Throws(int contentLength)
        {
            var file      = new StubHttpPostedFile(contentLength, "a.cer", Stream.Null);
            var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

            Assert.Equal("The file length is invalid.", exception.Message);
        }
        public void Validate_WhenFileExtensionIsInvalid_Throws(string fileName)
        {
            var file      = new StubHttpPostedFile(contentLength: 1024, fileName: fileName, inputStream: Stream.Null);
            var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

            Assert.Equal("The file extension must be .cer.", exception.Message);
        }
        public void Validate_WhenStreamIsDerEncodedCertificate_Succeeds(string fileName)
        {
            using (var stream = GetDerEncodedCertificateStream())
            {
                var file = new StubHttpPostedFile((int)stream.Length, fileName, stream);

                new CertificateValidator().Validate(file);
            }
        }
        public void Validate_WhenStreamLengthIsTooLarge_Throws()
        {
            using (var stream = new CustomLengthStream(MaximumSizeInBytes + 1))
            {
                var file      = new StubHttpPostedFile(contentLength: 1024, fileName: "a.cer", inputStream: stream);
                var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

                Assert.Equal($"The file exceeds the size limit of {MaximumSizeInBytes} bytes.", exception.Message);
            }
        }
        public void Validate_WhenStreamLengthIsTooSmall_Throws(long streamLength)
        {
            using (var stream = new CustomLengthStream(streamLength))
            {
                var file      = new StubHttpPostedFile(contentLength: 1024, fileName: "a.cer", inputStream: stream);
                var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

                Assert.Equal("The file length is invalid.", exception.Message);
            }
        }
        public void Validate_WhenStreamIsNotSeekable_Throws()
        {
            using (var stream = new NonSeekableStream())
            {
                var file      = new StubHttpPostedFile(contentLength: 1024, fileName: "a.cer", inputStream: stream);
                var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

                Assert.Equal("The file stream must be seekable.", exception.Message);
            }
        }
        public void Validate_WhenStreamIsPemEncodedCertificate_Throws()
        {
            using (var stream = GetPemEncodedCertificateStream())
            {
                var file      = new StubHttpPostedFile((int)stream.Length, fileName: "a.cer", inputStream: stream);
                var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

                Assert.Equal("The file must be a DER encoded binary X.509 certificate.", exception.Message);
            }
        }
        public void Validate_WhenStreamIsDerEncodingIsMalformedShortFormLength_Throws()
        {
            var bytes = new byte[3];

            bytes[0] = 0x30;  // constructed sequence
            bytes[1] = 0x02;  // short form length

            // The DER encoding says there's 2 bytes of content but the array only has 1 remaining byte.

            using (var stream = new MemoryStream(bytes, writable: false))
            {
                var file      = new StubHttpPostedFile((int)stream.Length, fileName: "a.cer", inputStream: stream);
                var exception = Assert.Throws <UserSafeException>(() => new CertificateValidator().Validate(file));

                Assert.Equal("The file must be a DER encoded binary X.509 certificate.", exception.Message);
            }
        }