Skip to content

Commit

Permalink
test: Improved tests
Browse files Browse the repository at this point in the history
* Added additional tests for input validation handling and data structure constraints
* Removed test related to string matching
  • Loading branch information
vHeemstra authored Aug 12, 2024
1 parent 9ba5bbf commit ad57624
Showing 1 changed file with 78 additions and 21 deletions.
99 changes: 78 additions & 21 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import isApng from '../dist/index.mjs'

/** Input validation tests */

test('returns false on number 0 input', (t) => {
t.false(isApng(0))
})

test('returns false on number 1 input', (t) => {
t.false(isApng(1))
})

test('returns false on empty string input', (t) => {
t.false(isApng(''))
})

test('returns false on non-empty string input', (t) => {
t.false(isApng('string'))
})

test('returns false on boolean true input', (t) => {
t.false(isApng(true))
})

test('returns false on boolean false input', (t) => {
t.false(isApng(false))
})

test('returns false on object input', (t) => {
t.false(isApng({}))
})

test('returns false on array input', (t) => {
t.false(isApng([]))
})

/** Real image tests */

const dir =
typeof __dirname !== 'undefined'
? __dirname
Expand All @@ -19,15 +55,17 @@ test('returns false on static PNG', (t) => {
t.false(check(dir + '/images/static.png'))
})

test('returns false on static PNG with `acTL` text in metadata', (t) => {
t.false(check(dir + '/images/staticWithMetadata.png'))
})

test('returns false on JPG', (t) => {
t.false(check(dir + '/images/static.jpg'))
})

test('returns false on static PNG with `acTL` text in metadata', (t) => {
t.false(check(dir + '/images/staticWithMetadata.png'))
})
/** Mock data tests */

test('returns true when IDAT follows acTL', (t) => {
test('returns true when acTL precedes any IDAT', (t) => {
t.true(
isApng(
new Uint8Array([
Expand All @@ -43,6 +81,31 @@ test('returns true when IDAT follows acTL', (t) => {
0x00, 0x00, 0x00, 0x00,
// Chunk type: IDAT
0x49, 0x44, 0x41, 0x54,
// Chunk CRC
0x00, 0x00, 0x00, 0x00,
]),
),
)
})

test('returns false when any IDAT precedes acTL', (t) => {
t.false(
isApng(
new Uint8Array([
// PNG header
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
// Chunk length: 0
0x00, 0x00, 0x00, 0x00,
// Chunk type: IDAT
0x49, 0x44, 0x41, 0x54,
// Chunk CRC
0x00, 0x00, 0x00, 0x00,
// Chunk length: 0
0x00, 0x00, 0x00, 0x00,
// Chunk type: acTL
0x61, 0x63, 0x54, 0x4c,
// Chunk CRC
0x00, 0x00, 0x00, 0x00,
]),
),
)
Expand All @@ -60,42 +123,36 @@ test('returns false when acTL is not a chunk type', (t) => {
0x00, 0x00, 0x00, 0x01,
// Chunk data: acTL
0x61, 0x63, 0x54, 0x4c,
// Chunk CRC
0x00, 0x00, 0x00, 0x00,
]),
),
)
})

test('returns false when IDAT precedes acTL', (t) => {
test('returns false on too small PNG', (t) => {
t.false(
isApng(
new Uint8Array([
// PNG header
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
// Chunk length: 0
0x00, 0x00, 0x00, 0x00,
// Chunk type: IDAT
0x49, 0x44, 0x41, 0x54,
// Chunk CRC
0x00, 0x00, 0x00, 0x00,
// Chunk length: 0
0x00, 0x00, 0x00, 0x00,
// Chunk type: acTL
0x61, 0x63, 0x54, 0x4c,
// Content omitted
]),
),
)
})

test('chunks should be found when preceded by a partial of themselves', (t) => {
t.true(
test('returns false on invalid chunk', (t) => {
t.false(
isApng(
new Uint8Array([
// PNG header
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
// Chunk length: a
0x00, 0x00, 0x00, 0x61,
// Chunk type: acTL
0x61, 0x63, 0x54, 0x4c,
// Chunk length: 4
0x00, 0x00, 0x00, 0x04,
// Chunk type: any
0x00, 0x00, 0x00, 0x01,
// Chunk CRC omitted
]),
),
)
Expand Down

0 comments on commit ad57624

Please sign in to comment.