Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  TestEncryptedRandomAccessBlock.cpp

  Sprache: C
 

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


#include <algorithm>
#include <array>
#include <cstring>
#include <numeric>

#include "EncryptedRandomAccessBlock.h"
#include "EncryptedRandomAccessBlockViewV1.h"
#include "gtest/gtest.h"

namespace mozilla::dom::quota::test {

bool IsAllZero(Span<const uint8_t> aSpan) {
  return std::all_of(aSpan.data(), aSpan.data() + aSpan.Length(),
                     [](uint8_t e) { return e == 0; });
}

TEST(EncryptedRandomAccessBlockTest, zeroInitializedEncryptedBlock)
{
  EncryptedRandomAccessBlock encryptedRandomAccessBlock;

  EXPECT_TRUE(IsAllZero(encryptedRandomAccessBlock.Header()));
  EXPECT_TRUE(IsAllZero(encryptedRandomAccessBlock.CipherMetadata()));
  EXPECT_TRUE(IsAllZero(encryptedRandomAccessBlock.CipherPayload()));
}

TEST(EncryptedRandomAccessBlockTest, accessors)
{
  std::array<uint8_t, EncryptedRandomAccessBlock::BlockSize> rawData{};
  std::iota(rawData.begin(), rawData.end(), 1);

  EncryptedRandomAccessBlock block;
  block.AssignFromBytes(Span<const uint8_t>{
      rawData.data(), EncryptedRandomAccessBlock::BlockSize});

  const Span<const uint8_t> raw{rawData.data(), rawData.size()};

  uint16_t expectedVersion;
  memcpy(&expectedVersion, rawData.data(), sizeof(uint16_t));
  EXPECT_EQ(block.Version(), expectedVersion);
  EXPECT_EQ(block.Header(), raw.First(32));
  EXPECT_EQ(block.ReservedBytes(), raw.Subspan(232 - 2));
  EXPECT_EQ(block.CipherMetadata(), raw.Subspan(3232));
  EXPECT_EQ(block.CipherPayload(), raw.Subspan(644096 - 64));

  EncryptedRandomAccessBlockViewV1 v1(block.MutableCipherMetadata());
  EXPECT_EQ(v1.Nonce(), raw.Subspan(3212));
  EXPECT_EQ(v1.AuthenticationTag(), raw.Subspan(32 + 1216));
}

TEST(EncryptedRandomAccessBlockTest, setVersion)
{
  EncryptedRandomAccessBlock block;
  block.SetVersion(0x0102);

  EXPECT_EQ(block.Version(), 0x0102u);

  // check if the adjacent area is not written
  EXPECT_TRUE(IsAllZero(block.ReservedBytes()));
}

TEST(EncryptedRandomAccessBlockTest, setNonce)
{
  EncryptedRandomAccessBlock block;
  EncryptedRandomAccessBlockViewV1 v1(block.MutableCipherMetadata());

  std::array<uint8_t, 12> nonce{};
  std::iota(nonce.begin(), nonce.end(), 1);
  v1.SetNonce(Span<const uint8_t, 12>{nonce.data(), 12});

  EXPECT_EQ(v1.Nonce(), Span<const uint8_t>(nonce.data(), nonce.size()));

  // check if the adjacent area is not written
  EXPECT_TRUE(IsAllZero(v1.AuthenticationTag()));
}

TEST(EncryptedRandomAccessBlockTest, setAuthenticationTag)
{
  EncryptedRandomAccessBlock block;
  EncryptedRandomAccessBlockViewV1 v1(block.MutableCipherMetadata());

  std::array<uint8_t, 16> authTag{};
  std::iota(authTag.begin(), authTag.end(), 1);
  v1.SetAuthenticationTag(Span<const uint8_t, 16>{authTag.data(), 16});

  EXPECT_EQ(v1.AuthenticationTag(),
            Span<const uint8_t>(authTag.data(), authTag.size()));

  // check if the adjacent area is not written
  EXPECT_TRUE(IsAllZero(v1.Nonce()));
}

TEST(EncryptedRandomAccessBlockTest, mutableCipherPayload)
{
  EncryptedRandomAccessBlock block;

  auto mutablePayload = block.MutableCipherPayload();
  std::iota(mutablePayload.data(),
            mutablePayload.data() + mutablePayload.Length(), 1);

  EXPECT_EQ(block.CipherPayload(), Span<const uint8_t>{mutablePayload});

  // check if the adjacent area is not written.
  EXPECT_TRUE(IsAllZero(block.CipherMetadata()));
}

}  // namespace mozilla::dom::quota::test

Messung V0.5 in Prozent
C=92 H=90 G=90

¤ Dauer der Verarbeitung: 0.5 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Open Source Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Statistik
#Sources=277311
#Domains=752002