Beispiel #1
0
                        public static void _Tests(cTrace.cContext pParentContext)
                        {
                            var lContext = pParentContext.NewMethod(nameof(cResponseBuilder), nameof(_Tests));

                            cResponseBuilder lBuilder;

                            byte[]        lBytes;
                            int           lBufferPosition;
                            cResponse     lResponse;
                            cResponseLine lLine;

                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("fred"), false))
                            {
                                throw new cTestsException("the line should have been fred", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }


                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred{5}\r\nanguschar");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse != null)
                            {
                                throw new cTestsException("no response should have been generated", lContext);
                            }

                            lBytes          = LMakeBuffer("lie\r\nfred\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition == lBytes.Length)
                            {
                                throw new cTestsException("the buffer should not have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 3)
                            {
                                throw new cTestsException("there should have been three lines", lContext);
                            }

                            lLine = lResponse[0];
                            if (!cASCII.Compare(lLine, new cBytes("fred"), false) || lLine.Literal)
                            {
                                throw new cTestsException("the line should have been fred", lContext);
                            }

                            lLine = lResponse[1];
                            if (!cASCII.Compare(lLine, new cBytes("angus"), false) || !lLine.Literal)
                            {
                                throw new cTestsException("the line should have been literal angus", lContext);
                            }

                            lLine = lResponse[2];
                            if (!cASCII.Compare(lLine, new cBytes("charlie"), false) || lLine.Literal)
                            {
                                throw new cTestsException("the line should have been charlie", lContext);
                            }

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("fred"), false))
                            {
                                throw new cTestsException("the line should have been fred", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }


                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("f\ra\n\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("f\ra\n"), false))
                            {
                                throw new cTestsException("the line should have been fran", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }


                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("{}\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("{}"), false))
                            {
                                throw new cTestsException("the line should have been {}", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }



                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred{}\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("fred{}"), false))
                            {
                                throw new cTestsException("the line should have been fred{}", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }


                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred{a}\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("fred{a}"), false))
                            {
                                throw new cTestsException("the line should have been fred{a}", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }


                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred123}\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 1)
                            {
                                throw new cTestsException("there should have been one line", lContext);
                            }
                            if (!cASCII.Compare(lResponse[0], new cBytes("fred123}"), false))
                            {
                                throw new cTestsException("the line should have been fred123}", lContext);
                            }
                            if (lResponse[0].Literal)
                            {
                                throw new cTestsException("the line should not have been a literal", lContext);
                            }



                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred{12345678901}\r\n");
                            lBufferPosition = 0;

                            bool lException = false;

                            try { lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext); }
                            catch { lException = true; }
                            if (!lException)
                            {
                                throw new cTestsException("expected failure", lContext);
                            }



                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred{0}\r\n{00}\r\n\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 3)
                            {
                                throw new cTestsException("there should have been three lines", lContext);
                            }

                            lLine = lResponse[0];
                            if (!cASCII.Compare(lLine, new cBytes("fred"), false) || lLine.Literal)
                            {
                                throw new cTestsException("the line should have been fred", lContext);
                            }

                            lLine = lResponse[1];
                            if (lLine.Count != 0 || !lLine.Literal)
                            {
                                throw new cTestsException("the line should have been empty literal", lContext);
                            }

                            lLine = lResponse[2];
                            if (lLine.Count != 0 || !lLine.Literal)
                            {
                                throw new cTestsException("the line should have been empty literal", lContext);
                            }



                            // test literal8

                            lBuilder        = new cResponseBuilder();
                            lBytes          = LMakeBuffer("fred~{0}\r\n~{00}\r\n\r\n");
                            lBufferPosition = 0;

                            lResponse = lBuilder.BuildFromBuffer(lBytes, ref lBufferPosition, lContext);

                            if (lBufferPosition != lBytes.Length)
                            {
                                throw new cTestsException("the buffer should have been fully read", lContext);
                            }
                            if (lResponse == null)
                            {
                                throw new cTestsException("a response should have been generated", lContext);
                            }
                            if (lResponse.Count != 3)
                            {
                                throw new cTestsException("there should have been three lines", lContext);
                            }

                            lLine = lResponse[0];
                            if (!cASCII.Compare(lLine, new cBytes("fred"), false) || lLine.Literal)
                            {
                                throw new cTestsException("the line should have been fred", lContext);
                            }

                            lLine = lResponse[1];
                            if (lLine.Count != 0 || !lLine.Literal)
                            {
                                throw new cTestsException("the line should have been empty literal", lContext);
                            }

                            lLine = lResponse[2];
                            if (lLine.Count != 0 || !lLine.Literal)
                            {
                                throw new cTestsException("the line should have been empty literal", lContext);
                            }

                            byte[] LMakeBuffer(string pString)
                            {
                                byte[] lResult = new byte[pString.Length];
                                for (int i = 0, j = 0; i < pString.Length; i++, j++)
                                {
                                    lResult[j] = (byte)pString[i];
                                }
                                return(lResult);
                            }
                        }