summaryrefslogtreecommitdiff
path: root/src/tests/pws.rs
blob: 5f5a3254ee1752bb7541acf6f444b2d255fed58e (plain)
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
use util::{CommandError, CommandStatus};
use pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};

fn get_pws() -> PasswordSafe<Target> {
    Target::connect()
        .unwrap()
        .get_password_safe(USER_PASSWORD)
        .unwrap()
}

#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn enable() {
    assert!(
        Target::connect()
            .unwrap()
            .get_password_safe(&(USER_PASSWORD.to_owned() + "123"))
            .is_err()
    );
    assert!(
        Target::connect()
            .unwrap()
            .get_password_safe(USER_PASSWORD)
            .is_ok()
    );
    assert!(
        Target::connect()
            .unwrap()
            .get_password_safe(ADMIN_PASSWORD)
            .is_err()
    );
    assert!(
        Target::connect()
            .unwrap()
            .get_password_safe(USER_PASSWORD)
            .is_ok()
    );
}

#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn get_status() {
    let pws = get_pws();
    for i in 0..SLOT_COUNT {
        assert_eq!(
            CommandStatus::Success,
            pws.erase_slot(i),
            "Could not erase slot {}",
            i
        );
    }
    let status = pws.get_slot_status().unwrap();
    assert_eq!(status, [false; SLOT_COUNT as usize]);

    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(1, "name", "login", "password")
    );
    let status = pws.get_slot_status().unwrap();
    for i in 0..SLOT_COUNT {
        assert_eq!(i == 1, status[i as usize]);
    }

    for i in 0..SLOT_COUNT {
        assert_eq!(
            CommandStatus::Success,
            pws.write_slot(i, "name", "login", "password")
        );
    }
    let status = pws.get_slot_status().unwrap();
    assert_eq!(status, [true; SLOT_COUNT as usize]);
}

#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn get_data() {
    let pws = get_pws();
    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(1, "name", "login", "password")
    );
    assert_eq!("name", pws.get_slot_name(1).unwrap());
    assert_eq!("login", pws.get_slot_login(1).unwrap());
    assert_eq!("password", pws.get_slot_password(1).unwrap());

    assert_eq!(CommandStatus::Success, pws.erase_slot(1));
    // TODO: check error codes
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_name(1));
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_login(1));
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_password(1));

    let name = "with å";
    let login = "pär@test.com";
    let password = "'i3lJc[09?I:,[u7dWz9";
    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(1, name, login, password)
    );
    assert_eq!(name, pws.get_slot_name(1).unwrap());
    assert_eq!(login, pws.get_slot_login(1).unwrap());
    assert_eq!(password, pws.get_slot_password(1).unwrap());

    assert_eq!(
        Err(CommandError::InvalidSlot),
        pws.get_slot_name(SLOT_COUNT)
    );
    assert_eq!(
        Err(CommandError::InvalidSlot),
        pws.get_slot_login(SLOT_COUNT)
    );
    assert_eq!(
        Err(CommandError::InvalidSlot),
        pws.get_slot_password(SLOT_COUNT)
    );
}

#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn write() {
    let pws = get_pws();

    assert_eq!(
        CommandStatus::Error(CommandError::InvalidSlot),
        pws.write_slot(SLOT_COUNT, "name", "login", "password")
    );

    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(0, "", "login", "password")
    );
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_name(0));
    assert_eq!(Ok(String::from("login")), pws.get_slot_login(0));
    assert_eq!(Ok(String::from("password")), pws.get_slot_password(0));

    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(0, "name", "", "password")
    );
    assert_eq!(Ok(String::from("name")), pws.get_slot_name(0));
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_login(0));
    assert_eq!(Ok(String::from("password")), pws.get_slot_password(0));

    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(0, "name", "login", "")
    );
    assert_eq!(Ok(String::from("name")), pws.get_slot_name(0));
    assert_eq!(Ok(String::from("login")), pws.get_slot_login(0));
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_password(0));
}

#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn erase() {
    let pws = get_pws();
    assert_eq!(
        CommandStatus::Error(CommandError::InvalidSlot),
        pws.erase_slot(SLOT_COUNT)
    );

    assert_eq!(
        CommandStatus::Success,
        pws.write_slot(0, "name", "login", "password")
    );
    assert_eq!(CommandStatus::Success, pws.erase_slot(0));
    assert_eq!(CommandStatus::Success, pws.erase_slot(0));
    assert_eq!(Err(CommandError::Unknown), pws.get_slot_name(0));
}