<commit_before>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/util/nigori.h"

#include <string>

#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(NigoriTest, PermuteIsConstant) {
  browser_sync::Nigori nigori1("example.com");
  EXPECT_TRUE(nigori1.Init("username", "password"));

  std::string permuted1;
  EXPECT_TRUE(nigori1.Permute(browser_sync::Nigori::Password,
                              "name",
                              &permuted1));

  browser_sync::Nigori nigori2("example.com");
  EXPECT_TRUE(nigori2.Init("username", "password"));

  std::string permuted2;
  EXPECT_TRUE(nigori2.Permute(browser_sync::Nigori::Password,
                              "name",
                              &permuted2));

  EXPECT_LT(0U, permuted1.size());
  EXPECT_EQ(permuted1, permuted2);
}

TEST(NigoriTest, EncryptDifferentIv) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("value");

  std::string encrypted1;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted1));

  std::string encrypted2;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted2));

  EXPECT_NE(encrypted1, encrypted2);
}

TEST(NigoriTest, EncryptDecrypt) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("value");

  std::string encrypted;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted));

  std::string decrypted;
  EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted));

  EXPECT_EQ(plaintext, decrypted);
}

TEST(NigoriTest, CorruptedIv) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("test");

  std::string encrypted;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted));<commit_msg>Add unit tests for Nigori permute and decrypt.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/util/nigori.h"

#include <string>

#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(NigoriTest, Permute) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string permuted;
  EXPECT_TRUE(nigori.Permute(browser_sync::Nigori::Password, "test name",
                             &permuted));

  std::string expected =
      "prewwdJj2PrGDczvmsHJEE5ndcCyVze8sY9kD5hjY/Tm"
      "c5kOjXFK7zB3Ss4LlHjEDirMu+vh85JwHOnGrMVe+g==";
  EXPECT_EQ(expected, permuted);
}

TEST(NigoriTest, PermuteIsConstant) {
  browser_sync::Nigori nigori1("example.com");
  EXPECT_TRUE(nigori1.Init("username", "password"));

  std::string permuted1;
  EXPECT_TRUE(nigori1.Permute(browser_sync::Nigori::Password,
                              "name",
                              &permuted1));

  browser_sync::Nigori nigori2("example.com");
  EXPECT_TRUE(nigori2.Init("username", "password"));

  std::string permuted2;
  EXPECT_TRUE(nigori2.Permute(browser_sync::Nigori::Password,
                              "name",
                              &permuted2));

  EXPECT_LT(0U, permuted1.size());
  EXPECT_EQ(permuted1, permuted2);
}

TEST(NigoriTest, EncryptDifferentIv) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("value");

  std::string encrypted1;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted1));

  std::string encrypted2;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted2));

  EXPECT_NE(encrypted1, encrypted2);
}

TEST(NigoriTest, Decrypt) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string encrypted =
      "<KEY>"
      "<KEY>;

  std::string plaintext;
  EXPECT_TRUE(nigori.Decrypt(encrypted, &plaintext));

  std::string expected("test, test, 1, 2, 3");
  EXPECT_EQ(expected, plaintext);
}

TEST(NigoriTest, EncryptDecrypt) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("value");

  std::string encrypted;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted));

  std::string decrypted;
  EXPECT_TRUE(nigori.Decrypt(encrypted, &decrypted));

  EXPECT_EQ(plaintext, decrypted);
}

TEST(NigoriTest, CorruptedIv) {
  browser_sync::Nigori nigori("example.com");
  EXPECT_TRUE(nigori.Init("username", "password"));

  std::string plaintext("test");

  std::string encrypted;
  EXPECT_TRUE(nigori.Encrypt(plaintext, &encrypted));
{'fraction_non_alphanumeric': 0.1446306641837368, 'fraction_numerical': 0.012828470929029587, 'mean_word_length': 4.518264840182648, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663427', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1703, 'n_words': 268}