• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38 class DRTPrinter : public TestEventPrinter {
39 public:
DRTPrinter()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 
50 class TestShellPrinter : public TestEventPrinter {
51 public:
TestShellPrinter()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 
createDRTPrinter()62 TestEventPrinter* TestEventPrinter::createDRTPrinter()
63 {
64     return new DRTPrinter;
65 }
66 
createTestShellPrinter()67 TestEventPrinter* TestEventPrinter::createTestShellPrinter()
68 {
69     return new TestShellPrinter;
70 }
71 
72 // ----------------------------------------------------------------
73 
handleTestHeader(const char *) const74 void DRTPrinter::handleTestHeader(const char*) const
75 {
76 }
77 
handleTimedOut() const78 void 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 
handleTextHeader() const84 void DRTPrinter::handleTextHeader() const
85 {
86     printf("Content-Type: text/plain\n");
87 }
88 
handleTextFooter() const89 void DRTPrinter::handleTextFooter() const
90 {
91     printf("#EOF\n");
92 }
93 
handleImage(const char * actualHash,const char * expectedHash,const unsigned char * imageData,size_t imageSize,const char *) const94 void 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 
handleImageFooter() const111 void DRTPrinter::handleImageFooter() const
112 {
113     printf("#EOF\n");
114 }
115 
handleTestFooter(bool) const116 void DRTPrinter::handleTestFooter(bool) const
117 {
118 }
119 
120 // ----------------------------------------------------------------
121 
handleTestHeader(const char * url) const122 void TestShellPrinter::handleTestHeader(const char* url) const
123 {
124     printf("#URL:%s\n", url);
125 }
126 
handleTimedOut() const127 void TestShellPrinter::handleTimedOut() const
128 {
129     puts("#TEST_TIMED_OUT\n");
130 }
131 
handleTextHeader() const132 void TestShellPrinter::handleTextHeader() const
133 {
134 }
135 
handleTextFooter() const136 void TestShellPrinter::handleTextFooter() const
137 {
138 }
139 
handleImage(const char * actualHash,const char *,const unsigned char * imageData,size_t imageSize,const char * fileName) const140 void 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 
handleImageFooter() const160 void TestShellPrinter::handleImageFooter() const
161 {
162 }
163 
handleTestFooter(bool dumpedAnything) const164 void TestShellPrinter::handleTestFooter(bool dumpedAnything) const
165 {
166     if (dumpedAnything)
167         printf("#EOF\n");
168 }
169