use super::{read_bool, v_i64, v_opt_str, v_str, DbnexusUserDeviceRepository};
use crate::dao::dao_session;
use crate::dao::repository::{make_statement, UserDeviceRepository, UserDeviceRow, MAX_DEVICES};
use crate::error::{GarrisonError, GarrisonResult};
use async_trait::async_trait;
use dbnexus::sea_orm::{ConnectionTrait, QueryResult};
use dbnexus::DbPool;
impl DbnexusUserDeviceRepository {
pub fn new(pool: DbPool) -> Self {
Self { pool }
}
}
#[async_trait]
impl UserDeviceRepository for DbnexusUserDeviceRepository {
async fn register_device(
&self,
tenant_id: i64,
login_id: &str,
identifier: &str,
ua: &str,
) -> GarrisonResult<String> {
dao_session!(
self.pool,
"dao-app-user-device-register-device",
session,
conn
);
let find_sql = "SELECT id FROM app_user_device \
WHERE tenant_id = ? AND login_id = ? AND device_identifier = ?";
let stmt = make_statement(
conn,
find_sql,
vec![v_i64(tenant_id), v_str(login_id), v_str(identifier)],
);
let existing = conn
.query_one_raw(stmt)
.await
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-query-exists::{}", e)))?;
if let Some(row) = existing {
let existing_id: String = row.try_get("", "id").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-parse-exists-id::{}", e))
})?;
let now = chrono::Utc::now().timestamp();
let update_sql = "UPDATE app_user_device SET last_seen_at = ? WHERE id = ?";
let stmt = make_statement(conn, update_sql, vec![v_i64(now), v_str(&existing_id)]);
conn.execute_raw(stmt).await.map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-update-last-seen-at::{}", e))
})?;
return Ok(existing_id);
}
let device_id = uuid::Uuid::new_v4().to_string();
let now = chrono::Utc::now().timestamp();
let device_name = parse_device_name(ua);
let insert_sql = "INSERT INTO app_user_device \
(id, tenant_id, login_id, device_identifier, device_name, user_agent, is_blocked, last_seen_at, created_at) \
SELECT ?, ?, ?, ?, ?, ?, 0, ?, ? FROM (SELECT 1) AS _g \
WHERE (SELECT COUNT(*) FROM app_user_device \
WHERE tenant_id = ? AND login_id = ?) < ?";
let stmt = make_statement(
conn,
insert_sql,
vec![
v_str(&device_id),
v_i64(tenant_id),
v_str(login_id),
v_str(identifier),
v_opt_str(&device_name),
v_opt_str(&Some(ua.to_string())),
v_i64(now),
v_i64(now),
v_i64(tenant_id),
v_str(login_id),
v_i64(MAX_DEVICES as i64),
],
);
let result = conn
.execute_raw(stmt)
.await
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-insert::{}", e)))?;
if result.rows_affected() == 0 {
let who = format!("tenant_id={}, login_id={}", tenant_id, login_id);
return Err(GarrisonError::InvalidParam(format!(
"dao-user-device-limit-exceeded::{}::{}",
who, MAX_DEVICES
)));
}
Ok(device_id)
}
async fn block_device(&self, tenant_id: i64, device_id: &str) -> GarrisonResult<()> {
dao_session!(self.pool, "dao-app-user-device-block-device", session, conn);
let sql = "UPDATE app_user_device SET is_blocked = 1 WHERE tenant_id = ? AND id = ?";
let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(device_id)]);
conn.execute_raw(stmt)
.await
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-block-update::{}", e)))?;
Ok(())
}
async fn unblock_device(&self, tenant_id: i64, device_id: &str) -> GarrisonResult<()> {
dao_session!(
self.pool,
"dao-app-user-device-unblock-device",
session,
conn
);
let sql = "UPDATE app_user_device SET is_blocked = 0 WHERE tenant_id = ? AND id = ?";
let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(device_id)]);
conn.execute_raw(stmt).await.map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-unblock-update::{}", e))
})?;
Ok(())
}
async fn list_user_devices(
&self,
tenant_id: i64,
login_id: &str,
) -> GarrisonResult<Vec<UserDeviceRow>> {
dao_session!(self.pool, "dao-app-user-device-list", session, conn);
let sql = "SELECT id, tenant_id, login_id, device_identifier, device_name, user_agent, \
is_blocked, last_seen_at, created_at \
FROM app_user_device WHERE tenant_id = ? AND login_id = ?";
let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(login_id)]);
let rows = conn
.query_all_raw(stmt)
.await
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-list-query::{}", e)))?;
rows.iter().map(parse_user_device_row).collect()
}
async fn count_user_devices(&self, tenant_id: i64, login_id: &str) -> GarrisonResult<usize> {
dao_session!(self.pool, "dao-app-user-device-count", session, conn);
let sql =
"SELECT COUNT(*) AS cnt FROM app_user_device WHERE tenant_id = ? AND login_id = ?";
let stmt = make_statement(conn, sql, vec![v_i64(tenant_id), v_str(login_id)]);
let row = conn
.query_one_raw(stmt)
.await
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-count-query::{}", e)))?
.ok_or_else(|| GarrisonError::Dao("dao-app-user-device-count-empty::".into()))?;
let count: i64 = row
.try_get("", "cnt")
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-parse-count::{}", e)))?;
Ok(count as usize)
}
}
fn parse_user_device_row(row: &QueryResult) -> GarrisonResult<UserDeviceRow> {
Ok(UserDeviceRow {
id: row
.try_get("", "id")
.map_err(|e| GarrisonError::Dao(format!("dao-app-user-device-row-parse-id::{}", e)))?,
tenant_id: row.try_get("", "tenant_id").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-tenant-id::{}", e))
})?,
login_id: row.try_get("", "login_id").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-login-id::{}", e))
})?,
device_identifier: row.try_get("", "device_identifier").map_err(|e| {
GarrisonError::Dao(format!(
"dao-app-user-device-row-parse-device-identifier::{}",
e
))
})?,
device_name: row.try_get("", "device_name").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-device-name::{}", e))
})?,
user_agent: row.try_get("", "user_agent").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-user-agent::{}", e))
})?,
is_blocked: read_bool(row, "is_blocked")?,
last_seen_at: row.try_get("", "last_seen_at").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-last-seen-at::{}", e))
})?,
created_at: row.try_get("", "created_at").map_err(|e| {
GarrisonError::Dao(format!("dao-app-user-device-row-parse-created-at::{}", e))
})?,
})
}
fn parse_device_name(ua: &str) -> Option<String> {
if ua.is_empty() {
return None;
}
let browser = detect_browser(ua);
let os = detect_os(ua);
match (browser, os) {
(Some(b), Some(o)) => Some(format!("{} on {}", b, o)),
(Some(b), None) => Some(b),
(None, Some(o)) => Some(o),
(None, None) => None,
}
}
fn detect_browser(ua: &str) -> Option<String> {
if ua.contains("Edg/") {
Some("Edge".into())
} else if ua.contains("Firefox/") {
Some("Firefox".into())
} else if ua.contains("Chrome/") {
Some("Chrome".into())
} else if ua.contains("Safari/") {
Some("Safari".into())
} else if ua.contains("OPR/") || ua.contains("Opera/") {
Some("Opera".into())
} else {
None
}
}
fn detect_os(ua: &str) -> Option<String> {
if ua.contains("Windows NT") {
Some("Windows".into())
} else if ua.contains("iPhone") || ua.contains("iPad") {
Some("iOS".into())
} else if ua.contains("Mac OS X") || ua.contains("Macintosh") {
Some("macOS".into())
} else if ua.contains("Android") {
Some("Android".into())
} else if ua.contains("Linux") {
Some("Linux".into())
} else {
None
}
}
#[cfg(all(test, feature = "db-sqlite"))]
mod tests {
use super::*;
use crate::dao::repository::sqlite::test_support::setup_db;
#[tokio::test(flavor = "multi_thread")]
async fn register_device_returns_uuid() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let device_id = repo
.register_device(
1,
"login-001",
"fingerprint-abc",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0",
)
.await
.expect("register_device 应成功");
let parsed = uuid::Uuid::parse_str(&device_id).expect("返回的 device_id 应为合法 UUID");
assert_eq!(
parsed.get_version(),
Some(uuid::Version::Random),
"应为 UUID v4"
);
let devices = repo
.list_user_devices(1, "login-001")
.await
.expect("list_user_devices 应成功");
assert_eq!(devices.len(), 1);
assert_eq!(devices[0].id, device_id);
assert_eq!(devices[0].device_identifier, "fingerprint-abc");
assert!(!devices[0].is_blocked);
assert_eq!(devices[0].device_name.as_deref(), Some("Chrome on Windows"));
}
#[tokio::test(flavor = "multi_thread")]
async fn register_device_idempotent_returns_same_id() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let id1 = repo
.register_device(1, "login-002", "fp-same", "Firefox/120.0")
.await
.expect("首次 register 应成功");
let id2 = repo
.register_device(1, "login-002", "fp-same", "Firefox/121.0")
.await
.expect("幂等 register 应成功");
assert_eq!(id1, id2, "幂等注册应返回相同 ID");
let count = repo
.count_user_devices(1, "login-002")
.await
.expect("count 应成功");
assert_eq!(count, 1, "幂等注册不应新增记录");
}
#[tokio::test(flavor = "multi_thread")]
async fn register_device_exceeds_max_devices() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
for i in 0..MAX_DEVICES {
repo.register_device(
1,
"login-max",
&format!("fp-{}", i),
"Mozilla/5.0 Chrome/120.0",
)
.await
.expect("注册设备应成功");
}
let count = repo
.count_user_devices(1, "login-max")
.await
.expect("count 应成功");
assert_eq!(count, MAX_DEVICES, "应有 MAX_DEVICES 个设备");
let result = repo
.register_device(1, "login-max", "fp-overflow", "Chrome/120.0")
.await;
assert!(result.is_err(), "超过 MAX_DEVICES 应返回错误");
let err = result.unwrap_err();
match err {
GarrisonError::InvalidParam(msg) => {
assert!(
msg.contains("dao-user-device-limit-exceeded")
|| msg.contains("MAX_DEVICES")
|| msg.contains("设备数已达上限"),
"错误信息应包含设备上限,实际: {}",
msg
);
},
other => panic!("应为 InvalidParam 错误,实际: {:?}", other),
}
}
#[tokio::test(flavor = "multi_thread")]
async fn block_and_unblock_device() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let device_id = repo
.register_device(1, "login-block", "fp-block", "Safari/17.0")
.await
.expect("register 应成功");
let devices = repo
.list_user_devices(1, "login-block")
.await
.expect("list 应成功");
assert!(!devices[0].is_blocked, "初始状态应未阻断");
repo.block_device(1, &device_id)
.await
.expect("block_device 应成功");
let devices = repo
.list_user_devices(1, "login-block")
.await
.expect("list 应成功");
assert!(devices[0].is_blocked, "阻断后 is_blocked 应为 true");
repo.unblock_device(1, &device_id)
.await
.expect("unblock_device 应成功");
let devices = repo
.list_user_devices(1, "login-block")
.await
.expect("list 应成功");
assert!(!devices[0].is_blocked, "解除后 is_blocked 应为 false");
}
#[tokio::test(flavor = "multi_thread")]
async fn list_user_devices_filters_by_login_id() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
repo.register_device(1, "login-a", "fp-a1", "Chrome/120.0")
.await
.expect("register 应成功");
repo.register_device(1, "login-a", "fp-a2", "Edge/120.0")
.await
.expect("register 应成功");
repo.register_device(1, "login-b", "fp-b1", "Firefox/120.0")
.await
.expect("register 应成功");
let list_a = repo
.list_user_devices(1, "login-a")
.await
.expect("list login-a 应成功");
let list_b = repo
.list_user_devices(1, "login-b")
.await
.expect("list login-b 应成功");
assert_eq!(list_a.len(), 2, "login-a 应有 2 个设备");
assert_eq!(list_b.len(), 1, "login-b 应有 1 个设备");
}
#[tokio::test(flavor = "multi_thread")]
async fn count_user_devices_empty_returns_zero() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let count = repo
.count_user_devices(1, "nonexistent-login")
.await
.expect("count 应成功");
assert_eq!(count, 0, "空用户设备数应为 0");
repo.register_device(1, "login-count", "fp-1", "Chrome/120.0")
.await
.expect("register 应成功");
let count = repo
.count_user_devices(1, "login-count")
.await
.expect("count 应成功");
assert_eq!(count, 1, "注册 1 个后应为 1");
}
#[test]
fn parse_device_name_chrome_on_windows() {
let ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome on Windows"));
}
#[test]
fn parse_device_name_firefox_on_macos() {
let ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) Firefox/121.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Firefox on macOS"));
}
#[test]
fn parse_device_name_edge_before_chrome() {
let ua = "Mozilla/5.0 (Windows NT 10.0) Edg/120.0 Chrome/120.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Edge on Windows"));
}
#[test]
fn parse_device_name_safari_on_ios() {
let ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0) Safari/604.1";
assert_eq!(parse_device_name(ua).as_deref(), Some("Safari on iOS"));
}
#[test]
fn parse_device_name_opera() {
let ua = "Mozilla/5.0 (Windows NT 10.0) OPR/120.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Opera on Windows"));
}
#[test]
fn parse_device_name_chrome_on_android() {
let ua = "Mozilla/5.0 (Linux; Android 14) Chrome/120.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome on Android"));
}
#[test]
fn parse_device_name_firefox_on_linux() {
let ua = "Mozilla/5.0 (X11; Linux x86_64) Firefox/121.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Firefox on Linux"));
}
#[test]
fn parse_device_name_empty_returns_none() {
assert!(parse_device_name("").is_none());
}
#[test]
fn parse_device_name_unknown_browser_with_os() {
let ua = "SomeBot/1.0 (Windows NT 10.0)";
assert_eq!(parse_device_name(ua).as_deref(), Some("Windows"));
}
#[test]
fn parse_device_name_browser_without_os() {
let ua = "Mozilla/5.0 Chrome/120.0";
assert_eq!(parse_device_name(ua).as_deref(), Some("Chrome"));
}
#[test]
fn parse_device_name_unknown_no_browser_no_os() {
let ua = "curl/8.0";
assert!(parse_device_name(ua).is_none());
}
#[test]
fn detect_browser_edge_priority() {
assert_eq!(
detect_browser("Edg/120 Chrome/120").as_deref(),
Some("Edge")
);
}
#[test]
fn detect_browser_firefox() {
assert_eq!(detect_browser("Firefox/121").as_deref(), Some("Firefox"));
}
#[test]
fn detect_browser_chrome() {
assert_eq!(detect_browser("Chrome/120").as_deref(), Some("Chrome"));
}
#[test]
fn detect_browser_safari() {
assert_eq!(detect_browser("Safari/604").as_deref(), Some("Safari"));
}
#[test]
fn detect_browser_opera_opr() {
assert_eq!(detect_browser("OPR/120").as_deref(), Some("Opera"));
}
#[test]
fn detect_browser_opera_opera() {
assert_eq!(detect_browser("Opera/120").as_deref(), Some("Opera"));
}
#[test]
fn detect_browser_unknown_returns_none() {
assert!(detect_browser("Bot/1.0").is_none());
}
#[test]
fn detect_os_windows() {
assert_eq!(detect_os("Windows NT 10.0").as_deref(), Some("Windows"));
}
#[test]
fn detect_os_ios_iphone() {
assert_eq!(detect_os("iPhone OS 17").as_deref(), Some("iOS"));
}
#[test]
fn detect_os_ios_ipad() {
assert_eq!(detect_os("iPad CPU OS 17").as_deref(), Some("iOS"));
}
#[test]
fn detect_os_macos() {
assert_eq!(detect_os("Mac OS X 10_15").as_deref(), Some("macOS"));
}
#[test]
fn detect_os_macos_macintosh() {
assert_eq!(detect_os("Macintosh").as_deref(), Some("macOS"));
}
#[test]
fn detect_os_android() {
assert_eq!(detect_os("Android 14").as_deref(), Some("Android"));
}
#[test]
fn detect_os_linux() {
assert_eq!(detect_os("Linux x86_64").as_deref(), Some("Linux"));
}
#[test]
fn detect_os_unknown_returns_none() {
assert!(detect_os("UnknownOS").is_none());
}
#[tokio::test(flavor = "multi_thread")]
async fn register_device_with_empty_ua() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let _device_id = repo
.register_device(1, "login-empty-ua", "fp-empty", "")
.await
.expect("空 UA register 应成功");
let devices = repo
.list_user_devices(1, "login-empty-ua")
.await
.expect("list 应成功");
assert_eq!(devices.len(), 1);
assert!(
devices[0].device_name.is_none(),
"空 UA device_name 应为 None"
);
assert!(devices[0].user_agent.as_deref() == Some(""));
}
#[tokio::test(flavor = "multi_thread")]
async fn block_nonexistent_device_is_noop() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
repo.block_device(1, "nonexistent-device")
.await
.expect("block 不存在的设备应为 no-op");
repo.unblock_device(1, "nonexistent-device")
.await
.expect("unblock 不存在的设备应为 no-op");
}
#[tokio::test(flavor = "multi_thread")]
async fn block_device_cross_tenant_is_noop() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let device_id = repo
.register_device(1, "login-cross", "fp-cross", "Chrome/120.0")
.await
.expect("register 应成功");
repo.block_device(2, &device_id)
.await
.expect("跨租户 block 应为 no-op 而非错误");
let devices = repo
.list_user_devices(1, "login-cross")
.await
.expect("list 应成功");
assert!(
!devices[0].is_blocked,
"跨租户 block 不得改动他租户设备的 is_blocked"
);
repo.unblock_device(2, &device_id)
.await
.expect("跨租户 unblock 应为 no-op 而非错误");
repo.block_device(1, &device_id)
.await
.expect("同租户 block 应生效");
let devices = repo
.list_user_devices(1, "login-cross")
.await
.expect("list 应成功");
assert!(devices[0].is_blocked, "同租户 block 应置 is_blocked = 1");
}
#[tokio::test(flavor = "multi_thread")]
async fn list_user_devices_nonexistent_returns_empty() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
let devices = repo
.list_user_devices(1, "nonexistent-login")
.await
.expect("list 应成功");
assert!(devices.is_empty(), "不存在的 login_id 应返回空列表");
}
#[tokio::test(flavor = "multi_thread")]
async fn register_device_isolates_by_tenant() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool);
repo.register_device(1, "shared-login", "fp-1", "Chrome/120")
.await
.expect("tenant 1 register 应成功");
repo.register_device(2, "shared-login", "fp-2", "Chrome/120")
.await
.expect("tenant 2 register 应成功");
let count_1 = repo
.count_user_devices(1, "shared-login")
.await
.expect("count tenant 1 应成功");
let count_2 = repo
.count_user_devices(2, "shared-login")
.await
.expect("count tenant 2 应成功");
assert_eq!(count_1, 1, "tenant 1 应有 1 个设备");
assert_eq!(count_2, 1, "tenant 2 应有 1 个设备");
}
#[tokio::test(flavor = "multi_thread")]
async fn register_device_returns_error_when_table_dropped() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool.clone());
{
let session = pool.get_session("admin").await.expect("获取 session 失败");
let conn = session.connection().expect("获取 connection 失败");
conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
.await
.expect("DROP TABLE 失败");
}
let result = repo.register_device(1, "u1", "fp1", "Chrome/120").await;
assert!(result.is_err(), "表删除后 register_device 应返回错误");
}
#[tokio::test(flavor = "multi_thread")]
async fn list_user_devices_returns_error_when_table_dropped() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool.clone());
{
let session = pool.get_session("admin").await.expect("获取 session 失败");
let conn = session.connection().expect("获取 connection 失败");
conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
.await
.expect("DROP TABLE 失败");
}
let result = repo.list_user_devices(1, "u1").await;
assert!(result.is_err(), "表删除后 list_user_devices 应返回错误");
}
#[tokio::test(flavor = "multi_thread")]
async fn count_user_devices_returns_error_when_table_dropped() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool.clone());
{
let session = pool.get_session("admin").await.expect("获取 session 失败");
let conn = session.connection().expect("获取 connection 失败");
conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
.await
.expect("DROP TABLE 失败");
}
let result = repo.count_user_devices(1, "u1").await;
assert!(result.is_err(), "表删除后 count_user_devices 应返回错误");
}
#[tokio::test(flavor = "multi_thread")]
async fn block_device_returns_error_when_table_dropped() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool.clone());
{
let session = pool.get_session("admin").await.expect("获取 session 失败");
let conn = session.connection().expect("获取 connection 失败");
conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
.await
.expect("DROP TABLE 失败");
}
let result = repo.block_device(1, "some-id").await;
assert!(result.is_err(), "表删除后 block_device 应返回错误");
}
#[tokio::test(flavor = "multi_thread")]
async fn unblock_device_returns_error_when_table_dropped() {
let pool = setup_db().await;
let repo = DbnexusUserDeviceRepository::new(pool.clone());
{
let session = pool.get_session("admin").await.expect("获取 session 失败");
let conn = session.connection().expect("获取 connection 失败");
conn.execute_unprepared("DROP TABLE IF EXISTS app_user_device")
.await
.expect("DROP TABLE 失败");
}
let result = repo.unblock_device(1, "some-id").await;
assert!(result.is_err(), "表删除后 unblock_device 应返回错误");
}
}