summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2020-01-12 16:40:58 +0900
committerGitHub <noreply@github.com>2020-01-12 16:40:58 +0900
commit9703ba53405b2f355c6e0317f714d82ff3d4dee3 (patch)
tree4cd80df78b5c78bb60d47179836d393ee8d805d4 /test
parentRefactor (diff)
downloadsharkey-9703ba53405b2f355c6e0317f714d82ff3d4dee3.tar.gz
sharkey-9703ba53405b2f355c6e0317f714d82ff3d4dee3.tar.bz2
sharkey-9703ba53405b2f355c6e0317f714d82ff3d4dee3.zip
ファイルと画像認識処理の改善 (#5690)
* dimensions制限とリファクタ * comment * 不要な変更削除 * use fromFile など * Add probe-image-size.d.ts * えーCRLFで作るなよ… * Update src/@types/probe-image-size.d.ts Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * fix d.ts * Update src/@types/probe-image-size.d.ts Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * Update src/@types/probe-image-size.d.ts Co-Authored-By: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * fix Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
Diffstat (limited to 'test')
-rw-r--r--test/get-file-info.ts152
-rw-r--r--test/resources/25000x25000.pngbin0 -> 75933 bytes
-rw-r--r--test/resources/anime.gifbin0 -> 2248 bytes
-rw-r--r--test/resources/anime.pngbin0 -> 1868 bytes
-rw-r--r--test/resources/emptyfile0
-rw-r--r--test/resources/with-alpha.pngbin0 -> 3772 bytes
-rw-r--r--test/resources/with-xml-def.svg2
7 files changed, 154 insertions, 0 deletions
diff --git a/test/get-file-info.ts b/test/get-file-info.ts
new file mode 100644
index 0000000000..920df07382
--- /dev/null
+++ b/test/get-file-info.ts
@@ -0,0 +1,152 @@
+/*
+ * Tests for detection of file information
+ *
+ * How to run the tests:
+ * > TS_NODE_FILES=true npx mocha test/get-file-info.ts --require ts-node/register
+ *
+ * To specify test:
+ * > TS_NODE_FILES=true npx mocha test/get-file-info.ts --require ts-node/register -g 'test name'
+ */
+
+import * as assert from 'assert';
+import { async } from './utils';
+import { getFileInfo } from '../src/misc/get-file-info';
+
+describe('Get file info', () => {
+ it('Empty file', async (async () => {
+ const path = `${__dirname}/resources/emptyfile`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 0,
+ md5: 'd41d8cd98f00b204e9800998ecf8427e',
+ type: {
+ mime: 'application/octet-stream',
+ ext: null
+ },
+ width: undefined,
+ height: undefined,
+ avgColor: undefined
+ });
+ }));
+
+ it('Generic JPEG', async (async () => {
+ const path = `${__dirname}/resources/Lenna.jpg`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 25360,
+ md5: '091b3f259662aa31e2ffef4519951168',
+ type: {
+ mime: 'image/jpeg',
+ ext: 'jpg'
+ },
+ width: 512,
+ height: 512,
+ avgColor: [ 181, 99, 106 ]
+ });
+ }));
+
+ it('Generic APNG', async (async () => {
+ const path = `${__dirname}/resources/anime.png`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 1868,
+ md5: '08189c607bea3b952704676bb3c979e0',
+ type: {
+ mime: 'image/apng',
+ ext: 'apng'
+ },
+ width: 256,
+ height: 256,
+ avgColor: [ 249, 253, 250 ]
+ });
+ }));
+
+ it('Generic AGIF', async (async () => {
+ const path = `${__dirname}/resources/anime.gif`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 2248,
+ md5: '32c47a11555675d9267aee1a86571e7e',
+ type: {
+ mime: 'image/gif',
+ ext: 'gif'
+ },
+ width: 256,
+ height: 256,
+ avgColor: [ 249, 253, 250 ]
+ });
+ }));
+
+ it('PNG with alpha', async (async () => {
+ const path = `${__dirname}/resources/with-alpha.png`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 3772,
+ md5: 'f73535c3e1e27508885b69b10cf6e991',
+ type: {
+ mime: 'image/png',
+ ext: 'png'
+ },
+ width: 256,
+ height: 256,
+ avgColor: [ 255, 255, 255 ]
+ });
+ }));
+
+ it('Generic SVG', async (async () => {
+ const path = `${__dirname}/resources/image.svg`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 505,
+ md5: 'b6f52b4b021e7b92cdd04509c7267965',
+ type: {
+ mime: 'image/svg+xml',
+ ext: 'svg'
+ },
+ width: 256,
+ height: 256,
+ avgColor: [ 255, 255, 255 ]
+ });
+ }));
+
+ it('SVG with XML definition', async (async () => {
+ // https://github.com/syuilo/misskey/issues/4413
+ const path = `${__dirname}/resources/with-xml-def.svg`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 544,
+ md5: '4b7a346cde9ccbeb267e812567e33397',
+ type: {
+ mime: 'image/svg+xml',
+ ext: 'svg'
+ },
+ width: 256,
+ height: 256,
+ avgColor: [ 255, 255, 255 ]
+ });
+ }));
+
+ it('Dimension limit', async (async () => {
+ const path = `${__dirname}/resources/25000x25000.png`;
+ const info = await getFileInfo(path);
+ delete info.warnings;
+ assert.deepStrictEqual(info, {
+ size: 75933,
+ md5: '268c5dde99e17cf8fe09f1ab3f97df56',
+ type: {
+ mime: 'application/octet-stream', // do not treat as image
+ ext: null
+ },
+ width: 25000,
+ height: 25000,
+ avgColor: undefined
+ });
+ }));
+});
diff --git a/test/resources/25000x25000.png b/test/resources/25000x25000.png
new file mode 100644
index 0000000000..0ed4666925
--- /dev/null
+++ b/test/resources/25000x25000.png
Binary files differ
diff --git a/test/resources/anime.gif b/test/resources/anime.gif
new file mode 100644
index 0000000000..256ba495ce
--- /dev/null
+++ b/test/resources/anime.gif
Binary files differ
diff --git a/test/resources/anime.png b/test/resources/anime.png
new file mode 100644
index 0000000000..f13600f7a4
--- /dev/null
+++ b/test/resources/anime.png
Binary files differ
diff --git a/test/resources/emptyfile b/test/resources/emptyfile
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/resources/emptyfile
diff --git a/test/resources/with-alpha.png b/test/resources/with-alpha.png
new file mode 100644
index 0000000000..adc8d01805
--- /dev/null
+++ b/test/resources/with-alpha.png
Binary files differ
diff --git a/test/resources/with-xml-def.svg b/test/resources/with-xml-def.svg
new file mode 100644
index 0000000000..90971215a6
--- /dev/null
+++ b/test/resources/with-xml-def.svg
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="utf-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><path fill="#FF40A4" d="M128 80c-16 4-20 24-20 48v16c0 8-8 16-20.3 8 4.3 20 24.3 28 40.3 24s20-24 20-48v-16c0-8 8-16 20.3-8C164 84 144 76 128 80"/><path fill="#FFBF40" d="M192 80c-16 4-20 24-20 48v16c0 8-8 16-20.3 8 4.3 20 24.3 28 40.3 24s20-24 20-48v-16c0-8 8-16 20.3-8C228 84 208 76 192 80"/><path fill="#408EFF" d="M64 80c-16 4-20 24-20 48v16c0 8-8 16-20.3 8C28 172 48 180 64 176s20-24 20-48v-16c0-8 8-16 20.3-8C100 84 80 76 64 80"/></svg>