1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "TestEventPrinter.h"
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <wtf/Assertions.h>
37
38class DRTPrinter : public TestEventPrinter {
39public:
40    DRTPrinter() {}
41    void handleTestHeader(const char* url) const;
42    void handleTimedOut() const;
43    void handleTextHeader() const;
44    void handleTextFooter() const;
45    void handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
46    void handleImageFooter() const;
47    void handleTestFooter(bool dumpedAnything) const;
48};
49
50class TestShellPrinter : public TestEventPrinter {
51public:
52    TestShellPrinter() {}
53    void handleTestHeader(const char* url) const;
54    void handleTimedOut() const;
55    void handleTextHeader() const;
56    void handleTextFooter() const;
57    void handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
58    void handleImageFooter() const;
59    void handleTestFooter(bool dumpedAnything) const;
60};
61
62TestEventPrinter* TestEventPrinter::createDRTPrinter()
63{
64    return new DRTPrinter;
65}
66
67TestEventPrinter* TestEventPrinter::createTestShellPrinter()
68{
69    return new TestShellPrinter;
70}
71
72// ----------------------------------------------------------------
73
74void DRTPrinter::handleTestHeader(const char*) const
75{
76}
77
78void DRTPrinter::handleTimedOut() const
79{
80    fprintf(stderr, "FAIL: Timed out waiting for notifyDone to be called\n");
81    fprintf(stdout, "FAIL: Timed out waiting for notifyDone to be called\n");
82}
83
84void DRTPrinter::handleTextHeader() const
85{
86    printf("Content-Type: text/plain\n");
87}
88
89void DRTPrinter::handleTextFooter() const
90{
91    printf("#EOF\n");
92}
93
94void DRTPrinter::handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char*) const
95{
96    ASSERT(actualHash);
97    printf("\nActualHash: %s\n", actualHash);
98    if (expectedHash && expectedHash[0])
99        printf("\nExpectedHash: %s\n", expectedHash);
100    if (imageData && imageSize) {
101        printf("Content-Type: image/png\n");
102        // Printf formatting for size_t on 32-bit, 64-bit, and on Windows is hard so just cast to an int.
103        printf("Content-Length: %d\n", static_cast<int>(imageSize));
104        if (fwrite(imageData, 1, imageSize, stdout) != imageSize) {
105            fprintf(stderr, "Short write to stdout.\n");
106            exit(1);
107        }
108    }
109}
110
111void DRTPrinter::handleImageFooter() const
112{
113    printf("#EOF\n");
114}
115
116void DRTPrinter::handleTestFooter(bool) const
117{
118}
119
120// ----------------------------------------------------------------
121
122void TestShellPrinter::handleTestHeader(const char* url) const
123{
124    printf("#URL:%s\n", url);
125}
126
127void TestShellPrinter::handleTimedOut() const
128{
129    puts("#TEST_TIMED_OUT\n");
130}
131
132void TestShellPrinter::handleTextHeader() const
133{
134}
135
136void TestShellPrinter::handleTextFooter() const
137{
138}
139
140void TestShellPrinter::handleImage(const char* actualHash, const char*, const unsigned char* imageData, size_t imageSize, const char* fileName) const
141{
142    ASSERT(actualHash);
143    if (imageData && imageSize) {
144        ASSERT(fileName);
145        FILE* fp = fopen(fileName, "wb");
146        if (!fp) {
147            perror(fileName);
148            exit(EXIT_FAILURE);
149        }
150        if (fwrite(imageData, 1, imageSize, fp) != imageSize) {
151            perror(fileName);
152            fclose(fp);
153            exit(EXIT_FAILURE);
154        }
155        fclose(fp);
156    }
157    printf("#MD5:%s\n", actualHash);
158}
159
160void TestShellPrinter::handleImageFooter() const
161{
162}
163
164void TestShellPrinter::handleTestFooter(bool dumpedAnything) const
165{
166    if (dumpedAnything)
167        printf("#EOF\n");
168}
169