1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <utility>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/hermes/hermes_clients.h"
#include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_profile_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/cellular_connection_handler.h"
#include "chromeos/ash/components/network/cellular_esim_installer.h"
#include "chromeos/ash/components/network/cellular_inhibitor.h"
#include "chromeos/ash/components/network/cellular_policy_handler.h"
#include "chromeos/ash/components/network/fake_network_connection_handler.h"
#include "chromeos/ash/components/network/managed_cellular_pref_handler.h"
#include "chromeos/ash/components/network/managed_network_configuration_handler_impl.h"
#include "chromeos/ash/components/network/mock_network_metadata_store.h"
#include "chromeos/ash/components/network/mock_network_state_handler.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_device_handler.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_metadata_store.h"
#include "chromeos/ash/components/network/network_policy_observer.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/policy_util.h"
#include "chromeos/ash/components/network/prohibited_technologies_handler.h"
#include "chromeos/ash/components/network/proxy/ui_proxy_config_service.h"
#include "chromeos/ash/components/network/shill_property_util.h"
#include "chromeos/ash/components/network/technology_state_controller.h"
#include "chromeos/ash/components/network/test_cellular_esim_profile_handler.h"
#include "chromeos/ash/components/network/text_message_suppression_state.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "chromeos/components/onc/onc_signature.h"
#include "chromeos/components/onc/onc_test_utils.h"
#include "chromeos/components/onc/onc_utils.h"
#include "chromeos/components/onc/onc_validator.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/onc/onc_pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace test_utils = ::chromeos::onc::test_utils;
using base::test::DictionaryHasValue;
using base::test::DictionaryHasValues;
namespace ash {
using testing::ElementsAre;
using testing::IsEmpty;
using testing::Optional;
using testing::Pointee;
using ::testing::Return;
namespace {
constexpr char kUser1[] = "user1";
constexpr char kUser1ProfilePath[] = "/profile/user1/shill";
// The GUID used by chromeos/components/test/data/onc/policy/*.{json,onc} files
// for a VPN.
constexpr char kTestGuidVpn[] = "{a3860e83-f03d-4cb1-bafa-b22c9e746950}";
// The GUID used by chromeos/components/test/data/onc/policy/*.{json,onc} files
// for a managed Wifi service.
constexpr char kTestGuidManagedWifi[] = "policy_wifi1";
// The GUID used by chromeos/components/test/data/onc/policy/policy_cellular.onc
// files for a managed Cellular service.
constexpr char kTestGuidManagedCellular[] = "policy_cellular";
// The GUID used by
// chromeos/components/test/data/onc/policy/policy_cellular_with_iccid.onc files
// for a managed Cellular service.
constexpr char kTestGuidManagedCellular2[] = "policy_cellular2";
// The GUID used by
// chromeos/components/test/data/onc/policy/policy_cellular_with_no_smdp.onc
// files for a managed Cellular service.
constexpr char kTestGuidManagedCellular3[] = "policy_cellular3";
// The GUID used by chromeos/components/test/data/onc/policy/*.{json,onc} files
// for an unmanaged Wifi service.
constexpr char kTestGuidUnmanagedWifi2[] = "wifi2";
// The GUID used by chromeos/components/test/data/onc/policy/*.{json,onc} files
// for a Wifi service.
constexpr char kTestGuidEthernetEap[] = "policy_ethernet_eap";
constexpr char kTestEuiccPath[] = "/org/chromium/Hermes/Euicc/0";
constexpr char kTestEid[] = "12345678901234567890123456789012";
constexpr char kTestCellularServicePath[] = "cellular_service_path";
constexpr char kTestCellularGuid[] = "cellular_guid";
// A valid but empty (no networks and no certificates) and unencrypted
// configuration.
constexpr char kEmptyUnencryptedConfiguration[] =
"{\"Type\":\"UnencryptedConfiguration\",\"NetworkConfigurations\":[],"
"\"Certificates\":[]}";
void ErrorCallback(const std::string& error_name) {
ADD_FAILURE() << "Unexpected error: " << error_name;
}
class TestNetworkPolicyObserver : public NetworkPolicyObserver {
public:
TestNetworkPolicyObserver() = default;
TestNetworkPolicyObserver(const TestNetworkPolicyObserver&) = delete;
TestNetworkPolicyObserver& operator=(const TestNetworkPolicyObserver&) =
delete;
void PoliciesApplied(const std::string& userhash) override {
policies_applied_count_++;
}
void PoliciesChanged(const std::string& userhash) override {
if (userhash.empty() && on_shared_profile_policies_changed_) {
std::move(on_shared_profile_policies_changed_).Run();
}
}
int GetPoliciesAppliedCountAndReset() {
int count = policies_applied_count_;
policies_applied_count_ = 0;
return count;
}
void RunOnSharedProfilePoliciesChanged(base::OnceClosure action) {
on_shared_profile_policies_changed_ = std::move(action);
}
private:
int policies_applied_count_ = 0;
base::OnceClosure on_shared_profile_policies_changed_;
};
} // namespace
class ManagedNetworkConfigurationHandlerTest : public testing::Test {
public:
ManagedNetworkConfigurationHandlerTest() = default;
ManagedNetworkConfigurationHandlerTest(
const ManagedNetworkConfigurationHandlerTest&) = delete;
ManagedNetworkConfigurationHandlerTest& operator=(
const ManagedNetworkConfigurationHandlerTest&) = delete;
~ManagedNetworkConfigurationHandlerTest() override = default;
// testing::Test:
void SetUp() override {
LoginState::Initialize();
shill_clients::InitializeFakes();
hermes_clients::InitializeFakes();
ShillManagerClient::Get()
->GetTestInterface()
->SetWifiServicesVisibleByDefault(false);
network_state_handler_ = MockNetworkStateHandler::InitializeForTest();
network_device_handler_ = NetworkDeviceHandler::InitializeForTesting(
network_state_handler_.get());
network_profile_handler_ = NetworkProfileHandler::InitializeForTesting();
technology_state_controller_ =
std::make_unique<TechnologyStateController>();
technology_state_controller_->Init(network_state_handler_.get());
network_configuration_handler_ =
NetworkConfigurationHandler::InitializeForTest(
network_state_handler_.get(), network_device_handler_.get());
network_connection_handler_ =
std::make_unique<FakeNetworkConnectionHandler>();
cellular_inhibitor_ = std::make_unique<CellularInhibitor>();
cellular_inhibitor_->Init(network_state_handler_.get(),
network_device_handler_.get());
cellular_esim_profile_handler_ =
std::make_unique<TestCellularESimProfileHandler>();
cellular_esim_profile_handler_->Init(network_state_handler_.get(),
cellular_inhibitor_.get());
cellular_connection_handler_ =
std::make_unique<CellularConnectionHandler>();
cellular_connection_handler_->Init(network_state_handler_.get(),
cellular_inhibitor_.get(),
cellular_esim_profile_handler_.get());
cellular_esim_installer_ = std::make_unique<CellularESimInstaller>();
// TODO(crbug.com/1248229): Create fake cellular esim installer for test
// setup.
cellular_esim_installer_->Init(
cellular_connection_handler_.get(), cellular_inhibitor_.get(),
network_connection_handler_.get(), network_profile_handler_.get(),
network_state_handler_.get());
cellular_policy_handler_ = std::make_unique<CellularPolicyHandler>();
// ProhibitedTechnologiesHandler's ctor is private.
prohibited_technologies_handler_.reset(new ProhibitedTechnologiesHandler);
managed_cellular_pref_handler_ =
std::make_unique<ManagedCellularPrefHandler>();
managed_cellular_pref_handler_->Init(network_state_handler_.get());
ManagedCellularPrefHandler::RegisterLocalStatePrefs(
device_prefs_.registry());
managed_cellular_pref_handler_->SetDevicePrefs(&device_prefs_);
// ManagedNetworkConfigurationHandlerImpl's ctor is private.
managed_network_configuration_handler_.reset(
new ManagedNetworkConfigurationHandlerImpl());
network_metadata_store_ =
base::WrapUnique(new testing::NiceMock<MockNetworkMetadataStore>());
managed_network_configuration_handler_
->set_network_metadata_store_for_testing(network_metadata_store_.get());
PrefProxyConfigTrackerImpl::RegisterProfilePrefs(user_prefs_.registry());
PrefProxyConfigTrackerImpl::RegisterPrefs(local_state_.registry());
::onc::RegisterProfilePrefs(user_prefs_.registry());
::onc::RegisterPrefs(local_state_.registry());
ui_proxy_config_service_ = std::make_unique<UIProxyConfigService>(
&user_prefs_, &local_state_, network_state_handler_.get(),
network_profile_handler_.get());
network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
NetworkHandler* network_handler = NetworkHandler::Get();
managed_network_configuration_handler_->Init(
cellular_policy_handler_.get(), managed_cellular_pref_handler_.get(),
network_state_handler_.get(), network_profile_handler_.get(),
network_configuration_handler_.get(), network_device_handler_.get(),
prohibited_technologies_handler_.get(),
network_handler->hotspot_controller());
managed_network_configuration_handler_->set_ui_proxy_config_service(
ui_proxy_config_service_.get());
managed_network_configuration_handler_->set_user_prefs(&user_prefs_);
managed_network_configuration_handler_->AddObserver(&policy_observer_);
cellular_policy_handler_->Init(
cellular_esim_profile_handler_.get(), cellular_esim_installer_.get(),
cellular_inhibitor_.get(), network_profile_handler_.get(),
network_state_handler_.get(), managed_cellular_pref_handler_.get(),
managed_network_configuration_handler_.get());
prohibited_technologies_handler_->Init(
managed_network_configuration_handler_.get(),
network_state_handler_.get(), technology_state_controller_.get());
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
// Run remaining tasks.
base::RunLoop().RunUntilIdle();
ResetManagedNetworkConfigurationHandler();
network_handler_test_helper_.reset();
cellular_policy_handler_.reset();
cellular_esim_installer_.reset();
cellular_esim_profile_handler_.reset();
cellular_connection_handler_.reset();
cellular_inhibitor_.reset();
managed_cellular_pref_handler_.reset();
network_configuration_handler_.reset();
ui_proxy_config_service_.reset();
technology_state_controller_.reset();
network_profile_handler_.reset();
network_device_handler_.reset();
network_state_handler_.reset();
network_connection_handler_.reset();
hermes_clients::Shutdown();
shill_clients::Shutdown();
LoginState::Shutdown();
}
TestNetworkPolicyObserver* policy_observer() { return &policy_observer_; }
ManagedNetworkConfigurationHandler* managed_handler() {
return managed_network_configuration_handler_.get();
}
ShillServiceClient::TestInterface* GetShillServiceClient() {
return ShillServiceClient::Get()->GetTestInterface();
}
ShillProfileClient::TestInterface* GetShillProfileClient() {
return ShillProfileClient::Get()->GetTestInterface();
}
void InitializeStandardProfiles() {
GetShillProfileClient()->AddProfile(kUser1ProfilePath, kUser1);
GetShillProfileClient()->AddProfile(
NetworkProfileHandler::GetSharedProfilePath(),
std::string() /* no userhash */);
}
void InitializeEuicc() {
HermesManagerClient::Get()->GetTestInterface()->ClearEuiccs();
HermesManagerClient::Get()->GetTestInterface()->AddEuicc(
dbus::ObjectPath(kTestEuiccPath), kTestEid, /*is_active=*/true,
/*physical_slot=*/0);
cellular_esim_profile_handler_->SetHasRefreshedProfilesForEuicc(
kTestEid, dbus::ObjectPath(kTestEuiccPath), /*has_refreshed=*/true);
base::RunLoop().RunUntilIdle();
}
bool SetPolicy(::onc::ONCSource onc_source,
const std::string& userhash,
const std::string& path_to_onc) {
if (path_to_onc.empty()) {
std::optional<base::Value::Dict> policy =
chromeos::onc::ReadDictionaryFromJson(kEmptyUnencryptedConfiguration);
if (!policy.has_value()) {
return false;
}
return SetPolicy(onc_source, userhash, std::move(policy.value()));
}
base::Value::Dict policy_value =
test_utils::ReadTestDictionary(path_to_onc);
return SetPolicy(onc_source, userhash, std::move(policy_value));
}
bool SetPolicy(::onc::ONCSource onc_source,
const std::string& userhash,
base::Value::Dict policy) {
chromeos::onc::Validator validator(/*error_on_unknown_field=*/true,
/*error_on_wrong_recommended=*/true,
/*error_on_missing_field=*/false,
/*managed_onc=*/true,
/*log_warnings=*/true);
validator.SetOncSource(onc_source);
chromeos::onc::Validator::Result validation_result;
std::optional<base::Value::Dict> validated_policy =
validator.ValidateAndRepairObject(
&chromeos::onc::kToplevelConfigurationSignature, policy,
&validation_result);
if (validation_result == chromeos::onc::Validator::INVALID) {
ADD_FAILURE() << "Network configuration invalid.";
return false;
}
base::Value::List network_configs;
const base::Value::List* found_network_configs = validated_policy->FindList(
::onc::toplevel_config::kNetworkConfigurations);
if (found_network_configs) {
for (const auto& network_config : *found_network_configs) {
network_configs.Append(network_config.Clone());
}
}
base::Value::Dict global_config;
const base::Value::Dict* found_global_config = validated_policy->FindDict(
::onc::toplevel_config::kGlobalNetworkConfiguration);
if (found_global_config) {
global_config = found_global_config->Clone();
}
managed_network_configuration_handler_->SetPolicy(
onc_source, userhash, network_configs, global_config);
return true;
}
void SetUpEntry(const std::string& path_to_shill_json,
const std::string& profile_path,
const std::string& entry_path) {
base::Value::Dict entry =
test_utils::ReadTestDictionary(path_to_shill_json);
GetShillProfileClient()->AddEntry(profile_path, entry_path, entry);
}
void ResetManagedNetworkConfigurationHandler() {
if (!managed_network_configuration_handler_)
return;
prohibited_technologies_handler_.reset();
managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
managed_network_configuration_handler_.reset();
}
NetworkHandlerTestHelper* network_handler_test_helper() {
return network_handler_test_helper_.get();
}
bool PropertiesMatch(const base::Value::Dict& v1,
const base::Value::Dict& v2) {
if (v1 == v2)
return true;
// EXPECT_EQ does not recursively log dictionaries, so use LOG instead.
LOG(ERROR) << "v1=" << v1;
LOG(ERROR) << "v2=" << v2;
return false;
}
void FastForwardProfileRefreshDelay() {
const base::TimeDelta kProfileRefreshCallbackDelay =
base::Milliseconds(150);
// Connect can result in two profile refresh calls before and after
// enabling profile. Fast forward by delay after refresh.
task_environment_.FastForwardBy(2 * kProfileRefreshCallbackDelay);
}
void FastForwardAutoConnectWaiting() {
task_environment_.FastForwardBy(
CellularConnectionHandler::kWaitingForAutoConnectTimeout);
}
void SetArcAlwaysOnUserPrefs(std::string package_name,
bool vpn_configured_allowed = false) {
user_prefs_.SetUserPref(arc::prefs::kAlwaysOnVpnPackage,
base::Value(package_name));
user_prefs_.SetUserPref(prefs::kVpnConfigAllowed,
base::Value(vpn_configured_allowed));
}
ProhibitedTechnologiesHandler* prohibited_technologies_handler() {
return prohibited_technologies_handler_.get();
}
void ConfigureCellularService(const std::string& service_path,
const std::string& type) {
base::Value::Dict properties;
shill_property_util::SetSSID(service_path, &properties);
properties.Set(shill::kNameProperty, service_path);
properties.Set(shill::kGuidProperty, kTestCellularGuid);
properties.Set(shill::kTypeProperty, type);
properties.Set(shill::kStateProperty, shill::kStateIdle);
properties.Set(shill::kProfileProperty,
NetworkProfileHandler::GetSharedProfilePath());
network_configuration_handler_->CreateShillConfiguration(
std::move(properties), base::DoNothing(),
base::BindOnce(&ErrorCallback));
base::RunLoop().RunUntilIdle();
}
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
base::test::ScopedFeatureList feature_list_;
TestNetworkPolicyObserver policy_observer_;
std::unique_ptr<MockNetworkStateHandler> network_state_handler_;
std::unique_ptr<TechnologyStateController> technology_state_controller_;
std::unique_ptr<NetworkProfileHandler> network_profile_handler_;
std::unique_ptr<NetworkConfigurationHandler> network_configuration_handler_;
std::unique_ptr<UIProxyConfigService> ui_proxy_config_service_;
std::unique_ptr<ManagedCellularPrefHandler> managed_cellular_pref_handler_;
std::unique_ptr<ManagedNetworkConfigurationHandlerImpl>
managed_network_configuration_handler_;
std::unique_ptr<NetworkDeviceHandler> network_device_handler_;
std::unique_ptr<CellularConnectionHandler> cellular_connection_handler_;
std::unique_ptr<CellularInhibitor> cellular_inhibitor_;
std::unique_ptr<TestCellularESimProfileHandler>
cellular_esim_profile_handler_;
std::unique_ptr<FakeNetworkConnectionHandler> network_connection_handler_;
std::unique_ptr<CellularESimInstaller> cellular_esim_installer_;
std::unique_ptr<CellularPolicyHandler> cellular_policy_handler_;
std::unique_ptr<ProhibitedTechnologiesHandler>
prohibited_technologies_handler_;
std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
std::unique_ptr<MockNetworkMetadataStore> network_metadata_store_;
sync_preferences::TestingPrefServiceSyncable user_prefs_;
TestingPrefServiceSimple local_state_, device_prefs_;
};
TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
InitializeStandardProfiles();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unconfigured_wifi1.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_with_redundant_fields.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
// A network policy uses a variable expansion which is set after the policy has
// been initially applied.
TEST_F(ManagedNetworkConfigurationHandlerTest, VariableSetAfterPolicy) {
InitializeStandardProfiles();
// Initial policy application.
const char* const onc_policy = R"(
{
"NetworkConfigurations": [
{
"GUID": "policy_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"Recommended": [ "AutoConnect"],
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Identity": "${LOGIN_ID}",
"Recommended": [
"AnonymousIdentity",
]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
// Expect that the variable has not been resolved because it didn't have a
// value.
{
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
const std::string* identity =
properties->FindString(shill::kEapIdentityProperty);
ASSERT_TRUE(identity);
EXPECT_EQ(*identity, "${LOGIN_ID}");
}
// Set a value for the variable.
managed_handler()->SetProfileWideVariableExpansions(
kUser1, {{"LOGIN_ID", "VarValue"}});
// Expect that a policy re-application happens and the variable gets resolved.
EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
{
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
const std::string* identity =
properties->FindString(shill::kEapIdentityProperty);
ASSERT_TRUE(identity);
EXPECT_EQ(*identity, "VarValue");
}
}
// A network policy uses a variable expansion which is set before the policy has
// been initially applied.
TEST_F(ManagedNetworkConfigurationHandlerTest, VariableSetBeforePolicy) {
InitializeStandardProfiles();
// Set a value for the variable.
managed_handler()->SetProfileWideVariableExpansions(
kUser1, {{"LOGIN_ID", "VarValue"}});
// Initial policy application.
const char* const onc_policy = R"(
{
"NetworkConfigurations": [
{
"GUID": "policy_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"Recommended": [ "AutoConnect"],
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Identity": "${LOGIN_ID}",
"Recommended": [
"AnonymousIdentity",
]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
// Expect that the variable has been resolved.
{
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
const std::string* identity =
properties->FindString(shill::kEapIdentityProperty);
ASSERT_TRUE(identity);
EXPECT_EQ(*identity, "VarValue");
}
}
// A variable expansion is changed which does not affect any network.
TEST_F(ManagedNetworkConfigurationHandlerTest, VariableDoesNotAffectPolicy) {
InitializeStandardProfiles();
// Initial policy application.
const char* const onc_policy = R"(
{
"NetworkConfigurations": [
{
"GUID": "policy_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"Recommended": [ "AutoConnect"],
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Identity": "no_variable",
"Recommended": [
"AnonymousIdentity",
]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
// Set a value for a variable which is not referenced by any network.
managed_handler()->SetProfileWideVariableExpansions(
kUser1, {{"LOGIN_ID", "VarValue"}});
// No policy re-application should be in progress.
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyProhibitedTechnology) {
const char* const empty =
"policy/policy_empty_global_network_configuration.onc";
const char* const prohibit_wifi =
"policy/policy_global_network_configuration_prohibit_wifi.onc";
// Technologies prohibited by policy are only enforced if the user policy has
// been applied and we are in an active user session.
LoginState::Get()->SetLoggedInState(
LoginState::LoggedInState::LOGGED_IN_ACTIVE,
LoginState::LoggedInUserType::LOGGED_IN_USER_REGULAR);
prohibited_technologies_handler()->PoliciesApplied(kUser1);
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(), empty));
base::RunLoop().RunUntilIdle();
EXPECT_THAT(
prohibited_technologies_handler()->GetCurrentlyProhibitedTechnologies(),
IsEmpty());
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(), prohibit_wifi));
base::RunLoop().RunUntilIdle();
EXPECT_THAT(
prohibited_technologies_handler()->GetCurrentlyProhibitedTechnologies(),
ElementsAre(shill::kTypeWifi));
// Not explicitly prohibiting any technology should result in all
// technologies being explicitly allowed.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(), empty));
base::RunLoop().RunUntilIdle();
EXPECT_THAT(
prohibited_technologies_handler()->GetCurrentlyProhibitedTechnologies(),
IsEmpty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, ModifyCustomApns) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(/*enabled_features=*/
{features::kApnRevamp,
features::kAllowApnModificationPolicy},
/*disabled_features=*/{});
ConfigureCellularService(kTestCellularServicePath, shill::kTypeCellular);
auto custom_apn_list = base::Value::List().Append(
base::Value::Dict()
.Set(::onc::cellular_apn::kAccessPointName, "apn1")
.Set(::onc::cellular_apn::kState, ::onc::cellular_apn::kStateEnabled)
.Set(::onc::cellular_apn::kApnTypes,
base::Value::List().Append(
::onc::cellular_apn::kApnTypeDefault)));
EXPECT_CALL(*(network_metadata_store_.get()),
GetCustomApnList(kTestCellularGuid))
.WillRepeatedly(Return(&custom_apn_list));
// Set 'AllowApnModification' policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_apn_modification.onc"));
base::RunLoop().RunUntilIdle();
std::optional<base::Value::List> shill_custom_apns =
network_handler_test_helper()->GetServiceListProperty(
kTestCellularServicePath, shill::kCellularCustomApnListProperty);
ASSERT_FALSE(shill_custom_apns.has_value());
EXPECT_TRUE(SetPolicy(
::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"managed_cellular_no_recommended_allow_apn_modification_true.onc"));
base::RunLoop().RunUntilIdle();
shill_custom_apns = network_handler_test_helper()->GetServiceListProperty(
"service_path_for_cellular_guid", shill::kCellularCustomApnListProperty);
ASSERT_TRUE(shill_custom_apns.has_value());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManagedCellular) {
InitializeStandardProfiles();
InitializeEuicc();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unconfigured_cellular.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_cellular.onc"));
FastForwardProfileRefreshDelay();
FastForwardAutoConnectWaiting();
base::RunLoop().RunUntilIdle();
std::string service_path = GetShillServiceClient()->FindServiceMatchingGUID(
kTestGuidManagedCellular);
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
EXPECT_THAT(*properties, DictionaryHasValues(expected_shill_properties));
const std::string* iccid = properties->FindString(shill::kIccidProperty);
ASSERT_TRUE(iccid);
EXPECT_TRUE(managed_cellular_pref_handler_->GetESimMetadata(*iccid));
// Verify that applying a new cellular policy with same ICCID should update
// the old shill configuration.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_cellular_with_iccid.onc"));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(std::string(), GetShillServiceClient()->FindServiceMatchingGUID(
kTestGuidManagedCellular));
service_path = GetShillServiceClient()->FindServiceMatchingGUID(
kTestGuidManagedCellular2);
const base::Value::Dict* properties2 =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties2);
std::optional<bool> auto_connect =
properties2->FindBool(shill::kAutoConnectProperty);
ASSERT_TRUE(*auto_connect);
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyIgnoreNoSmdpManagedCellular) {
InitializeStandardProfiles();
InitializeEuicc();
// Verify that applying managed eSIM policy with no SMDP address in the ONC
// should not create a new shill configuration for it.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_cellular_with_no_smdp.onc"));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
std::string service_path = GetShillServiceClient()->FindServiceMatchingGUID(
kTestGuidManagedCellular3);
ASSERT_EQ(service_path, std::string());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnconfigured) {
InitializeStandardProfiles();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unconfigured_wifi1.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsWiFi) {
InitializeStandardProfiles();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_autoconnect_on_unconfigured_wifi1.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_autoconnect.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsVPN) {
InitializeStandardProfiles();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_vpn_autoconnect.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidVpn);
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
EXPECT_EQ(expected_shill_properties, *properties);
}
// Ensure that EAP settings for ethernet are matched with the right profile
// entry and written to the dedicated EthernetEAP service.
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyManageUnmanagedEthernetEAP) {
InitializeStandardProfiles();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/"
"shill_policy_on_unmanaged_ethernet_eap.json");
GetShillServiceClient()->AddService(
"eth_entry", std::string() /* guid */, std::string() /* name */,
"etherneteap", std::string() /* state */, true /* visible */);
GetShillProfileClient()->AddService(kUser1ProfilePath, "eth_entry");
SetUpEntry("policy/shill_unmanaged_ethernet_eap.json", kUser1ProfilePath,
"eth_entry");
// Also setup an unrelated WiFi configuration to verify that the right entry
// is matched.
GetShillServiceClient()->AddService(
"wifi_entry", std::string() /* guid */, "wifi1", shill::kTypeWifi,
std::string() /* state */, true /* visible */);
SetUpEntry("policy/shill_unmanaged_wifi1.json", kUser1ProfilePath,
"wifi_entry");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_ethernet_eap.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidEthernetEap);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmodified) {
InitializeStandardProfiles();
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json", kUser1ProfilePath,
"some_entry_path");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, PolicyApplicationRunning) {
InitializeStandardProfiles();
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
managed_handler()->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
/*userhash=*/std::string(),
/*network_configs_onc=*/base::Value::List(),
/*global_network_config=*/base::Value::Dict());
EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json", kUser1ProfilePath,
"some_entry_path");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_update.onc"));
EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyAfterFinished) {
InitializeStandardProfiles();
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json", kUser1ProfilePath,
"some_entry_path");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_update.onc"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyBeforeFinished) {
InitializeStandardProfiles();
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
// Usually the first call will cause a profile entry to be created, which we
// don't fake here.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_update.onc"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
}
// Regression test for b/240237232: A shill profile disappears before triggering
// policy application and the actual policy application run.
TEST_F(ManagedNetworkConfigurationHandlerTest,
ProfileDisappearsAfterPolicySet) {
InitializeStandardProfiles();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
// Pretend that NetworkProfileHandler doesn't know the network profile
// anymore.
network_profile_handler_->OnPropertyChanged(
shill::kProfilesProperty, base::Value(base::Value::Type::LIST));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, policy_observer()->GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json", kUser1ProfilePath,
"old_entry_path");
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unmanaged_wifi1.json");
// Before setting policy, old_entry_path should exist.
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Verify old_entry_path is deleted.
EXPECT_FALSE(GetShillProfileClient()->HasService("old_entry_path"));
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedNewGUID) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_managed_wifi1.json", kUser1ProfilePath,
"old_entry_path");
// Note that this test case expects that the UIData user settings are copied
// to the entry with the new GUID.
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unmanaged_wifi1.json");
// The passphrase isn't sent again, because it's configured by the user and
// Shill doesn't send it on GetProperties calls.
expected_shill_properties.Remove(shill::kPassphraseProperty);
expected_shill_properties.Remove(shill::kPassphraseRequiredProperty);
// Before setting policy, old_entry_path should exist.
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Verify old_entry_path is deleted.
EXPECT_FALSE(GetShillProfileClient()->HasService("old_entry_path"));
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUpdateManagedVPN) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_vpn.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidVpn);
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
base::Value::Dict expected_shill_properties =
test_utils::ReadTestDictionary("policy/shill_policy_on_managed_vpn.json");
EXPECT_EQ(expected_shill_properties, *properties);
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyUpdateManagedVPNOpenVPNPlusUi) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
// Apply a policy that does not provide an authentication type.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_vpn_no_auth.onc"));
base::RunLoop().RunUntilIdle();
// Apply additional configuration (e.g. from the UI). This includes password
// and OTP which should be allowed when authentication type is not explicitly
// set. See https://crbug.com/817617 for details.
const NetworkState* network_state =
network_state_handler_->GetNetworkStateFromGuid(kTestGuidVpn);
ASSERT_TRUE(network_state);
base::Value::Dict ui_config =
test_utils::ReadTestDictionary("policy/policy_vpn_ui.json");
managed_network_configuration_handler_->SetProperties(
network_state->path(), ui_config, base::DoNothing(),
base::BindOnce(&ErrorCallback));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidVpn);
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_managed_vpn_plus_ui.json");
EXPECT_EQ(expected_shill_properties, *properties);
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyUpdateManagedVPNL2TPIPsecPlusUi) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_managed_vpn_ipsec.json", kUser1ProfilePath,
"entry_path");
// Apply the VPN L2TP-IPsec policy that will be updated.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_vpn_ipsec.onc"));
base::RunLoop().RunUntilIdle();
// Update the VPN L2TP-IPsec policy.
const NetworkState* network_state =
network_state_handler_->GetNetworkStateFromGuid(kTestGuidVpn);
ASSERT_TRUE(network_state);
base::Value::Dict ui_config =
test_utils::ReadTestDictionary("policy/policy_vpn_ipsec_ui.json");
managed_network_configuration_handler_->SetProperties(
network_state->path(), ui_config, base::DoNothing(),
base::BindOnce(&ErrorCallback));
base::RunLoop().RunUntilIdle();
// Get shill service properties after the update.
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidVpn);
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_managed_vpn_ipsec_plus_ui.json");
EXPECT_EQ(expected_shill_properties, *properties);
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyUpdateManagedVPNNoUserAuthType) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_managed_vpn.json", kUser1ProfilePath, "entry_path");
base::Value::Dict expected_shill_properties =
test_utils::ReadTestDictionary("policy/shill_policy_on_managed_vpn.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_vpn_no_user_auth_type.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidVpn);
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
EXPECT_EQ(expected_shill_properties, *properties);
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyReapplyToManaged) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json", kUser1ProfilePath,
"old_entry_path");
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unmanaged_wifi1.json");
// The passphrase isn't sent again, because it's configured by the user and
// Shill doesn't send it on GetProperties calls.
expected_shill_properties.Remove(shill::kPassphraseProperty);
expected_shill_properties.Remove(shill::kPassphraseRequiredProperty);
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
{
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
// If we apply the policy again, without change, then the Shill profile will
// not be modified.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
{
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyUnmanageManaged) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json", kUser1ProfilePath,
"old_entry_path");
// Before setting policy, old_entry_path should exist.
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
std::string() /* path_to_onc */));
base::RunLoop().RunUntilIdle();
// Verify old_entry_path is deleted.
EXPECT_FALSE(GetShillProfileClient()->HasService("old_entry_path"));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetEmptyPolicyIgnoreUnmanaged) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json", kUser1ProfilePath,
"old_entry_path");
// Before setting policy, old_entry_path should exist.
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
std::string() /* path_to_onc */));
base::RunLoop().RunUntilIdle();
// Verify old_entry_path is kept.
EXPECT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_EQ(1, policy_observer()->GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi2.json", kUser1ProfilePath,
"wifi2_entry_path");
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unconfigured_wifi1.json");
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
// Regression test for b/237657704.
// Profile entries that don't have a "Profile" property don't break application
// of new policy-provided networks.
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicyIgnoreNetworkWithoutProfile) {
InitializeStandardProfiles();
// This shill entry is missing the "Profile" property.
// It has a "wifi2" SSID.
base::Value::Dict wifi_without_profile_property =
base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "wifi2",
"Mode": "managed",
"Passphrase": "user's passphrase",
"PassphraseRequired": false,
"SecurityClass": "psk",
"Type": "wifi",
"WiFi.HexSSID": "7769666932"
})");
GetShillProfileClient()->AddEntry(kUser1ProfilePath,
"wifi_without_profile_prop_entry_path",
std::move(wifi_without_profile_property));
// Apply a policy which:
// - Disallows unmanaged networks (such as wifi2 above) to auto-connect
// This will trigger policy_applicator.cc to try to modify wifi2
// - Apply a new network (policy_wifi1).
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"AllowOnlyPolicyNetworksToAutoconnect": true
},
"NetworkConfigurations": [
{
"GUID": "policy_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"Passphrase": "policy's passphrase",
"SSID": "wifi1",
"Security": "WPA-PSK"
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// Expect that "policy_wifi1" policy has been applied by checking that the
// GUID exists and it has properties from the above policy.
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID("policy_wifi1");
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(
GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(
base::Value::Dict()
.Set(shill::kWifiHexSsid, "7769666931")
.Set(shill::kPassphraseProperty, "policy's passphrase"))));
}
// There is a policy with a Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is not enabled.
// Tests that initial policy application does not reset "Recommended" fields,
// even when `TriggerEphemeralNetworkConfigActions` is called.
TEST_F(ManagedNetworkConfigurationHandlerTest,
ResetRecommendedFields_Disabled_Initial) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
const std::string kOriginalEntryPath = "orig_entry_path";
base::Value::Dict original_wifi_config = base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "guid_wifi1",
"Mode": "managed",
"EAP.EAP": "PEAP",
"EAP.Identity": "user_identity",
"EAP.Password": "user_password",
"Profile": "/profile/default",
"SecurityClass": "802_1x",
"SaveCredentials": true,
"Type": "wifi",
"WiFi.HexSSID": "7769666931",
"UIData": "{\"onc_source\":\"device_policy\"}"
})");
GetShillProfileClient()->AddEntry(
NetworkProfileHandler::GetSharedProfilePath(), kOriginalEntryPath,
std::move(original_wifi_config));
// Call TriggerEphemeralNetworkConfigActions when policies are available
// In production code, EphemeralNetworkConfigHandler will do this.
policy_observer_.RunOnSharedProfilePoliciesChanged(base::BindOnce(
&ManagedNetworkConfigurationHandlerImpl::
TriggerEphemeralNetworkConfigActions,
base::Unretained(managed_network_configuration_handler_.get())));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
},
"NetworkConfigurations": [
{
"GUID": "guid_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Recommended": ["Identity", "Password"]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry still exists and has kept the user-provided Passphrase.
std::string profile_path;
EXPECT_THAT(
GetShillProfileClient()->GetService(kOriginalEntryPath, &profile_path),
Optional(DictionaryHasValue(shill::kEapPasswordProperty,
base::Value("user_password"))));
}
// There is a policy with a Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is enabled.
// Tests that initial policy application resets "Recommended" fields by
// re-creating the configuration.
TEST_F(ManagedNetworkConfigurationHandlerTest,
ResetRecommendedFields_Enabled_Initial) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
const std::string kOriginalEntryPath = "orig_entry_path";
base::Value::Dict original_wifi_config = base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "guid_wifi1",
"Mode": "managed",
"EAP.EAP": "PEAP",
"EAP.Identity": "user_identity",
"EAP.Password": "user_password",
"Profile": "/profile/default",
"SecurityClass": "802_1x",
"SaveCredentials": true,
"Type": "wifi",
"WiFi.HexSSID": "7769666931",
"UIData": "{\"onc_source\":\"device_policy\"}"
})");
GetShillProfileClient()->AddEntry(
NetworkProfileHandler::GetSharedProfilePath(), kOriginalEntryPath,
std::move(original_wifi_config));
// Call TriggerEphemeralNetworkConfigActions when policies are available
// In production code, EphemeralNetworkConfigHandler will do this.
policy_observer_.RunOnSharedProfilePoliciesChanged(base::BindOnce(
&ManagedNetworkConfigurationHandlerImpl::
TriggerEphemeralNetworkConfigActions,
base::Unretained(managed_network_configuration_handler_.get())));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"RecommendedValuesAreEphemeral": true
},
"NetworkConfigurations": [
{
"GUID": "guid_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Recommended": ["Identity", "Password"]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The original entry has been wiped.
EXPECT_FALSE(GetShillProfileClient()->HasService(kOriginalEntryPath));
// A new one has been created.
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID("guid_wifi1");
ASSERT_FALSE(service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(service_path);
ASSERT_TRUE(properties);
EXPECT_THAT(properties->FindString(shill::kEapPasswordProperty),
testing::IsNull());
}
// There is a policy with a Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is enabled.
// Tests that a `TriggerEphemeralNetworkConfigActions` call triggered after the
// initial policy application leads to clearing of the Recommended fields by
// re-creating the configuration.
TEST_F(ManagedNetworkConfigurationHandlerTest,
ResetRecommendedFields_Enabled_AfterInitialApplication) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
const std::string kOncWifiGuid = "guid_wifi1";
const std::string kTestPassword = "test_password";
InitializeStandardProfiles();
const std::string onc_policy = base::StringPrintf(R"(
{
"GlobalNetworkConfiguration": {
"RecommendedValuesAreEphemeral": true
},
"NetworkConfigurations": [
{
"GUID": "%s",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Recommended": ["Identity", "Password"]
}
}
}
],
"Type": "UnencryptedConfiguration"
})",
kOncWifiGuid.c_str());
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// Set a recommended field.
std::string initial_service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kOncWifiGuid);
EXPECT_TRUE(GetShillServiceClient()->SetServiceProperty(
initial_service_path, shill::kEapPasswordProperty,
base::Value(kTestPassword)));
managed_network_configuration_handler_
->TriggerEphemeralNetworkConfigActions();
base::RunLoop().RunUntilIdle();
// The config does not have the recommended field value anymore.
std::string new_service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kOncWifiGuid);
{
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(new_service_path);
ASSERT_TRUE(properties);
EXPECT_THAT(properties->FindString(shill::kEapPasswordProperty),
testing::IsNull());
}
// Set a recommended field again.
EXPECT_TRUE(GetShillServiceClient()->SetServiceProperty(
new_service_path, shill::kEapPasswordProperty,
base::Value(kTestPassword)));
// Re-apply policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The re-application of policy (without TriggerEphemeralNetworkConfigActions)
// did not wipe the recommended field or re-create the entry.
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(new_service_path),
Pointee(DictionaryHasValue(shill::kEapPasswordProperty,
base::Value(kTestPassword))));
}
// There is a policy with a Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is enabled.
// The feature flags/policies guarding it are however disabled.
TEST_F(ManagedNetworkConfigurationHandlerTest,
ResetRecommendedFields_Enabled_FeatureOff) {
InitializeStandardProfiles();
const std::string kOriginalEntryPath = "orig_entry_path";
base::Value::Dict original_wifi_config = base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "guid_wifi1",
"Mode": "managed",
"EAP.EAP": "PEAP",
"EAP.Identity": "user_identity",
"EAP.Password": "user_password",
"Profile": "/profile/default",
"SecurityClass": "802_1x",
"SaveCredentials": true,
"Type": "wifi",
"WiFi.HexSSID": "7769666931",
"UIData": "{\"onc_source\":\"device_policy\"}"
})");
GetShillProfileClient()->AddEntry(
NetworkProfileHandler::GetSharedProfilePath(), kOriginalEntryPath,
std::move(original_wifi_config));
// Don't call TriggerEphemeralNetworkConfigActions - it will only be called in
// production code if the feature is enabled.
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"RecommendedValuesAreEphemeral": true
},
"NetworkConfigurations": [
{
"GUID": "guid_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Recommended": ["Identity", "Password"]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry still exists and has kept the user-provided Passphrase.
std::string profile_path;
EXPECT_THAT(
GetShillProfileClient()->GetService(kOriginalEntryPath, &profile_path),
Optional(DictionaryHasValue(shill::kEapPasswordProperty,
base::Value("user_password"))));
}
// There is a policy with no Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is enabled.
// Tests that initial policy application does not attempt to re-create the
// configuration (because no field in there is Recommended).
TEST_F(ManagedNetworkConfigurationHandlerTest,
ResetRecommendedFields_Enabled_NoFieldRecommended_Initial) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
const std::string kOriginalEntryPath = "orig_entry_path";
base::Value::Dict original_wifi_config = base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "guid_wifi1",
"Mode": "managed",
"EAP.EAP": "PEAP",
"EAP.Identity": "user_identity",
"EAP.Password": "user_password",
"Profile": "/profile/default",
"SecurityClass": "802_1x",
"SaveCredentials": true,
"Type": "wifi",
"WiFi.HexSSID": "7769666931",
"UIData": "{\"onc_source\":\"device_policy\"}"
})");
GetShillProfileClient()->AddEntry(
NetworkProfileHandler::GetSharedProfilePath(), kOriginalEntryPath,
std::move(original_wifi_config));
// Call TriggerEphemeralNetworkConfigActions when policies are available
// In production code, EphemeralNetworkConfigHandler will do this.
policy_observer_.RunOnSharedProfilePoliciesChanged(base::BindOnce(
&ManagedNetworkConfigurationHandlerImpl::
TriggerEphemeralNetworkConfigActions,
base::Unretained(managed_network_configuration_handler_.get())));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"RecommendedValuesAreEphemeral": true
},
"NetworkConfigurations": [
{
"GUID": "guid_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Identity": "user_identity",
"Password": "user_password"
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The original entry has been preserved.
EXPECT_TRUE(GetShillProfileClient()->HasService(kOriginalEntryPath));
}
// There is an unmanaged entry.
// The UserCreatedNetworkConfigurationsAreEphemeral policy is not enabled.
// Tests that initial policy application does not delete the entry even when
// `TriggerEphemeralNetworkConfigActions` is called.
TEST_F(ManagedNetworkConfigurationHandlerTest,
RemoveUnmanagedConfigs_Disabled_Initial) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json",
NetworkProfileHandler::GetSharedProfilePath(), "old_entry_path");
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
policy_observer_.RunOnSharedProfilePoliciesChanged(base::BindOnce(
&ManagedNetworkConfigurationHandlerImpl::
TriggerEphemeralNetworkConfigActions,
base::Unretained(managed_network_configuration_handler_.get())));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry still exists.
EXPECT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
}
// There is an unmanaged entry.
// The UserCreatedNetworkConfigurationsAreEphemeral policy is enabled.
// Tests that initial policy application deletes the entry when
// `TriggerEphemeralNetworkConfigActions` is called.
TEST_F(ManagedNetworkConfigurationHandlerTest,
RemoveUnmanagedConfigs_Enabled_Initial) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json",
NetworkProfileHandler::GetSharedProfilePath(), "old_entry_path");
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
policy_observer_.RunOnSharedProfilePoliciesChanged(base::BindOnce(
&ManagedNetworkConfigurationHandlerImpl::
TriggerEphemeralNetworkConfigActions,
base::Unretained(managed_network_configuration_handler_.get())));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"UserCreatedNetworkConfigurationsAreEphemeral": true
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry has been removed.
EXPECT_FALSE(GetShillProfileClient()->HasService("old_entry_path"));
}
// There is an unmanaged entry.
// The UserCreatedNetworkConfigurationsAreEphemeral policy is enabled.
// The feature flags/policies guarding it are however disabled.
TEST_F(ManagedNetworkConfigurationHandlerTest,
RemoveUnmanagedConfigs_Enabled_FeatureOff) {
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json",
NetworkProfileHandler::GetSharedProfilePath(), "old_entry_path");
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
// Don't call TriggerEphemeralNetworkConfigActions - it will only be called in
// production code if the feature is enabled.
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"UserCreatedNetworkConfigurationsAreEphemeral": true
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry is still there
EXPECT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
}
// There is an unmanaged entry.
// The UserCreatedNetworkConfigurationsAreEphemeral policy is enabled.
// Tests that a `TriggerEphemeralNetworkConfigActions` call triggered after the
// initial policy application leads to deletion of the unmanaged entry.
TEST_F(ManagedNetworkConfigurationHandlerTest,
RemoveUnmanagedConfigs_Enabled_AfterInitialApplication) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
InitializeStandardProfiles();
SetUpEntry("policy/shill_unmanaged_wifi1.json",
NetworkProfileHandler::GetSharedProfilePath(), "old_entry_path");
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"UserCreatedNetworkConfigurationsAreEphemeral": true
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry is still there.
EXPECT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
managed_network_configuration_handler_
->TriggerEphemeralNetworkConfigActions();
base::RunLoop().RunUntilIdle();
// The entry has been removed.
EXPECT_FALSE(GetShillProfileClient()->HasService("old_entry_path"));
// Re-create it and test that re-applying policies does not trigger the
// "ephemeral network config" actions.
SetUpEntry("policy/shill_unmanaged_wifi1.json", kUser1ProfilePath,
"old_entry_path");
ASSERT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry is still there
EXPECT_TRUE(GetShillProfileClient()->HasService("old_entry_path"));
}
// There is a policy with a Recommended field.
// The RecommendedValuesAreEphemeralAccessor policy is enabled.
// Tests that initial policy application does not reset "Recommended" fields,
// if `TriggerEphemeralNetworkConfigActions` is not called.
TEST_F(ManagedNetworkConfigurationHandlerTest,
NoEphemeralNetworkConfigActionsTriggered) {
// Don't call `TriggerEphemeralNetworkConfigActions`.
const std::string original_entry_path = "orig_entry_path";
base::Value::Dict original_wifi_config = base::test::ParseJsonDict(R"(
{
"AutoConnect": true,
"GUID": "guid_wifi1",
"Mode": "managed",
"EAP.EAP": "PEAP",
"EAP.Identity": "user_identity",
"EAP.Password": "user_password",
"Profile": "/profile/default",
"SecurityClass": "802_1x",
"SaveCredentials": true,
"Type": "wifi",
"WiFi.HexSSID": "7769666931",
"UIData": "{\"onc_source\":\"device_policy\"}"
})");
GetShillProfileClient()->AddEntry(
NetworkProfileHandler::GetSharedProfilePath(), original_entry_path,
std::move(original_wifi_config));
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
},
"NetworkConfigurations": [
{
"GUID": "guid_wifi1",
"Type": "WiFi",
"Name": "Managed wifi1",
"WiFi": {
"HexSSID": "7769666931", // "wifi1"
"SSID": "wifi1",
"Security": "WPA-EAP",
"EAP": {
"Outer": "PEAP",
"Inner": "MSCHAPv2",
"SaveCredentials": true,
"Recommended": ["Identity", "Password"]
}
}
}
],
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
base::RunLoop().RunUntilIdle();
// The entry still exists and has kept the user-provided Passphrase.
std::string profile_path;
EXPECT_THAT(
GetShillProfileClient()->GetService(original_entry_path, &profile_path),
Optional(DictionaryHasValue(shill::kEapPasswordProperty,
base::Value("user_password"))));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AutoConnectDisallowed) {
InitializeStandardProfiles();
// Setup an unmanaged network.
SetUpEntry("policy/shill_unmanaged_wifi2.json", kUser1ProfilePath,
"wifi2_entry_path");
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_disallow_autoconnect_on_unmanaged_wifi2.json");
// Apply the user policy with global autoconnect config and expect that
// autoconnect is disabled in the network's profile entry.
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_allow_only_policy_networks_to_autoconnect.onc"));
base::RunLoop().RunUntilIdle();
std::string wifi2_service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidUnmanagedWifi2);
ASSERT_FALSE(wifi2_service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(wifi2_service_path);
ASSERT_TRUE(properties);
EXPECT_TRUE(PropertiesMatch(expected_shill_properties, *properties));
// Verify that GetManagedProperties correctly augments the properties with the
// global config from the user policy.
// GetManagedProperties requires the device policy to be set or explicitly
// unset.
managed_handler()->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
/*userhash=*/std::string(),
/*network_configs_onc=*/base::Value::List(),
/*global_network_config=*/base::Value::Dict());
base::RunLoop get_properties_run_loop;
std::optional<base::Value::Dict> dictionary;
managed_handler()->GetManagedProperties(
kUser1, wifi2_service_path,
base::BindOnce(
[](std::optional<base::Value::Dict>* dictionary_out,
base::RepeatingClosure quit_closure,
const std::string& service_path,
std::optional<base::Value::Dict> dictionary,
std::optional<std::string> error) {
if (dictionary) {
*dictionary_out = std::move(*dictionary);
} else {
FAIL();
}
quit_closure.Run();
},
&dictionary, get_properties_run_loop.QuitClosure()));
get_properties_run_loop.Run();
ASSERT_TRUE(dictionary.has_value());
base::Value::Dict expected_managed_onc = test_utils::ReadTestDictionary(
"policy/"
"managed_onc_disallow_autoconnect_on_unmanaged_wifi2.onc");
EXPECT_TRUE(PropertiesMatch(expected_managed_onc, dictionary.value()));
}
TEST_F(ManagedNetworkConfigurationHandlerTest, LateProfileLoading) {
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
base::Value::Dict expected_shill_properties = test_utils::ReadTestDictionary(
"policy/shill_policy_on_unconfigured_wifi1.json");
InitializeStandardProfiles();
base::RunLoop().RunUntilIdle();
std::string service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(service_path.empty());
EXPECT_THAT(GetShillServiceClient()->GetServiceProperties(service_path),
Pointee(DictionaryHasValues(expected_shill_properties)));
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
ShutdownDuringPolicyApplication) {
InitializeStandardProfiles();
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
// Reset the network configuration manager after setting policy and before
// calling RunUntilIdle to simulate shutdown during policy application.
ResetManagedNetworkConfigurationHandler();
base::RunLoop().RunUntilIdle();
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AllowOnlyPolicyWiFiToConnect) {
InitializeStandardProfiles();
// Check transfer to NetworkStateHandler
EXPECT_CALL(
*network_state_handler_,
UpdateBlockedWifiNetworks(true, false, std::vector<std::string>()))
.Times(1);
// Set 'AllowOnlyPolicyWiFiToConnect' policy and another arbitrary user
// policy.
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_only_policy_networks_to_connect.onc"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
AllowOnlyPolicyWiFiToConnectIfAvailable) {
InitializeStandardProfiles();
// Check transfer to NetworkStateHandler
EXPECT_CALL(
*network_state_handler_,
UpdateBlockedWifiNetworks(false, true, std::vector<std::string>()))
.Times(1);
// Set 'AllowOnlyPolicyWiFiToConnectIfAvailable' policy and another
// arbitrary user policy.
EXPECT_TRUE(SetPolicy(
::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/"
"policy_allow_only_policy_networks_to_connect_if_available.onc"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
AllowOnlyPolicyNetworksToAutoconnect) {
InitializeStandardProfiles();
// Check transfer to NetworkStateHandler
EXPECT_CALL(
*network_state_handler_,
UpdateBlockedWifiNetworks(false, false, std::vector<std::string>()))
.Times(1);
// Set 'AllowOnlyPolicyNetworksToAutoconnect' policy and another arbitrary
// user policy.
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_only_policy_networks_to_autoconnect.onc"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
AllowOnlyPolicyCellularNetworksToConnect) {
InitializeStandardProfiles();
InitializeEuicc();
// Check transfer to NetworkStateHandler.
EXPECT_CALL(*network_state_handler_, UpdateBlockedCellularNetworks(true))
.Times(1);
// Set 'AllowOnlyPolicyCellularNetworks' policy.
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_only_policy_cellular_networks.onc"));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, DisconnectWiFiOnEthernet) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
const char* const onc_policy_connected = R"(
{
"GlobalNetworkConfiguration": {
"DisconnectWiFiOnEthernet": "WhenConnected"
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy_connected)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
auto properties =
ShillManagerClient::Get()->GetTestInterface()->GetStubProperties();
EXPECT_NE(properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
nullptr);
EXPECT_EQ(*properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
std::string(shill::kDisconnectWiFiOnEthernetConnected));
// Unknown policy value should reset property value to Off.
const char* const onc_policy_invalid = R"(
{
"GlobalNetworkConfiguration": {
"DisconnectWiFiOnEthernet": "Unknown"
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy_invalid)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
properties =
ShillManagerClient::Get()->GetTestInterface()->GetStubProperties();
EXPECT_NE(properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
nullptr);
EXPECT_EQ(*properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
std::string(shill::kDisconnectWiFiOnEthernetOff));
const char* const onc_policy_online = R"(
{
"GlobalNetworkConfiguration": {
"DisconnectWiFiOnEthernet": "WhenOnline"
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy_online)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
properties =
ShillManagerClient::Get()->GetTestInterface()->GetStubProperties();
EXPECT_NE(properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
nullptr);
EXPECT_EQ(*properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
std::string(shill::kDisconnectWiFiOnEthernetOnline));
// Field not existing in policy should leave property value unchanged.
const char* const onc_policy_off = R"(
{
"GlobalNetworkConfiguration": {},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy_off)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
properties =
ShillManagerClient::Get()->GetTestInterface()->GetStubProperties();
EXPECT_NE(properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
nullptr);
EXPECT_EQ(*properties.FindString(shill::kDisconnectWiFiOnEthernetProperty),
std::string(shill::kDisconnectWiFiOnEthernetOnline));
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
RecommendedValuesAreEphemeralAccessor) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
EXPECT_FALSE(managed_handler()->RecommendedValuesAreEphemeral());
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"RecommendedValuesAreEphemeral": true
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(managed_handler()->RecommendedValuesAreEphemeral());
}
TEST_F(ManagedNetworkConfigurationHandlerTest,
UserCreatedNetworkConfigurationsAreEphemeral) {
policy_util::SetEphemeralNetworkPoliciesEnabled();
EXPECT_FALSE(
managed_handler()->UserCreatedNetworkConfigurationsAreEphemeral());
const char* const onc_policy = R"(
{
"GlobalNetworkConfiguration": {
"UserCreatedNetworkConfigurationsAreEphemeral": true
},
"Type": "UnencryptedConfiguration"
})";
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
base::test::ParseJsonDict(onc_policy)));
FastForwardProfileRefreshDelay();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(
managed_handler()->UserCreatedNetworkConfigurationsAreEphemeral());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AllowApnModification) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(/*enabled_features=*/
{features::kApnRevamp,
features::kAllowApnModificationPolicy},
/*disabled_features=*/{});
// TODO(b/333100319): When feature is fully enabled, test
// AllowApnModification() in other unit tests to be consistent.
EXPECT_TRUE(managed_handler()->AllowApnModification());
// Set 'AllowApnModification' policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_apn_modification.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_FALSE(managed_handler()->AllowApnModification());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AllowCellularSimLock) {
// Set 'AllowCellularSimLock' policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_cellular_sim_lock.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowCellularSimLock());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AllowTextMessages) {
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_empty_global_network_configuration.onc"));
// Check that the field returns Unset when it isn't set.
EXPECT_EQ(managed_handler()->GetAllowTextMessages(),
PolicyTextMessageSuppressionState::kUnset);
// Set 'AllowTextMessages' policy to Suppress.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_text_messages_suppress.onc"));
// Check that the field is updated to Suppress.
EXPECT_EQ(managed_handler()->GetAllowTextMessages(),
PolicyTextMessageSuppressionState::kSuppress);
// Set 'AllowTextMessages' policy to Unset.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_text_messages_unset.onc"));
// Check that the field is updated to Unset.
EXPECT_EQ(managed_handler()->GetAllowTextMessages(),
PolicyTextMessageSuppressionState::kUnset);
// Set 'AllowTextMessages' policy to Allow.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_text_messages_allow.onc"));
// Check that the field is updated to Allow.
EXPECT_EQ(managed_handler()->GetAllowTextMessages(),
PolicyTextMessageSuppressionState::kAllow);
// Check other ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, AllowCellularHotspot) {
// Set 'AllowCellularHotspot' policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_allow_cellular_hotspot.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_FALSE(managed_handler()->AllowCellularHotspot());
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->GetBlockedHexSSIDs().empty());
}
// Test deprecated BlacklistedHexSSIDs property.
TEST_F(ManagedNetworkConfigurationHandlerTest, GetBlacklistedHexSSIDs) {
InitializeStandardProfiles();
std::vector<std::string> blocked = {"476F6F676C65477565737450534B"};
// Check transfer to NetworkStateHandler
EXPECT_CALL(*network_state_handler_,
UpdateBlockedWifiNetworks(false, false, blocked))
.Times(1);
// Set 'BlacklistedHexSSIDs' policy and another arbitrary user policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_deprecated_blacklisted_hex_ssids.onc"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_EQ(blocked, managed_handler()->GetBlockedHexSSIDs());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, GetBlockedHexSSIDs) {
InitializeStandardProfiles();
std::vector<std::string> blocked = {"476F6F676C65477565737450534B"};
// Check transfer to NetworkStateHandler
EXPECT_CALL(*network_state_handler_,
UpdateBlockedWifiNetworks(false, false, blocked))
.Times(1);
// Set 'BlockedHexSSIDs' policy and another arbitrary user policy.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_blocked_hex_ssids.onc"));
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
base::RunLoop().RunUntilIdle();
// Check ManagedNetworkConfigurationHandler policy accessors.
EXPECT_TRUE(managed_handler()->AllowCellularSimLock());
EXPECT_TRUE(managed_handler()->AllowCellularHotspot());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_EQ(blocked, managed_handler()->GetBlockedHexSSIDs());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, WipeGlobalNetworkConfiguration) {
InitializeStandardProfiles();
// A user policy must be present to apply some global config, e.g. blocked
// SSIDs, even though they are actually given in device policy. It does not
// really matter which user policy is configured for this test.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1.onc"));
// Step 1: Apply a device policy which sets all possible entries in
// GlobalNetworkConfiguration.
EXPECT_CALL(*network_state_handler_,
UpdateBlockedWifiNetworks(
/*only_managed=*/true, /*available_only=*/true,
std::vector<std::string>({"blocked_ssid"})))
.Times(1);
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_exhaustive_global_network_configuration.onc"));
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(network_state_handler_.get());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_TRUE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_THAT(managed_handler()->GetBlockedHexSSIDs(),
testing::ElementsAre("blocked_ssid"));
// TODO(b/219568567): Also test that DisableNetworkTypes are propagated to
// ProhibitedTechnologiesHandler.
// Step 2: Now apply a device policy with an empty GlobalNetworkConfiguration.
EXPECT_CALL(*network_state_handler_,
UpdateBlockedWifiNetworks(
/*only_managed=*/false, /*available_only=*/false,
std::vector<std::string>()))
.Times(1);
EXPECT_TRUE(
SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, std::string(),
"policy/policy_empty_global_network_configuration.onc"));
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(network_state_handler_.get());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyCellularNetworks());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyNetworksToAutoconnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnect());
EXPECT_FALSE(managed_handler()->AllowOnlyPolicyWiFiToConnectIfAvailable());
EXPECT_THAT(managed_handler()->GetBlockedHexSSIDs(), testing::IsEmpty());
}
// Proxy settings can come from different sources. Proxy enforced by user policy
// (provided by kProxy prefence) should have precedence over configurations set
// by ONC policy. This test verifies that the order of preference is respected.
TEST_F(ManagedNetworkConfigurationHandlerTest, ActiveProxySettingsPreference) {
// Configure network.
InitializeStandardProfiles();
GetShillServiceClient()->AddService(
"wifi_entry", std::string() /* guid */, "wifi1", shill::kTypeWifi,
std::string() /* state */, true /* visible */);
// Use proxy configured network.
EXPECT_TRUE(SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
"policy/policy_wifi1_proxy.onc"));
base::RunLoop().RunUntilIdle();
std::string wifi_service_path =
GetShillServiceClient()->FindServiceMatchingGUID(kTestGuidManagedWifi);
ASSERT_FALSE(wifi_service_path.empty());
const base::Value::Dict* properties =
GetShillServiceClient()->GetServiceProperties(wifi_service_path);
ASSERT_TRUE(properties);
managed_handler()->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
/*userhash=*/std::string(),
/*network_configs_onc=*/base::Value::List(),
/*global_network_config=*/base::Value::Dict());
std::optional<base::Value::Dict> dictionary_before_pref;
std::optional<base::Value::Dict> dictionary_after_pref;
base::RunLoop get_initial_properties_run_loop;
// Get properties and verify that proxy is used.
managed_handler()->GetManagedProperties(
kUser1, wifi_service_path,
base::BindOnce(
[](std::optional<base::Value::Dict>* dictionary_out,
base::RepeatingClosure quit_closure,
const std::string& service_path,
std::optional<base::Value::Dict> dictionary,
std::optional<std::string> error) {
if (dictionary) {
*dictionary_out = std::move(*dictionary);
} else {
ADD_FAILURE() << error.value_or("Failed");
}
quit_closure.Run();
},
&dictionary_before_pref,
get_initial_properties_run_loop.QuitClosure()));
get_initial_properties_run_loop.Run();
std::string* policy_before_pref =
dictionary_before_pref->FindStringByDottedPath(
"ProxySettings.Type.UserPolicy");
ASSERT_TRUE(dictionary_before_pref.has_value());
ASSERT_EQ(*policy_before_pref, "PAC");
// Set pref not to use proxy.
user_prefs_.SetManagedPref(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateDirect());
base::RunLoop get_merged_properties_run_loop;
// Fetch managed properties after preference is set.
managed_handler()->GetManagedProperties(
kUser1, wifi_service_path,
base::BindOnce(
[](std::optional<base::Value::Dict>* dictionary_out,
base::RepeatingClosure quit_closure,
const std::string& service_path,
std::optional<base::Value::Dict> dictionary,
std::optional<std::string> error) {
if (dictionary) {
*dictionary_out = std::move(*dictionary);
} else {
ADD_FAILURE() << error.value_or("Failed");
}
quit_closure.Run();
},
&dictionary_after_pref,
get_merged_properties_run_loop.QuitClosure()));
get_merged_properties_run_loop.Run();
std::string* policy_after_pref =
dictionary_after_pref->FindStringByDottedPath(
"ProxySettings.Type.UserPolicy");
ASSERT_TRUE(dictionary_after_pref.has_value());
ASSERT_NE(dictionary_before_pref, dictionary_after_pref);
ASSERT_EQ(*policy_after_pref, "Direct");
}
TEST_F(ManagedNetworkConfigurationHandlerTest, IsProhibitedFromConfiguringVpn) {
arc::prefs::RegisterProfilePrefs(user_prefs_.registry());
user_prefs_.registry()->RegisterBooleanPref(prefs::kVpnConfigAllowed, true);
for (const std::string& package_name : {"", "package_name"}) {
for (const bool vpn_configure_allowed : {true, false}) {
SetArcAlwaysOnUserPrefs(package_name, vpn_configure_allowed);
if (package_name.empty() || vpn_configure_allowed) {
EXPECT_FALSE(managed_network_configuration_handler_
->IsProhibitedFromConfiguringVpn());
continue;
}
EXPECT_TRUE(managed_network_configuration_handler_
->IsProhibitedFromConfiguringVpn());
}
}
}
} // namespace ash
|