Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/image/test/gtest/TestLoader.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "gtest/gtest.h"
8
9
#include "Common.h"
10
#include "imgLoader.h"
11
#include "nsMimeTypes.h"
12
#include "nsString.h"
13
14
using namespace mozilla;
15
using namespace mozilla::image;
16
17
static void
18
CheckMimeType(const char* aContents, size_t aLength, const char* aExpected)
19
0
{
20
0
  nsAutoCString detected;
21
0
  nsresult rv = imgLoader::GetMimeTypeFromContent(aContents, aLength, detected);
22
0
  if (aExpected) {
23
0
    ASSERT_TRUE(NS_SUCCEEDED(rv));
24
0
    EXPECT_TRUE(detected.EqualsASCII(aExpected));
25
0
  } else {
26
0
    ASSERT_TRUE(NS_FAILED(rv));
27
0
    EXPECT_TRUE(detected.IsEmpty());
28
0
  }
29
0
}
30
31
class ImageLoader : public ::testing::Test
32
{
33
protected:
34
  AutoInitializeImageLib mInit;
35
};
36
37
TEST_F(ImageLoader, DetectGIF)
38
0
{
39
0
  const char buffer[] = "GIF87a";
40
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_GIF);
41
0
}
42
43
TEST_F(ImageLoader, DetectPNG)
44
0
{
45
0
  const char buffer[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
46
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_PNG);
47
0
}
48
49
TEST_F(ImageLoader, DetectJPEG)
50
0
{
51
0
  const char buffer[] = "\xFF\xD8\xFF";
52
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_JPEG);
53
0
}
54
55
TEST_F(ImageLoader, DetectART)
56
0
{
57
0
  const char buffer[] = "\x4A\x47\xFF\xFF\x00";
58
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_ART);
59
0
}
60
61
TEST_F(ImageLoader, DetectBMP)
62
0
{
63
0
  const char buffer[] = "BM";
64
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_BMP);
65
0
}
66
67
TEST_F(ImageLoader, DetectICO)
68
0
{
69
0
  const char buffer[] = "\x00\x00\x01\x00";
70
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_ICO);
71
0
}
72
73
TEST_F(ImageLoader, DetectWebP)
74
0
{
75
0
  const char buffer[] = "RIFF\xFF\xFF\xFF\xFFWEBPVP8L";
76
0
  CheckMimeType(buffer, sizeof(buffer), IMAGE_WEBP);
77
0
}
78
79
TEST_F(ImageLoader, DetectNone)
80
0
{
81
0
  const char buffer[] = "abcdefghijklmnop";
82
0
  CheckMimeType(buffer, sizeof(buffer), nullptr);
83
0
}
84