summaryrefslogtreecommitdiffstats
path: root/image/test/gtest/TestLoader.cpp
blob: d18302f37abfe32b506210bfea92840dae2e81a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "Common.h"
#include "imgLoader.h"
#include "nsMimeTypes.h"
#include "nsString.h"

using namespace mozilla;
using namespace mozilla::image;

static void CheckMimeType(const char* aContents, size_t aLength,
                          const char* aExpected) {
  nsAutoCString detected;
  nsresult rv = imgLoader::GetMimeTypeFromContent(aContents, aLength, detected);
  if (aExpected) {
    ASSERT_NS_SUCCEEDED(rv);
    EXPECT_TRUE(detected.EqualsASCII(aExpected));
  } else {
    ASSERT_NS_FAILED(rv);
    EXPECT_TRUE(detected.IsEmpty());
  }
}

class ImageLoader : public ::testing::Test {
 protected:
  AutoInitializeImageLib mInit;
};

TEST_F(ImageLoader, DetectGIF) {
  const char buffer[] = "GIF87a";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_GIF);
}

TEST_F(ImageLoader, DetectPNG) {
  const char buffer[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_PNG);
}

TEST_F(ImageLoader, DetectJPEG) {
  const char buffer[] = "\xFF\xD8\xFF";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_JPEG);
}

TEST_F(ImageLoader, DetectART) {
  const char buffer[] = "\x4A\x47\xFF\xFF\x00";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_ART);
}

TEST_F(ImageLoader, DetectBMP) {
  const char buffer[] = "BM";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_BMP);
}

TEST_F(ImageLoader, DetectICO) {
  const char buffer[] = "\x00\x00\x01\x00";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_ICO);
}

TEST_F(ImageLoader, DetectWebP) {
  const char buffer[] = "RIFF\xFF\xFF\xFF\xFFWEBPVP8L";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_WEBP);
}

TEST_F(ImageLoader, DetectAVIFMajorBrand) {
  const char buffer[] =
      "\x00\x00\x00\x20"   // box length
      "ftyp"               // box type
      "avif"               // major brand
      "\x00\x00\x00\x00"   // minor version
      "avifmif1miafMA1B";  // compatible brands
  CheckMimeType(buffer, sizeof(buffer), IMAGE_AVIF);
}

TEST_F(ImageLoader, DetectAVIFCompatibleBrand) {
  const char buffer[] =
      "\x00\x00\x00\x20"   // box length
      "ftyp"               // box type
      "XXXX"               // major brand
      "\x00\x00\x00\x00"   // minor version
      "avifmif1miafMA1B";  // compatible brands
  CheckMimeType(buffer, sizeof(buffer), IMAGE_AVIF);
}

#ifdef MOZ_JXL
TEST_F(ImageLoader, DetectJXLCodestream) {
  const char buffer[] = "\xff\x0a";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL);
}

TEST_F(ImageLoader, DetectJXLContainer) {
  const char buffer[] =
      "\x00\x00\x00\x0c"
      "JXL "
      "\x0d\x0a\x87\x0a";
  CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL);
}
#endif

TEST_F(ImageLoader, DetectNonImageMP4) {
  const char buffer[] =
      "\x00\x00\x00\x1c"  // box length
      "ftyp"              // box type
      "isom"              // major brand
      "\x00\x00\x02\x00"  // minor version
      "isomiso2mp41";     // compatible brands
  CheckMimeType(buffer, sizeof(buffer), nullptr);
}

TEST_F(ImageLoader, DetectNone) {
  const char buffer[] = "abcdefghijklmnop";
  CheckMimeType(buffer, sizeof(buffer), nullptr);
}