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_signature.h" 11 #include "chromeos/network/onc/onc_test_utils.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace chromeos { 15 namespace onc { 16 17 TEST(ONCDecrypterTest, BrokenEncryptionIterations) { 18 scoped_ptr<base::DictionaryValue> encrypted_onc = 19 test_utils::ReadTestDictionary("broken-encrypted-iterations.onc"); 20 21 scoped_ptr<base::DictionaryValue> decrypted_onc = 22 Decrypt("test0000", *encrypted_onc); 23 24 EXPECT_EQ(NULL, decrypted_onc.get()); 25 } 26 27 TEST(ONCDecrypterTest, BrokenEncryptionZeroIterations) { 28 scoped_ptr<base::DictionaryValue> encrypted_onc = 29 test_utils::ReadTestDictionary("broken-encrypted-zero-iterations.onc"); 30 31 std::string error; 32 scoped_ptr<base::DictionaryValue> decrypted_onc = 33 Decrypt("test0000", *encrypted_onc); 34 35 EXPECT_EQ(NULL, decrypted_onc.get()); 36 } 37 38 TEST(ONCDecrypterTest, LoadEncryptedOnc) { 39 scoped_ptr<base::DictionaryValue> encrypted_onc = 40 test_utils::ReadTestDictionary("encrypted.onc"); 41 scoped_ptr<base::DictionaryValue> expected_decrypted_onc = 42 test_utils::ReadTestDictionary("decrypted.onc"); 43 44 std::string error; 45 scoped_ptr<base::DictionaryValue> actual_decrypted_onc = 46 Decrypt("test0000", *encrypted_onc); 47 48 base::DictionaryValue emptyDict; 49 EXPECT_TRUE(test_utils::Equals(expected_decrypted_onc.get(), 50 actual_decrypted_onc.get())); 51 } 52 53 namespace { 54 55 const char* kLoginId = "hans"; 56 const char* kLoginEmail = "hans (at) my.domain.com"; 57 58 class StringSubstitutionStub : public StringSubstitution { 59 public: 60 StringSubstitutionStub() {} 61 virtual bool GetSubstitute(const std::string& placeholder, 62 std::string* substitute) const OVERRIDE { 63 if (placeholder == substitutes::kLoginIDField) 64 *substitute = kLoginId; 65 else if (placeholder == substitutes::kEmailField) 66 *substitute = kLoginEmail; 67 else 68 return false; 69 return true; 70 } 71 private: 72 DISALLOW_COPY_AND_ASSIGN(StringSubstitutionStub); 73 }; 74 75 } // namespace 76 77 TEST(ONCStringExpansion, OpenVPN) { 78 scoped_ptr<base::DictionaryValue> vpn_onc = 79 test_utils::ReadTestDictionary("valid_openvpn.onc"); 80 81 StringSubstitutionStub substitution; 82 ExpandStringsInOncObject(kNetworkConfigurationSignature, substitution, 83 vpn_onc.get()); 84 85 std::string actual_expanded; 86 vpn_onc->GetString("VPN.OpenVPN.Username", &actual_expanded); 87 EXPECT_EQ(actual_expanded, std::string("abc ") + kLoginEmail + " def"); 88 } 89 90 TEST(ONCStringExpansion, WiFi_EAP) { 91 scoped_ptr<base::DictionaryValue> wifi_onc = 92 test_utils::ReadTestDictionary("wifi_clientcert_with_cert_pems.onc"); 93 94 StringSubstitutionStub substitution; 95 ExpandStringsInOncObject(kNetworkConfigurationSignature, substitution, 96 wifi_onc.get()); 97 98 std::string actual_expanded; 99 wifi_onc->GetString("WiFi.EAP.Identity", &actual_expanded); 100 EXPECT_EQ(actual_expanded, std::string("abc ") + kLoginId + "@my.domain.com"); 101 } 102 103 TEST(ONCResolveServerCertRefs, ResolveServerCertRefs) { 104 scoped_ptr<base::DictionaryValue> test_cases = 105 test_utils::ReadTestDictionary( 106 "network_configs_with_resolved_certs.json"); 107 108 CertPEMsByGUIDMap certs; 109 certs["cert_google"] = "pem_google"; 110 certs["cert_webkit"] = "pem_webkit"; 111 112 for (base::DictionaryValue::Iterator it(*test_cases); 113 !it.IsAtEnd(); it.Advance()) { 114 SCOPED_TRACE("Test case: " + it.key()); 115 116 const base::DictionaryValue* test_case = NULL; 117 it.value().GetAsDictionary(&test_case); 118 119 const base::ListValue* networks_with_cert_refs = NULL; 120 test_case->GetList("WithCertRefs", &networks_with_cert_refs); 121 122 const base::ListValue* expected_resolved_onc = NULL; 123 test_case->GetList("WithResolvedRefs", &expected_resolved_onc); 124 125 bool expected_success = (networks_with_cert_refs->GetSize() == 126 expected_resolved_onc->GetSize()); 127 128 scoped_ptr<base::ListValue> actual_resolved_onc( 129 networks_with_cert_refs->DeepCopy()); 130 131 bool success = ResolveServerCertRefsInNetworks(certs, 132 actual_resolved_onc.get()); 133 EXPECT_EQ(expected_success, success); 134 EXPECT_TRUE(test_utils::Equals(expected_resolved_onc, 135 actual_resolved_onc.get())); 136 } 137 } 138 139 } // namespace onc 140 } // namespace chromeos 141