Welcome to the Linux Foundation Forum!

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

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:

  1. assert.equal(base64, Buffer.from([

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

  1. const str = 'abc'
  2. const buf0 = Buffer.from(str)
  3. const buf1 = Buffer.from(str)
  4. console.log(buf0 == buf1) // false
  5. console.log(buf0.equals(buf1)) // true

Perhaps, it should be rewritten as

  1. assert.deepStrictEqual(base64, Buffer.from([

Regards,
Dmytro

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • Posts: 160

    really nice catch

  • Posts: 160

    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.

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

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

    instead of

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

    ?

  • Posts: 160

    @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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training