Welcome to the Linux Foundation Forum!

Probably Mistake Lab 11.2 - Convert a String to base64 Encoded String by Using a Buffer

Options

Hi,

I assume that the code that checks the result should not be changed.
If so, it seems there is a mistake in code that checks the result in this lab:

assert.equal(base64, Buffer.from([

assert.equal uses operator==, which for 2 distinct buffers always return false.

const str = 'abc'
const buf0 = Buffer.from(str)
const buf1 = Buffer.from(str)
console.log(buf0 == buf1) // false
console.log(buf0.equals(buf1)) // true

Perhaps, it should be rewritten as

assert.deepStrictEqual(base64, Buffer.from([

Regards,
Dmytro

Comments

  • xdxmxc
    xdxmxc Posts: 148
    Options

    really nice catch

  • xdxmxc
    xdxmxc Posts: 148
    Options

    actually no - the labs says "base64 string" a string is expected and therefore the other buffer is coerced to string. If you use a buffer instead of a string it's incorrect.

  • dmsheiko
    Options

    Well, if it has to be a string, why not check this as

    assert.equal(typeof base64, 'string')
    assert.equal(base64, 'YnVmZmVycyBhcmUgbmVhdA==')
    

    instead of

    assert.equal(base64, Buffer.from([
      89,110,86,109,90,109,86,121,99,
      121,66,104,99,109,85,103,98,109,
      86,104,100,65,61,61]))
    

    ?

  • xdxmxc
    xdxmxc Posts: 148
    Options

    @dmsheiko there is deliberate slight obfuscation throughout the validation tests, where the crux of an answer is deliberately hidden to avoid immediately ruining for those who see those details. It also means that you, or others, may explore the validation logic and convert the buffer to a string just to see what it says, in which case you've kind of earned the answer through a different means

Categories

Upcoming Training