@@ -36,6 +36,31 @@ fn persist_puzzle_input() {
36
36
assert_eq ! ( & read_cached_input( Day ( 3 ) , Year ( 2000 ) ) , "foobar" ) ;
37
37
}
38
38
39
+ #[ test]
40
+ fn try_load_unencrypted_input ( ) {
41
+ let cache_dir = tempdir ( ) . unwrap ( ) ;
42
+ let encryption_token: Option < String > = None ;
43
+
44
+ // Write input data with a temporary client.
45
+ {
46
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token. clone ( ) ) ;
47
+ puzzle_cache
48
+ . save_input ( "foobar" , Day ( 19 ) , Year ( 2000 ) )
49
+ . unwrap ( ) ;
50
+ }
51
+
52
+ // Read input data back with another temporary client.
53
+ let read_cached_input = |day : Day , year : Year | {
54
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token. clone ( ) ) ;
55
+ puzzle_cache. try_load_input ( day, year) . unwrap ( )
56
+ } ;
57
+
58
+ assert_eq ! (
59
+ read_cached_input( Day ( 19 ) , Year ( 2000 ) ) ,
60
+ Some ( "foobar" . to_string( ) )
61
+ ) ;
62
+ }
63
+
39
64
#[ test]
40
65
fn try_load_input_exists ( ) {
41
66
let cache_dir = tempdir ( ) . unwrap ( ) ;
@@ -83,3 +108,81 @@ fn try_load_missing_puzzle_input() {
83
108
84
109
assert ! ( result. is_none( ) )
85
110
}
111
+
112
+ #[ test]
113
+ fn load_input_err_if_wrong_password ( ) {
114
+ let cache_dir = tempdir ( ) . unwrap ( ) ;
115
+
116
+ // Write encrypted input data with a temporary client.
117
+ {
118
+ let encryption_token = Some ( "TEST" . to_string ( ) ) ;
119
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
120
+ puzzle_cache
121
+ . save_input ( "foobar" , Day ( 19 ) , Year ( 2000 ) )
122
+ . unwrap ( ) ;
123
+ }
124
+
125
+ // Read input data back with another temporary client.
126
+ let read_cached_input = |day : Day , year : Year | {
127
+ let encryption_token = Some ( "wrong password" . to_string ( ) ) ;
128
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
129
+ puzzle_cache. load_input ( day, year)
130
+ } ;
131
+
132
+ assert ! ( matches!(
133
+ read_cached_input( Day ( 19 ) , Year ( 2000 ) ) ,
134
+ Err ( CacheError :: Decryption ( _) )
135
+ ) ) ;
136
+ }
137
+
138
+ #[ test]
139
+ fn load_input_err_if_password_missing ( ) {
140
+ let cache_dir = tempdir ( ) . unwrap ( ) ;
141
+
142
+ // Write encrypted input data with a temporary client.
143
+ {
144
+ let encryption_token = Some ( "TEST" . to_string ( ) ) ;
145
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
146
+ puzzle_cache
147
+ . save_input ( "foobar" , Day ( 19 ) , Year ( 2000 ) )
148
+ . unwrap ( ) ;
149
+ }
150
+
151
+ // Read input data back with another temporary client.
152
+ let read_cached_input = |day : Day , year : Year | {
153
+ let encryption_token: Option < String > = None ;
154
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
155
+ puzzle_cache. load_input ( day, year)
156
+ } ;
157
+
158
+ assert ! ( matches!(
159
+ read_cached_input( Day ( 19 ) , Year ( 2000 ) ) ,
160
+ Err ( CacheError :: EncryptionTokenNotSet )
161
+ ) ) ;
162
+ }
163
+
164
+ #[ test]
165
+ fn load_input_err_uses_unencrypted_input_if_possible ( ) {
166
+ let cache_dir = tempdir ( ) . unwrap ( ) ;
167
+
168
+ // Write encrypted input data with a temporary client.
169
+ {
170
+ let encryption_token: Option < String > = None ;
171
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
172
+ puzzle_cache
173
+ . save_input ( "foobar" , Day ( 19 ) , Year ( 2000 ) )
174
+ . unwrap ( ) ;
175
+ }
176
+
177
+ // Read input data back with another temporary client.
178
+ let read_cached_input = |day : Day , year : Year | {
179
+ let encryption_token = Some ( "TEST" . to_string ( ) ) ;
180
+ let puzzle_cache = PuzzleFsCache :: new ( cache_dir. path ( ) , encryption_token) ;
181
+ puzzle_cache. load_input ( day, year)
182
+ } ;
183
+
184
+ assert ! ( matches!(
185
+ read_cached_input( Day ( 19 ) , Year ( 2000 ) ) ,
186
+ Err ( CacheError :: EncryptionTokenNotNeeded )
187
+ ) ) ;
188
+ }
0 commit comments