Advertisement
banepwn

mock.js

Mar 26th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // mock lightdm for testing purposes
  2. // modified from https://github.com/pedropenna/musfealle/blob/master/js/mock.js
  3. // originally from https://github.com/Antergos/web-greeter/blob/master/themes/_vendor/js/mock.js
  4. if (typeof lightdm == 'undefined') {
  5.     lightdm = {};
  6.     lightdm.hostname = "test-host";
  7.     lightdm.languages = [
  8.         {code: "en_US", name: "English(US)", territory: "USA"},
  9.         {code: "en_UK", name: "English(UK)", territory: "UK"}
  10.     ];
  11.     lightdm.default_language = lightdm.languages[0];
  12.     lightdm.layouts = [{name: "test", short_description: "test description", short_description:"really long epic description"}];
  13.     lightdm.default_layout = lightdm.layouts[0];
  14.     lightdm.layout = lightdm.layouts[0];
  15.     lightdm.sessions=[
  16.         {key: "gnome", name: "GNOME", comment: "no comment"},
  17.         {key: "cinnamon", name: "Cinnamon", comment: "no comment"},
  18.         {key: "openbox", name: "Openbox", comment: "no comment"},
  19.         {key: "kde", name: "KDE", comment: "no comment"}
  20.     ];
  21.  
  22.     lightdm.default_session=lightdm.sessions[0];
  23.     lightdm.authentication_user = null;
  24.     lightdm.is_authenticated = false;
  25.     lightdm.can_suspend = true;
  26.     lightdm.can_hibernate = true;
  27.     lightdm.can_restart = true;
  28.     lightdm.can_shutdown = true;
  29.  
  30.     lightdm.users = [
  31.         {name: "clarkk", real_name: "Superman", display_name: "Clark Kent", image: "", language: "en_US", layout: null, session: "gnome", logged_in: false},
  32.         {name: "brucew", real_name: "Batman", display_name: "Bruce Wayne", image: "", language: "en_US", layout: null, session: "cinnamon", logged_in: false},
  33.         {name: "peterp", real_name: "Spiderman", display_name: "Peter Parker", image: "", language: "en_US", layout: null, session: "openbox", logged_in: true},
  34.     ];
  35.  
  36.     lightdm.num_users = lightdm.users.length;
  37.     lightdm.timed_login_delay = 0; //set to a number higher than 0 for timed login simulation
  38.     lightdm.timed_login_user = lightdm.timed_login_delay > 0 ? lightdm.users[0] : null;
  39.    
  40.     lightdm.get_string_property = function() {};
  41.     lightdm.get_integer_property = function() {};
  42.     lightdm.get_boolean_property = function() {};
  43.     lightdm.cancel_timed_login = function() {
  44.         _lightdm_mock_check_argument_length(arguments, 0);
  45.         lightdm._timed_login_cancelled = true;
  46.     };
  47.  
  48.     lightdm.provide_secret = function(secret) {
  49.         console.log("provide_secret: " + secret);
  50.         console.log("lightdm._username : " + lightdm._username);
  51.         if (typeof lightdm._username == 'undefined' || !lightdm._username) {
  52.             throw "must call start_authentication first"
  53.         }
  54.         _lightdm_mock_check_argument_length(arguments, 1);
  55.         var user = _lightdm_mock_get_user(lightdm.username);
  56.        
  57.         if (!user && secret == lightdm._username) {
  58.             lightdm.is_authenticated = true;
  59.             lightdm.authentication_user = user;
  60.         } else {
  61.             lightdm.is_authenticated = false;
  62.             lightdm.authentication_user = null;
  63.             lightdm._username = null;
  64.         }
  65.         authentication_complete();
  66.     };
  67.  
  68.     lightdm.start_authentication = function(username) {
  69.         console.log("start_auth: " + username);
  70.         _lightdm_mock_check_argument_length(arguments, 1);
  71.         if (lightdm._username) {
  72.             throw "Already authenticating!";
  73.         }
  74.         var user = _lightdm_mock_get_user(username);
  75.         if (!user) {
  76.             show_error(username + " is an invalid user");
  77.         }
  78.         lightdm._username = username;
  79.         show_prompt("Password: ");
  80.     };
  81.  
  82.     lightdm.cancel_authentication = function() {
  83.         console.log("cancel_auth");
  84.         _lightdm_mock_check_argument_length(arguments, 0);
  85.         if (!lightdm._username) {
  86.             throw "we are not authenticating";
  87.         }
  88.         lightdm._username = null;
  89.     };
  90.  
  91.     lightdm.suspend = function() {
  92.         alert("Suspend");
  93.     };
  94.  
  95.     lightdm.hibernate = function() {
  96.         alert("Hibernate");
  97.     };
  98.  
  99.     lightdm.restart = function() {
  100.         alert("Reboot");
  101.     };
  102.  
  103.     lightdm.shutdown = function() {
  104.         alert("Shutdown");
  105.     };
  106.  
  107.     lightdm.login = function(user, session) {
  108.         console.log("login: " + user + ", session: " + session);
  109.         _lightdm_mock_check_argument_length(arguments, 2);
  110.         if (!lightdm.is_authenticated) {
  111.             throw "The system is not authenticated";
  112.         }
  113.         if (user !== lightdm.authentication_user) {
  114.             throw "this user is not authenticated";
  115.         }
  116.         alert("logged in successfully!!");
  117.     };
  118.  
  119.     if (lightdm.timed_login_delay > 0) {
  120.         setTimeout(function() {
  121.             if (!lightdm._timed_login_cancelled()) {
  122.                 timed_login();
  123.             }
  124.         }, lightdm.timed_login_delay);
  125.     }
  126. }
  127.  
  128. function _lightdm_mock_check_argument_length(args, length) {
  129.     if (args.length != length) {
  130.         throw "incorrect number of arguments in function call";
  131.     }
  132. }
  133.  
  134. function _lightdm_mock_get_user(username) {
  135.     var user = null;
  136.     for (var i = 0; i < lightdm.users.length; ++i) {
  137.         if (lightdm.users[i].name == username) {
  138.             user = lightdm.users[i];
  139.             break;
  140.         }
  141.     }
  142.     return user;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement