onc_utils_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chromeos/network/onc/onc_utils.h" 6 7#include <string> 8 9#include "base/values.h" 10#include "chromeos/network/onc/onc_test_utils.h" 11#include "testing/gtest/include/gtest/gtest.h" 12 13namespace chromeos { 14namespace onc { 15 16TEST(ONCDecrypterTest, BrokenEncryptionIterations) { 17 scoped_ptr<base::DictionaryValue> encrypted_onc = 18 test_utils::ReadTestDictionary("broken-encrypted-iterations.onc"); 19 20 scoped_ptr<base::DictionaryValue> decrypted_onc = 21 Decrypt("test0000", *encrypted_onc); 22 23 EXPECT_EQ(NULL, decrypted_onc.get()); 24} 25 26TEST(ONCDecrypterTest, BrokenEncryptionZeroIterations) { 27 scoped_ptr<base::DictionaryValue> encrypted_onc = 28 test_utils::ReadTestDictionary("broken-encrypted-zero-iterations.onc"); 29 30 std::string error; 31 scoped_ptr<base::DictionaryValue> decrypted_onc = 32 Decrypt("test0000", *encrypted_onc); 33 34 EXPECT_EQ(NULL, decrypted_onc.get()); 35} 36 37TEST(ONCDecrypterTest, LoadEncryptedOnc) { 38 scoped_ptr<base::DictionaryValue> encrypted_onc = 39 test_utils::ReadTestDictionary("encrypted.onc"); 40 scoped_ptr<base::DictionaryValue> expected_decrypted_onc = 41 test_utils::ReadTestDictionary("decrypted.onc"); 42 43 std::string error; 44 scoped_ptr<base::DictionaryValue> actual_decrypted_onc = 45 Decrypt("test0000", *encrypted_onc); 46 47 base::DictionaryValue emptyDict; 48 EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc.get(), 49 actual_decrypted_onc.get())); 50} 51 52namespace { 53 54const char* kLoginId = "hans"; 55const char* kLoginEmail = "hans@my.domain.com"; 56 57class StringSubstitutionStub : public StringSubstitution { 58 public: 59 StringSubstitutionStub() {} 60 virtual bool GetSubstitute(std::string placeholder, 61 std::string* substitute) const OVERRIDE { 62 if (placeholder == substitutes::kLoginIDField) 63 *substitute = kLoginId; 64 else if (placeholder == substitutes::kEmailField) 65 *substitute = kLoginEmail; 66 else 67 return false; 68 return true; 69 } 70 private: 71 DISALLOW_COPY_AND_ASSIGN(StringSubstitutionStub); 72}; 73 74} // namespace 75 76TEST(ONCStringExpansion, OpenVPN) { 77 scoped_ptr<base::DictionaryValue> vpn_onc = 78 test_utils::ReadTestDictionary("valid_openvpn.onc"); 79 80 StringSubstitutionStub substitution; 81 ExpandStringsInOncObject(kNetworkConfigurationSignature, substitution, 82 vpn_onc.get()); 83 84 std::string actual_expanded; 85 vpn_onc->GetString("VPN.OpenVPN.Username", &actual_expanded); 86 EXPECT_EQ(actual_expanded, std::string("abc ") + kLoginEmail + " def"); 87} 88 89TEST(ONCStringExpansion, WiFi_EAP) { 90 scoped_ptr<base::DictionaryValue> wifi_onc = 91 test_utils::ReadTestDictionary("valid_wifi_clientcert.onc"); 92 93 StringSubstitutionStub substitution; 94 ExpandStringsInOncObject(kNetworkConfigurationSignature, substitution, 95 wifi_onc.get()); 96 97 std::string actual_expanded; 98 wifi_onc->GetString("WiFi.EAP.Identity", &actual_expanded); 99 EXPECT_EQ(actual_expanded, std::string("abc ") + kLoginId + "@my.domain.com"); 100} 101 102} // namespace onc 103} // namespace chromeos 104