Skip to content

Commit

Permalink
Consistent testing and validation
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <berend@animo.id>
  • Loading branch information
berendsliedrecht committed May 12, 2021
1 parent fde8a98 commit 775cc7f
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 110 deletions.
8 changes: 6 additions & 2 deletions src/utils/MultibaseEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import multibase from 'multibase'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Buffer } from './buffer'

export type BaseName = multibase.BaseName
Expand All @@ -25,8 +26,11 @@ export class MultibaseEncoder {
* @returns {string} encodingAlgorithm name of the encoding algorithm
*/
public static decode(data: string | Uint8Array): { data: Uint8Array; baseName: string } {
const baseName = multibase.encodingFromData(data).name
return { data: multibase.decode(data), baseName }
if (this.validate(data)) {
const baseName = multibase.encodingFromData(data).name
return { data: multibase.decode(data), baseName }
}
throw new Error(`Invalid multibase: ${data}`)
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/utils/MultihashEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export class MultihashEncoder {
* @param {Uint8Array} data the multihash that has to be decoded
*/
public static decode(data: Uint8Array): { data: Uint8Array; hashName: string } {
const decodedHash = multihash.decode(data)
return { data: decodedHash.digest, hashName: decodedHash.name }
if (this.validate(data)) {
const decodedHash = multihash.decode(data)
return { data: decodedHash.digest, hashName: decodedHash.name }
}
throw new Error(`Invalid multihash: ${data}`)
}

/**
Expand Down
47 changes: 25 additions & 22 deletions src/utils/__tests__/MultibaseEncoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,36 @@ const validMultibase = 'zKWfinQuRQ3ekD1danFHqvKRg9koFp8vpokUeREEgjSyHwweeKDFaxVH
const invalidMultibase = 'gKWfinQuRQ3ekD1danFHqvKRg9koFp8vpokUeREEgjSyHwweeKDFaxVHi'

describe('MultiBaseEncoder', () => {
describe('encode()', () => {
})
})
it('Encodes multibase', () => {
const multibase = BufferEncoder.toUtf8String(MultibaseEncoder.encode(validData, 'base58btc'))
expect(multibase).toEqual('z2NEpo7TZRRrLZSi2U')
describe('encode()', () => {
it('Encodes valid multibase', () => {
const multibase = BufferEncoder.toUtf8String(MultibaseEncoder.encode(validData, 'base58btc'))
expect(multibase).toEqual('z2NEpo7TZRRrLZSi2U')
})
})

it('Decodes multibase', () => {
const { data, baseName } = MultibaseEncoder.decode(validMultibase)
expect(BufferEncoder.toUtf8String(data)).toEqual('This is a valid base58btc encoded string!')
expect(baseName).toEqual('base58btc')
})
describe('Decodes()', () => {
it('Decodes multibase', () => {
const { data, baseName } = MultibaseEncoder.decode(validMultibase)
expect(BufferEncoder.toUtf8String(data)).toEqual('This is a valid base58btc encoded string!')
expect(baseName).toEqual('base58btc')
})

it('Validates valid multibase', () => {
const bool = MultibaseEncoder.validate(validMultibase)
expect(bool).toEqual(true)
it('Decodes invalid multibase', () => {
expect(() => {
MultibaseEncoder.decode(invalidMultibase)
}).toThrow(/^Invalid multibase: /)
})
})

it('Validates invalid multibase', () => {
const bool = MultibaseEncoder.validate(invalidMultibase)
expect(bool).toEqual(false)
})
describe('validate()', () => {
it('Validates valid multibase', () => {
const bool = MultibaseEncoder.validate(validMultibase)
expect(bool).toEqual(true)
})

it('Decodes invalid multibase', () => {
expect(() => {
MultibaseEncoder.decode(invalidMultibase)
}).toThrow(/^Unsupported encoding: g/)
it('Validates invalid multibase', () => {
const bool = MultibaseEncoder.validate(invalidMultibase)
expect(bool).toEqual(false)
})
})
})
44 changes: 25 additions & 19 deletions src/utils/__tests__/MultihashEncoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,36 @@ const validMultihash = new Uint8Array([18, 12, 72, 101, 108, 108, 111, 32, 87, 1
const invalidMultihash = new Uint8Array([99, 12, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])

describe('multihash', () => {
it('encodes multihash', () => {
const multihash = MultihashEncoder.encode(validData, 'sha2-256')
expect(multihash).toEqual(new Uint8Array([18, 12, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
describe('encode()', () => {
it('encodes multihash', () => {
const multihash = MultihashEncoder.encode(validData, 'sha2-256')
expect(multihash).toEqual(new Uint8Array([18, 12, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
})
})

it('Decodes multihash', () => {
const { data, hashName } = MultihashEncoder.decode(validMultihash)
expect(hashName).toEqual('sha2-256')
expect(BufferEncoder.toUtf8String(data)).toEqual('Hello World!')
})
describe('decode()', () => {
it('Decodes multihash', () => {
const { data, hashName } = MultihashEncoder.decode(validMultihash)
expect(hashName).toEqual('sha2-256')
expect(BufferEncoder.toUtf8String(data)).toEqual('Hello World!')
})

it('Validates valid multihash', () => {
const bool = MultihashEncoder.validate(validMultihash)
expect(bool).toEqual(true)
it('Decodes invalid multihash', () => {
expect(() => {
MultihashEncoder.decode(invalidMultihash)
}).toThrow()
})
})

it('Validates invalid multihash', () => {
const bool = MultihashEncoder.validate(invalidMultihash)
expect(bool).toEqual(false)
})
describe('validate()', () => {
it('Validates valid multihash', () => {
const bool = MultihashEncoder.validate(validMultihash)
expect(bool).toEqual(true)
})

it('Decodes invalid multihash', () => {
expect(() => {
MultihashEncoder.decode(invalidMultihash)
}).toThrow()
it('Validates invalid multihash', () => {
const bool = MultihashEncoder.validate(invalidMultihash)
expect(bool).toEqual(false)
})
})
})
65 changes: 0 additions & 65 deletions src/utils/__tests__/hashlink.test.ts

This file was deleted.

78 changes: 78 additions & 0 deletions src/utils/__tests__/hashlinkEncoder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Buffer } from 'buffer'
import { HashlinkEncoder } from '../hashlinkEncoder'

const validData = {
data: Buffer.from('Hello World!'),
metaData: {
urls: ['https://example.org/hw.txt'],
contentType: 'text/plain',
},
}

const invalidData = {
data: Buffer.from('Hello World!'),
metaData: {
unknownKey: 'unkownValue',
contentType: 'image/png',
},
}

const validHashlink =
'hl:zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e:zCwQVpeF6FPqFyc4pvisK5cpW4kc358NTX6ZbhqfawZTBmXm372zoAj5oLSh'

const invalidHashlink =
'hl:gQmWvQxTqbqwlkhhhhh9w8e7rJenbyYTWkjgF3e:z51a94WAQfNv1KEcPeoV3V2isZFPFqSzE9ghNFQ8DuQu4hTHtFRug8SDgug14Ff'

const invalidMetadata =
'hl:zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e:zHCwSqQisPgCc2sMSNmHWyQtCKu4kgQVD6Q1Nhxff7uNRqN6r'

describe('HashlinkEncoder', () => {
describe('encode()', () => {
it('Encodes string to hashlink', () => {
const hashlink = HashlinkEncoder.encode(validData.data, 'sha2-256')
expect(hashlink).toEqual('hl:zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e')
})

it('Encodes string and metadata to hashlink', () => {
const hashlink = HashlinkEncoder.encode(validData.data, 'sha2-256', 'base58btc', validData.metaData)
expect(hashlink).toEqual(validHashlink)
})

it('Encodes invalid metadata in hashlink', () => {
expect(() => {
HashlinkEncoder.encode(validData.data, 'sha2-256', 'base58btc', invalidData.metaData)
}).toThrow(/^Invalid metadata: /)
})
})

describe('decode()', () => {
it('Decodes hashlink', () => {
const decodedHashlink = HashlinkEncoder.decode(validHashlink)
expect(decodedHashlink).toEqual({
checksum: 'zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e',
metadata: { contentType: 'text/plain', urls: ['https://example.org/hw.txt'] },
})
})

it('Decodes invalid hashlink', () => {
expect(() => {
HashlinkEncoder.decode(invalidHashlink)
}).toThrow(/^Invalid hashlink: /)
})

it('Decodes invalid metadata in hashlink', () => {
expect(() => {
HashlinkEncoder.decode(invalidMetadata)
}).toThrow(/^Invalid metadata: /)
})
})

describe('validate()', () => {
it('Validate hashlink', () => {
expect(HashlinkEncoder.validate(validHashlink)).toEqual(true)
})
it('Validate invalid hashlink', () => {
expect(HashlinkEncoder.validate(invalidHashlink)).toEqual(false)
})
})
})
Loading

0 comments on commit 775cc7f

Please sign in to comment.