TestLoader.cpp (3408B)
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 CheckMimeType(const char* aContents, size_t aLength, 18 const char* aExpected) { 19 nsAutoCString detected; 20 nsresult rv = imgLoader::GetMimeTypeFromContent(aContents, aLength, detected); 21 if (aExpected) { 22 ASSERT_NS_SUCCEEDED(rv); 23 EXPECT_TRUE(detected.EqualsASCII(aExpected)); 24 } else { 25 ASSERT_NS_FAILED(rv); 26 EXPECT_TRUE(detected.IsEmpty()); 27 } 28 } 29 30 class ImageLoader : public ::testing::Test { 31 protected: 32 AutoInitializeImageLib mInit; 33 }; 34 35 TEST_F(ImageLoader, DetectGIF) { 36 const char buffer[] = "GIF87a"; 37 CheckMimeType(buffer, sizeof(buffer), IMAGE_GIF); 38 } 39 40 TEST_F(ImageLoader, DetectPNG) { 41 const char buffer[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; 42 CheckMimeType(buffer, sizeof(buffer), IMAGE_PNG); 43 } 44 45 TEST_F(ImageLoader, DetectJPEG) { 46 const char buffer[] = "\xFF\xD8\xFF"; 47 CheckMimeType(buffer, sizeof(buffer), IMAGE_JPEG); 48 } 49 50 TEST_F(ImageLoader, DetectART) { 51 const char buffer[] = "\x4A\x47\xFF\xFF\x00"; 52 CheckMimeType(buffer, sizeof(buffer), IMAGE_ART); 53 } 54 55 TEST_F(ImageLoader, DetectBMP) { 56 const char buffer[] = "BM"; 57 CheckMimeType(buffer, sizeof(buffer), IMAGE_BMP); 58 } 59 60 TEST_F(ImageLoader, DetectICO) { 61 const char buffer[] = "\x00\x00\x01\x00"; 62 CheckMimeType(buffer, sizeof(buffer), IMAGE_ICO); 63 } 64 65 TEST_F(ImageLoader, DetectWebP) { 66 const char buffer[] = "RIFF\xFF\xFF\xFF\xFFWEBPVP8L"; 67 CheckMimeType(buffer, sizeof(buffer), IMAGE_WEBP); 68 } 69 70 TEST_F(ImageLoader, DetectAVIFMajorBrand) { 71 const char buffer[] = 72 "\x00\x00\x00\x20" // box length 73 "ftyp" // box type 74 "avif" // major brand 75 "\x00\x00\x00\x00" // minor version 76 "avifmif1miafMA1B"; // compatible brands 77 CheckMimeType(buffer, sizeof(buffer), IMAGE_AVIF); 78 } 79 80 TEST_F(ImageLoader, DetectAVIFCompatibleBrand) { 81 const char buffer[] = 82 "\x00\x00\x00\x20" // box length 83 "ftyp" // box type 84 "XXXX" // major brand 85 "\x00\x00\x00\x00" // minor version 86 "avifmif1miafMA1B"; // compatible brands 87 CheckMimeType(buffer, sizeof(buffer), IMAGE_AVIF); 88 } 89 90 #ifdef MOZ_JXL 91 TEST_F(ImageLoader, DetectJXLCodestream) { 92 const char buffer[] = "\xff\x0a"; 93 CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL); 94 } 95 96 TEST_F(ImageLoader, DetectJXLContainer) { 97 const char buffer[] = 98 "\x00\x00\x00\x0c" 99 "JXL " 100 "\x0d\x0a\x87\x0a"; 101 CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL); 102 } 103 #endif 104 105 TEST_F(ImageLoader, DetectNonImageMP4) { 106 const char buffer[] = 107 "\x00\x00\x00\x1c" // box length 108 "ftyp" // box type 109 "isom" // major brand 110 "\x00\x00\x02\x00" // minor version 111 "isomiso2mp41"; // compatible brands 112 CheckMimeType(buffer, sizeof(buffer), nullptr); 113 } 114 115 TEST_F(ImageLoader, DetectNone) { 116 const char buffer[] = "abcdefghijklmnop"; 117 CheckMimeType(buffer, sizeof(buffer), nullptr); 118 }