void WriteEmail(MojRefCountedPtr<AsyncEmailWriter> writer, std::string& output) { // Write to a counter output stream MojRefCountedPtr<CounterOutputStream> counter( new CounterOutputStream() ); writer->SetOutputStream(counter); MojRefCountedPtr<AsyncEmailWriterResult> writeResult(new AsyncEmailWriterResult()); writer->WriteEmail(writeResult->GetSlot()); // Make sure it's done writing ASSERT_TRUE( writeResult->Done() ); // Check if any exceptions were reported ASSERT_TRUE( !writeResult->GetException() ); // Now write it again to a buffer writeResult.reset(new AsyncEmailWriterResult()); MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); writer->SetOutputStream(bbos); writer->WriteEmail(writeResult->GetSlot()); // Make sure it's done writing ASSERT_TRUE( writeResult->Done() ); // Check if any exceptions were reported ASSERT_TRUE( !writeResult->GetException() ); // Read generated email into output string ReadBuffer(output, bbos.get()); // Make sure the count matches the actual bytes written ASSERT_EQ(counter->GetBytesWritten(), output.size()); }
TEST(AsyncEmailWriterTest, TestMissingFile) { Email email; InitEmail(email); fprintf(stderr, "Testing missing part file\n"); MojRefCountedPtr<AsyncEmailWriter> writer( new AsyncEmailWriter(email) ); // Create a body part EmailPartList partList; partList.push_back( CreateBodyPart("does-not-exist") ); writer->SetPartList(partList); MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); writer->SetOutputStream(bbos); boost::shared_ptr<MockAsyncIOChannelFactory> ioFactory( new MockAsyncIOChannelFactory() ); writer->SetAsyncIOChannelFactory(ioFactory); MojRefCountedPtr<AsyncEmailWriterResult> writeResult(new AsyncEmailWriterResult()); writer->WriteEmail(writeResult->GetSlot()); // Make sure it's done writing ASSERT_TRUE( writeResult->Done() ); // Should have reported an exception ASSERT_TRUE( writeResult->GetException() ); }
/* Compare an input string filtered through the QP encoder to the expected output. We iterate over all possible split points, testing interruption of the write between all input characters, with zero-to-three one-character runs at the split, so we should single-step through all states. We then feed it back through the decoder to make sure it survives a round-trip. */ void MATCH(const char * in, const char * expect) { int BUF_SIZE = 4096; char inbuf[BUF_SIZE]; char xform[BUF_SIZE]; char decoded[BUF_SIZE]; int nread = 0; memset(inbuf, '\0', BUF_SIZE); memset(xform, '\0', BUF_SIZE); memset(decoded, '\0', BUF_SIZE); // printf("Testing encoding of {%s}\n", in); for (unsigned int junction = 0; junction < 4 && junction < strlen(in); junction++) { for (unsigned int split = 0; split < strlen(in) - junction; split++) { MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); MojRefCountedPtr<QuotedPrintableEncoderOutputStream> qpdos( new QuotedPrintableEncoderOutputStream(bbos) ) ; if (split > 0) qpdos->Write(in, split); if (junction > 0) qpdos->Write(in+split, 1); if (junction > 1) qpdos->Write(in+split+1, 1); if (junction > 2) qpdos->Write(in+split+2, 1); if (junction > 3) qpdos->Write(in+split+3, 1); if (split < strlen(in)-junction) qpdos->Write(in + split + junction, strlen(in) - split - junction); qpdos->Flush(); nread = bbos->ReadFromBuffer(xform, BUF_SIZE); ASSERT_EQ((int)strlen(expect), (int)nread); xform[nread] = '\0'; ASSERT_STREQ(expect, std::string(xform, nread).c_str()); } } // printf("Got {%s}\n", xform); // Take the last encoding, and try to decode it. MojRefCountedPtr<ByteBufferOutputStream> bbos2( new ByteBufferOutputStream() ); MojRefCountedPtr<QuotePrintableDecoderOutputStream> qpeos( new QuotePrintableDecoderOutputStream(bbos2) ) ; qpeos->Write(xform, nread); qpeos->Flush(); nread = bbos2->ReadFromBuffer(decoded, BUF_SIZE); ASSERT_EQ((int)strlen(in), (int)nread); ASSERT_STREQ(in, std::string(decoded, nread).c_str()); }
TEST(QuotePrintableDecoderOutputStreamTest, TestQuotePrintableDecoder) { int BUF_SIZE = 100; char buf[BUF_SIZE]; int nread; MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); QuotePrintableDecoderOutputStream qpdos(bbos); qpdos.Write("If you believe that truth=3Dbeauty, then surely=20=\r\nmathematics is the most beautiful branch of philosophy.", 108); nread = bbos->ReadFromBuffer(buf, 1024); ASSERT_EQ(101, (int) nread); ASSERT_EQ("If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy.", std::string(buf, 101)); }
TEST(UTF8DecoderOutputStreamTest, TestUTF8Decoder) { int BUF_SIZE = 1024; char buf[BUF_SIZE]; int nread; const char source[] = { 0xE0, 0xE8, 0xEC, 0xF2, 0xF9, 0x0000 }; //"à, è, ì, ò, ù," in ISO-8859-1; int32_t srcCount=sizeof(source); MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); UTF8DecoderOutputStream utf8dos(bbos, "ISO-8859-1"); utf8dos.Write(source, srcCount); utf8dos.Flush(); nread = bbos->ReadFromBuffer(buf, BUF_SIZE); ASSERT_EQ(11, (int) nread); const char control[] = {0xC3, 0xA0, 0xC3, 0xA8, 0xC3, 0xAC, 0xC3, 0xB2, 0xC3, 0xB9, 0x0000}; //"à, è, ì, ò, ù," in UTF8 ASSERT_STREQ(control, std::string(buf, 11).c_str()); }
TEST(Base64OutputStreamTest, TestBase64Encoder) { int BUF_SIZE = 1024; char buf[BUF_SIZE]; int nread; MojRefCountedPtr<ByteBufferOutputStream> bbos( new ByteBufferOutputStream() ); MojRefCountedPtr<Base64EncoderOutputStream> b64eos( new Base64EncoderOutputStream(bbos)); // Write and read strncpy(buf, "XXX@1", BUF_SIZE); b64eos->Write("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.", 269); b64eos->Flush(); nread = bbos->ReadFromBuffer(buf, 1024); ASSERT_EQ( 370, (int) nread ); ASSERT_EQ("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n" "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n" "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n" "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\r\n", std::string(buf, nread)); }